diff --git a/build.gradle.kts b/build.gradle.kts index 123e1c98..063f8062 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,4 +1,6 @@ import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer +import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext import groovy.json.JsonOutput import groovy.json.JsonSlurper import kotlinx.serialization.encodeToString @@ -12,6 +14,8 @@ import okhttp3.MultipartBody import okhttp3.Request import okhttp3.RequestBody.Companion.asRequestBody import okhttp3.internal.immutableListOf +import org.apache.tools.zip.ZipEntry +import org.apache.tools.zip.ZipOutputStream import org.objectweb.asm.ClassReader import org.objectweb.asm.ClassWriter import org.objectweb.asm.tree.ClassNode @@ -313,7 +317,83 @@ tasks.jar { enabled = false } +class MixinConfigMergingTransformer : Transformer { + private val JSON = JsonSlurper() + + @Input lateinit var modId: String + @Input lateinit var packageName: String + @Input lateinit var mixinPlugin: String + + override fun getName(): String { + return "MixinConfigMergingTransformer" + } + + override fun canTransformResource(element: FileTreeElement?): Boolean { + return element != null && (element.name.endsWith(".mixins.json") || element.name.endsWith("-refmap.json")) + } + + private var transformed = false + + private var mixins = ArrayList() + private var refMaps = HashMap>() + + override fun transform(context: TransformerContext?) { + if (context == null) + return + + this.transformed = true + + val parsed = JSON.parse(context.`is`) as Map<*, *> + if (parsed.contains("client")) { + @Suppress("UNCHECKED_CAST") + mixins.addAll(parsed["client"] as List) + } else { + @Suppress("UNCHECKED_CAST") + refMaps.putAll(parsed["mappings"] as Map>) + } + } + + override fun hasTransformedResource(): Boolean { + return transformed + } + + override fun modifyOutputStream(os: ZipOutputStream?, preserveFileTimestamps: Boolean) { + val mixinConfigEntry = ZipEntry("${modId}.mixins.json") + os!!.putNextEntry(mixinConfigEntry) + os.write(JsonOutput.prettyPrint(JsonOutput.toJson(mapOf( + "required" to true, + "minVersion" to "0.8", + "package" to packageName, + "plugin" to mixinPlugin, + "compatibilityLevel" to "JAVA_8", + "mixins" to emptyList(), + "client" to mixins, + "injectors" to mapOf( + "defaultRequire" to 1, + ), + "refmap" to "${modId}-refmap.json", + ))).toByteArray()) + + val refMapEntry = ZipEntry("${modId}-refmap.json") + os.putNextEntry(refMapEntry) + os.write(JsonOutput.prettyPrint(JsonOutput.toJson(mapOf( + "mappings" to refMaps, + ))).toByteArray()) + + transformed = false + mixins.clear() + refMaps.clear() + } + +} + tasks.shadowJar { + transform(MixinConfigMergingTransformer::class.java) { + modId = "mod_id"() + packageName = "dev.nolij.zume.mixin" + mixinPlugin = "dev.nolij.zume.impl.ZumeMixinPlugin" + } + val shadowJar = this from("LICENSE") { rename { "${it}_${"mod_id"()}" } @@ -358,7 +438,7 @@ tasks.shadowJar { attributes( "FMLCorePluginContainsFMLMod" to true, "ForceLoadAsMod" to true, - "MixinConfigs" to lexForgeImpls.joinToString(",") { "zume-${it}.mixins.json" }, + "MixinConfigs" to "zume.mixins.json", "TweakClass" to "org.spongepowered.asm.launch.MixinTweaker", ) } diff --git a/src/main/resources/fabric.mod.json b/src/main/resources/fabric.mod.json index 635fd586..b8f0291d 100644 --- a/src/main/resources/fabric.mod.json +++ b/src/main/resources/fabric.mod.json @@ -29,11 +29,7 @@ "dev.nolij.zume.primitive.event.KeyBindingRegistrar" ] }, - "mixins": [ - "zume-modern.mixins.json", - "zume-legacy.mixins.json", - "zume-primitive.mixins.json" - ], + "mixins": ["zume.mixins.json"], "depends": { "fabricloader": [">=${fabric_version}", "${babric_version}"] }