summaryrefslogtreecommitdiff
path: root/plugin/src/main/kotlin/moe/nea/archenemy/mojang/YarnMappingDependency.kt
blob: 46b428e3cc54f6cd765ad12b022d434381fabca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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()
    }
}