Initial support for remapping bundled libraries

This commit is contained in:
Jason Penilla 2023-12-12 21:58:16 -08:00
parent 63838710fc
commit 7d6c9738e3
No known key found for this signature in database
GPG key ID: 0E75A301420E48F8
5 changed files with 139 additions and 53 deletions

View file

@ -24,6 +24,8 @@ package io.papermc.paperweight.taskcontainers
import com.google.gson.JsonObject import com.google.gson.JsonObject
import io.papermc.paperweight.tasks.* import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.tasks.packaging.CreateBundlerJar
import io.papermc.paperweight.tasks.packaging.CreatePaperclipJar
import io.papermc.paperweight.util.* import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.constants.* import io.papermc.paperweight.util.constants.*
import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.NamedDomainObjectContainer
@ -76,6 +78,19 @@ class BundlerJarTasks(
vanillaJar, vanillaJar,
reobfJar.flatMap { it.outputJar }, reobfJar.flatMap { it.outputJar },
) )
createReobfBundlerJar {
remapLibraries.configureEach {
mappingsFile.set(reobfJar.flatMap { it.mappingsFile })
fromNamespace.set(reobfJar.flatMap { it.fromNamespace })
toNamespace.set(reobfJar.flatMap { it.toNamespace })
remapClasspath.from(reobfJar.map { it.remapClasspath })
// add mc to remap classpath
remapClasspath.from(reobfJar.flatMap { it.inputJar })
remapper.setFrom(reobfJar.map { it.remapper })
remapperArgs.set(reobfJar.flatMap { it.remapperArgs })
jvmArgs.set(reobfJar.flatMap { it.jvmArgs })
}
}
createPaperclipJar.configureWith(vanillaJar, createBundlerJar, mcVersion) createPaperclipJar.configureWith(vanillaJar, createBundlerJar, mcVersion)
createReobfPaperclipJar.configureWith(vanillaJar, createReobfBundlerJar, mcVersion) createReobfPaperclipJar.configureWith(vanillaJar, createReobfBundlerJar, mcVersion)

View file

@ -25,6 +25,8 @@ package io.papermc.paperweight.taskcontainers
import io.papermc.paperweight.extension.RelocationExtension import io.papermc.paperweight.extension.RelocationExtension
import io.papermc.paperweight.taskcontainers.BundlerJarTasks.Companion.registerVersionArtifact import io.papermc.paperweight.taskcontainers.BundlerJarTasks.Companion.registerVersionArtifact
import io.papermc.paperweight.tasks.* import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.tasks.packaging.CreateBundlerJar
import io.papermc.paperweight.tasks.packaging.CreatePaperclipJar
import io.papermc.paperweight.util.* import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.constants.* import io.papermc.paperweight.util.constants.*
import java.nio.file.Path import java.nio.file.Path

View file

