Skip to content

Commit

Permalink
fix caching for CompressJarTask (#25)
Browse files Browse the repository at this point in the history
* fix caching for CompressJarTask

* remove old comments

* undo the patch thing
  • Loading branch information
rhysdh540 authored Nov 5, 2024
1 parent 2107ce0 commit 41aab75
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 33 deletions.
13 changes: 6 additions & 7 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ val patchHistory = releaseTags
.map { name -> name.substring(minorTagPrefix.length) }

val maxPatch = patchHistory.maxOfOrNull { it.substringBefore('-').toInt() }
val patch =
val patch =
maxPatch?.plus(
if (patchHistory.contains(maxPatch.toString()))
releaseIncrement
Expand Down Expand Up @@ -175,11 +175,6 @@ allprojects {
apply(plugin = "maven-publish")

repositories {
mavenCentral {
content {
excludeGroup("ca.weblite")
}
}
maven("https://repo.spongepowered.org/maven")
maven("https://jitpack.io/")
exclusiveContent {
Expand All @@ -201,6 +196,7 @@ allprojects {
languageVersion = JavaLanguageVersion.of(21)
}
options.compilerArgs.addAll(arrayOf("-Xplugin:Manifold no-bootstrap", "-Xplugin:jabel"))
options.forkOptions.jvmArgs?.add("-XX:+EnableDynamicAgentLoading")
}
}

Expand Down Expand Up @@ -402,7 +398,7 @@ tasks.shadowJar {
exclude("LICENSE_libnolij")

configurations = immutableListOf(shade)
archiveClassifier = null
archiveClassifier = "deobfuscated"
isPreserveFileTimestamps = false
isReproducibleFileOrder = true

Expand Down Expand Up @@ -457,6 +453,9 @@ val compressJar = tasks.register<CompressJarTask>("compressJar") {

val shadowJar = tasks.shadowJar.get()
inputJar = shadowJar.archiveFile.get().asFile
outputJar = shadowJar.archiveFile.get().asFile.let {
it.parentFile.resolve("${it.nameWithoutExtension.removeSuffix("-deobfuscated")}.jar")
}

deflateAlgorithm = releaseChannel.deflation
jsonShrinkingType = releaseChannel.json
Expand Down
56 changes: 31 additions & 25 deletions buildSrc/src/main/kotlin/dev/nolij/zumegradle/JarCompressing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ import net.fabricmc.mappingio.format.MappingFormat
import net.fabricmc.mappingio.tree.MappingTree.ClassMapping
import net.fabricmc.mappingio.tree.MemoryMappingTree
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.*
import org.gradle.api.tasks.options.Option
import org.gradle.kotlin.dsl.support.uppercaseFirstChar
import org.objectweb.asm.ClassReader
Expand Down Expand Up @@ -82,7 +79,7 @@ fun squishJar(jar: File, jsonProcessing: JsonShrinkingType, mappingsFile: File?)
}

if (name.endsWith(".class")) {
bytes = processClassFile(bytes, mappings!!)
bytes = processClassFile(bytes, mappings)
}

out.putNextEntry(JarEntry(name))
Expand All @@ -97,25 +94,27 @@ fun squishJar(jar: File, jsonProcessing: JsonShrinkingType, mappingsFile: File?)
private fun remapMixinConfig(bytes: ByteArray, mappings: MemoryMappingTree): ByteArray {
val json = (JsonSlurper().parse(bytes) as Map<String, Any>).toMutableMap()
val old = json["plugin"] as String
val obf = mappings.obfuscate(old)
val obf = mappings.map(old)
json["plugin"] = obf

json["package"] = "zume.mixin"

return JsonOutput.toJson(json).toByteArray()
}

private fun processClassFile(bytes: ByteArray, mappings: MemoryMappingTree): ByteArray {
private fun processClassFile(bytes: ByteArray, mappings: MemoryMappingTree?): ByteArray {
val classNode = ClassNode()
ClassReader(bytes).accept(classNode, 0)

for (annotation in classNode.visibleAnnotations ?: emptyList()) {
if (annotation.desc.endsWith("fml/common/Mod;")) {
for (i in 0 until annotation.values.size step 2) {
if (annotation.values[i] == "guiFactory") {
val old = annotation.values[i + 1] as String
annotation.values[i + 1] = mappings.obfuscate(old)
println("Remapped guiFactory: $old -> ${annotation.values[i + 1]}")
if(mappings != null) {
for (annotation in classNode.visibleAnnotations ?: emptyList()) {
if (annotation.desc.endsWith("fml/common/Mod;")) {
for (i in 0 until annotation.values.size step 2) {
if (annotation.values[i] == "guiFactory") {
val old = annotation.values[i + 1] as String
annotation.values[i + 1] = mappings.map(old)
println("Remapped guiFactory: $old -> ${annotation.values[i + 1]}")
}
}
}
}
Expand Down Expand Up @@ -262,9 +261,10 @@ fun applyProguard(jar: File, minecraftConfigs: List<MinecraftConfig>, configDir:
}
}

open class CompressJarTask : DefaultTask() {
@InputFile
lateinit var inputJar: File
abstract class CompressJarTask : DefaultTask() {
@get:InputFile
@get:PathSensitive(PathSensitivity.RELATIVE)
abstract val inputJar: RegularFileProperty

@Input
var deflateAlgorithm = DeflateAlgorithm.LIBDEFLATE
Expand All @@ -278,13 +278,15 @@ open class CompressJarTask : DefaultTask() {
private var minecraftConfigs: List<MinecraftConfig> = emptyList()

@get:OutputFile
val outputJar get() = inputJar // compressed jar will replace the input jar
abstract val outputJar: RegularFileProperty

@get:OutputFile
@get:Optional
val mappingsFile
get() = if (useProguard)
inputJar.parentFile.resolve("${inputJar.nameWithoutExtension}-mappings.txt")
inputJar.get().asFile.let {
it.parentFile.resolve("${it.nameWithoutExtension}-mappings.txt")
}
else null

@Option(option = "compression-type", description = "How to recompress the jar")
Expand All @@ -306,10 +308,14 @@ open class CompressJarTask : DefaultTask() {

@TaskAction
fun compressJar() {
if (useProguard)
applyProguard(inputJar, minecraftConfigs, project.rootDir)
squishJar(inputJar, jsonShrinkingType, mappingsFile)
deflate(outputJar, deflateAlgorithm)
val jar = inputJar.get().asFile
val temp = jar.copyTo(temporaryDir.resolve("temp.jar"), true)
if(useProguard) {
applyProguard(temp, minecraftConfigs, project.rootDir)
}
squishJar(temp, jsonShrinkingType, mappingsFile)
deflate(temp, deflateAlgorithm)
temp.copyTo(outputJar.get().asFile, true)
}
}

Expand All @@ -324,7 +330,7 @@ fun mappings(file: File, format: MappingFormat = MappingFormat.PROGUARD): Memory
}

@Suppress("INACCESSIBLE_TYPE", "NAME_SHADOWING")
fun MemoryMappingTree.obfuscate(src: String): String {
fun MemoryMappingTree.map(src: String): String {
val src = src.replace('.', '/')
val dstNamespaceIndex = getNamespaceId(dstNamespaces[0])
val classMapping: ClassMapping? = getClass(src)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ mixin_version = 0.8.5
# https://central.sonatype.com/artifact/org.jetbrains/annotations
jetbrains_annotations_version = 24.1.0
# https://github.com/manifold-systems/manifold
manifold_version = 2024.1.30
manifold_version = 2024.1.39

# Gradle Plugins
# https://maven.fabricmc.net/net/fabricmc/mapping-io/
Expand Down

0 comments on commit 41aab75

Please sign in to comment.