From 4c36f04f56bfce8927f99ccace309bfeb6fdb617 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Mon, 24 Apr 2023 11:49:56 -0400 Subject: [PATCH 1/2] Fix the Minecraft window teleporting back to the center of the screen when early load finishes --- .../core/config/ModernFixEarlyConfig.java | 1 + .../WindowMixin.java | 68 +++++++++++++++++++ src/main/resources/modernfix.mixins.json | 1 + 3 files changed, 70 insertions(+) create mode 100644 src/main/java/org/embeddedt/modernfix/mixin/bugfix/preserve_early_window_pos/WindowMixin.java diff --git a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java index 27c986b7..23ed4ede 100644 --- a/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java +++ b/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java @@ -48,6 +48,7 @@ public class ModernFixEarlyConfig { this.addMixinRule("perf.biome_zoomer", true); this.addMixinRule("perf.compress_blockstate", false); this.addMixinRule("bugfix.concurrency", true); + this.addMixinRule("bugfix.preserve_early_window_pos", true); this.addMixinRule("bugfix.edge_chunk_not_saved", true); this.addMixinRule("bugfix.starlight_emptiness", modPresent("starlight")); this.addMixinRule("bugfix.packet_leak", false); diff --git a/src/main/java/org/embeddedt/modernfix/mixin/bugfix/preserve_early_window_pos/WindowMixin.java b/src/main/java/org/embeddedt/modernfix/mixin/bugfix/preserve_early_window_pos/WindowMixin.java new file mode 100644 index 00000000..1333440b --- /dev/null +++ b/src/main/java/org/embeddedt/modernfix/mixin/bugfix/preserve_early_window_pos/WindowMixin.java @@ -0,0 +1,68 @@ +package org.embeddedt.modernfix.mixin.bugfix.preserve_early_window_pos; + +import com.mojang.blaze3d.platform.Monitor; +import com.mojang.blaze3d.platform.ScreenManager; +import com.mojang.blaze3d.platform.Window; +import net.minecraftforge.fml.common.ObfuscationReflectionHelper; +import net.minecraftforge.fml.loading.progress.EarlyProgressVisualization; +import org.lwjgl.glfw.GLFW; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Redirect; + +import java.util.function.IntSupplier; +import java.util.function.LongSupplier; +import java.util.function.Supplier; + +@Mixin(Window.class) +public class WindowMixin { + @Shadow private boolean fullscreen; + @Shadow private int width; + @Shadow private int height; + @Shadow private int windowedWidth; + @Shadow private int windowedHeight; + private static Class VISUALIZER; + + static { + try { + VISUALIZER = Class.forName("net.minecraftforge.fml.loading.progress.ClientVisualization"); + } catch(ClassNotFoundException e) { + VISUALIZER = null; + } + } + + private Object getEarlyProgressVisualizer() { + if(VISUALIZER == null || this.fullscreen) + return null; + Object o = ObfuscationReflectionHelper.getPrivateValue(EarlyProgressVisualization.class, EarlyProgressVisualization.INSTANCE, "visualization"); + return VISUALIZER.isAssignableFrom(o.getClass()) ? o : null; + } + + /** + * Return a null monitor if not in fullscreen and the visualizer is present, so that the code grabs the + * original X/Y position of the window. + */ + @Redirect(method = "", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/platform/ScreenManager;getMonitor(J)Lcom/mojang/blaze3d/platform/Monitor;")) + private Monitor getMonitor(ScreenManager manager, long id) { + return getEarlyProgressVisualizer() != null ? null : manager.getMonitor(id); + } + + /** + * Grab the original width/height from the window and inject them into our state variables. + */ + @SuppressWarnings("unchecked") + @Redirect(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/loading/progress/EarlyProgressVisualization;handOffWindow(Ljava/util/function/IntSupplier;Ljava/util/function/IntSupplier;Ljava/util/function/Supplier;Ljava/util/function/LongSupplier;)J")) + private long performHandoff(EarlyProgressVisualization instance, IntSupplier width, IntSupplier height, Supplier title, LongSupplier monitor) { + Object visualizer = getEarlyProgressVisualizer(); + if(visualizer != null) { + long windowId = ObfuscationReflectionHelper.getPrivateValue((Class)visualizer.getClass(), visualizer, "window"); + int[] w = new int[1]; + int[] h = new int[1]; + GLFW.glfwGetWindowSize(windowId, w, h); + this.windowedWidth = this.width = w[0]; + this.windowedHeight = this.height = h[0]; + } + return instance.handOffWindow(width, height, title, monitor); + } +} diff --git a/src/main/resources/modernfix.mixins.json b/src/main/resources/modernfix.mixins.json index 5cb83fa3..776cbbbf 100644 --- a/src/main/resources/modernfix.mixins.json +++ b/src/main/resources/modernfix.mixins.json @@ -78,6 +78,7 @@ "feature.measure_time.MinecraftMixin", "feature.reduce_loading_screen_freezes.ModelBakeryMixin", "perf.skip_first_datapack_reload.MinecraftMixin", + "bugfix.preserve_early_window_pos.WindowMixin", "bugfix.concurrency.RenderTypeMixin", "bugfix.concurrency.MinecraftMixin", "bugfix.concurrency.StaticTagHelperMixin", From da33aa7ef92f4f000d53eee1c0216f04dd9cbf6f Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Mon, 24 Apr 2023 13:46:07 -0400 Subject: [PATCH 2/2] Clear vanilla memory reserve --- src/main/java/org/embeddedt/modernfix/ModernFixClient.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/embeddedt/modernfix/ModernFixClient.java b/src/main/java/org/embeddedt/modernfix/ModernFixClient.java index 4fce8533..66087dab 100644 --- a/src/main/java/org/embeddedt/modernfix/ModernFixClient.java +++ b/src/main/java/org/embeddedt/modernfix/ModernFixClient.java @@ -6,6 +6,7 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.components.DebugScreenOverlay; import net.minecraft.client.gui.screens.ConnectScreen; import net.minecraft.client.gui.screens.TitleScreen; +import net.minecraft.util.MemoryReserve; import net.minecraftforge.client.event.ScreenEvent; import net.minecraft.network.syncher.EntityDataAccessor; import net.minecraft.network.syncher.SynchedEntityData; @@ -48,8 +49,7 @@ public class ModernFixClient { public ModernFixClient() { // clear reserve as it's not needed - // TODO: port 1.18+ - // ObfuscationReflectionHelper.setPrivateValue(Minecraft.class, null, new byte[0], "field_71444_a"); + MemoryReserve.release(); if(ModernFixMixinPlugin.instance.isOptionEnabled("perf.faster_singleplayer_load.ClientEvents")) { MinecraftForge.EVENT_BUS.register(new LoadEvents()); }