@ -36,36 +36,13 @@ import org.gradle.api.tasks.*
import org.gradle.jvm.toolchain.JavaLauncher import org.gradle.jvm.toolchain.JavaLauncher
@CacheableTask @CacheableTask
abstract class RemapJar : JavaLauncherTask() { abstract class RemapJar : JavaLauncherTask(), RemapJarParams {
@get:Classpath @get:Classpath
abstract val inputJar: RegularFileProperty abstract val inputJar: RegularFileProperty
@get:InputFile
@get:PathSensitive(PathSensitivity.NONE)
abstract val mappingsFile: RegularFileProperty
@get:Input
abstract val fromNamespace: Property<String>
@get:Input
abstract val toNamespace: Property<String>
@get:CompileClasspath
abstract val remapClasspath: ConfigurableFileCollection
@get:Classpath
abstract val remapper: ConfigurableFileCollection
@get:Input
abstract val remapperArgs: ListProperty<String>
@get:OutputFile @get:OutputFile
abstract val outputJar: RegularFileProperty abstract val outputJar: RegularFileProperty
@get:Internal
abstract val jvmArgs: ListProperty<String>
override fun init() { override fun init() {
super.init() super.init()
@ -76,24 +53,41 @@ abstract class RemapJar : JavaLauncherTask() {
@TaskAction @TaskAction
fun run() { fun run() {
val logFile = layout.cache.resolve(paperTaskOutput("log"))
TinyRemapper.run( TinyRemapper.run(
argsList = remapperArgs.get(), inputJar.path,
logFile = logFile, outputJar.path,
inputJar = inputJar.path, this,
mappingsFile = mappingsFile.path, launcher.get(),
fromNamespace = fromNamespace.get(), layout.cache.resolve(paperTaskOutput("log")),
toNamespace = toNamespace.get(), layout.cache
remapClasspath = remapClasspath.files.map { it.toPath() },
remapper = remapper,
outputJar = outputJar.path,
launcher = launcher.get(),
workingDir = layout.cache,
jvmArgs = jvmArgs.get()
) )
} }
} }
interface RemapJarParams {
@get:InputFile
@get:PathSensitive(PathSensitivity.NONE)
val mappingsFile: RegularFileProperty
@get:Input
val fromNamespace: Property<String>
@get:Input
val toNamespace: Property<String>
@get:CompileClasspath
val remapClasspath: ConfigurableFileCollection
@get:Classpath
val remapper: ConfigurableFileCollection
@get:Input
val remapperArgs: ListProperty<String>
@get:Internal
val jvmArgs: ListProperty<String>
}
object TinyRemapper { object TinyRemapper {
private const val minecraftLvPattern = "\\$\\$\\d+" private const val minecraftLvPattern = "\\$\\$\\d+"
private const val fixPackageAccessArg = "--fixpackageaccess" private const val fixPackageAccessArg = "--fixpackageaccess"
@ -177,6 +171,30 @@ object TinyRemapper {
return args return args
} }
fun run(
inputJar: Path,
outputJar: Path,
params: RemapJarParams,
launcher: JavaLauncher,
logFile: Path,
workingDir: Path,
) {
run(
argsList = params.remapperArgs.get(),
logFile = logFile,
inputJar = inputJar,
mappingsFile = params.mappingsFile.path,
fromNamespace = params.fromNamespace.get(),
toNamespace = params.toNamespace.get(),
remapClasspath = params.remapClasspath.files.map { it.toPath() },
remapper = params.remapper,
outputJar = outputJar,
launcher = launcher,
workingDir = workingDir,
jvmArgs = params.jvmArgs.get()
)
}
fun run( fun run(
argsList: List<String>, argsList: List<String>,
logFile: Path, logFile: Path,

View file

@ -20,13 +20,16 @@
* USA * USA
*/ */
package io.papermc.paperweight.tasks package io.papermc.paperweight.tasks.packaging
import io.papermc.paperweight.PaperweightException import io.papermc.paperweight.PaperweightException
import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.util.* import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.constants.*
import io.papermc.paperweight.util.data.* import io.papermc.paperweight.util.data.*
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.* import kotlin.io.path.*
import org.gradle.api.Named
import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.artifacts.Configuration import org.gradle.api.artifacts.Configuration
import org.gradle.api.artifacts.component.ModuleComponentIdentifier import org.gradle.api.artifacts.component.ModuleComponentIdentifier
@ -39,7 +42,7 @@ import org.gradle.api.tasks.*
import org.gradle.kotlin.dsl.* import org.gradle.kotlin.dsl.*
@CacheableTask @CacheableTask
abstract class CreateBundlerJar : ZippedTask() { abstract class CreateBundlerJar : JavaLauncherZippedTask() {
interface VersionArtifact { interface VersionArtifact {
@get:Input @get:Input
@ -75,6 +78,14 @@ abstract class CreateBundlerJar : ZippedTask() {
@get:OutputFile @get:OutputFile
abstract val libraryChangesJson: RegularFileProperty abstract val libraryChangesJson: RegularFileProperty
interface LibraryRemap : RemapJarParams, Named {
@Input
override fun getName(): String
}
@get:Nested
abstract val remapLibraries: NamedDomainObjectContainer<LibraryRemap>
private fun createVersionArtifactContainer(): NamedDomainObjectContainer<VersionArtifact> = private fun createVersionArtifactContainer(): NamedDomainObjectContainer<VersionArtifact> =
objects.domainObjectContainer(VersionArtifact::class) { objects.newInstance(it) } objects.domainObjectContainer(VersionArtifact::class) { objects.newInstance(it) }
@ -122,6 +133,8 @@ abstract class CreateBundlerJar : ZippedTask() {
val outputDir = rootDir.resolve("META-INF/libraries") val outputDir = rootDir.resolve("META-INF/libraries")
val workingDir = createTempDirectory("paperweight-bundler-remap")
val dependencies = collectDependencies() val dependencies = collectDependencies()
for (dep in dependencies) { for (dep in dependencies) {
val serverLibrary = serverLibraryEntries.firstOrNull { val serverLibrary = serverLibraryEntries.firstOrNull {
@ -129,29 +142,65 @@ abstract class CreateBundlerJar : ZippedTask() {
it.id.name == dep.module.name && it.id.name == dep.module.name &&
it.id.classifier == dep.module.classifier it.id.classifier == dep.module.classifier
} }
val remapInfo = remapLibraries.findByName(dep.module.group + ':' + dep.module.name)
if (serverLibrary != null) { if (serverLibrary != null) {
if (serverLibrary.id.version == dep.module.version) { if (serverLibrary.id.version == dep.module.version && remapInfo == null) {
// nothing to do // nothing to do (no remap or version change)
libraries += serverLibrary libraries += serverLibrary
dep.copyTo(outputDir.resolve(dep.module.toPath())) dep.copyTo(outputDir.resolve(dep.module.toPath()))
} else { } else {
// we have a different version of this library if (remapInfo != null) {
val newId = dep.module // we are remapping this library, may or may not be a different version
val newPath = newId.toPath() val newId = dep.module.copy(version = dep.module.version + "-remapped")
changedLibraries += LibraryChange(serverLibrary.id, serverLibrary.path, newId, newPath) val newPath = newId.toPath()
changedLibraries += LibraryChange(serverLibrary.id, serverLibrary.path, newId, newPath)
val jarFile = dep.copyTo(outputDir.resolve(newPath)) TinyRemapper.run(
dep.file.toPath(),
outputDir.resolve(newPath),
remapInfo,
launcher.get(),
layout.cache.resolve(paperTaskOutput("remap${remapInfo.name.capitalized()}.log")),
workingDir
)
libraries += FileEntry(jarFile.sha256asHex(), newId, newPath) libraries += FileEntry(outputDir.resolve(newPath).sha256asHex(), newId, newPath)
} else {
// we have a different version of this library (not remapped)
val newId = dep.module
val newPath = newId.toPath()
changedLibraries += LibraryChange(serverLibrary.id, serverLibrary.path, newId, newPath)
val jarFile = dep.copyTo(outputDir.resolve(newPath))
libraries += FileEntry(jarFile.sha256asHex(), newId, newPath)
}
} }
} else { } else {
// New dependency if (remapInfo != null) {
val id = dep.module // New dependency, remapped
val path = id.toPath() val id = dep.module.copy(version = dep.module.version + "-remapped")
val jarFile = dep.copyTo(outputDir.resolve(path)) val path = id.toPath()
libraries += FileEntry(jarFile.sha256asHex(), id, path) TinyRemapper.run(
dep.file.toPath(),
outputDir.resolve(path),
remapInfo,
launcher.get(),
layout.cache.resolve(paperTaskOutput("remap${remapInfo.name.capitalized()}.log")),
workingDir
)
libraries += FileEntry(outputDir.resolve(path).sha256asHex(), id, path)
} else {
// New dependency, not remapped
val id = dep.module
val path = id.toPath()
val jarFile = dep.copyTo(outputDir.resolve(path))
libraries += FileEntry(jarFile.sha256asHex(), id, path)
}
} }
} }
@ -204,6 +253,7 @@ abstract class CreateBundlerJar : ZippedTask() {
val version = capability.version ?: throw PaperweightException("Unknown version for ${capability.group}:${capability.name}") val version = capability.version ?: throw PaperweightException("Unknown version for ${capability.group}:${capability.name}")
ModuleId(capability.group, capability.name, version) ModuleId(capability.group, capability.name, version)
} }
else -> throw PaperweightException("Unknown artifact result type: ${ident::class.java.name}") else -> throw PaperweightException("Unknown artifact result type: ${ident::class.java.name}")
} }
} }

View file

@ -20,9 +20,10 @@
* USA * USA
*/ */
package io.papermc.paperweight.tasks package io.papermc.paperweight.tasks.packaging
import io.papermc.paperweight.PaperweightException import io.papermc.paperweight.PaperweightException
import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.util.* import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.data.* import io.papermc.paperweight.util.data.*
import io.sigpipe.jbsdiff.Diff import io.sigpipe.jbsdiff.Diff