Replace the GL info crash callable with a safer version

Closes #11
This commit is contained in:
Raven Szewczyk committed 2023-02-01 18:38:20 +00:00
1 parent 62fbea4aa0
commit 869f8b39ae
4 files changed
+68 -4

No files matched your search

+2 -2
View File
@@ -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
-2
View File
@@ -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")
}
@@ -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<ICrashCallable> crashCallables =
(List<ICrashCallable>) 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) {
@@ -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();
}
}
}