Skip to content

Commit

Permalink
update hypo, implement fuzzy patching, start playing with patch remap
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Sep 30, 2023
1 parent 7332ed9 commit fc64771
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 8 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/config-kotlin.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

java {
//withSourcesJar()
//withSourcesJar() // TODO
}

kotlin {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/config-publish.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun ShadowJar.configureStandard() {
mergeServiceFiles()
}

//val sourcesJar by tasks.existing(AbstractArchiveTask::class) {
//val sourcesJar by tasks.existing(AbstractArchiveTask::class) { // TODO
// from(
// zipTree(project(":paperweight-lib").tasks
// .named("sourcesJar", AbstractArchiveTask::class)
Expand Down
5 changes: 3 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
asm = "9.5"
lorenz = "0.5.8"
hypo = "1.2.4"
hypo = "2.3.0"
serialize = "1.5.1"

[libraries]
Expand All @@ -20,6 +20,7 @@ cadix-lorenz-proguard = { module = "org.cadixdev:lorenz-io-proguard", version.re
cadix-atlas = "org.cadixdev:atlas:0.2.1"
cadix-at = "org.cadixdev:at:0.1.0-rc1"
cadix-mercury = "org.cadixdev:mercury:0.1.1-paperweight-SNAPSHOT"
cadix-bombe-jar = "org.cadixdev:bombe-jar:0.4.4"

hypo-model = { module = "dev.denwav.hypo:hypo-model", version.ref = "hypo" }
hypo-core = { module = "dev.denwav.hypo:hypo-core", version.ref = "hypo" }
Expand Down Expand Up @@ -47,5 +48,5 @@ gradle-plugin-publish = "com.gradle.publish:plugin-publish-plugin:1.1.0"

[bundles]
asm = ["asm-core", "asm-tree"]
cadix = ["cadix-lorenz-core", "cadix-lorenz-asm", "cadix-lorenz-proguard", "cadix-atlas", "cadix-at", "cadix-mercury"]
cadix = ["cadix-lorenz-core", "cadix-lorenz-asm", "cadix-lorenz-proguard", "cadix-atlas", "cadix-at", "cadix-mercury", "cadix-bombe-jar"]
hypo = ["hypo-model", "hypo-core", "hypo-hydrate", "hypo-asm-core", "hypo-asm-hydrate", "hypo-mappings"]
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class PaperweightCore : Plugin<Project> {
ext.mainClass
)

val softSpoonTasks = SoftSpoonTasks(target)
val softSpoonTasks = SoftSpoonTasks(target, tasks)

target.createPatchRemapTask(tasks)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package io.papermc.paperweight.core.taskcontainers

import io.papermc.paperweight.core.ext
import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.tasks.mache.*
import io.papermc.paperweight.tasks.mache.RemapJar
import io.papermc.paperweight.tasks.patchremapv2.RemapCBPatches
import io.papermc.paperweight.tasks.softspoon.ApplyPatches
import io.papermc.paperweight.tasks.softspoon.ApplyPatchesFuzzy
import io.papermc.paperweight.tasks.softspoon.RebuildPatches
import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.constants.*
Expand All @@ -22,6 +26,7 @@ import org.gradle.api.tasks.Sync

open class SoftSpoonTasks(
val project: Project,
val allTasks: AllTasks,
tasks: TaskContainer = project.tasks
) {

Expand Down Expand Up @@ -102,6 +107,15 @@ open class SoftSpoonTasks(
patches.set(project.layout.projectDirectory.dir("patches/sources"))
}

val applySourcePatchesFuzzy by tasks.registering(ApplyPatchesFuzzy::class) {
group = "softspoon"
description = "Applies patches to the vanilla sources"

input.set(macheSetupSources.flatMap { it.sourceDir })
output.set(project.ext.serverProject.map { it.layout.projectDirectory.dir("src/vanilla/java") })
patches.set(project.layout.projectDirectory.dir("patches/sources"))
}

val applyResourcePatches by tasks.registering(ApplyPatches::class) {
group = "softspoon"
description = "Applies patches to the vanilla resources"
Expand Down Expand Up @@ -142,6 +156,24 @@ open class SoftSpoonTasks(
dependsOn(rebuildSourcePatches, rebuildResourcePatches)
}

// patch remap stuff
val macheSpigotDecompileJar by tasks.registering<SpigotDecompileJar> {
group = "mache"
inputJar.set(macheRemapJar.flatMap { it.outputJar })
fernFlowerJar.set(project.ext.craftBukkit.fernFlowerJar)
decompileCommand.set(allTasks.buildDataInfo.map { it.decompileCommand })
}

val remapCBPatches by tasks.registering(RemapCBPatches::class) {
group = "paperweight"
base.set(layout.cache.resolve(BASE_PROJECT).resolve("sources"))
//craftBukkit.set(allTasks.patchCraftBukkit.flatMap { it.outputDir })
craftBukkit.set(project.layout.cache.resolve("paperweight/taskCache/patchCraftBukkit.repo"))
outputPatchDir.set(project.layout.projectDirectory.dir("patches/remapped-cb"))
//mappingsFile.set(allTasks.patchMappings.flatMap { it.outputMappings })
mappingsFile.set(layout.cache.resolve(PATCHED_SPIGOT_MOJANG_YARN_MAPPINGS))
}

fun afterEvaluate() {
val download = project.download.get()

Expand Down Expand Up @@ -210,7 +242,7 @@ open class SoftSpoonTasks(
name = repository.name
mavenContent {
for (group in repository.groups ?: listOf()) {
includeGroupAndSubgroups(group)
includeGroupByRegex(group + ".*")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ open class SpigotTasks(
val generateSpigotMappings by tasks.registering<GenerateSpigotMappings> {
classMappings.set(addAdditionalSpigotMappings.flatMap { it.outputClassSrg })

sourceMappings.set(generateMappings.flatMap { it.outputMappings })
// todo hypo update breaks generate mappings, hardcode for now
//sourceMappings.set(generateMappings.flatMap { it.outputMappings })
sourceMappings.set(Path.of("D:\\IntellijProjects\\PaperClean\\.gradle\\caches\\paperweight\\mappings\\official-mojang+yarn.tiny"))

outputMappings.set(cache.resolve(SPIGOT_MOJANG_YARN_MAPPINGS))
notchToSpigotMappings.set(cache.resolve(OBF_SPIGOT_MAPPINGS))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ open class VanillaTasks(

val remapJar by tasks.registering<RemapJar> {
inputJar.set(filterVanillaJar.flatMap { it.outputJar })
mappingsFile.set(generateMappings.flatMap { it.outputMappings })
//mappingsFile.set(generateMappings.flatMap { it.outputMappings })
mappingsFile.set(Path.of("D:\\IntellijProjects\\PaperClean\\.gradle\\caches\\paperweight\\mappings\\official-mojang+yarn.tiny"))
fromNamespace.set(OBF_NAMESPACE)
toNamespace.set(DEOBF_NAMESPACE)
remapper.from(project.configurations.named(REMAPPER_CONFIG))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package io.papermc.paperweight.tasks.patchremapv2

import com.github.difflib.DiffUtils
import com.github.difflib.UnifiedDiffUtils
import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.constants.*
import java.nio.file.Path
import kotlin.io.path.*
import org.cadixdev.mercury.Mercury
import org.cadixdev.mercury.remapper.MercuryRemapper
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFileProperty
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.api.tasks.UntrackedTask
import org.gradle.kotlin.dsl.*

@UntrackedTask(because = "RemapCBPatches should always run when requested")
abstract class RemapCBPatches : BaseTask() {

@get:InputDirectory
abstract val base: DirectoryProperty

@get:InputDirectory
abstract val craftBukkit: DirectoryProperty

@get:InputFile
abstract val mappingsFile: RegularFileProperty

@get:OutputDirectory
abstract val outputPatchDir: DirectoryProperty

@TaskAction
fun run() {
val workDir = layout.cache.resolve(REMAPPED_CB).ensureClean()
val patches = outputPatchDir.convertToPath().ensureClean()
val mappings = MappingFormats.TINY.read(mappingsFile.convertToPath(), SPIGOT_NAMESPACE, DEOBF_NAMESPACE)

val configFiles = project.project(":paper-server").configurations["runtimeClasspath"].resolve().map { it.toPath() }
val classpath = configFiles + listOf(
project.project(":paper-api").projectDir.toPath().resolve("src/main/java"),
project.file(".gradle/caches/paperweight/taskCache/spigotRemapJar.jar").toPath(),
Path.of("C:\\Users\\Martin\\.m2\\repository\\org\\jetbrains\\annotations\\24.0.1\\annotations-24.0.1.jar")
)

println("dum $classpath")

val merc = Mercury()
merc.classPath.addAll(classpath)
merc.processors.addAll(
listOf(
MercuryRemapper.create(mappings)
)
)
merc.isGracefulClasspathChecks = true
merc.rewrite(craftBukkit.convertToPath().resolve("src/main/java"), workDir)

val inputDir = workDir
val baseDir = Path.of("D:\\IdeaProjects\\Paper\\.gradle\\caches\\paperweight\\mache\\dum")

val patchesCreated = baseDir.walk()
.sumOf {
diffFile(inputDir, baseDir, it.relativeTo(baseDir).toString().replace("\\", "/"), patches)
}

logger.lifecycle("Rebuilt $patchesCreated patches")
}

private fun diffFile(sourceRoot: Path, decompRoot: Path, relativePath: String, patchDir: Path): Int {
val source = sourceRoot.resolve(relativePath)
val decomp = decompRoot.resolve(relativePath)

if (!source.exists() || !decomp.exists()) return 0

val sourceLines = source.readLines(Charsets.UTF_8)
val decompLines = decomp.readLines(Charsets.UTF_8)

val patch = DiffUtils.diff(decompLines, sourceLines)
if (patch.deltas.isEmpty()) {
return 0
}

// remove all non craftbukkit chunks
patch.deltas.removeIf {
!it.target.lines.any { l -> l.contains("CraftBukkit") }
}

val unifiedPatch = UnifiedDiffUtils.generateUnifiedDiff(
"a/$relativePath",
"b/$relativePath",
decompLines,
patch,
3,
)

val patchFile = patchDir.resolve("$relativePath.patch")
patchFile.parent.createDirectories()
patchFile.writeText(unifiedPatch.joinToString("\n", postfix = "\n"), Charsets.UTF_8)

return 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.papermc.paperweight.tasks.softspoon

import io.papermc.paperweight.util.patches.*
import io.papermc.paperweight.util.patches.Patcher
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.UntrackedTask
import org.gradle.api.tasks.options.Option

@UntrackedTask(because = "Always apply patches")
abstract class ApplyPatchesFuzzy : ApplyPatches() {

@get:Input
@get:Option(
option = "max-fuzz",
description = "Max fuzz. Cannot be set higher than context (3). Setting this " +
"value higher increases the chances of a faulty patch.",
)
abstract val maxFuzz: Property<String>

override fun createPatcher(): Patcher {
return if (useNativeDiff.get()) {
NativePatcherFuzzy(exec, patchExecutable.get(), maxFuzz.get().toInt())
} else {
JavaPatcherFuzzy(maxFuzz.get().toInt())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ private const val TASK_CACHE = "$PAPER_PATH/taskCache"
const val FINAL_REMAPPED_JAR = "$TASK_CACHE/minecraft.jar"
const val FINAL_FILTERED_REMAPPED_JAR = "$TASK_CACHE/filteredMinecraft.jar"
const val FINAL_DECOMPILE_JAR = "$TASK_CACHE/decompileJar.jar"
const val SPIGOT_MACHE_DECOMPILE_JAR = "$TASK_CACHE/macheSpigotDecompileJar.jar"
const val DECOMP_CFG = "$TASK_CACHE/decomp_cfg.txt"

const val MC_DEV_SOURCES_DIR = "$PAPER_PATH/mc-dev-sources"
Expand All @@ -125,6 +126,7 @@ const val PATCHED_JAR = "$MACHE_PATH/patched.jar"
const val FAILED_PATCH_JAR = "$MACHE_PATH/failed.jar"
const val PATCHES_FOLDER = "$MACHE_PATH/patches"
const val BASE_PROJECT = "$MACHE_PATH/base"
const val REMAPPED_CB = "$MACHE_PATH/remapped-cb"

fun paperSetupOutput(name: String, ext: String) = "$SETUP_CACHE/$name.$ext"
fun Task.paperTaskOutput(ext: String) = paperTaskOutput(name, ext)
Expand Down

0 comments on commit fc64771

Please sign in to comment.