Skip to content

Commit

Permalink
merge all mixin configs and refmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Nolij committed Apr 26, 2024
1 parent 16c6a46 commit 4d05381
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 6 deletions.
82 changes: 81 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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<String>()
private var refMaps = HashMap<String, Map<String, String>>()

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<String>)
} else {
@Suppress("UNCHECKED_CAST")
refMaps.putAll(parsed["mappings"] as Map<String, Map<String, String>>)
}
}

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<String>(),
"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"()}" }
Expand Down Expand Up @@ -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",
)
}
Expand Down
6 changes: 1 addition & 5 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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}"]
}
Expand Down

0 comments on commit 4d05381

Please sign in to comment.