From 8d0bd35b3caa6cbfa3abb89e290cb71b1f90f997 Mon Sep 17 00:00:00 2001 From: zani Date: Sat, 19 Oct 2024 00:31:42 -0600 Subject: [PATCH] refactor: switch to using a tweaker --- versions/build.gradle.kts | 6 +- .../impl/forge/asm/ClassTransformer.kt | 11 +--- .../platform/impl/forge/asm/SpiceTweaker.kt | 56 +++++++++++++++++++ .../impl/forge/asm/TransformerPlugin.kt | 27 --------- 4 files changed, 59 insertions(+), 41 deletions(-) create mode 100644 versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/SpiceTweaker.kt delete mode 100644 versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/TransformerPlugin.kt diff --git a/versions/build.gradle.kts b/versions/build.gradle.kts index 8d932ea..784f092 100644 --- a/versions/build.gradle.kts +++ b/versions/build.gradle.kts @@ -12,8 +12,7 @@ plugins { `java-library` } -val tweakClass = "org.spongepowered.asm.launch.MixinTweaker" -val transformerPlugin = "org.polyfrost.spice.platform.impl.forge.asm.TransformerPlugin" +val tweakClass = "org.polyfrost.spice.platform.impl.forge.asm.SpiceTweaker" base.archivesName = "Spice-${platform}" @@ -26,7 +25,6 @@ loom { runConfigs { "client" { if (project.platform.isLegacyForge) { - property("fml.coreMods.load", transformerPlugin) programArgs("--tweakClass", tweakClass) } property("mixin.debug.export", "true") @@ -104,8 +102,6 @@ tasks { if (platform.isLegacyForge) { manifest { attributes += mapOf( - "FMLCorePluginContainsFMLMod" to "Yes, yes it does", - "FMLCorePlugin" to transformerPlugin, "ModSide" to "CLIENT", "ForceLoadAsMod" to true, "TweakOrder" to "0", diff --git a/versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/ClassTransformer.kt b/versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/ClassTransformer.kt index 21632e8..4c24b13 100644 --- a/versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/ClassTransformer.kt +++ b/versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/ClassTransformer.kt @@ -2,7 +2,7 @@ package org.polyfrost.spice.platform.impl.forge.asm //#if FORGE import com.google.common.base.Stopwatch -import net.minecraft.launchwrapper.LaunchClassLoader +import net.minecraft.launchwrapper.Launch import net.minecraft.launchwrapper.LogWrapper import org.apache.logging.log4j.LogManager import org.objectweb.asm.ClassReader @@ -29,14 +29,7 @@ class ClassTransformer : LaunchTransformer, Transformer { private val transformerCache: Map - private val loader = - ClassTransformer::class.java - .classLoader - .let { - assert(it is LaunchClassLoader) { "Class loader isn't LaunchClassLoader..?" } - - it as LaunchClassLoader - } + private val loader = Launch.classLoader private val logger = LogManager.getLogger("Spice/Forge/Transformer") private val transformers = mutableListOf() diff --git a/versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/SpiceTweaker.kt b/versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/SpiceTweaker.kt new file mode 100644 index 0000000..35ce22c --- /dev/null +++ b/versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/SpiceTweaker.kt @@ -0,0 +1,56 @@ +package org.polyfrost.spice.platform.impl.forge.asm +//#if FORGE + +import net.minecraft.launchwrapper.ITweaker +import net.minecraft.launchwrapper.Launch +import net.minecraft.launchwrapper.LaunchClassLoader +import org.spongepowered.asm.launch.MixinBootstrap +import org.spongepowered.asm.mixin.Mixins +import java.io.File + + +class SpiceTweaker : ITweaker { + private val mixinTweaker: String = "org.spongepowered.asm.launch.MixinTweaker" + + init { + injectMixinTweaker() + } + + override fun acceptOptions(args: MutableList, gameDir: File?, assetsDir: File?, profile: String?) { + + } + + override fun injectIntoClassLoader(classLoader: LaunchClassLoader) { + MixinBootstrap.init() + Mixins.addConfiguration("spice.mixins.json") + + Launch.classLoader.registerTransformer(ClassTransformer::class.java.name) + } + + override fun getLaunchTarget(): String? = null + override fun getLaunchArguments(): Array = arrayOf() + + @Suppress("UNCHECKED_CAST") + @Throws(ClassNotFoundException::class, IllegalAccessException::class, InstantiationException::class) + private fun injectMixinTweaker() { + val tweakClasses = Launch.blackboard["TweakClasses"]!! as List + + if (tweakClasses.contains(mixinTweaker)) { + initMixinTweaker() + return + } + + if (Launch.blackboard["mixin.initialised"] != null) return + + (Launch.blackboard["Tweaks"]!! as MutableList).add(initMixinTweaker()) + } + + @Throws(ClassNotFoundException::class, IllegalAccessException::class, InstantiationException::class) + private fun initMixinTweaker(): ITweaker { + Launch.classLoader.addClassLoaderExclusion(mixinTweaker.substring(0, mixinTweaker.lastIndexOf("."))) + + return Class.forName(mixinTweaker, true, Launch.classLoader).newInstance() as ITweaker + } +} + +//#endif diff --git a/versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/TransformerPlugin.kt b/versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/TransformerPlugin.kt deleted file mode 100644 index 39d7eed..0000000 --- a/versions/src/main/kotlin/org/polyfrost/spice/platform/impl/forge/asm/TransformerPlugin.kt +++ /dev/null @@ -1,27 +0,0 @@ -package org.polyfrost.spice.platform.impl.forge.asm -//#if FORGE - -import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin - -@IFMLLoadingPlugin.Name("Spice") -@IFMLLoadingPlugin.MCVersion("1.8.9") -class TransformerPlugin : IFMLLoadingPlugin { - override fun getASMTransformerClass(): Array { - return arrayOf(ClassTransformer::class.java.getName()) - } - - override fun getModContainerClass(): String? { - return null - } - - override fun getSetupClass(): String? { - return null - } - - override fun injectData(map: Map) {} - override fun getAccessTransformerClass(): String? { - return null - } -} - -//#endif