From e2c012af25318e2ac8eb2ff80c20a9bd40c9701c Mon Sep 17 00:00:00 2001 From: Rhys <98863820+rhysdh540@users.noreply.github.com> Date: Sat, 27 Apr 2024 19:38:44 -0400 Subject: [PATCH] something else? AAAAAA --- build.gradle.kts | 27 ++++++++----- .../dev/nolij/zumegradle/ZumeProGuard.kt | 39 ++++++++++++------- gradle.properties | 5 +-- modern/build.gradle.kts | 8 ++++ 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index d4e4426e..cef75f7a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -19,6 +19,7 @@ import okhttp3.internal.immutableListOf import org.objectweb.asm.ClassReader import org.objectweb.asm.ClassWriter import org.objectweb.asm.tree.ClassNode +import proguard.gradle.ProGuardTask import xyz.wagyourtail.unimined.api.minecraft.task.RemapJarTask import java.time.ZonedDateTime @@ -29,7 +30,6 @@ plugins { id("me.modmuss50.mod-publish-plugin") id("xyz.wagyourtail.unimined") id("org.ajoberstar.grgit") - id("dev.nolij.zume-proguard") } operator fun String.invoke(): String = rootProject.properties[this] as? String ?: error("Property $this not found") @@ -40,6 +40,7 @@ enum class ReleaseChannel( val deflation: JarShrinkingType, val classes: ClassShrinkingType, val json: JsonShrinkingType, + val proguard: Boolean = false ) { DEV_BUILD( suffix = "dev", @@ -55,16 +56,18 @@ enum class ReleaseChannel( suffix = "rc", deflation = JarShrinkingType.SEVENZIP, classes = ClassShrinkingType.STRIP_ALL, - json = JsonShrinkingType.MINIFY), + json = JsonShrinkingType.MINIFY, + proguard = true), RELEASE( releaseType = ReleaseType.STABLE, deflation = JarShrinkingType.SEVENZIP, classes = ClassShrinkingType.STRIP_ALL, - json = JsonShrinkingType.MINIFY), + json = JsonShrinkingType.MINIFY, + proguard = true), } val isRelease = rootProject.hasProperty("release_channel") -val releaseChannel = if (isRelease) ReleaseChannel.valueOf("release_channel"()) else ReleaseChannel.DEV_BUILD +val releaseChannel = if (isRelease) ReleaseChannel.valueOf("release_channel"().uppercase()) else ReleaseChannel.DEV_BUILD println("Release Channel: $releaseChannel") @@ -222,6 +225,7 @@ subprojects { if (implName in uniminedImpls) { apply(plugin = "xyz.wagyourtail.unimined") apply(plugin = "com.github.johnrengelman.shadow") + apply(plugin = "dev.nolij.zume-proguard") val shade: Configuration by configurations.creating { configurations.compileClasspath.get().extendsFrom(this) @@ -340,7 +344,6 @@ tasks.shadowJar { mixinPlugin = "dev.nolij.zume.ZumeMixinPlugin" } - val shadowJar = this from("LICENSE") { rename { "${it}_${"mod_id"()}" } } @@ -353,10 +356,16 @@ tasks.shadowJar { isReproducibleFileOrder = true uniminedImpls.forEach { impl -> - val remapJars = project(":${impl}").tasks.withType() - shadowJar.dependsOn(remapJars) - remapJars.forEach { remapJar -> - from(zipTree(remapJar.archiveFile.get())) { + if(releaseChannel.proguard) { + val task = project(":${impl}").tasks.withType()["proguard"] + this.dependsOn(task) + from(zipTree(task.outJarFiles.single() as File)) { + exclude("fabric.mod.json", "mcmod.info", "META-INF/mods.toml", "pack.mcmeta") + } + } else { + val task = project(":${impl}").tasks.withType()["remapJar"] + this.dependsOn(task) + from(zipTree(task.archiveFile.get())) { exclude("fabric.mod.json", "mcmod.info", "META-INF/mods.toml", "pack.mcmeta") } } diff --git a/buildSrc/src/main/kotlin/dev/nolij/zumegradle/ZumeProGuard.kt b/buildSrc/src/main/kotlin/dev/nolij/zumegradle/ZumeProGuard.kt index e7cff40e..14a54992 100644 --- a/buildSrc/src/main/kotlin/dev/nolij/zumegradle/ZumeProGuard.kt +++ b/buildSrc/src/main/kotlin/dev/nolij/zumegradle/ZumeProGuard.kt @@ -3,6 +3,7 @@ package dev.nolij.zumegradle import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.artifacts.Dependency import org.gradle.api.file.RegularFileProperty import org.gradle.api.tasks.InputFile import org.gradle.jvm.tasks.Jar @@ -14,15 +15,28 @@ import xyz.wagyourtail.unimined.api.unimined import xyz.wagyourtail.unimined.util.sourceSets import java.io.File +fun applyProGuard(task: ProGuardTask) { + task.apply { + outputs.upToDateWhen { false } + val javaHome = System.getProperty("java.home") + libraryjars(mapOf("jarfilter" to "!**.jar", "filter" to "!module-info.class"), + arrayOf("$javaHome/jmods/java.base.jmod", "$javaHome/jmods/java.desktop.jmod")) + + dontwarn("java.lang.invoke.MethodHandle") + allowaccessmodification() + optimizationpasses(10) // 10 is a lot but if nothing happens after a pass, it will stop + dontusemixedcaseclassnames() + keepattributes("RuntimeVisibleAnnotations") + overloadaggressively() + } +} + class ZumeProGuard : Plugin { override fun apply(project: Project) { - if(project != project.rootProject) - throw IllegalStateException("This plugin must be applied to the root project") - project.afterEvaluate { tasks.register("proguard") { group = "build" - outputs.upToDateWhen { false } + applyProGuard(this) val jar = tasks.withType()["remapJar"] val jarchive = jar.archiveFile.get().asFile @@ -32,14 +46,12 @@ class ZumeProGuard : Plugin { val outFile = jar.destinationDirectory.get().asFile .resolve("${jarchive.nameWithoutExtension}-proguard.jar") outjars(outFile) - + val filter = mapOf( "jarfilter" to "!**.jar", "filter" to "!module-info.class" ) - val javaHome = System.getProperty("java.home") - libraryjars(filter, arrayOf("$javaHome/jmods/java.base.jmod", "$javaHome/jmods/java.desktop.jmod")) libraryjars(filter, getUnmappedMinecraftJar().absolutePath) listOf("compileClasspath", "minecraftLibraries").forEach { configurations[it].forEach fe@ { @@ -48,22 +60,18 @@ class ZumeProGuard : Plugin { } } - dontwarn("java.lang.invoke.MethodHandle") keep("class dev.nolij.zume.api.** { *; }") keep("class dev.nolij.zume.mixin.** { @org.spongepowered.asm.mixin.** ; }") - keepattributes("RuntimeVisibleAnnotations") + keep("class ** implements net.fabricmc.api.ClientModInitializer { void onInitializeClient(); }") + keep("@net.minecraftforge.fml.common.Mod class * { *; }") + keep("@net.neoforged.fml.common.Mod class * { *; }") - optimizationpasses(10) // 10 is a lot but if nothing happens after a pass, it will stop - dontusemixedcaseclassnames() - - overloadaggressively() printmapping(layout.buildDirectory.dir("proguard").get().file("mapping.txt").asFile.apply { parentFile.mkdirs() if(exists()) delete() createNewFile() }) repackageclasses("dev.nolij.zume") - allowaccessmodification() }.get() } } @@ -72,4 +80,7 @@ class ZumeProGuard : Plugin { val mcc = unimined.minecrafts[sourceSets["main"]] return mcc.getMinecraft(mcc.mcPatcher.prodNamespace, mcc.mcPatcher.prodNamespace).toFile() } + + private val Dependency.value: String + get() = "${group}:${name}:${version}" } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 35d84c55..28fdf622 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,10 +15,7 @@ nolij = Nolij (@xdMatthewbx#1337) mod_url = https://modrinth.com/mod/zume repo_url = https://github.com/Nolij/Zume issue_url = https://github.com/Nolij/Zume/issues - -# JAR compression settings -strip_lvts = true -strip_source_files = true +release_channel = release # Fabric # https://modmuss50.me/fabric.html diff --git a/modern/build.gradle.kts b/modern/build.gradle.kts index 4c2b195b..982b9654 100644 --- a/modern/build.gradle.kts +++ b/modern/build.gradle.kts @@ -1,3 +1,5 @@ +import proguard.gradle.ProGuardTask + operator fun String.invoke(): String = rootProject.properties[this] as? String ?: error("Property $this not found") unimined.minecraft { @@ -32,4 +34,10 @@ dependencies { "modImplementation"(fabricApi.fabricModule("fabric-key-binding-api-v1", "modern_fabric_api_version"())) "modImplementation"("com.terraformersmc:modmenu:7.+") +} + +afterEvaluate { + tasks.withType()["proguard"].apply { + keep("interface io.github.prospector.modmenu.api.ModMenuApi { *; }") + } } \ No newline at end of file