maruohon / malilib

Library mod for masa's client-side Minecraft mods
GNU Lesser General Public License v3.0
294 stars 124 forks source link

Lazy load `SHADER_HUE` in GuiColorEditorHSV to prevent crashes from the class being loaded too early #82

Open solonovamax opened 2 years ago

solonovamax commented 2 years ago

I am currently using the InvMove mod, which does some cursed black magic, requiring classes to be loaded during the startup phase. This is so it can identify non-mc screens & allow the user to enable/disable movement with those screens.

The code for loading the classes can be found here

However, this causes an issue with malilib, when it attempts to load GuiColorEditorHSV. This is because it statically loads a ShaderProgram. If this ShaderProgram is loaded during the startup phase, the following exception will be thrown:

FATAL ERROR in native method: Thread[Render thread,5,main]: No context is current or a function that is not available in the current context was called. The JVM will abort execution.
    at org.lwjgl.opengl.GL20C.glCreateProgram(Native Method)
    at org.lwjgl.opengl.GL20.glCreateProgram(GL20.java:188)
    at fi.dy.masa.malilib.render.shader.ShaderProgram.init(ShaderProgram.java:38)
    at fi.dy.masa.malilib.render.shader.ShaderProgram.<init>(ShaderProgram.java:27)
    at fi.dy.masa.malilib.gui.GuiColorEditorHSV.<clinit>(GuiColorEditorHSV.java:24)
    at java.lang.Class.forName0(java.base@18.0.1/Native Method)
    at java.lang.Class.forName(java.base@18.0.1/Class.java:383)
    at java.lang.Class.forName(java.base@18.0.1/Class.java:376)
    at me.pieking1215.invmove.InvMoveConfig.readUnrecognizedConfig(InvMoveConfig.java:441)
    at me.pieking1215.invmove.InvMoveConfig.load(InvMoveConfig.java:336)
    at me.pieking1215.invmove.InvMove.init(InvMove.java:43)
    at me.pieking1215.invmove.fabric.InvMoveFabric.onInitializeClient(InvMoveFabric.java:35)
    at net.fabricmc.loader.impl.game.minecraft.Hooks$$Lambda$3914/0x0000000800b01850.accept(Unknown Source)
    at net.fabricmc.loader.impl.entrypoint.EntrypointUtils.invoke0(EntrypointUtils.java:47)
    at net.fabricmc.loader.impl.entrypoint.EntrypointUtils.invoke(EntrypointUtils.java:35)
    at net.fabricmc.loader.impl.game.minecraft.Hooks.startClient(Hooks.java:53)
    at fudge.notenoughcrashes.fabric.mixinhandlers.ModLoaders.fabricEntrypoints(ModLoaders.java:9)
    at net.minecraft.class_310.redirect$fdj000$catchFabricInit(class_310.java:21453)
    at net.minecraft.class_310.<init>(class_310.java:452)
    at net.minecraft.client.main.Main.main(Main.java:197)

A fix for this would be to lazy-load the ShaderProgram. It would be initialized to null, and only loaded when needed. Alternatively, move it to a separate class.