Skip to content

Commit

Permalink
Resolve restamp at runtime and run it using isolated workers
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Dec 8, 2024
1 parent a38e7aa commit 362bdd6
Show file tree
Hide file tree
Showing 25 changed files with 677 additions and 274 deletions.
6 changes: 3 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ lorenz = "0.5.8"
hypo = "1.2.4"
serialize = "1.5.1"
feather = "1.1.0"
restamp = "1.1.0"

[libraries]
asm-core = { module = "org.ow2.asm:asm", version.ref = "asm" }
Expand All @@ -21,7 +22,6 @@ 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.2-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 All @@ -41,7 +41,7 @@ diffpatch = "codechicken:DiffPatch:1.5.0.30"
serialize-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "serialize" }
serialize-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "serialize" }

restamp = "io.papermc.restamp:restamp:1.1.1-SNAPSHOT"
restamp = {module = "io.papermc.restamp:restamp", version.ref = "restamp" }

# test
mockk = "io.mockk:mockk:1.13.8"
Expand All @@ -56,6 +56,6 @@ gradle-plugin-publish = "com.gradle.publish:plugin-publish-plugin:1.2.1"

[bundles]
asm = ["asm-core", "asm-tree"]
cadix = ["cadix-lorenz-core", "cadix-lorenz-asm", "cadix-lorenz-proguard", "cadix-atlas", "cadix-at", "cadix-mercury", "cadix-bombe-jar"]
cadix = ["cadix-lorenz-core", "cadix-lorenz-asm", "cadix-lorenz-proguard", "cadix-atlas", "cadix-at", "cadix-mercury"]
hypo = ["hypo-model", "hypo-core", "hypo-hydrate", "hypo-asm-core", "hypo-asm-hydrate", "hypo-mappings"]
kotson = ["kotson", "gson"]
33 changes: 33 additions & 0 deletions paperweight-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ plugins {
`config-publish`
}

val restamp: Configuration by configurations.creating
configurations.implementation {
extendsFrom(restamp)
}

dependencies {
shade(projects.paperweightLib)
shade(project(projects.paperweightLib.dependencyProject.path, "sharedRuntime"))
restamp(project(projects.paperweightLib.dependencyProject.path, "restampRuntime"))

implementation(libs.bundles.kotson)
implementation(libs.coroutines)
Expand All @@ -16,3 +23,29 @@ gradlePlugin {
implementationClass = "io.papermc.paperweight.core.PaperweightCore"
}
}

