From 4681411ca1f9fcbe5eba5c4009175033e76c865c Mon Sep 17 00:00:00 2001 From: NebelNidas Date: Mon, 9 Oct 2023 12:29:35 +0200 Subject: Use `VisitableMappingTree` where possible --- .../main/java/cuchaz/enigma/gui/GuiController.java | 5 ++- .../mapping/serde/MappingIoConverter.java | 47 +++++++++++++--------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java index 0eecc7b..2c15305 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/GuiController.java @@ -33,6 +33,7 @@ import net.fabricmc.mappingio.MappingReader; import net.fabricmc.mappingio.MappingWriter; import net.fabricmc.mappingio.tree.MemoryMappingTree; import net.fabricmc.mappingio.tree.VisitOrder; +import net.fabricmc.mappingio.tree.VisitableMappingTree; import cuchaz.enigma.Enigma; import cuchaz.enigma.EnigmaProfile; @@ -167,7 +168,7 @@ public class GuiController implements ClientPacketHandler { } progress.init(1, loadingMessage); - MemoryMappingTree mappingTree = new MemoryMappingTree(); + VisitableMappingTree mappingTree = new MemoryMappingTree(); MappingReader.read(path, format.getMappingIoCounterpart(), mappingTree); mappings = MappingIoConverter.fromMappingIo(mappingTree, progress); } else { @@ -229,7 +230,7 @@ public class GuiController implements ClientPacketHandler { loadedMappingPath = path; if (useMappingIo) { - MemoryMappingTree mappingTree = MappingIoConverter.toMappingIo(mapper.getObfToDeobf(), progress); + VisitableMappingTree mappingTree = MappingIoConverter.toMappingIo(mapper.getObfToDeobf(), progress); progress.init(1, I18n.translate("progress.mappings.writing")); MappingWriter writer = MappingWriter.create(path, format.getMappingIoCounterpart()); 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 index 05d862a..a0912fa 100644 --- a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingIoConverter.java +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/MappingIoConverter.java @@ -1,5 +1,7 @@ package cuchaz.enigma.translation.mapping.serde; +import java.io.IOException; +import java.io.UncheckedIOException; import java.util.Deque; import java.util.LinkedList; import java.util.List; @@ -7,6 +9,7 @@ import java.util.stream.StreamSupport; import net.fabricmc.mappingio.MappedElementKind; import net.fabricmc.mappingio.tree.MemoryMappingTree; +import net.fabricmc.mappingio.tree.VisitableMappingTree; import net.fabricmc.mappingio.tree.MappingTree.ClassMapping; import net.fabricmc.mappingio.tree.MappingTree.FieldMapping; import net.fabricmc.mappingio.tree.MappingTree.MethodArgMapping; @@ -29,27 +32,31 @@ import cuchaz.enigma.translation.representation.entry.MethodEntry; import cuchaz.enigma.utils.I18n; public class MappingIoConverter { - public static MemoryMappingTree toMappingIo(EntryTree mappings, ProgressListener progress) { - List> classes = StreamSupport.stream(mappings.spliterator(), false) - .filter(node -> node.getEntry() instanceof ClassEntry) - .toList(); + public static VisitableMappingTree toMappingIo(EntryTree mappings, ProgressListener progress) { + try { + List> classes = StreamSupport.stream(mappings.spliterator(), false) + .filter(node -> node.getEntry() instanceof ClassEntry) + .toList(); - progress.init(classes.size(), I18n.translate("progress.mappings.converting.to_mappingio")); - int steps = 0; + progress.init(classes.size(), I18n.translate("progress.mappings.converting.to_mappingio")); + int steps = 0; - MemoryMappingTree mappingTree = new MemoryMappingTree(); - mappingTree.visitNamespaces("intermediary", List.of("named")); + MemoryMappingTree mappingTree = new MemoryMappingTree(); + mappingTree.visitNamespaces("intermediary", List.of("named")); - for (EntryTreeNode classNode : classes) { - progress.step(steps++, classNode.getEntry().getFullName()); - writeClass(classNode, mappings, mappingTree); - } + for (EntryTreeNode classNode : classes) { + progress.step(steps++, classNode.getEntry().getFullName()); + writeClass(classNode, mappings, mappingTree); + } - mappingTree.visitEnd(); - return mappingTree; + mappingTree.visitEnd(); + return mappingTree; + } catch (IOException e) { + throw new UncheckedIOException(e); + } } - private static void writeClass(EntryTreeNode classNode, EntryMap oldMappingTree, MemoryMappingTree newMappingTree) { + private static void writeClass(EntryTreeNode classNode, EntryMap oldMappingTree, VisitableMappingTree newMappingTree) throws IOException { ClassEntry classEntry = (ClassEntry) classNode.getEntry(); EntryMapping mapping = oldMappingTree.get(classEntry); Deque parts = new LinkedList<>(); @@ -83,7 +90,7 @@ public class MappingIoConverter { } } - private static void writeField(EntryTreeNode fieldNode, MemoryMappingTree mappingTree) { + private static void writeField(EntryTreeNode fieldNode, VisitableMappingTree mappingTree) throws IOException { if (fieldNode.getValue() == null || fieldNode.getValue().equals(EntryMapping.DEFAULT)) { return; // Shortcut } @@ -101,7 +108,7 @@ public class MappingIoConverter { mappingTree.visitComment(MappedElementKind.FIELD, fieldMapping.javadoc()); } - private static void writeMethod(EntryTreeNode methodNode, MemoryMappingTree mappingTree) { + private static void writeMethod(EntryTreeNode methodNode, VisitableMappingTree mappingTree) throws IOException { MethodEntry methodEntry = ((MethodEntry) methodNode.getEntry()); mappingTree.visitMethod(methodEntry.getName(), methodEntry.getDesc().toString()); @@ -127,7 +134,7 @@ public class MappingIoConverter { } } - private static void writeMethodArg(EntryTreeNode argNode, MemoryMappingTree mappingTree) { + private static void writeMethodArg(EntryTreeNode argNode, VisitableMappingTree mappingTree) throws IOException { if (argNode.getValue() == null || argNode.getValue().equals(EntryMapping.DEFAULT)) { return; // Shortcut } @@ -145,7 +152,7 @@ public class MappingIoConverter { mappingTree.visitComment(MappedElementKind.METHOD_ARG, argMapping.javadoc()); } - private static void writeMethodVar(EntryTreeNode varNode, MemoryMappingTree mappingTree) { + private static void writeMethodVar(EntryTreeNode varNode, VisitableMappingTree mappingTree) throws IOException { if (varNode.getValue() == null || varNode.getValue().equals(EntryMapping.DEFAULT)) { return; // Shortcut } @@ -163,7 +170,7 @@ public class MappingIoConverter { mappingTree.visitComment(MappedElementKind.METHOD_VAR, varMapping.javadoc()); } - public static EntryTree fromMappingIo(MemoryMappingTree mappingTree, ProgressListener progress) { + public static EntryTree fromMappingIo(VisitableMappingTree mappingTree, ProgressListener progress) { EntryTree dstMappingTree = new HashEntryTree<>(); progress.init(mappingTree.getClasses().size(), I18n.translate("progress.mappings.converting.from_mappingio")); int steps = 0; -- cgit v1.2.3