Skip to content

Commit

Permalink
refactor: switch to using a tweaker
Browse files Browse the repository at this point in the history
  • Loading branch information
Zxnii committed Oct 19, 2024
1 parent 5c26459 commit 8d0bd35
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 41 deletions.
6 changes: 1 addition & 5 deletions versions/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand All @@ -26,7 +25,6 @@ loom {
runConfigs {
"client" {
if (project.platform.isLegacyForge) {
property("fml.coreMods.load", transformerPlugin)
programArgs("--tweakClass", tweakClass)
}
property("mixin.debug.export", "true")
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,14 +29,7 @@ class ClassTransformer : LaunchTransformer, Transformer {

private val transformerCache: Map<String, ByteArray>

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<IClassTransformer>()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String>, 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<String> = arrayOf()

@Suppress("UNCHECKED_CAST")
@Throws(ClassNotFoundException::class, IllegalAccessException::class, InstantiationException::class)
private fun injectMixinTweaker() {
val tweakClasses = Launch.blackboard["TweakClasses"]!! as List<String>

if (tweakClasses.contains(mixinTweaker)) {
initMixinTweaker()
return
}

if (Launch.blackboard["mixin.initialised"] != null) return

(Launch.blackboard["Tweaks"]!! as MutableList<ITweaker>).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

This file was deleted.

0 comments on commit 8d0bd35

Please sign in to comment.