Skip to content

Commit

Permalink
Add plugin remap tool
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Apr 26, 2024
1 parent 62fd5f2 commit ded4074
Show file tree
Hide file tree
Showing 8 changed files with 1,254 additions and 1 deletion.
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import xyz.jpenilla.resourcefactory.bukkit.BukkitPluginYaml

plugins {
`java-library`
id("io.papermc.paperweight.userdev") version "1.6.0"
id("io.papermc.paperweight.userdev")
id("xyz.jpenilla.run-paper") version "2.2.4" // Adds runServer and runMojangMappedServer tasks for testing
id("xyz.jpenilla.resource-factory-bukkit-convention") version "1.1.1" // Generates plugin.yml based on the Gradle config

`remap-plugin-src`
}

group = "io.papermc.paperweight"
Expand Down
12 changes: 12 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
`kotlin-dsl`
}

repositories {
mavenCentral()
gradlePluginPortal()
}

dependencies {
implementation("io.papermc.paperweight:paperweight-userdev:1.6.0")
}
514 changes: 514 additions & 0 deletions buildSrc/src/main/java/modified/mercury/RemapperVisitor.java

Large diffs are not rendered by default.

496 changes: 496 additions & 0 deletions buildSrc/src/main/java/modified/mercury/SimpleRemapperVisitor.java

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions buildSrc/src/main/kotlin/MercuryRemapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import modified.mercury.RemapperVisitor
import modified.mercury.SimpleRemapperVisitor
import paper.libs.org.cadixdev.lorenz.MappingSet
import paper.libs.org.cadixdev.mercury.RewriteContext
import paper.libs.org.cadixdev.mercury.SourceRewriter
import java.util.Objects

class MercuryRemapper private constructor(mappings: MappingSet, simple: Boolean, javadoc: Boolean) : SourceRewriter {
private val mappings: MappingSet
private val simple: Boolean
private val javadoc: Boolean

init {
this.mappings = Objects.requireNonNull(mappings, "mappings")
this.simple = simple
this.javadoc = javadoc
}

override fun getFlags(): Int = SourceRewriter.FLAG_RESOLVE_BINDINGS

override fun rewrite(context: RewriteContext) {
context.compilationUnit.accept(if (simple) SimpleRemapperVisitor(context, mappings, this.javadoc) else RemapperVisitor(context, mappings, this.javadoc))
}

companion object {
fun create(mappings: MappingSet): SourceRewriter {
return MercuryRemapper(mappings, false, true)
}

fun create(mappings: MappingSet, javadoc: Boolean): SourceRewriter {
return MercuryRemapper(mappings, false, javadoc)
}

fun createSimple(mappings: MappingSet): SourceRewriter {
return MercuryRemapper(mappings, true, true)
}

fun createSimple(mappings: MappingSet, javadoc: Boolean): SourceRewriter {
return MercuryRemapper(mappings, true, javadoc)
}
}
}
98 changes: 98 additions & 0 deletions buildSrc/src/main/kotlin/RemapPluginSources.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import io.papermc.paperweight.tasks.JavaLauncherTask
import io.papermc.paperweight.tasks.RemapSources
import io.papermc.paperweight.util.MappingFormats
import io.papermc.paperweight.util.constants.DEOBF_NAMESPACE
import io.papermc.paperweight.util.constants.SPIGOT_NAMESPACE
import io.papermc.paperweight.util.deleteRecursive
import io.papermc.paperweight.util.path
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.CompileClasspath
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.submit
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import org.gradle.workers.WorkerExecutor
import paper.libs.org.cadixdev.mercury.Mercury
import paper.libs.org.eclipse.jdt.core.JavaCore
import java.nio.file.Files
import javax.inject.Inject

