Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed preview state file updating too early, and reuse file handle for writing state changes #1

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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