From c8c3959ff81de9052c4383540756687dbd2b5a84 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 10 Dec 2023 19:38:18 +0100 Subject: Add yarn support --- .../archenemy/mojang/ArchenemyMojangExtension.kt | 4 ++ .../archenemy/mojang/MappedRepositoryProvider.kt | 3 +- .../moe/nea/archenemy/mojang/MappingDependency.kt | 1 - .../nea/archenemy/mojang/YarnMappingDependency.kt | 75 ++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 plugin/src/main/kotlin/moe/nea/archenemy/mojang/YarnMappingDependency.kt (limited to 'plugin/src') diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ArchenemyMojangExtension.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ArchenemyMojangExtension.kt index b80fe89..db667ae 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ArchenemyMojangExtension.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ArchenemyMojangExtension.kt @@ -32,6 +32,10 @@ abstract class ArchenemyMojangExtension(val project: Project) { private val mappedRepositoryProvider = MappedRepositoryProvider(this) + fun yarnMappings(dependency: Dependency): MappingDependency { + dependency as ModuleDependency + return YarnMappingDependency(this, dependency) + } fun officialMappings(version: String, side: MCSide): MappingDependency { _registerMinecraftProvider diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappedRepositoryProvider.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappedRepositoryProvider.kt index ed158b5..1e87b54 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappedRepositoryProvider.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappedRepositoryProvider.kt @@ -39,7 +39,8 @@ class MappedRepositoryProvider( fun getDependencyCoordiante(coordinates: MappedCoordinates): String { providers[coordinates.transformerHash] = coordinates - return "archenemy.remapped.${coordinates.transformerHash}.${coordinates.mappings.title()}-${coordinates.to}.${coordinates.dependency.group}:${coordinates.dependency.name}:${coordinates.dependency.version}" + //-${coordinates.to}.${coordinates.dependency.group} + return "archenemy.remapped.${coordinates.transformerHash}.${coordinates.mappings.title()}:${coordinates.dependency.name}:${coordinates.dependency.version}" } fun getDependencyCoordiante( diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappingDependency.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappingDependency.kt index 76f109c..292db4f 100644 --- a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappingDependency.kt +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/MappingDependency.kt @@ -1,6 +1,5 @@ package moe.nea.archenemy.mojang -import org.gradle.api.Project import org.gradle.api.artifacts.Dependency import org.gradle.api.provider.Provider import java.io.File diff --git a/plugin/src/main/kotlin/moe/nea/archenemy/mojang/YarnMappingDependency.kt b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/YarnMappingDependency.kt new file mode 100644 index 0000000..46b428e --- /dev/null +++ b/plugin/src/main/kotlin/moe/nea/archenemy/mojang/YarnMappingDependency.kt @@ -0,0 +1,75 @@ +package moe.nea.archenemy.mojang + +import moe.nea.archenemy.util.update +import moe.nea.archenemy.util.updateField +import net.fabricmc.tinyremapper.OutputConsumerPath +import net.fabricmc.tinyremapper.TinyRemapper +import net.fabricmc.tinyremapper.TinyUtils +import org.gradle.api.artifacts.Dependency +import org.gradle.api.artifacts.ModuleDependency +import org.gradle.api.provider.Provider +import java.io.BufferedReader +import java.io.File +import java.io.StringReader +import java.security.MessageDigest +import java.util.zip.ZipFile + +class YarnMappingDependency( + val extension: ArchenemyMojangExtension, + val dependency: ModuleDependency +) : MappingDependency, + Provider by extension.project.provider({ dependency }) { + var tinySource: String? = null + + init { + val artifact = dependency.artifacts.single() + require(artifact.classifier == "v2") + require(artifact.extension == "jar") + } + + override fun updateHash(digest: MessageDigest) { + digest.update("yarn") + digest.updateField("group", dependency.group ?: "null") + digest.updateField("name", dependency.name) + digest.updateField("version", dependency.version ?: "null") + digest.updateField("classifier", dependency.artifacts.single().classifier ?: "") + digest.updateField("extension", dependency.artifacts.single().extension ?: "") + } + + override fun title(): String { + return "yarn-${dependency.group?.replace(".", "-")}-${dependency.name}-${dependency.version?.replace(".", "-")}" + } + + override fun resolveMappingsOnce(extension: ArchenemyMojangExtension) { + if (tinySource != null) return + val v2Jar = extension.project.configurations.detachedConfiguration(get()).resolve().single() + ZipFile(v2Jar).use { zip -> + val ze = zip.getEntry("mappings/mappings.tiny") + zip.getInputStream(ze).use { inp -> + tinySource = inp.reader().readText() + } + } + } + + override fun applyMapping( + sourceFile: File, + targetFile: File, + sourceNameSpace: String, + targetNameSpace: String + ) { + val remapper = TinyRemapper.newRemapper() + .withMappings( + TinyUtils.createTinyMappingProvider( + BufferedReader(StringReader(tinySource ?: error("Mappings have never been resolved"))), + sourceNameSpace, + targetNameSpace, + ) + ) + .build() + OutputConsumerPath.Builder(targetFile.toPath()).build().use { output -> + remapper.readInputs(sourceFile.toPath()) + remapper.apply(output) + } + remapper.finish() + } +} \ No newline at end of file -- cgit