abstract class RemapPluginSources : JavaLauncherTask() {
@get:CompileClasspath
abstract val remapClasspath: ConfigurableFileCollection

@get:InputDirectory
abstract val inputSources: DirectoryProperty

@get:OutputDirectory
abstract val outputDir: DirectoryProperty

@get:InputFile
abstract val mappingsFile: RegularFileProperty

@get:Input
abstract val addExplicitThis: Property<Boolean>

@get:Inject
abstract val workerExecutor: WorkerExecutor

override fun init() {
super.init()
addExplicitThis.convention(false)
}

@TaskAction
fun run() {
val queue = workerExecutor.processIsolation {
forkOptions.jvmArgs("-Xmx2G")
forkOptions.executable(launcher.get().executablePath.path.toAbsolutePath().toString())
}

queue.submit(RemapPluginSourcesAction::class) {
remapClasspath.setFrom(this@RemapPluginSources.remapClasspath)
inputSources.set(this@RemapPluginSources.inputSources)
outputDir.set(this@RemapPluginSources.outputDir)
mappingsFile.set(this@RemapPluginSources.mappingsFile)
addExplicitThis.set(this@RemapPluginSources.addExplicitThis)
}
}

abstract class RemapPluginSourcesAction : WorkAction<RemapPluginSourcesAction.Params> {
interface Params : WorkParameters {
val remapClasspath: ConfigurableFileCollection
val inputSources: DirectoryProperty
val outputDir: DirectoryProperty
val mappingsFile: RegularFileProperty
val addExplicitThis: Property<Boolean>
}

override fun execute() {
val mappingSet = MappingFormats.TINY.read(
parameters.mappingsFile.path,
SPIGOT_NAMESPACE,
DEOBF_NAMESPACE
)

val merc = Mercury()
merc.isGracefulClasspathChecks = true
merc.sourceCompatibility = JavaCore.VERSION_17
merc.classPath.addAll(parameters.remapClasspath.map { it.toPath() })
if (parameters.addExplicitThis.get()) {
merc.processors.add(RemapSources.ExplicitThisAdder)
}
merc.processors.add(MercuryRemapper.create(mappingSet, false))

parameters.outputDir.path.deleteRecursive()
Files.createDirectories(parameters.outputDir.path)

merc.rewrite(parameters.inputSources.path, parameters.outputDir.path)
}
}
}
43 changes: 43 additions & 0 deletions buildSrc/src/main/kotlin/ReverseMappings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import io.papermc.paperweight.util.MappingFormats
import io.papermc.paperweight.util.constants.DEOBF_NAMESPACE
import io.papermc.paperweight.util.constants.SPIGOT_NAMESPACE
import io.papermc.paperweight.util.path
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import java.nio.file.Files

abstract class ReverseMappings : io.papermc.paperweight.tasks.BaseTask() {
@get:InputFile
abstract val inputMappings: RegularFileProperty

@get:OutputFile
abstract val outputMappings: RegularFileProperty

@get:Input
abstract val fromNs: Property<String>

@get:Input
abstract val toNs: Property<String>

@TaskAction
fun run() {
val mappingSet = MappingFormats.TINY.read(
inputMappings.path,
fromNs.get(),
toNs.get()
).reverse()

Files.deleteIfExists(outputMappings.path)
Files.createDirectories(outputMappings.path.parent)
MappingFormats.TINY.write(
mappingSet,
outputMappings.path,
toNs.get(),
fromNs.get()
)
}
}
46 changes: 46 additions & 0 deletions buildSrc/src/main/kotlin/remap-plugin-src.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import io.papermc.paperweight.tasks.RemapJar
import io.papermc.paperweight.util.constants.DEOBF_NAMESPACE
import io.papermc.paperweight.util.constants.SPIGOT_NAMESPACE
import io.papermc.paperweight.util.registering

plugins {
java
id("io.papermc.paperweight.userdev")
}

tasks {
val reverseMappings by registering<ReverseMappings> {
inputMappings.set(layout.projectDirectory.file(".gradle/caches/paperweight/setupCache/extractDevBundle.dir/data/mojang+yarn-spigot-reobf.tiny"))
outputMappings.set(layout.buildDirectory.file("reversed-reobf-mappings.tiny"))

fromNs.set(DEOBF_NAMESPACE)
toNs.set(SPIGOT_NAMESPACE)
}

val reobfPaperJar by tasks.registering<RemapJar> {
// won't be runnable as we don't fixjarforreobf
// pretty hacky but should work
inputJar.set(layout.file(configurations.mojangMappedServerRuntime.flatMap {
it.elements.map { elements ->
elements.filter { element ->
val p = element.asFile.absolutePath
p.contains("ivyRepository") && p.contains("paper-server-userdev")
}.single().asFile
}
}))
outputJar.set(layout.buildDirectory.file("reobfPaper.jar"))
fromNamespace.set(DEOBF_NAMESPACE)
toNamespace.set(SPIGOT_NAMESPACE)
mappingsFile.set(reverseMappings.flatMap { it.outputMappings })
remapper.from(configurations.remapper)
}

@Suppress("unused_variable")
val remapPluginSources by registering<RemapPluginSources> {
remapClasspath.from(reobfPaperJar.flatMap { it.outputJar })
remapClasspath.from(configurations.compileClasspath)
inputSources.set(layout.projectDirectory.dir("src/main/java"))
outputDir.set(layout.projectDirectory.dir("src/main/mojangMappedJava"))
mappingsFile.set(reverseMappings.flatMap { it.outputMappings })
}
}

0 comments on commit ded4074

Please sign in to comment.