|
| 1 | +package org.parchmentmc.tasks |
| 2 | + |
| 3 | +import daomephsta.unpick.constantmappers.datadriven.parser.v3.UnpickV3Reader |
| 4 | +import daomephsta.unpick.constantmappers.datadriven.parser.v3.UnpickV3Remapper |
| 5 | +import daomephsta.unpick.constantmappers.datadriven.parser.v3.UnpickV3Writer |
| 6 | +import daomephsta.unpick.constantmappers.datadriven.tree.UnpickV3Visitor |
| 7 | +import net.fabricmc.mappingio.adapter.MappingSourceNsSwitch |
| 8 | +import net.fabricmc.mappingio.format.proguard.ProGuardFileReader |
| 9 | +import net.fabricmc.mappingio.format.tiny.Tiny2FileReader |
| 10 | +import net.fabricmc.mappingio.tree.MemoryMappingTree |
| 11 | +import org.gradle.api.DefaultTask |
| 12 | +import org.gradle.api.file.ConfigurableFileCollection |
| 13 | +import org.gradle.api.file.RegularFileProperty |
| 14 | +import org.gradle.api.tasks.CacheableTask |
| 15 | +import org.gradle.api.tasks.InputFile |
| 16 | +import org.gradle.api.tasks.InputFiles |
| 17 | +import org.gradle.api.tasks.OutputFile |
| 18 | +import org.gradle.api.tasks.PathSensitive |
| 19 | +import org.gradle.api.tasks.PathSensitivity |
| 20 | +import org.gradle.api.tasks.TaskAction |
| 21 | +import org.parchmentmc.util.openZip |
| 22 | +import org.parchmentmc.util.path |
| 23 | +import kotlin.io.path.bufferedReader |
| 24 | +import kotlin.io.path.createParentDirectories |
| 25 | +import kotlin.io.path.writeText |
| 26 | + |
| 27 | +@CacheableTask |
| 28 | +abstract class RemapUnpickDefinitions : DefaultTask() { |
| 29 | + @get:InputFiles |
| 30 | + @get:PathSensitive(PathSensitivity.NONE) |
| 31 | + abstract val inputDefinitionsJar: ConfigurableFileCollection |
| 32 | + |
| 33 | + @get:OutputFile |
| 34 | + abstract val outputDefinitionsFile: RegularFileProperty |
| 35 | + |
| 36 | + @get:InputFiles |
| 37 | + @get:PathSensitive(PathSensitivity.NONE) |
| 38 | + abstract val intermediaryJar: ConfigurableFileCollection |
| 39 | + |
| 40 | + @get:InputFile |
| 41 | + @get:PathSensitive(PathSensitivity.NONE) |
| 42 | + abstract val mojangMappings: RegularFileProperty |
| 43 | + |
| 44 | + @TaskAction |
| 45 | + fun remap() { |
| 46 | + val fromOfficial = MemoryMappingTree() |
| 47 | + intermediaryJar.files.single().toPath().openZip().use { fs -> |
| 48 | + fs.getPath("mappings/mappings.tiny").bufferedReader().use { reader -> |
| 49 | + Tiny2FileReader.read(reader, fromOfficial) |
| 50 | + } |
| 51 | + } |
| 52 | + mojangMappings.path.bufferedReader().use { |
| 53 | + ProGuardFileReader.read( |
| 54 | + it, |
| 55 | + "mojang", |
| 56 | + "official", |
| 57 | + MappingSourceNsSwitch(fromOfficial, "official") |
| 58 | + ) |
| 59 | + } |
| 60 | + val fromIntermediary = MemoryMappingTree() |
| 61 | + fromIntermediary.srcNamespace = "intermediary" |
| 62 | + fromOfficial.accept(MappingSourceNsSwitch(fromIntermediary, "intermediary")) |
| 63 | + |
| 64 | + inputDefinitionsJar.files.single().toPath().openZip().use { fs -> |
| 65 | + fs.getPath("extras/definitions.unpick").bufferedReader().use { reader -> |
| 66 | + val reader = UnpickV3Reader(reader) |
| 67 | + val writer = UnpickV3Writer() |
| 68 | + val remapper = makeRemapper(writer, fromIntermediary) |
| 69 | + reader.accept(remapper) |
| 70 | + outputDefinitionsFile.path.createParentDirectories() |
| 71 | + outputDefinitionsFile.path.writeText(writer.output) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + fun makeRemapper(downstream: UnpickV3Visitor, tree: MemoryMappingTree): UnpickV3Remapper { |
| 77 | + return object : UnpickV3Remapper(downstream) { |
| 78 | + override fun mapClassName(className: String): String { |
| 79 | + return tree.getClass(className.replace(".", "/")) |
| 80 | + ?.getName("mojang")?.replace("/", ".") |
| 81 | + ?: className |
| 82 | + } |
| 83 | + |
| 84 | + override fun mapFieldName( |
| 85 | + className: String, |
| 86 | + fieldName: String, |
| 87 | + fieldDesc: String |
| 88 | + ): String { |
| 89 | + val classMapping = tree.getClass(className.replace(".", "/")) ?: return fieldName |
| 90 | + val fieldMapping = classMapping.getField(fieldName, fieldDesc) ?: return fieldName |
| 91 | + return fieldMapping.getName("mojang") ?: fieldName |
| 92 | + } |
| 93 | + |
| 94 | + override fun mapMethodName( |
| 95 | + className: String, |
| 96 | + methodName: String, |
| 97 | + methodDesc: String |
| 98 | + ): String { |
| 99 | + val classMapping = tree.getClass(className.replace(".", "/")) ?: return methodName |
| 100 | + val methodMapping = classMapping.getMethod(methodName, methodDesc) ?: return methodName |
| 101 | + return methodMapping.getName("mojang") ?: methodName |
| 102 | + } |
| 103 | + |
| 104 | + override fun getClassesInPackage(pkg: String): List<String> { |
| 105 | + return tree.classes |
| 106 | + .filter { |
| 107 | + val pkgSlash = pkg.replace(".", "/") + "/" |
| 108 | + it.srcName.startsWith(pkgSlash) && !it.srcName.substringAfter(pkgSlash).contains('/') |
| 109 | + } |
| 110 | + .map { it.srcName.replace("/", ".") } |
| 111 | + } |
| 112 | + |
| 113 | + override fun getFieldDesc(className: String, fieldName: String): String { |
| 114 | + val classMapping = tree.getClass(className.replace(".", "/")) ?: return "" |
| 115 | + val fieldMapping = classMapping.getField(fieldName, null) ?: return "" |
| 116 | + return fieldMapping.srcDesc ?: "" |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments