diff options
| author | 2020-06-03 13:39:42 -0400 | |
|---|---|---|
| committer | 2020-06-03 18:39:42 +0100 | |
| commit | 0f47403d0220757fed189b76e2071e25b1025cb8 (patch) | |
| tree | 879bf72c4476f0a5e0d82da99d7ff2b2276bcaca /src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java | |
| parent | Fix search dialog hanging for a short time sometimes (#250) (diff) | |
| download | enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.gz enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.xz enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.zip | |
Split GUI code to separate module (#242)
* Split into modules
* Post merge compile fixes
Co-authored-by: modmuss50 <modmuss50@gmail.com>
Diffstat (limited to 'src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java | 148 |
1 files changed, 0 insertions, 148 deletions
diff --git a/src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java b/src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java deleted file mode 100644 index fc7afbc..0000000 --- a/src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java +++ /dev/null | |||
| @@ -1,148 +0,0 @@ | |||
| 1 | package cuchaz.enigma.command; | ||
| 2 | |||
| 3 | import cuchaz.enigma.ProgressListener; | ||
| 4 | import cuchaz.enigma.throwables.MappingParseException; | ||
| 5 | import cuchaz.enigma.translation.MappingTranslator; | ||
| 6 | import cuchaz.enigma.translation.Translator; | ||
| 7 | import cuchaz.enigma.translation.mapping.EntryMapping; | ||
| 8 | import cuchaz.enigma.translation.mapping.MappingSaveParameters; | ||
| 9 | import cuchaz.enigma.translation.mapping.VoidEntryResolver; | ||
| 10 | import cuchaz.enigma.translation.mapping.serde.*; | ||
| 11 | import cuchaz.enigma.translation.mapping.tree.EntryTree; | ||
| 12 | import cuchaz.enigma.translation.mapping.tree.EntryTreeNode; | ||
| 13 | import cuchaz.enigma.translation.mapping.tree.HashEntryTree; | ||
| 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.MethodEntry; | ||
| 18 | |||
| 19 | import java.io.IOException; | ||
| 20 | import java.nio.file.Files; | ||
| 21 | import java.nio.file.Path; | ||
| 22 | import java.util.HashSet; | ||
| 23 | import java.util.Set; | ||
| 24 | |||
| 25 | public final class MappingCommandsUtil { | ||
| 26 | private MappingCommandsUtil() {} | ||
| 27 | |||
| 28 | public static EntryTree<EntryMapping> invert(EntryTree<EntryMapping> mappings) { | ||
| 29 | Translator translator = new MappingTranslator(mappings, VoidEntryResolver.INSTANCE); | ||
| 30 | EntryTree<EntryMapping> result = new HashEntryTree<>(); | ||
| 31 | |||
| 32 | for (EntryTreeNode<EntryMapping> node : mappings) { | ||
| 33 | Entry<?> leftEntry = node.getEntry(); | ||
| 34 | EntryMapping leftMapping = node.getValue(); | ||
| 35 | |||
| 36 | if (!(leftEntry instanceof ClassEntry || leftEntry instanceof MethodEntry || leftEntry instanceof FieldEntry)) { | ||
| 37 | result.insert(translator.translate(leftEntry), leftMapping); | ||
| 38 | continue; | ||
| 39 | } | ||
| 40 | |||
| 41 | Entry<?> rightEntry = translator.translate(leftEntry); | ||
| 42 | |||
| 43 | result.insert(rightEntry, leftMapping == null ? null : new EntryMapping(leftEntry.getName())); // TODO: leftMapping.withName once javadoc PR is merged | ||
| 44 | } | ||
| 45 | |||
| 46 | return result; | ||
| 47 | } | ||
| 48 | |||
| 49 | public static EntryTree<EntryMapping> compose(EntryTree<EntryMapping> left, EntryTree<EntryMapping> right, boolean keepLeftOnly, boolean keepRightOnly) { | ||
| 50 | Translator leftTranslator = new MappingTranslator(left, VoidEntryResolver.INSTANCE); | ||
| 51 | EntryTree<EntryMapping> result = new HashEntryTree<>(); | ||
| 52 | Set<Entry<?>> addedMappings = new HashSet<>(); | ||
| 53 | |||
| 54 | for (EntryTreeNode<EntryMapping> node : left) { | ||
| 55 | Entry<?> leftEntry = node.getEntry(); | ||
| 56 | EntryMapping leftMapping = node.getValue(); | ||
| 57 | |||
| 58 | Entry<?> rightEntry = leftTranslator.translate(leftEntry); | ||
| 59 | |||
| 60 | EntryMapping rightMapping = right.get(rightEntry); | ||
| 61 | if (rightMapping != null) { | ||
| 62 | result.insert(leftEntry, rightMapping); | ||
| 63 | addedMappings.add(rightEntry); | ||
| 64 | } else if (keepLeftOnly) { | ||
| 65 | result.insert(leftEntry, leftMapping); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | if (keepRightOnly) { | ||
| 70 | Translator leftInverseTranslator = new MappingTranslator(invert(left), VoidEntryResolver.INSTANCE); | ||
| 71 | for (EntryTreeNode<EntryMapping> node : right) { | ||
| 72 | Entry<?> rightEntry = node.getEntry(); | ||
| 73 | EntryMapping rightMapping = node.getValue(); | ||
| 74 | |||
| 75 | if (!addedMappings.contains(rightEntry)) { | ||
| 76 | result.insert(leftInverseTranslator.translate(rightEntry), rightMapping); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | return result; | ||
| 81 | } | ||
| 82 | |||
| 83 | public static EntryTree<EntryMapping> read(String type, Path path, MappingSaveParameters saveParameters) throws MappingParseException, IOException { | ||
| 84 | if (type.equals("enigma")) { | ||
| 85 | return (Files.isDirectory(path) ? EnigmaMappingsReader.DIRECTORY : EnigmaMappingsReader.ZIP).read(path, ProgressListener.none(), saveParameters); | ||
| 86 | } | ||
| 87 | |||
| 88 | if (type.equals("tiny")) { | ||
| 89 | return TinyMappingsReader.INSTANCE.read(path, ProgressListener.none(), saveParameters); | ||
| 90 | } | ||
| 91 | |||
| 92 | MappingFormat format = null; | ||
| 93 | try { | ||
| 94 | format = MappingFormat.valueOf(type.toUpperCase()); | ||
| 95 | } catch (IllegalArgumentException ignored) { | ||
| 96 | if (type.equals("tinyv2")) { | ||
| 97 | format = MappingFormat.TINY_V2; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | if (format != null) { | ||
| 102 | return format.getReader().read(path, ProgressListener.none(), saveParameters); | ||
| 103 | } | ||
| 104 | |||
| 105 | throw new IllegalArgumentException("no reader for " + type); | ||
| 106 | } | ||
| 107 | |||
| 108 | public static void write(EntryTree<EntryMapping> mappings, String type, Path path, MappingSaveParameters saveParameters) { | ||
| 109 | if (type.equals("enigma")) { | ||
| 110 | EnigmaMappingsWriter.DIRECTORY.write(mappings, path, ProgressListener.none(), saveParameters); | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | |||
| 114 | if (type.startsWith("tinyv2:") || type.startsWith("tiny_v2:")) { | ||
| 115 | String[] split = type.split(":"); | ||
| 116 | |||
| 117 | if (split.length != 3) { | ||
| 118 | throw new IllegalArgumentException("specify column names as 'tinyv2:from_namespace:to_namespace'"); | ||
| 119 | } | ||
| 120 | |||
| 121 | new TinyV2Writer(split[1], split[2]).write(mappings, path, ProgressListener.none(), saveParameters); | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | if (type.startsWith("tiny:")) { | ||
| 126 | String[] split = type.split(":"); | ||
| 127 | |||
| 128 | if (split.length != 3) { | ||
| 129 | throw new IllegalArgumentException("specify column names as 'tiny:from_column:to_column'"); | ||
| 130 | } | ||
| 131 | |||
| 132 | new TinyMappingsWriter(split[1], split[2]).write(mappings, path, ProgressListener.none(), saveParameters); | ||
| 133 | return; | ||
| 134 | } | ||
| 135 | |||
| 136 | MappingFormat format = null; | ||
| 137 | try { | ||
| 138 | format = MappingFormat.valueOf(type.toUpperCase()); | ||
| 139 | } catch (IllegalArgumentException ignored) {} | ||
| 140 | |||
| 141 | if (format != null) { | ||
| 142 | format.getWriter().write(mappings, path, ProgressListener.none(), saveParameters); | ||
| 143 | return; | ||
| 144 | } | ||
| 145 | |||
| 146 | throw new IllegalArgumentException("no writer for " + type); | ||
| 147 | } | ||
| 148 | } | ||