From caf1fdd62542b5ab94c209a67956b6fe2507342a Mon Sep 17 00:00:00 2001 From: IMS212 Date: Sat, 7 Sep 2024 11:54:49 -0700 Subject: [PATCH] Do not reference Minecraft classes in module scanner and message box --- .../context_creation/WindowMixin.java | 8 ++++-- .../compatibility/checks/ModuleScanner.java | 28 ++++--------------- .../compatibility/checks/PreLaunchChecks.java | 2 +- .../sodium/client/platform/MessageBox.java | 13 ++++----- .../mods/sodium/neoforge/SodiumForgeMod.java | 3 -- .../neoforge/mixin/EntrypointMixin.java | 21 ++++++++++++++ .../main/resources/sodium-forge.mixins.json | 1 + 7 files changed, 40 insertions(+), 36 deletions(-) create mode 100644 neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/mixin/EntrypointMixin.java diff --git a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/workarounds/context_creation/WindowMixin.java b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/workarounds/context_creation/WindowMixin.java index bb4ac80d6a..cff7c45b81 100644 --- a/common/src/main/java/net/caffeinemc/mods/sodium/mixin/workarounds/context_creation/WindowMixin.java +++ b/common/src/main/java/net/caffeinemc/mods/sodium/mixin/workarounds/context_creation/WindowMixin.java @@ -37,6 +37,10 @@ public class WindowMixin { @Final private static Logger LOGGER; + @Shadow + @Final + private long window; + @Unique private long wglPrevContext = MemoryUtil.NULL; @@ -95,7 +99,7 @@ private void postContextReady(WindowEventHandler eventHandler, ScreenManager mon } PostLaunchChecks.onContextInitialized(); - ModuleScanner.checkModules(); + ModuleScanner.checkModules(this.window); } @Inject(method = "updateDisplay", at = @At(value = "INVOKE", target = "Lcom/mojang/blaze3d/systems/RenderSystem;flipFrame(J)V", shift = At.Shift.AFTER)) @@ -117,7 +121,7 @@ private void preSwapBuffers(CallbackInfo ci) { // Likely, this indicates a module was injected into the current process. We should check that // nothing problematic was just installed. - ModuleScanner.checkModules(); + ModuleScanner.checkModules(this.window); // If we didn't find anything problematic (which would have thrown an exception), then let's just record // the new context pointer and carry on. diff --git a/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/compatibility/checks/ModuleScanner.java b/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/compatibility/checks/ModuleScanner.java index 98b6f0d891..1cbab8713f 100644 --- a/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/compatibility/checks/ModuleScanner.java +++ b/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/compatibility/checks/ModuleScanner.java @@ -9,9 +9,6 @@ import net.caffeinemc.mods.sodium.client.platform.windows.WindowsFileVersion; import net.caffeinemc.mods.sodium.client.platform.windows.api.Kernel32; import net.caffeinemc.mods.sodium.client.platform.windows.api.version.Version; -import net.minecraft.client.Minecraft; -import net.minecraft.util.NativeModuleLister; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -19,7 +16,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; -import java.util.regex.Pattern; /** * Utility class for determining whether the current process has been injected into or otherwise modified. This should @@ -40,7 +36,7 @@ public class ModuleScanner { "GTIII-OSD64.dll", "GTIII-OSD.dll" }; - public static void checkModules() { + public static void checkModules(long window) { List modules; try { @@ -58,29 +54,17 @@ public static void checkModules() { // is blacklisted in the settings. The only way to stop it from injecting is to close the server process // entirely. if (BugChecks.ISSUE_2048 && isModuleLoaded(modules, RTSS_HOOKS_MODULE_NAMES)) { - checkRTSSModules(); + checkRTSSModules(window); } // ASUS GPU Tweak III hooks SwapBuffers() function to inject itself, and does so even if the On-Screen // Display (OSD) is disabled. The only way to stop it from hooking the game is to add the Java process to // the blacklist, or uninstall the application entirely. if (BugChecks.ISSUE_2637 && isModuleLoaded(modules, ASUS_GPU_TWEAK_MODULE_NAMES)) { - checkASUSGpuTweakIII(); + checkASUSGpuTweakIII(window); } } - private static List enumerateLoadedModules() { - List modules = null; - - try { - modules = NativeModuleLister.listModules(); - } catch (Throwable t) { - LOGGER.warn("Failed to scan the currently loaded modules", t); - } - - return modules != null ? modules : List.of(); - } - private static List listModules() { if (!Platform.isWindows()) { return ImmutableList.of(); @@ -97,7 +81,7 @@ private static List listModules() { } } - private static void checkRTSSModules() { + private static void checkRTSSModules(long window) { LOGGER.warn("RivaTuner Statistics Server (RTSS) has injected into the process! Attempting to apply workarounds for compatibility..."); @Nullable WindowsFileVersion version = null; @@ -115,7 +99,6 @@ private static void checkRTSSModules() { } if (version == null || !isRTSSCompatible(version)) { - Window window = Minecraft.getInstance().getWindow(); MessageBox.showMessageBox(window, MessageBox.IconType.ERROR, "Sodium Renderer", """ You appear to be using an older version of RivaTuner Statistics Server (RTSS) which is not compatible with Sodium. @@ -139,8 +122,7 @@ private static boolean isRTSSCompatible(WindowsFileVersion version) { return x > 7 || (x == 7 && y > 3) || (x == 7 && y == 3 && z >= 4); } - private static void checkASUSGpuTweakIII() { - Window window = Minecraft.getInstance().getWindow(); + private static void checkASUSGpuTweakIII(long window) { MessageBox.showMessageBox(window, MessageBox.IconType.ERROR, "Sodium Renderer", """ ASUS GPU Tweak III is not compatible with Minecraft, and causes extreme performance issues and severe graphical corruption when used with Minecraft. diff --git a/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/compatibility/checks/PreLaunchChecks.java b/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/compatibility/checks/PreLaunchChecks.java index d89ebae6ef..d6804a83d4 100644 --- a/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/compatibility/checks/PreLaunchChecks.java +++ b/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/compatibility/checks/PreLaunchChecks.java @@ -103,7 +103,7 @@ private static void showCriticalErrorAndClose(String title, String message, Stri .replace("###HELP_URL###", url == null ? "" : url)); // Try to show a graphical message box (if the platform supports it) and shut down the game. - MessageBox.showMessageBox(null, MessageBox.IconType.ERROR, title, message, url); + MessageBox.showMessageBox(0L, MessageBox.IconType.ERROR, title, message, url); System.exit(1 /* failure code */); } diff --git a/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/platform/MessageBox.java b/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/platform/MessageBox.java index 0ff28c6638..4eccd3aa2a 100644 --- a/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/platform/MessageBox.java +++ b/common/src/workarounds/java/net/caffeinemc/mods/sodium/client/platform/MessageBox.java @@ -8,7 +8,6 @@ import org.lwjgl.glfw.GLFWNativeWin32; import org.lwjgl.system.MemoryStack; import org.lwjgl.system.MemoryUtil; -import com.mojang.blaze3d.platform.Window; import org.lwjgl.util.tinyfd.TinyFileDialogs; import java.awt.*; @@ -21,7 +20,7 @@ public class MessageBox { private static final @Nullable MessageBoxImpl IMPL = MessageBoxImpl.chooseImpl(); - public static void showMessageBox(@Nullable Window window, + public static void showMessageBox(long window, IconType icon, String title, String description, @Nullable String helpUrl) @@ -42,7 +41,7 @@ private interface MessageBoxImpl { } } - void showMessageBox(@Nullable Window window, + void showMessageBox(long window, IconType icon, String title, String description, @Nullable String helpUrl); @@ -53,7 +52,7 @@ private static class TFDMessageBoxImpl implements MessageBoxImpl { private static final String NOTICE = "\n\nFor more information, click OK; otherwise, click Cancel."; @Override - public void showMessageBox(@Nullable Window window, IconType icon, String title, String description, @Nullable String helpUrl) { + public void showMessageBox(long window, IconType icon, String title, String description, @Nullable String helpUrl) { boolean clicked = TinyFileDialogs.tinyfd_messageBox(title, helpUrl == null ? description : description + NOTICE, helpUrl == null ? "ok" : "okcancel", icon.name().toLowerCase(Locale.ROOT), false); if (clicked && helpUrl != null) { @@ -68,7 +67,7 @@ public void showMessageBox(@Nullable Window window, IconType icon, String title, private static class WindowsMessageBoxImpl implements MessageBoxImpl { @Override - public void showMessageBox(@Nullable Window window, + public void showMessageBox(long window, IconType icon, String title, String description, @Nullable String helpUrl) { @@ -92,8 +91,8 @@ public void showMessageBox(@Nullable Window window, final long hWndOwner; - if (window != null) { - hWndOwner = GLFWNativeWin32.glfwGetWin32Window(window.getWindow()); + if (window != 0L) { + hWndOwner = GLFWNativeWin32.glfwGetWin32Window(window); } else { hWndOwner = MemoryUtil.NULL; } diff --git a/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/SodiumForgeMod.java b/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/SodiumForgeMod.java index 10d8a55f94..958baa618b 100644 --- a/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/SodiumForgeMod.java +++ b/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/SodiumForgeMod.java @@ -1,13 +1,11 @@ package net.caffeinemc.mods.sodium.neoforge; -import net.caffeinemc.mods.sodium.client.SodiumClientMod; import net.caffeinemc.mods.sodium.client.gui.SodiumOptionsGUI; import net.caffeinemc.mods.sodium.client.render.frapi.SodiumRenderer; import net.caffeinemc.mods.sodium.client.render.texture.SpriteFinderCache; import net.fabricmc.fabric.api.renderer.v1.RendererAccess; import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.ModContainer; -import net.neoforged.fml.ModList; import net.neoforged.fml.common.Mod; import net.neoforged.neoforge.client.event.RegisterClientReloadListenersEvent; import net.neoforged.neoforge.client.gui.IConfigScreenFactory; @@ -15,7 +13,6 @@ @Mod("sodium") public class SodiumForgeMod { public SodiumForgeMod(IEventBus bus, ModContainer modContainer) { - SodiumClientMod.onInitialization(ModList.get().getModContainerById("sodium").orElseThrow().getModInfo().getVersion().toString()); bus.addListener(this::onResourceReload); modContainer.registerExtensionPoint(IConfigScreenFactory.class, (minecraft, screen) -> SodiumOptionsGUI.createScreen(screen)); RendererAccess.INSTANCE.registerRenderer(SodiumRenderer.INSTANCE); diff --git a/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/mixin/EntrypointMixin.java b/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/mixin/EntrypointMixin.java new file mode 100644 index 0000000000..dc96f9a19a --- /dev/null +++ b/neoforge/src/main/java/net/caffeinemc/mods/sodium/neoforge/mixin/EntrypointMixin.java @@ -0,0 +1,21 @@ +package net.caffeinemc.mods.sodium.neoforge.mixin; + +import net.caffeinemc.mods.sodium.client.SodiumClientMod; +import net.minecraft.client.Minecraft; +import net.minecraft.client.main.GameConfig; +import net.neoforged.fml.ModList; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +/** + * This Mixin is specially designed so the Sodium initializer always runs even if mod initialization has failed. + */ +@Mixin(Minecraft.class) +public class EntrypointMixin { + @Inject(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/Options;loadSelectedResourcePacks(Lnet/minecraft/server/packs/repository/PackRepository;)V")) + private void sodium$loadConfig(GameConfig gameConfig, CallbackInfo ci) { + SodiumClientMod.onInitialization(ModList.get().getModContainerById("sodium").map(t -> t.getModInfo().getVersion().toString()).orElse("UNKNOWN")); + } +} diff --git a/neoforge/src/main/resources/sodium-forge.mixins.json b/neoforge/src/main/resources/sodium-forge.mixins.json index b1e8217a29..f57ceb17a1 100644 --- a/neoforge/src/main/resources/sodium-forge.mixins.json +++ b/neoforge/src/main/resources/sodium-forge.mixins.json @@ -18,6 +18,7 @@ "AuxiliaryLightManagerMixin", "ChunkRenderTypeSetAccessor", "ClientHooksMixin", + "EntrypointMixin", "LevelSliceMixin", "ModelDataMixin", "ResourcePackLoaderMixin",