From 869f8b39ae973547a0b830d1833db147cdd474c0 Mon Sep 17 00:00:00 2001 From: Raven Szewczyk Date: Wed, 1 Feb 2023 18:30:11 +0000 Subject: [PATCH] Replace the GL info crash callable with a safer version Closes #11 --- build.gradle | 4 +- dependencies.gradle | 2 - .../me/eigenraven/lwjgl3ify/ClientProxy.java | 29 +++++++++++++++ .../lwjgl3ify/client/GLInfoCrashCallable.java | 37 +++++++++++++++++++ 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 src/main/java/me/eigenraven/lwjgl3ify/client/GLInfoCrashCallable.java diff --git a/build.gradle b/build.gradle index e2e8960..447de38 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,4 @@ -//version: 1675173326 +//version: 1675268365 /* DO NOT CHANGE THIS FILE! Also, you may replace this file at any time if there is an update available. @@ -66,7 +66,7 @@ plugins { id 'com.diffplug.spotless' version '6.7.2' apply false id 'com.modrinth.minotaur' version '2.+' apply false id 'com.matthewprenger.cursegradle' version '1.4.0' apply false - id 'com.gtnewhorizons.retrofuturagradle' version '1.1.3' + id 'com.gtnewhorizons.retrofuturagradle' version '1.1.4' } boolean settingsupdated = verifySettingsGradle() settingsupdated = verifyGitAttributes() || settingsupdated diff --git a/dependencies.gradle b/dependencies.gradle index 6a8c59b..8df8983 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -36,8 +36,6 @@ dependencies { runtimeOnly("com.github.GTNewHorizons:MrTJPCore:1.1.4:dev") runtimeOnly("com.github.GTNewHorizons:ForgeMultipart:1.3.1:dev") runtimeOnly("com.github.GTNewHorizons:ForgeRelocation:0.0.3:dev") - runtimeOnly("com.github.GTNewHorizons:inventory-tweaks:1.5.15:dev") - runtimeOnly("com.github.GTNewHorizons:MouseTweaks:2.4.7-GTNH:dev") reobfJarConfiguration group: 'curse.maven', name: 'cofh-core-69162', version: '2388750' runtimeOnly("com.github.GTNewHorizons:AppleCore:3.2.6-pre:dev") } diff --git a/src/main/java/me/eigenraven/lwjgl3ify/ClientProxy.java b/src/main/java/me/eigenraven/lwjgl3ify/ClientProxy.java index 73e3c1f..327597b 100644 --- a/src/main/java/me/eigenraven/lwjgl3ify/ClientProxy.java +++ b/src/main/java/me/eigenraven/lwjgl3ify/ClientProxy.java @@ -1,6 +1,11 @@ package me.eigenraven.lwjgl3ify; +import cpw.mods.fml.common.FMLCommonHandler; +import cpw.mods.fml.common.ICrashCallable; import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import java.lang.reflect.Field; +import java.util.List; +import me.eigenraven.lwjgl3ify.client.GLInfoCrashCallable; import me.eigenraven.lwjgl3ify.core.Config; import net.minecraft.client.Minecraft; import net.minecraftforge.client.event.RenderGameOverlayEvent; @@ -18,11 +23,35 @@ public class ClientProxy extends CommonProxy { javaVersion = javaVersionRaw; } + @Override + public void runCompatHooks() { + super.runCompatHooks(); + replaceOpenGLCrashHandler(); + } + @Override public void registerF3Handler() { MinecraftForge.EVENT_BUS.register(this); } + private void replaceOpenGLCrashHandler() { + // Use a crash handler that's safe against being called from another thread + try { + final Field callablesField = FMLCommonHandler.class.getDeclaredField("crashCallables"); + callablesField.setAccessible(true); + List crashCallables = + (List) callablesField.get(FMLCommonHandler.instance()); + for (int i = 0; i < crashCallables.size(); i++) { + ICrashCallable original = crashCallables.get(i); + if ("GL info".equals(original.getLabel()) && !(original instanceof GLInfoCrashCallable)) { + crashCallables.set(i, new GLInfoCrashCallable()); + } + } + } catch (ReflectiveOperationException e) { + Lwjgl3ify.LOG.error("Could not access crash callables to make them OpenGL thread-safe", e); + } + } + @SubscribeEvent @SuppressWarnings("unused") // event handler public void onRenderGameOverlayTextEvent(RenderGameOverlayEvent.Text event) { diff --git a/src/main/java/me/eigenraven/lwjgl3ify/client/GLInfoCrashCallable.java b/src/main/java/me/eigenraven/lwjgl3ify/client/GLInfoCrashCallable.java new file mode 100644 index 0000000..8e3c06b --- /dev/null +++ b/src/main/java/me/eigenraven/lwjgl3ify/client/GLInfoCrashCallable.java @@ -0,0 +1,37 @@ +package me.eigenraven.lwjgl3ify.client; + +import static org.lwjgl.opengl.GL11.GL_RENDERER; +import static org.lwjgl.opengl.GL11.GL_VENDOR; +import static org.lwjgl.opengl.GL11.GL_VERSION; +import static org.lwjgl.opengl.GL11.glGetString; + +import cpw.mods.fml.common.ICrashCallable; +import org.lwjgl.opengl.GL; + +public class GLInfoCrashCallable implements ICrashCallable { + @Override + public String getLabel() { + return "GL info"; + } + + @Override + public String call() throws Exception { + boolean hasGlContext = true; + try { + if (GL.getCapabilities() == null) { + hasGlContext = false; + } + } catch (IllegalStateException ise) { + // No OpenGL context in current thread + hasGlContext = false; + } + if (hasGlContext) { + return "' Vendor: '" + glGetString(GL_VENDOR) + "' Version: '" + + glGetString(GL_VERSION) + "' Renderer: '" + + glGetString(GL_RENDERER) + "'"; + } else { + return "No OpenGL context present in the calling thread: " + + Thread.currentThread().getName(); + } + } +}