Skip to content

Commit

Permalink
something else?
Browse files Browse the repository at this point in the history
AAAAAA
  • Loading branch information
rhysdh540 committed Apr 27, 2024
1 parent c3408af commit e2c012a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
27 changes: 18 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")
Expand All @@ -40,6 +40,7 @@ enum class ReleaseChannel(
val deflation: JarShrinkingType,
val classes: ClassShrinkingType,
val json: JsonShrinkingType,
val proguard: Boolean = false
) {
DEV_BUILD(
suffix = "dev",
Expand All @@ -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")

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -340,7 +344,6 @@ tasks.shadowJar {
mixinPlugin = "dev.nolij.zume.ZumeMixinPlugin"
}

val shadowJar = this
from("LICENSE") {
rename { "${it}_${"mod_id"()}" }
}
Expand All @@ -353,10 +356,16 @@ tasks.shadowJar {
isReproducibleFileOrder = true

uniminedImpls.forEach { impl ->
val remapJars = project(":${impl}").tasks.withType<RemapJarTask>()
shadowJar.dependsOn(remapJars)
remapJars.forEach { remapJar ->
from(zipTree(remapJar.archiveFile.get())) {
if(releaseChannel.proguard) {
val task = project(":${impl}").tasks.withType<ProGuardTask>()["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<RemapJarTask>()["remapJar"]
this.dependsOn(task)
from(zipTree(task.archiveFile.get())) {
exclude("fabric.mod.json", "mcmod.info", "META-INF/mods.toml", "pack.mcmeta")
}
}
Expand Down
39 changes: 25 additions & 14 deletions buildSrc/src/main/kotlin/dev/nolij/zumegradle/ZumeProGuard.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Project> {
override fun apply(project: Project) {
if(project != project.rootProject)
throw IllegalStateException("This plugin must be applied to the root project")

project.afterEvaluate {
tasks.register<ProGuardTask>("proguard") {
group = "build"
outputs.upToDateWhen { false }
applyProGuard(this)

val jar = tasks.withType<RemapJarTask>()["remapJar"]
val jarchive = jar.archiveFile.get().asFile
Expand All @@ -32,14 +46,12 @@ class ZumeProGuard : Plugin<Project> {
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@ {
Expand All @@ -48,22 +60,18 @@ class ZumeProGuard : Plugin<Project> {
}
}

dontwarn("java.lang.invoke.MethodHandle")
keep("class dev.nolij.zume.api.** { *; }")
keep("class dev.nolij.zume.mixin.** { @org.spongepowered.asm.mixin.** <methods>; }")
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()
}
}
Expand All @@ -72,4 +80,7 @@ class ZumeProGuard : Plugin<Project> {
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}"
}
5 changes: 1 addition & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions modern/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<ProGuardTask>()["proguard"].apply {
keep("interface io.github.prospector.modmenu.api.ModMenuApi { *; }")
}
}

0 comments on commit e2c012a

Please sign in to comment.