Skip to content

Commit

Permalink
Do not reference Minecraft classes in module scanner and message box
Browse files Browse the repository at this point in the history
  • Loading branch information
IMS212 committed Sep 7, 2024
1 parent e1bae36 commit caf1fdd
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class WindowMixin {
@Final
private static Logger LOGGER;

@Shadow
@Final
private long window;

@Unique
private long wglPrevContext = MemoryUtil.NULL;

Expand Down Expand Up @@ -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))
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@
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;

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
Expand All @@ -40,7 +36,7 @@ public class ModuleScanner {
"GTIII-OSD64.dll", "GTIII-OSD.dll"
};

public static void checkModules() {
public static void checkModules(long window) {
List<String> modules;

try {
Expand All @@ -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<NativeModuleLister.NativeModuleInfo> enumerateLoadedModules() {
List<NativeModuleLister.NativeModuleInfo> 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<String> listModules() {
if (!Platform.isWindows()) {
return ImmutableList.of();
Expand All @@ -97,7 +81,7 @@ private static List<String> 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;
Expand All @@ -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.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 */);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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)
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
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;

@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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 = "<init>", 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"));
}
}
1 change: 1 addition & 0 deletions neoforge/src/main/resources/sodium-forge.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"AuxiliaryLightManagerMixin",
"ChunkRenderTypeSetAccessor",
"ClientHooksMixin",
"EntrypointMixin",
"LevelSliceMixin",
"ModelDataMixin",
"ResourcePackLoaderMixin",
Expand Down

0 comments on commit caf1fdd

Please sign in to comment.