diff --git a/gradle.properties b/gradle.properties index 36537b3..081263b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,12 +3,12 @@ org.gradle.jvmargs=-Xmx1G org.gradle.parallel=true # BTA -bta_version=nightly-2024-09-10 +bta_version=nightly-2024-09-19 # Loader loader_version=0.15.6-babric.6-bta # Mod Properties - mod_version = 1.0.0 + mod_version = 1.0.1 maven_group = com.github.zarzelcow archives_base_name = legacy-lwjgl3 diff --git a/src/main/java/com/github/zarzelcow/legacylwjgl3/EarlyRiser.kt b/src/main/java/com/github/zarzelcow/legacylwjgl3/EarlyRiser.kt deleted file mode 100644 index 16b0c37..0000000 --- a/src/main/java/com/github/zarzelcow/legacylwjgl3/EarlyRiser.kt +++ /dev/null @@ -1,184 +0,0 @@ -package com.github.zarzelcow.legacylwjgl3 - -import javassist.* -import javassist.util.proxy.DefineClassHelper -import net.fabricmc.loader.impl.launch.FabricLauncherBase -import org.lwjgl.openal.ALCapabilities -import org.lwjgl.opengl.GL - -/** - * Uses gross hacks to "redefine" classes that i couldnt find a way to edit with mixins. - * - * Uses javassist to edit the classes e.x adding fields and methods for compatibility with legacy LWJGL2 code. - * - * @author Zarzelcow - */ -class EarlyRiser : Runnable { - override fun run() { - LegacyLWJGL3.LOGGER.debug("EarlyRiser running") - val pool = ClassPool() - pool.appendClassPath(LoaderClassPath(FabricLauncherBase.getLauncher().targetClassLoader)) // knot class loader contains the fat jar with lwjgl 3 bundled - - macroRedefineWithErrorHandling(pool, ::addMissingGLCapabilities) - macroRedefineWithErrorHandling(pool, ::addLegacyCompatibilityMethodsToGL11) - macroRedefineWithErrorHandling(pool, ::gl20AddCompatibilityMethods) - macroRedefineWithErrorHandling(pool, ::copyAlExtensions) - macroRedefineWithErrorHandling(pool, ::addLegacyCompatibilityMethodsToAL10) - } - - // rather than handling errors is every method manually, this will do it for you - private inline fun macroRedefineWithErrorHandling(pool: ClassPool, method: (ClassPool) -> Unit) { - try { - method(pool) - } catch (e: Exception) { - LegacyLWJGL3.LOGGER.error("Failed in early riser while attempting to do hacky things", e) - } - } - - companion object { - // Adds missing extension checks from LWJGL2 for use in ContextCapabilities - private fun addMissingGLCapabilities(classPool: ClassPool) { - with(classPool.get("org.lwjgl.opengl.GLCapabilities")) { - this.addField(CtField.make("public final boolean GL_EXT_multi_draw_arrays;", this)) - this.addField(CtField.make("public final boolean GL_EXT_paletted_texture;", this)) - this.addField(CtField.make("public final boolean GL_EXT_rescale_normal;", this)) - this.addField(CtField.make("public final boolean GL_EXT_texture_3d;", this)) - this.addField(CtField.make("public final boolean GL_EXT_texture_lod_bias;", this)) - this.addField(CtField.make("public final boolean GL_EXT_vertex_shader;", this)) - this.addField(CtField.make("public final boolean GL_EXT_vertex_weighting;", this)) - val constructor = - this.getConstructor("(Lorg/lwjgl/system/FunctionProvider;Ljava/util/Set;ZLjava/util/function/IntFunction;)V") - constructor.insertAfter( - """ - GL_EXT_multi_draw_arrays = ext.contains("GL_EXT_multi_draw_arrays"); - GL_EXT_paletted_texture = ext.contains("GL_EXT_paletted_texture"); - GL_EXT_rescale_normal = ext.contains("GL_EXT_rescale_normal"); - GL_EXT_texture_3d = ext.contains("GL_EXT_texture_3d"); - GL_EXT_texture_lod_bias = ext.contains("GL_EXT_texture_lod_bias"); - GL_EXT_vertex_shader = ext.contains("GL_EXT_vertex_shader"); - GL_EXT_vertex_weighting = ext.contains("GL_EXT_vertex_weighting"); - """.trimIndent() - ) - defineCtClass(this, GL::class.java, classPool.classLoader) - } - } - - // list of legacy methods that we need to add to GL11. moved out of method for readability - private val gl11Translations = listOf( - Triple("glGetFloat", "glGetFloatv", "(ILjava/nio/FloatBuffer;)V"), - Triple("glGetInteger", "glGetIntegerv", "(ILjava/nio/IntBuffer;)V"), - Triple("glFog", "glFogfv", "(ILjava/nio/FloatBuffer;)V"), - Triple("glLight", "glLightfv", "(IILjava/nio/FloatBuffer;)V"), - Triple("glLightModel", "glLightModelfv", "(ILjava/nio/FloatBuffer;)V"), - Triple("glMultMatrix", "glMultMatrixf", "(Ljava/nio/FloatBuffer;)V"), - Triple("glTexEnv", "glTexEnvfv", "(IILjava/nio/FloatBuffer;)V") - ) - - private val al10Translations = listOf( - Triple("alListener", "alListenerfv", "(ILjava/nio/FloatBuffer;)V"), - Triple("alSource", "alSourcefv", "(IILjava/nio/FloatBuffer;)V"), - Triple("alSourceStop", "alSourceStopv", "(Ljava/nio/IntBuffer;)V") - ) - - /* - * Adds a few legacy methods to GL11 like glGetFloat which was renamed to glGetFloatv in LWJGL3 - */ - fun addLegacyCompatibilityMethodsToGL11(classPool: ClassPool) { - val cc = classPool.get("org.lwjgl.opengl.GL11") - for ((legacy, current, desc) in gl11Translations) { - val original = cc.getMethod(current, desc) - // copy original method and rename it - val renamed = CtNewMethod.copy(original, cc, null) - .rename(legacy) - - cc.addMethod(renamed) - debug("Added legacy compat method $legacy") - } - defineCtClass(cc, GL::class.java, classPool.classLoader) - } - - // new GL20 doesn't have a way to supply a shader source using ByteBuffer so this adds a method to do it - fun gl20AddCompatibilityMethods(classPool: ClassPool) { - val cc = classPool.get("org.lwjgl.opengl.GL20") - val code = """ - public static void glShaderSource(int shader, java.nio.ByteBuffer string) { - byte[] data = new byte[string.limit()]; - string.position(0); - string.get(data); - string.position(0); - org.lwjgl.opengl.GL20.glShaderSource(shader, new String(data)); - } - """.trimIndent() - cc.addMethod(CtNewMethod.make(code, cc)) - defineCtClass(cc, GL::class.java, classPool.classLoader) - } - - /** - * It copies all the methods and fields from the extension class to the real class - */ - fun copyAlExtensions(classPool: ClassPool) { - val extension = classPool.get("com.github.zarzelcow.legacylwjgl3.ALExtensions") - val target = classPool.get("org.lwjgl.openal.AL") - // this code is hacky, but it replaces the stub method with the real one - target.getMethod("destroy", "()V") - .rename("al_destroy") // rename destroy to al_destroy so it doesnt conflict - extension.removeMethod( - extension.getMethod( - "al_destroy", - "()V" - ) - ) // remove stub method from extension so it calls the real one - - // copy methods and felds from cc to target - for (method in extension.declaredMethods) { - val copied = CtNewMethod.copy(method, target, null) - // dont add method if it already exists - target.addMethod(copied) - debug("Added AL extension method ${method.name}") - } - - for (field in extension.declaredFields) { - val copied = CtField(field, target) - target.addField(copied) - debug("Added AL extension field ${field.name}") - } - defineCtClass(target, ALCapabilities::class.java, classPool.classLoader) - } - - /** - * Adds legacy compatibility methods to AL10 - */ - fun addLegacyCompatibilityMethodsToAL10(classPool: ClassPool) { - val cc = classPool.get("org.lwjgl.openal.AL10") - for ((legacy, current, desc) in al10Translations) { - val original = cc.getMethod(current, desc) - // copy original method and rename it - val copied = CtNewMethod.copy(original, cc, null) - cc.addMethod(copied.rename(legacy)) - - debug("Added legacy compat method $legacy") - } - defineCtClass(cc, ALCapabilities::class.java, classPool.classLoader) - } - - private fun CtMethod.rename(legacy: String): CtMethod { - this.name = legacy - return this - } - - private fun debug(message: String) = LegacyLWJGL3.LOGGER.debug(message) - - // all the Javassist [CtClass.toClass] methods use new java features, - // DefineClassHelper.toClass has support for much older versions of java so use that instead - private fun defineCtClass(cc: CtClass, neighbor: Class<*>, classLoader: ClassLoader) { - DefineClassHelper.toClass( - cc.name, - neighbor, - classLoader, - null, - cc.toBytecode() - ) - cc.detach() - } - } -} \ No newline at end of file diff --git a/src/main/java/com/github/zarzelcow/legacylwjgl3/EarlyRiserLoader.java b/src/main/java/com/github/zarzelcow/legacylwjgl3/EarlyRiserLoader.java deleted file mode 100644 index 029f4d0..0000000 --- a/src/main/java/com/github/zarzelcow/legacylwjgl3/EarlyRiserLoader.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.github.zarzelcow.legacylwjgl3; - -import net.fabricmc.loader.impl.launch.FabricLauncherBase; -import org.objectweb.asm.tree.ClassNode; -import org.spongepowered.asm.mixin.extensibility.IMixinConfigPlugin; -import org.spongepowered.asm.mixin.extensibility.IMixinInfo; - -import java.nio.file.Path; -import java.util.Collections; -import java.util.List; -import java.util.Optional; -import java.util.Set; - -/* - * Attempts to fix issues from LWJGL 2 and LWJGL 3 being in the classpath at the same time - * and runs [EarlyRiser] which uses a very hacky way to modify LWJGL 3 classes - * - * Uses a MixinConfigPlugin to transform classes before they get loaded - * Idea taken from https://github.com/Chocohead/Fabric-ASM 🥰 - */ -public class EarlyRiserLoader implements IMixinConfigPlugin { - @Override - public void onLoad(String mixinPackage) { - // Add paulscode to the knot classpath (specifically the librarylwjglopenal as that load lwjgl classes). - // by default, paulscode is a part of the system class loader which only has LWJGL 2 in its classpath - // adding it to the knot classpath means paulscode will instead load classes with the knot class loader - // meaning it will load LWJGL 3 classes from the knot classpath instead of LWJGL 2 from the system classpath - // - // This was such a pain to figure out and caused me so much trouble - getPaulscodePath().ifPresent(FabricLauncherBase.getLauncher()::addToClassPath); - // taken from https://github.com/Chocohead/Fabric-ASM - new EarlyRiser().run(); - } - - public static Optional getPaulscodePath() { - for (Path path : FabricLauncherBase.getLauncher().getClassPath()) { - if (path.toString().contains("librarylwjglopenal")) { - return Optional.of(path); - } - } - return Optional.empty(); - } - - @Override - public String getRefMapperConfig() { - return null; - } - - @Override - public boolean shouldApplyMixin(String targetClassName, String mixinClassName) { - return true; - } - - @Override - public void acceptTargets(Set myTargets, Set otherTargets) { - } - - @Override - public List getMixins() { - return Collections.emptyList(); - } - - @Override - public void preApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { - } - - @Override - public void postApply(String targetClassName, ClassNode targetClass, String mixinClassName, IMixinInfo mixinInfo) { - } -} diff --git a/src/main/java/com/github/zarzelcow/legacylwjgl3/LegacyLWJGL3.java b/src/main/java/com/github/zarzelcow/legacylwjgl3/LegacyLWJGL3.java deleted file mode 100644 index 34efd2d..0000000 --- a/src/main/java/com/github/zarzelcow/legacylwjgl3/LegacyLWJGL3.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.github.zarzelcow.legacylwjgl3; - -import net.fabricmc.api.ModInitializer; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -public class LegacyLWJGL3 implements ModInitializer { - public static final Logger LOGGER = LogManager.getLogger(); - - @Override - public void onInitialize() { - LOGGER.info("This is definitely a 100% legit legacy LWJGL2 mod!"); - } -} diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 32b067e..2454c2a 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -12,16 +12,12 @@ "contact": { "sources": "https://github.com/Zarzelcow/legacy-lwjgl3" }, - "license": "BSD-Zero", + "license": "LGPL-2.1", "icon": "assets/legacy-lwjgl3/icon.png", "environment": "client", "entrypoints": { - "main": [ - "com.github.zarzelcow.legacylwjgl3.LegacyLWJGL3" - ] }, "mixins": [ - "legacy-lwjgl3.mixins.json" ], "depends": { } diff --git a/src/main/resources/legacy-lwjgl3.mixins.json b/src/main/resources/legacy-lwjgl3.mixins.json index 684bcb5..4acc377 100644 --- a/src/main/resources/legacy-lwjgl3.mixins.json +++ b/src/main/resources/legacy-lwjgl3.mixins.json @@ -2,7 +2,6 @@ "required": true, "minVersion": "0.8", "package": "com.github.zarzelcow.legacylwjgl3.mixin", - "plugin": "com.github.zarzelcow.legacylwjgl3.EarlyRiserLoader", "compatibilityLevel": "JAVA_8", "client": [ ],