diff options
| author | 2022-09-22 11:59:53 +0200 | |
|---|---|---|
| committer | 2023-10-09 11:51:28 +0200 | |
| commit | 47de69a821c6e089b01187e93f4f916aceeeea85 (patch) | |
| tree | b6e2ccab4e3e3896b9fc32b6f013c35f7ef0995d /enigma/src | |
| parent | Bump version (diff) | |
| download | enigma-fork-47de69a821c6e089b01187e93f4f916aceeeea85.tar.gz enigma-fork-47de69a821c6e089b01187e93f4f916aceeeea85.tar.xz enigma-fork-47de69a821c6e089b01187e93f4f916aceeeea85.zip | |
Add initial Mapping-IO export support
Diffstat (limited to 'enigma/src')
| -rw-r--r-- | enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingFormat.java | 25 | ||||
| -rw-r--r-- | enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingIoConverter.java | 126 |
2 files changed, 142 insertions, 9 deletions
diff --git a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingFormat.java b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingFormat.java index 367af3b..4790fee 100644 --- a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingFormat.java +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingFormat.java | |||
| @@ -21,21 +21,23 @@ import cuchaz.enigma.translation.mapping.serde.tinyv2.TinyV2Writer; | |||
| 21 | import cuchaz.enigma.translation.mapping.tree.EntryTree; | 21 | import cuchaz.enigma.translation.mapping.tree.EntryTree; |
| 22 | 22 | ||
| 23 | public enum MappingFormat { | 23 | public enum MappingFormat { |
| 24 | ENIGMA_FILE(EnigmaMappingsWriter.FILE, EnigmaMappingsReader.FILE), | 24 | ENIGMA_FILE(EnigmaMappingsWriter.FILE, EnigmaMappingsReader.FILE, null), |
| 25 | ENIGMA_DIRECTORY(EnigmaMappingsWriter.DIRECTORY, EnigmaMappingsReader.DIRECTORY), | 25 | ENIGMA_DIRECTORY(EnigmaMappingsWriter.DIRECTORY, EnigmaMappingsReader.DIRECTORY, net.fabricmc.mappingio.format.MappingFormat.ENIGMA), |
| 26 | ENIGMA_ZIP(EnigmaMappingsWriter.ZIP, EnigmaMappingsReader.ZIP), | 26 | ENIGMA_ZIP(EnigmaMappingsWriter.ZIP, EnigmaMappingsReader.ZIP, null), |
| 27 | TINY_V2(new TinyV2Writer("intermediary", "named"), new TinyV2Reader()), | 27 | TINY_V2(new TinyV2Writer("intermediary", "named"), new TinyV2Reader(), net.fabricmc.mappingio.format.MappingFormat.TINY_2), |
| 28 | TINY_FILE(TinyMappingsWriter.INSTANCE, TinyMappingsReader.INSTANCE), | 28 | TINY_FILE(TinyMappingsWriter.INSTANCE, TinyMappingsReader.INSTANCE, net.fabricmc.mappingio.format.MappingFormat.TINY), |
| 29 | SRG_FILE(SrgMappingsWriter.INSTANCE, null), | 29 | SRG_FILE(SrgMappingsWriter.INSTANCE, null, net.fabricmc.mappingio.format.MappingFormat.SRG), |
| 30 | PROGUARD(null, ProguardMappingsReader.INSTANCE), | 30 | PROGUARD(null, ProguardMappingsReader.INSTANCE, net.fabricmc.mappingio.format.MappingFormat.PROGUARD), |
| 31 | RECAF(RecafMappingsWriter.INSTANCE, RecafMappingsReader.INSTANCE); | 31 | RECAF(RecafMappingsWriter.INSTANCE, RecafMappingsReader.INSTANCE, null); |
| 32 | 32 | ||
| 33 | private final MappingsWriter writer; | 33 | private final MappingsWriter writer; |
| 34 | private final MappingsReader reader; | 34 | private final MappingsReader reader; |
| 35 | private final net.fabricmc.mappingio.format.MappingFormat mappingIoCounterpart; | ||
| 35 | 36 | ||
| 36 | MappingFormat(MappingsWriter writer, MappingsReader reader) { | 37 | MappingFormat(MappingsWriter writer, MappingsReader reader, net.fabricmc.mappingio.format.MappingFormat mappingIoCounterpart) { |
| 37 | this.writer = writer; | 38 | this.writer = writer; |
| 38 | this.reader = reader; | 39 | this.reader = reader; |
| 40 | this.mappingIoCounterpart = mappingIoCounterpart; | ||
| 39 | } | 41 | } |
| 40 | 42 | ||
| 41 | public void write(EntryTree<EntryMapping> mappings, Path path, ProgressListener progressListener, MappingSaveParameters saveParameters) { | 43 | public void write(EntryTree<EntryMapping> mappings, Path path, ProgressListener progressListener, MappingSaveParameters saveParameters) { |
| @@ -67,4 +69,9 @@ public enum MappingFormat { | |||
| 67 | public MappingsReader getReader() { | 69 | public MappingsReader getReader() { |
| 68 | return reader; | 70 | return reader; |
| 69 | } | 71 | } |
| 72 | |||
| 73 | @Nullable | ||
| 74 | public net.fabricmc.mappingio.format.MappingFormat getMappingIoCounterpart() { | ||
| 75 | return mappingIoCounterpart; | ||
| 76 | } | ||
| 70 | } | 77 | } |
diff --git a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingIoConverter.java b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingIoConverter.java new file mode 100644 index 0000000..3a86476 --- /dev/null +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingIoConverter.java | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | package cuchaz.enigma.translation.mapping.serde; | ||
| 2 | |||
| 3 | import java.util.Deque; | ||
| 4 | import java.util.LinkedList; | ||
| 5 | import java.util.List; | ||
| 6 | |||
| 7 | import net.fabricmc.mappingio.MappedElementKind; | ||
| 8 | import net.fabricmc.mappingio.tree.MemoryMappingTree; | ||
| 9 | |||
| 10 | import cuchaz.enigma.translation.mapping.EntryMap; | ||
| 11 | import cuchaz.enigma.translation.mapping.EntryMapping; | ||
| 12 | import cuchaz.enigma.translation.mapping.tree.EntryTree; | ||
| 13 | import cuchaz.enigma.translation.mapping.tree.EntryTreeNode; | ||
| 14 | import cuchaz.enigma.translation.representation.entry.ClassEntry; | ||
| 15 | import cuchaz.enigma.translation.representation.entry.Entry; | ||
| 16 | import cuchaz.enigma.translation.representation.entry.FieldEntry; | ||
| 17 | import cuchaz.enigma.translation.representation.entry.LocalVariableEntry; | ||
| 18 | import cuchaz.enigma.translation.representation.entry.MethodEntry; | ||
| 19 | |||
| 20 | public class MappingIoConverter { | ||
| 21 | public static MemoryMappingTree toMappingIo(EntryTree<EntryMapping> mappings) { | ||
| 22 | MemoryMappingTree mappingTree = new MemoryMappingTree(); | ||
| 23 | mappingTree.visitNamespaces("intermediary", List.of("named")); | ||
| 24 | |||
| 25 | for (EntryTreeNode<EntryMapping> node : mappings) { | ||
| 26 | if (node.getEntry() instanceof ClassEntry) { | ||
| 27 | writeClass(node, mappings, mappingTree); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | |||
| 31 | mappingTree.visitEnd(); | ||
| 32 | return mappingTree; | ||
| 33 | } | ||
| 34 | |||
| 35 | private static void writeClass(EntryTreeNode<EntryMapping> classNode, EntryMap<EntryMapping> oldMappingTree, MemoryMappingTree newMappingTree) { | ||
| 36 | ClassEntry classEntry = (ClassEntry) classNode.getEntry(); | ||
| 37 | EntryMapping mapping = oldMappingTree.get(classEntry); | ||
| 38 | Deque<String> parts = new LinkedList<>(); | ||
| 39 | |||
| 40 | newMappingTree.visitClass(classEntry.getFullName()); | ||
| 41 | newMappingTree.visitComment(MappedElementKind.CLASS, mapping.javadoc()); | ||
| 42 | |||
| 43 | do { | ||
| 44 | mapping = oldMappingTree.get(classEntry); | ||
| 45 | |||
| 46 | if (mapping != null && mapping.targetName() != null) { | ||
| 47 | parts.addFirst(mapping.targetName()); | ||
| 48 | } else { | ||
| 49 | parts.addFirst(classEntry.getName()); | ||
| 50 | } | ||
| 51 | |||
| 52 | classEntry = classEntry.getOuterClass(); | ||
| 53 | } while (classEntry != null); | ||
| 54 | |||
| 55 | String mappedName = String.join("$", parts); | ||
| 56 | newMappingTree.visitDstName(MappedElementKind.CLASS, 0, mappedName); | ||
| 57 | |||
| 58 | for (EntryTreeNode<EntryMapping> child : classNode.getChildNodes()) { | ||
| 59 | Entry<?> entry = child.getEntry(); | ||
| 60 | |||
| 61 | if (entry instanceof FieldEntry) { | ||
| 62 | writeField(child, newMappingTree); | ||
| 63 | } else if (entry instanceof MethodEntry) { | ||
| 64 | writeMethod(child, newMappingTree); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | private static void writeField(EntryTreeNode<EntryMapping> fieldNode, MemoryMappingTree mappingTree) { | ||
| 70 | if (fieldNode.getValue() == null || fieldNode.getValue().equals(EntryMapping.DEFAULT)) { | ||
| 71 | return; // Shortcut | ||
| 72 | } | ||
| 73 | |||
| 74 | FieldEntry fieldEntry = ((FieldEntry) fieldNode.getEntry()); | ||
| 75 | mappingTree.visitField(fieldEntry.getName(), fieldEntry.getDesc().toString()); | ||
| 76 | |||
| 77 | EntryMapping fieldMapping = fieldNode.getValue(); | ||
| 78 | |||
| 79 | if (fieldMapping == null) { | ||
| 80 | fieldMapping = EntryMapping.DEFAULT; | ||
| 81 | } | ||
| 82 | |||
| 83 | mappingTree.visitDstName(MappedElementKind.FIELD, 0, fieldMapping.targetName()); | ||
| 84 | mappingTree.visitComment(MappedElementKind.FIELD, fieldMapping.javadoc()); | ||
| 85 | } | ||
| 86 | |||
| 87 | private static void writeMethod(EntryTreeNode<EntryMapping> methodNode, MemoryMappingTree mappingTree) { | ||
| 88 | MethodEntry methodEntry = ((MethodEntry) methodNode.getEntry()); | ||
| 89 | mappingTree.visitMethod(methodEntry.getName(), methodEntry.getDesc().toString()); | ||
| 90 | |||
| 91 | EntryMapping methodMapping = methodNode.getValue(); | ||
| 92 | |||
| 93 | if (methodMapping == null) { | ||
| 94 | methodMapping = EntryMapping.DEFAULT; | ||
| 95 | } | ||
| 96 | |||
| 97 | mappingTree.visitDstName(MappedElementKind.METHOD, 0, methodMapping.targetName()); | ||
| 98 | mappingTree.visitComment(MappedElementKind.METHOD, methodMapping.javadoc()); | ||
| 99 | |||
| 100 | for (EntryTreeNode<EntryMapping> child : methodNode.getChildNodes()) { | ||
| 101 | Entry<?> entry = child.getEntry(); | ||
| 102 | |||
| 103 | if (entry instanceof LocalVariableEntry) { | ||
| 104 | writeMethodArg(child, mappingTree); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | private static void writeMethodArg(EntryTreeNode<EntryMapping> methodArgNode, MemoryMappingTree mappingTree) { | ||
| 110 | if (methodArgNode.getValue() == null || methodArgNode.getValue().equals(EntryMapping.DEFAULT)) { | ||
| 111 | return; // Shortcut | ||
| 112 | } | ||
| 113 | |||
| 114 | LocalVariableEntry methodArgEntry = ((LocalVariableEntry) methodArgNode.getEntry()); | ||
| 115 | mappingTree.visitMethodArg(-1, methodArgEntry.getIndex(), methodArgEntry.getName()); | ||
| 116 | |||
| 117 | EntryMapping methodArgMapping = methodArgNode.getValue(); | ||
| 118 | |||
| 119 | if (methodArgMapping == null) { | ||
| 120 | methodArgMapping = EntryMapping.DEFAULT; | ||
| 121 | } | ||
| 122 | |||
| 123 | mappingTree.visitDstName(MappedElementKind.METHOD_ARG, 0, methodArgMapping.targetName()); | ||
| 124 | mappingTree.visitComment(MappedElementKind.METHOD_ARG, methodArgMapping.javadoc()); | ||
| 125 | } | ||
| 126 | } | ||