Skip to content

Commit

Permalink
Merge pull request #13 from rhysdh540/master
Browse files Browse the repository at this point in the history
move plugin dependencies and MixinConfigMergingTransformer to buildSrc
  • Loading branch information
Nolij authored Apr 27, 2024
2 parents 391a0ea + d51f6cc commit 0cdb460
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 97 deletions.
75 changes: 0 additions & 75 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
@file:Suppress("UnstableApiUsage")
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
import me.modmuss50.mpp.HttpUtils
import me.modmuss50.mpp.PublishModTask
Expand All @@ -15,8 +11,6 @@ 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 @@ -334,75 +328,6 @@ 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",
"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"()
Expand Down
21 changes: 21 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,30 @@ plugins {

repositories {
mavenCentral()
maven("https://maven.fabricmc.net/")
maven("https://maven.glass-launcher.net/babric")
maven("https://repo.legacyfabric.net/repository/legacyfabric/")
maven("https://maven.wagyourtail.xyz/releases")
maven("https://maven.wagyourtail.xyz/snapshots")
gradlePluginPortal {
content {
excludeGroup("org.apache.logging.log4j")
}
}
}

fun DependencyHandler.plugin(id: String, version: String) {
this.implementation(group = id, name = "$id.gradle.plugin", version = version)
}

dependencies {
implementation("org.ow2.asm:asm-tree:9.7")
implementation("com.google.code.gson:gson:2.10.1")

implementation("org.apache.ant:ant:1.10.13")
plugin(id = "com.github.johnrengelman.shadow", version = "8.1.1")
plugin(id = "xyz.wagyourtail.unimined", version = "1.2.3")
plugin(id = "com.github.gmazzo.buildconfig", version = "5.2.0")
plugin(id = "org.ajoberstar.grgit", version = "5.2.2")
plugin(id = "me.modmuss50.mod-publish-plugin", version = "0.4.5")
}
84 changes: 84 additions & 0 deletions buildSrc/src/main/kotlin/MixinConfigMergingTransformer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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 org.gradle.api.file.FileTreeElement
import org.gradle.api.tasks.Input
import org.apache.tools.zip.ZipOutputStream
import org.apache.tools.zip.ZipEntry

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",
"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()
}

}
23 changes: 1 addition & 22 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,32 +1,11 @@
pluginManagement {
repositories {
mavenCentral()
maven("https://maven.fabricmc.net/")
maven("https://maven.glass-launcher.net/babric")
maven("https://repo.legacyfabric.net/repository/legacyfabric/")
maven("https://maven.wagyourtail.xyz/releases")
maven("https://maven.wagyourtail.xyz/snapshots")
gradlePluginPortal {
content {
excludeGroup("org.apache.logging.log4j")
}
}
gradlePluginPortal()
}
}

buildscript {
dependencies {
classpath("org.ow2.asm:asm-tree:9.7")
}
}

plugins {
id("org.gradle.toolchains.foojay-resolver-convention") version("0.7.0")
id("xyz.wagyourtail.unimined") version("1.2.3") apply(false)
id("com.github.gmazzo.buildconfig") version("5.2.0") apply(false)
id("com.github.johnrengelman.shadow") version("8.1.1") apply(false)
id("me.modmuss50.mod-publish-plugin") version("0.4.5") apply(false)
id("org.ajoberstar.grgit") version("5.2.2") apply(false)
}

rootProject.name = "zume"
Expand Down

0 comments on commit 0cdb460

Please sign in to comment.