val finalJar = tasks.register("finalJar", Zip::class) {
archiveExtension.set("jar")
from(zipTree(tasks.shadowJar.flatMap { it.archiveFile }))
from(zipTree(restamp.elements.map { it.single() })) {
exclude("META-INF/MANIFEST.MF")
}
}
val finalRuntimeElements by configurations.registering {
attributes {
attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY))
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.SHADOWED))
attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.JAR))
attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.JAVA_RUNTIME))
}
outgoing.artifact(finalJar)
}
val javaComponent = project.components.getByName("java") as AdhocComponentWithVariants
afterEvaluate {
javaComponent.withVariantsFromConfiguration(configurations.shadowRuntimeElements.get()) {
skip()
}
}
javaComponent.addVariantsFromConfiguration(finalRuntimeElements.get()) {
mapToMavenScope("runtime")
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ open class PaperweightCoreExtension(project: Project, objects: ObjectFactory, la
val paramMappingsRepo: Property<String> = objects.property()
val decompileRepo: Property<String> = objects.property()
val remapRepo: Property<String> = objects.property()
val macheRepo: Property<String> = objects.property<String>().convention("https://repo.papermc.io/repository/maven-public/")
val macheRepo: Property<String> = objects.property<String>().convention(PAPER_MAVEN_REPO_URL)

val macheOldPath: DirectoryProperty = objects.directoryProperty()
val gitFilePatches: Property<Boolean> = objects.property<Boolean>().convention(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package io.papermc.paperweight.core.taskcontainers

import io.papermc.paperweight.core.ext
import io.papermc.paperweight.restamp.RestampVersion
import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.tasks.mache.*
import io.papermc.paperweight.tasks.mache.RemapJar
Expand Down Expand Up @@ -70,6 +71,11 @@ open class SoftSpoonTasks(
}
val macheMinecraft by project.configurations.registering
val macheMinecraftExtended by project.configurations.registering
val restampConfig = project.configurations.register(RESTAMP_CONFIG) {
defaultDependencies {
add(project.dependencies.create("io.papermc.restamp:restamp:${RestampVersion.VERSION}"))
}
}

val macheRemapJar by tasks.registering(RemapJar::class) {
group = "mache"
Expand Down Expand Up @@ -128,6 +134,8 @@ open class SoftSpoonTasks(
inputFile.set(macheDecompileJar.flatMap { it.outputJar })
predicate.set { Files.isRegularFile(it) && it.toString().endsWith(".java") }
outputDir.set(layout.cache.resolve(BASE_PROJECT).resolve("sources"))

restamp.from(restampConfig)
}

val setupMacheResources by tasks.registering(SetupVanilla::class) {
Expand Down Expand Up @@ -203,6 +211,8 @@ open class SoftSpoonTasks(
input.set(layout.projectDirectory.dir("src/vanilla/java"))
patches.set(project.ext.paper.sourcePatchDir)
gitFilePatches.set(project.ext.gitFilePatches)

restamp.from(restampConfig)
}

val rebuildResourcePatches by tasks.registering(RebuildFilePatches::class) {
Expand Down
45 changes: 43 additions & 2 deletions paperweight-lib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,55 @@
plugins {
`config-kotlin`
id("net.kyori.blossom") version "2.1.0"
}

repositories {
gradlePluginPortal()
}

val shared = sourceSets.create("shared")
val sharedJar by tasks.creating(Jar::class) {
archiveClassifier = "shared"
from(shared.output)
}
tasks.jar {
from(shared.output)
}
val restamp = sourceSets.create("restamp") {
blossom {
kotlinSources {
properties.put("restamp_version", libs.versions.restamp)
}
}
}
val restampJar by tasks.creating(Jar::class) {
archiveClassifier = "restamp"
from(restamp.output)
}

configurations {
consumable("restampRuntime") {
outgoing.artifact(restampJar)
}
consumable("sharedRuntime") {
outgoing.artifact(sharedJar)
}
}

dependencies {
shared.compileOnlyConfigurationName(gradleApi())
shared.compileOnlyConfigurationName(gradleKotlinDsl())
compileOnly(shared.output)
testImplementation(shared.output)

restamp.implementationConfigurationName(libs.restamp)
restamp.implementationConfigurationName(shared.output)
restamp.compileOnlyConfigurationName(gradleApi())
restamp.compileOnlyConfigurationName(gradleKotlinDsl())
compileOnly(restamp.output)
testImplementation(restamp.output)
testImplementation(libs.restamp)

implementation(libs.httpclient)
implementation(libs.bundles.kotson)
implementation(libs.coroutines)
Expand All @@ -25,8 +68,6 @@ dependencies {

implementation(libs.jbsdiff)

implementation(libs.restamp)

implementation(variantOf(libs.diffpatch) { classifier("all") }) {
isTransitive = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ package io.papermc.paperweight.tasks.mache
import codechicken.diffpatch.cli.PatchOperation
import codechicken.diffpatch.util.LoggingOutputStream
import codechicken.diffpatch.util.archiver.ArchiveFormat
import io.papermc.paperweight.restamp.SetupVanillaRestampWorker
import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.util.*
import io.papermc.restamp.Restamp
import io.papermc.restamp.RestampContextConfiguration
import io.papermc.restamp.RestampInput
import java.nio.file.Path
import java.util.function.Predicate
import javax.inject.Inject
import kotlin.io.path.*
import org.cadixdev.at.io.AccessTransformFormats
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.api.ResetCommand
import org.eclipse.jgit.lib.PersonIdent
Expand All @@ -44,9 +42,10 @@ import org.gradle.api.file.RegularFileProperty
import org.gradle.api.logging.LogLevel
import org.gradle.api.provider.Property
import org.gradle.api.tasks.*
import org.openrewrite.InMemoryExecutionContext
import org.gradle.kotlin.dsl.*
import org.gradle.workers.WorkerExecutor

abstract class SetupVanilla : BaseTask() {
abstract class SetupVanilla : JavaLauncherTask() {

@get:PathSensitive(PathSensitivity.NONE)
@get:InputFile
Expand Down Expand Up @@ -90,6 +89,12 @@ abstract class SetupVanilla : BaseTask() {
@get:InputDirectory
abstract val macheOld: DirectoryProperty

@get:Inject
abstract val workerExecutor: WorkerExecutor

@get:CompileClasspath
abstract val restamp: ConfigurableFileCollection

@TaskAction
fun run() {
val outputPath = outputDir.convertToPath()
Expand Down Expand Up @@ -175,23 +180,20 @@ abstract class SetupVanilla : BaseTask() {
classPath.add(outputPath)

println("Applying access transformers...")
val configuration = RestampContextConfiguration.builder()
.accessTransformers(ats.convertToPath(), AccessTransformFormats.FML)
.sourceRoot(outputPath)
.sourceFilesFromAccessTransformers(false)
.classpath(classPath)
.executionContext(InMemoryExecutionContext { it.printStackTrace() })
.failWithNotApplicableAccessTransformers()
.build()

val parsedInput = RestampInput.parseFrom(configuration)
val results = Restamp.run(parsedInput).allResults

results.forEach { result ->
if (result.after != null) {
outputPath.resolve(result.after!!.sourcePath).writeText(result.after!!.printAll())
val queue = workerExecutor.processIsolation {
forkOptions {
maxHeapSize = "2G"
executable(launcher.get().executablePath.path.absolutePathString())
classpath.from(restamp)
}
}
queue.submit(SetupVanillaRestampWorker::class) {
this.ats.set(this@SetupVanilla.ats.pathOrNull)
this.outputPath.set(outputPath)
this.classpath.from(classPath)
}
queue.await()

commitAndTag(git, "ATs")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,20 @@

package io.papermc.paperweight.tasks.softspoon

import io.papermc.paperweight.restamp.ApplySourceATWorker
import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.util.*
import io.papermc.restamp.Restamp
import io.papermc.restamp.RestampContextConfiguration
import io.papermc.restamp.RestampInput
import java.nio.file.Files
import javax.inject.Inject
import kotlin.io.path.*
import org.cadixdev.at.io.AccessTransformFormats
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.*
import org.gradle.kotlin.dsl.*
import org.gradle.workers.WorkAction
import org.gradle.workers.WorkParameters
import org.gradle.workers.WorkerExecutor
import org.openrewrite.InMemoryExecutionContext

// TODO: This task is only used in tests?
@CacheableTask
abstract class ApplySourceAT : BaseTask() {
abstract class ApplySourceAT : JavaLauncherTask() {

@get:Classpath
abstract val inputJar: RegularFileProperty
Expand Down Expand Up @@ -70,66 +64,18 @@ abstract class ApplySourceAT : BaseTask() {
val queue = worker.processIsolation {
forkOptions {
maxHeapSize = "2G"
executable(launcher.get().executablePath.path.absolutePathString())
}
}

val classPath = minecraftClasspath.files.map { it.toPath() }.toMutableList()
classPath.add(inputJar.convertToPath())

queue.submit(RestampWorker::class) {
queue.submit(ApplySourceATWorker::class) {
minecraftClasspath.from(minecraftClasspath)
atFile.set(atFile)
inputJar.set(inputJar)
outputJar.set(outputJar)
}
}
}

abstract class RestampWorker : WorkAction<RestampWorker.Params> {
interface Params : WorkParameters {
val minecraftClasspath: ConfigurableFileCollection
val atFile: RegularFileProperty
val inputJar: RegularFileProperty
val outputJar: RegularFileProperty
}

override fun execute() {
val inputZip = parameters.inputJar.convertToPath().openZip()

val classPath = parameters.minecraftClasspath.files.map { it.toPath() }.toMutableList()
classPath.add(parameters.inputJar.convertToPath())

val configuration = RestampContextConfiguration.builder()
.accessTransformers(parameters.atFile.convertToPath(), AccessTransformFormats.FML)
.sourceRoot(inputZip.getPath("/"))
.sourceFilesFromAccessTransformers()
.classpath(classPath)
.executionContext(InMemoryExecutionContext { it.printStackTrace() })
.failWithNotApplicableAccessTransformers()
.build()

val parsedInput = RestampInput.parseFrom(configuration)
val results = Restamp.run(parsedInput).allResults

parameters.outputJar.convertToPath().writeZip().use { zip ->
val alreadyWritten = mutableSetOf<String>()
results.forEach { result ->
if (result.after == null) {
println("Ignoring ${result.before?.sourcePath} because result.after is null?")
return@forEach
}
result.after?.let { after ->
zip.getPath(after.sourcePath.toString()).writeText(after.printAll())
alreadyWritten.add("/" + after.sourcePath.toString())
}
}

inputZip.walk().filter { Files.isRegularFile(it) }.filter { !alreadyWritten.contains(it.toString()) }.forEach { file ->
zip.getPath(file.toString()).writeText(file.readText())
}

zip.close()
}
inputZip.close()
}
}
Loading

0 comments on commit 362bdd6

Please sign in to comment.