Skip to content

Commit

Permalink
Convert to precompiled task (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jikoo authored Feb 14, 2025
1 parent cd73430 commit e834e94
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 64 deletions.
60 changes: 60 additions & 0 deletions buildSrc/src/main/kotlin/ReobfTask.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.bundling.Jar
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths

abstract class ReobfTask: Jar() {

@get:Input
open val spigotVersion: Property<String> = objectFactory.property(String::class.java)

@get:InputFile
open val inputFile: Property<RegularFile> = objectFactory.fileProperty()

@get:Input
open val intermediaryClassifier: Property<String> = objectFactory.property(String::class.java).convention("mojang-mapped")

init {
archiveClassifier.convention("reobf")
}

@TaskAction
override fun copy() {
val spigotVer = spigotVersion.get()
val inFile = inputFile.get().asFile
val obfPath = inFile.resolveSibling(inFile.name.replace(".jar", "-${intermediaryClassifier.get()}.jar"))

// https://www.spigotmc.org/threads/510208/#post-4184317
val specialsource = project.configurations.named("spigotRemap").get().incoming.artifacts.artifacts
.first { it.id.componentIdentifier.toString() == "net.md-5:SpecialSource:1.11.4" }.file.path
val repo = Paths.get(project.repositories.mavenLocal().url)
val spigotDir = repo.resolve("org/spigotmc/spigot/$spigotVer/")
val mappingDir = repo.resolve("org/spigotmc/minecraft-server/$spigotVer/")

// Remap original Mojang-mapped jar to obfuscated intermediary
val mojangServer = spigotDir.resolve("spigot-$spigotVer-remapped-mojang.jar")
val mojangMappings = mappingDir.resolve("minecraft-server-$spigotVer-maps-mojang.txt")
remapPartial(specialsource, mojangServer, mojangMappings, inFile, obfPath, true)

// Remap obfuscated intermediary jar to Spigot and replace original
val obfServer = spigotDir.resolve("spigot-$spigotVer-remapped-obf.jar")
val spigotMappings = mappingDir.resolve("minecraft-server-$spigotVer-maps-spigot.csrg")
remapPartial(specialsource, obfServer, spigotMappings, obfPath, archiveFile.get().asFile, false)
}

private fun remapPartial(specialSource: String, serverJar: Path, mapping: Path, input: File, output: File, reverse: Boolean) {
project.providers.exec {
commandLine("java", "-cp", "$specialSource${File.pathSeparator}$serverJar",
"net.md_5.specialsource.SpecialSource", "--live",
"-i", input.path, "-o", output.path,
"-m", "$mapping",
if (reverse) "--reverse" else "")
}.result.get()
}

}
55 changes: 0 additions & 55 deletions buildSrc/src/main/kotlin/remap-spigot.gradle.kts

This file was deleted.

29 changes: 22 additions & 7 deletions internal/spigot/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
plugins {
`remap-spigot`
`openinv-base`
alias(libs.plugins.shadow)
}
Expand All @@ -9,6 +8,7 @@ repositories {
}

val spigotVer = "1.21.4-R0.1-SNAPSHOT"
rootProject.extra["spigotVersion"] = spigotVer
rootProject.extra["craftbukkitPackage"] = "v1_21_R3"

configurations.all {
Expand All @@ -25,7 +25,10 @@ configurations.all {
}
}

val spigotRemap = configurations.create("spigotRemap")

dependencies {
spigotRemap("net.md-5:SpecialSource:1.11.4:shaded")
compileOnly(libs.spigotapi)
compileOnly(create("org.spigotmc", "spigot", spigotVer, classifier = "remapped-mojang"))

Expand All @@ -37,15 +40,27 @@ dependencies {
}

tasks.shadowJar {
notCompatibleWithConfigurationCache("reobf task replaces output artifact") // TODO use an output of reobf
relocate("com.lishid.openinv.internal.common", "com.lishid.openinv.internal.reobf")
}

// TODO this appears to be a deprecated way to do things
// may want to just move all helper methods here.
tasks.register<Remap_spigot_gradle.RemapTask>("reobf") {
val reobfTask = tasks.register<ReobfTask>("reobfTask") {
notCompatibleWithConfigurationCache("gradle is hard")
dependsOn(tasks.shadowJar)
inputs.files(tasks.shadowJar.get().outputs.files.files)
spigotVersion = spigotVer
inputFile.value(tasks.shadowJar.get().archiveFile.get())
spigotVersion.value(spigotVer)
}

configurations {
consumable("reobf") {
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY))
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.JAR))
}
}
}

artifacts {
add("reobf", reobfTask)
}
3 changes: 1 addition & 2 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies {
implementation(project(":openinvadaptercommon"))
implementation(project(":openinvadapterpaper1_21_3"))
implementation(project(":openinvadapterpaper1_21_1"))
implementation(project(":openinvadapterspigot", configuration = "shadow"))
implementation(project(":openinvadapterspigot", configuration = "reobf"))
implementation(libs.planarwrappers)
}

Expand All @@ -26,7 +26,6 @@ tasks.jar {
}

tasks.shadowJar {
dependsOn(":openinvadapterspigot:reobf")
minimize {
exclude(":openinv**")
}
Expand Down

0 comments on commit e834e94

Please sign in to comment.