Skip to content

Commit

Permalink
Merge pull request #1 from jojoe77777/main
Browse files Browse the repository at this point in the history
Fixed preview state file updating too early, and reuse file handle for writing state changes
  • Loading branch information
pixfumy authored Apr 13, 2023
2 parents 2434096 + 240ca1a commit 988b7ab
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/main/java/me/voidxwalker/worldpreview/StateOutputHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import org.apache.logging.log4j.Level;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutorService;
Expand All @@ -21,6 +21,8 @@ public final class StateOutputHelper {
public static boolean titleHasEverLoaded = false;
private static String lastOutput = "";

private static RandomAccessFile stateFile;


private StateOutputHelper() {
}
Expand All @@ -44,8 +46,12 @@ public static void outputState(String string) {

private static void outputStateInternal(String string) {
try {
// Java 11+ method
Files.write(OUT_PATH, string.getBytes(StandardCharsets.UTF_8));
if(stateFile == null){ // opening file only once is better for performance
stateFile = new RandomAccessFile(OUT_PATH.toString(), "rw");
}
stateFile.setLength(0); // clear existing file contents
stateFile.seek(0); // move pointer back to start of file
stateFile.write(string.getBytes(StandardCharsets.UTF_8));
WorldPreview.log(Level.INFO, "WorldPreview State: " + string);
} catch (IOException ignored) {
WorldPreview.log(Level.ERROR, "Failed to write state output!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class WorldPreview implements ClientModInitializer {
public static ClientPlayerEntity player;
public static ClientWorld clientWord;
public static boolean inPreview;
public static boolean renderingPreview;
public static BlockPos spawnPos;
public static int kill=0;
public static int playerSpawn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,14 @@ private void worldpreview_outputInWorldState(CallbackInfo info) {
StateOutputHelper.outputState("inworld,gamescreenopen");
}
}

@Inject(method = "render", at = @At(value = "INVOKE", target="Lnet/minecraft/client/util/Window;swapBuffers()V", shift = At.Shift.AFTER))
private void worldpreview_actuallyInPreview(boolean tick, CallbackInfo ci) {
if (WorldPreview.inPreview && !WorldPreview.renderingPreview) {
WorldPreview.renderingPreview = true;
StateOutputHelper.outputState("previewing," + StateOutputHelper.loadingProgress);
WorldPreview.log(Level.INFO, "Starting Preview at (" + WorldPreview.player.getX() + ", " + (double) Math.floor(WorldPreview.player.getY()) + ", " + WorldPreview.player.getZ() + ")");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public abstract class WorldGenerationProgressLoggerMixin {
private void worldpreview_outputGenerationState(ChunkPos pos, ChunkStatus status, CallbackInfo info) {
// Using the getProgressPercentage to recalculate is slightly unoptimized but prevents needing to do locals capture, making it easier to port this mixin.
StateOutputHelper.loadingProgress = MathHelper.clamp(getProgressPercentage(), 0, 100);
StateOutputHelper.outputState((WorldPreview.inPreview ? "previewing," : "generating,") + StateOutputHelper.loadingProgress);
StateOutputHelper.outputState((WorldPreview.renderingPreview ? "previewing," : "generating,") + StateOutputHelper.loadingProgress);
}

@Shadow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ public void worldpreview_render(MatrixStack matrices, int mouseX, int mouseY, fl
WorldPreview.camera.update(WorldPreview.world, WorldPreview.player, this.client.options.perspective > 0, this.client.options.perspective == 2, 0.2F);
WorldPreview.player.refreshPositionAndAngles(WorldPreview.player.getX(), WorldPreview.player.getY() - 1.5, WorldPreview.player.getZ(), 0.0F, 0.0F);
WorldPreview.inPreview=true;
StateOutputHelper.outputState("previewing," + StateOutputHelper.loadingProgress);
WorldPreview.log(Level.INFO,"Starting Preview at ("+ WorldPreview.player.getX() + ", "+(double)Math.floor(WorldPreview.player.getY())+ ", "+ WorldPreview.player.getZ()+")");
}
MatrixStack matrixStack = new MatrixStack();
matrixStack.peek().getModel().multiply(this.worldpreview_getBasicProjectionMatrix());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public void worldpreview_kill(CallbackInfo ci){
@Inject(method="runServer",at=@At(value="INVOKE",target="Lnet/minecraft/server/MinecraftServer;setupServer()Z",shift = At.Shift.AFTER), cancellable = true)
public void worldpreview_kill2(CallbackInfo ci){
WorldPreview.inPreview=false;
WorldPreview.renderingPreview=false;
LockSupport.unpark(((MinecraftClientMixin)MinecraftClient.getInstance()).invokeGetThread());
if(WorldPreview.kill==1){
ci.cancel();
Expand Down

0 comments on commit 988b7ab

Please sign in to comment.