diff options
author | Linnea Gräf <nea@nea.moe> | 2023-12-10 19:38:18 +0100 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2023-12-10 19:38:18 +0100 |
commit | c8c3959ff81de9052c4383540756687dbd2b5a84 (patch) | |
tree | 30b1dbe25f758a5a2bad6c52979d99fa197a0372 /plugin/src/main/kotlin/moe/nea/archenemy/mojang | |
parent | cffdbe65b993a9ba81c30e4345e1a22f3cabbff2 (diff) | |
download | archenemy-c8c3959ff81de9052c4383540756687dbd2b5a84.tar.gz archenemy-c8c3959ff81de9052c4383540756687dbd2b5a84.tar.bz2 archenemy-c8c3959ff81de9052c4383540756687dbd2b5a84.zip |
Add yarn support
Diffstat (limited to 'plugin/src/main/kotlin/moe/nea/archenemy/mojang')
4 files changed, 81 insertions, 2 deletions
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<Dependency> 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 |