Skip to content

Commit 3a57e63

Browse files
committed
initial patch remapping architecture
1 parent 8f6c384 commit 3a57e63

8 files changed

Lines changed: 497 additions & 2 deletions

File tree

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
group = io.canvasmc.weaver
2-
version = 2.4.0-SNAPSHOT
2+
version = 2.4.0-remap-SNAPSHOT
33

44
org.gradle.caching = true
55
org.gradle.configuration-cache = true

gradle/libs.versions.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cadix-lorenz-asm = { module = "org.cadixdev:lorenz-asm", version.ref = "lorenz"
2323
cadix-lorenz-proguard = { module = "org.cadixdev:lorenz-io-proguard", version.ref = "lorenz" }
2424
cadix-atlas = "org.cadixdev:atlas:0.2.1"
2525
cadix-at = "org.cadixdev:at:0.1.0-rc1"
26+
cadix-mercury = "org.cadixdev:mercury:0.1.0"
2627

2728
hypo-model = { module = "dev.denwav.hypo:hypo-model", version.ref = "hypo" }
2829
hypo-core = { module = "dev.denwav.hypo:hypo-core", version.ref = "hypo" }
@@ -59,6 +60,6 @@ zCheckstyle = { module = "com.puppycrawl.tools:checkstyle", version.ref = "check
5960

6061
[bundles]
6162
asm = ["asm-core", "asm-tree"]
62-
cadix = ["cadix-lorenz-core", "cadix-lorenz-asm", "cadix-lorenz-proguard", "cadix-atlas", "cadix-at"]
63+
cadix = ["cadix-lorenz-core", "cadix-lorenz-asm", "cadix-lorenz-proguard", "cadix-atlas", "cadix-at", "cadix-mercury"]
6364
hypo = ["hypo-model", "hypo-core", "hypo-hydrate", "hypo-asm-core", "hypo-asm-hydrate", "hypo-mappings"]
6465
kotson = ["kotson", "gson"]

paperweight-core/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies {
88

99
implementation(libs.bundles.kotson)
1010
implementation(libs.coroutines)
11+
implementation(libs.lorenzTiny)
1112
implementation(variantOf(libs.diffpatch) { classifier("all") }) {
1213
isTransitive = false
1314
}

paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/MinecraftPatchingTasks.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import io.papermc.paperweight.core.tasks.patching.ApplyFilePatches
3131
import io.papermc.paperweight.core.tasks.patching.ApplyFilePatchesFuzzy
3232
import io.papermc.paperweight.core.tasks.patching.FixupFilePatches
3333
import io.papermc.paperweight.core.tasks.patching.RebuildFilePatches
34+
import io.papermc.paperweight.core.tasks.remap.RemapPatches
3435
import io.papermc.paperweight.core.util.coreExt
3536
import io.papermc.paperweight.tasks.*
3637
import io.papermc.paperweight.util.*
@@ -205,6 +206,19 @@ class MinecraftPatchingTasks(
205206
validateATs.set(project.coreExt.validateATs)
206207
}
207208

209+
val remapPatches = tasks.register<RemapPatches>("remap${namePart}Patches") {
210+
upstreamRepoDir.set(setup.flatMap { it.outputDir })
211+
inputPatchDir.convention(basePatchDir.dir("unmapped").fileExists())
212+
mappingsFile.convention(config.buildDataDir.file("parchment-unobf.tiny"))
213+
ats.set(mergeCollectedAts.flatMap { it.outputFile })
214+
classpathJars.from(coreTasks.macheRemapJar, coreTasks.extractFromBundler)
215+
decompJar.set(coreTasks.macheDecompileJar.flatMap { it.outputJar })
216+
devImports.set(config.devImports.fileExists())
217+
mcLibrarySourcesDir.set(coreTasks.downloadMcLibrariesSources.flatMap { it.outputDir })
218+
runtimeClasspath.from(project.configurations["runtimeClasspath"])
219+
outputPatchDir.convention(basePatchDir.dir("remapped"))
220+
}
221+
208222
applyBasePatches.configure {
209223
input.set(setup.flatMap { it.outputDir })
210224
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* paperweight is a Gradle plugin for the PaperMC project.
3+
*
4+
* Copyright (c) 2023 Kyle Wood (DenWav)
5+
* Contributors
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation;
10+
* version 2.1 only, no later versions.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20+
* USA
21+
*/
22+
23+
package io.papermc.paperweight.core.tasks.remap
24+
25+
import io.papermc.paperweight.PaperweightException
26+
import io.papermc.paperweight.util.*
27+
import java.nio.file.Path
28+
import kotlin.io.path.*
29+
30+
class PatchApplier(
31+
private val remappedBranch: String,
32+
private val unmappedBranch: String,
33+
private val ignoreGitIgnore: Boolean,
34+
targetDir: Path
35+
) {
36+
37+
private val git = Git(targetDir)
38+
39+
private var commitMessage: String? = null
40+
private var commitAuthor: String? = null
41+
private var commitTime: String? = null
42+
43+
private val remappedBaseTag: String = "remapped-base"
44+
45+
fun checkoutRemapped() {
46+
println("Switching to $remappedBranch without losing changes")
47+
git("symbolic-ref", "HEAD", "refs/heads/$remappedBranch").executeSilently()
48+
}
49+
50+
fun checkoutOld() {
51+
println("Resetting back to $unmappedBranch branch")
52+
git("checkout", unmappedBranch).executeSilently()
53+
}
54+
55+
fun commitPlain(message: String) {
56+
git(*Git.add(ignoreGitIgnore, ".")).executeSilently()
57+
git("commit", "-m", message, "--author=Initial <auto@mated.null>").executeSilently()
58+
}
59+
60+
fun createBranches() {
61+
git("checkout", "-b", unmappedBranch).executeSilently()
62+
git("branch", remappedBranch).executeSilently()
63+
}
64+
65+
fun commitInitialRemappedSource() {
66+
git(*Git.add(ignoreGitIgnore, ".")).executeSilently()
67+
git("commit", "-m", "Initial Remapped Source", "--author=Initial <auto@mated.null>").executeSilently()
68+
git("tag", remappedBaseTag).executeSilently()
69+
}
70+
71+
fun recordCommit() {
72+
commitMessage = git("log", "--format=%B", "-n", "1", "HEAD").getText()
73+
commitAuthor = git("log", "--format=%an <%ae>", "-n", "1", "HEAD").getText()
74+
commitTime = git("log", "--format=%aD", "-n", "1", "HEAD").getText()
75+
}
76+
77+
private fun clearCommit() {
78+
commitMessage = null
79+
commitAuthor = null
80+
commitTime = null
81+
}
82+
83+
fun commitChanges() {
84+
println("Committing remapped changes to $remappedBranch")
85+
val message = commitMessage ?: throw PaperweightException("commitMessage not set")
86+
val author = commitAuthor ?: throw PaperweightException("commitAuthor not set")
87+
val time = commitTime ?: throw PaperweightException("commitTime not set")
88+
clearCommit()
89+
90+
git(*Git.add(ignoreGitIgnore, ".")).executeSilently()
91+
git("commit", "-m", message, "--author=$author", "--date=$time").execute()
92+
}
93+
94+
fun applyPatch(patch: Path) {
95+
val result = git("am", "--3way", "--ignore-whitespace", patch.absolutePathString()).runOut()
96+
if (result != 0) {
97+
throw RuntimeException("Patch failed to apply: $patch")
98+
}
99+
}
100+
101+
fun generatePatches(target: Path) {
102+
target.deleteRecursive()
103+
target.createDirectories()
104+
git("checkout", remappedBranch).executeSilently()
105+
git(
106+
"format-patch", "--diff-algorith=myers", "--zero-commit", "--full-index", "--no-signature", "--no-stat", "-N", "-o",
107+
target.absolutePathString(), remappedBaseTag
108+
).executeOut()
109+
}
110+
111+
fun isUnfinishedPatch(): Boolean {
112+
if (git("branch", "--show-current").getText().trim() != unmappedBranch) {
113+
return false
114+
}
115+
116+
git("update-index", "--refresh").executeSilently()
117+
if (git("diff-index", "--diff-algorithm=myers", "--quiet", "HEAD", "--").runSilently() == 0) {
118+
return git("log", unmappedBranch, "-1", "--pretty=%B").getText().trim() !=
119+
git("log", remappedBranch, "-1", "--pretty=%B").getText().trim()
120+
}
121+
122+
throw PaperweightException("Unknown state: repo has uncommitted changes")
123+
}
124+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* paperweight is a Gradle plugin for the PaperMC project.
3+
*
4+
* Copyright (c) 2023 Kyle Wood (DenWav)
5+
* Contributors
6+
*
7+
* This library is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU Lesser General Public
9+
* License as published by the Free Software Foundation;
10+
* version 2.1 only, no later versions.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20+
* USA
21+
*/
22+
23+
package io.papermc.paperweight.core.tasks.remap
24+
25+
import io.papermc.paperweight.util.*
26+
import io.papermc.paperweight.util.constants.*
27+
import java.nio.file.Path
28+
import kotlin.io.path.*
29+
import org.cadixdev.at.AccessTransformSet
30+
import org.cadixdev.lorenz.MappingSet
31+
import org.cadixdev.mercury.Mercury
32+
import org.cadixdev.mercury.at.AccessTransformerRewriter
33+
import org.cadixdev.mercury.remapper.MercuryRemapper
34+
35+
class PatchSourceRemapWorker(
36+
mappings: MappingSet,
37+
ats: AccessTransformSet,
38+
classpath: Collection<Path>,
39+
private val inputDir: Path,
40+
private val outputDir: Path
41+
) {
42+
43+
private val merc: Mercury = Mercury()
44+
45+
init {
46+
merc.classPath.addAll(classpath)
47+
48+
merc.processors.addAll(
49+
listOf(
50+
MercuryRemapper.create(mappings),
51+
AccessTransformerRewriter.create(ats)
52+
)
53+
)
54+
55+
merc.isGracefulClasspathChecks = true
56+
}
57+
58+
fun remap() {
59+
setup()
60+
61+
println("mapping to $OFFICIAL_NAMESPACE")
62+
63+
merc.rewrite(inputDir, outputDir)
64+
65+
cleanup()
66+
}
67+
68+
private fun setup() {
69+
outputDir.deleteRecursive()
70+
outputDir.createDirectories()
71+
}
72+
73+
private fun cleanup() {
74+
inputDir.deleteRecursive()
75+
outputDir.moveTo(inputDir)
76+
outputDir.deleteRecursive()
77+
}
78+
}

0 commit comments

Comments
 (0)