diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/command')
4 files changed, 251 insertions, 38 deletions
diff --git a/src/main/java/cuchaz/enigma/command/ComposeMappingsCommand.java b/src/main/java/cuchaz/enigma/command/ComposeMappingsCommand.java new file mode 100644 index 0000000..1f6a069 --- /dev/null +++ b/src/main/java/cuchaz/enigma/command/ComposeMappingsCommand.java | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | package cuchaz.enigma.command; | ||
| 2 | |||
| 3 | import cuchaz.enigma.throwables.MappingParseException; | ||
| 4 | import cuchaz.enigma.translation.mapping.EntryMapping; | ||
| 5 | import cuchaz.enigma.translation.mapping.tree.EntryTree; | ||
| 6 | import cuchaz.enigma.utils.Utils; | ||
| 7 | |||
| 8 | import java.io.IOException; | ||
| 9 | import java.nio.file.Path; | ||
| 10 | import java.nio.file.Paths; | ||
| 11 | |||
| 12 | public class ComposeMappingsCommand extends Command { | ||
| 13 | public ComposeMappingsCommand() { | ||
| 14 | super("compose-mappings"); | ||
| 15 | } | ||
| 16 | |||
| 17 | @Override | ||
| 18 | public String getUsage() { | ||
| 19 | return "<left-format> <left> <right-format> <right> <result-format> <result> <keep-mode>"; | ||
| 20 | } | ||
| 21 | |||
| 22 | @Override | ||
| 23 | public boolean isValidArgument(int length) { | ||
| 24 | return length == 7; | ||
| 25 | } | ||
| 26 | |||
| 27 | @Override | ||
| 28 | public void run(String... args) throws IOException, MappingParseException { | ||
| 29 | EntryTree<EntryMapping> left = MappingCommandsUtil.read(args[0], Paths.get(args[1])); | ||
| 30 | EntryTree<EntryMapping> right = MappingCommandsUtil.read(args[2], Paths.get(args[3])); | ||
| 31 | EntryTree<EntryMapping> result = MappingCommandsUtil.compose(left, right, args[6].equals("left") || args[6].equals("both"), args[6].equals("right") || args[6].equals("both")); | ||
| 32 | |||
| 33 | Path output = Paths.get(args[5]); | ||
| 34 | Utils.delete(output); | ||
| 35 | MappingCommandsUtil.write(result, args[4], output); | ||
| 36 | } | ||
| 37 | } | ||
diff --git a/src/main/java/cuchaz/enigma/command/ConvertMappingsCommand.java b/src/main/java/cuchaz/enigma/command/ConvertMappingsCommand.java index 75d3791..775bd3e 100644 --- a/src/main/java/cuchaz/enigma/command/ConvertMappingsCommand.java +++ b/src/main/java/cuchaz/enigma/command/ConvertMappingsCommand.java | |||
| @@ -1,47 +1,35 @@ | |||
| 1 | package cuchaz.enigma.command; | 1 | package cuchaz.enigma.command; |
| 2 | 2 | ||
| 3 | import cuchaz.enigma.throwables.MappingParseException; | ||
| 3 | import cuchaz.enigma.translation.mapping.EntryMapping; | 4 | import cuchaz.enigma.translation.mapping.EntryMapping; |
| 4 | import cuchaz.enigma.translation.mapping.serde.MappingFormat; | ||
| 5 | import cuchaz.enigma.translation.mapping.tree.EntryTree; | 5 | import cuchaz.enigma.translation.mapping.tree.EntryTree; |
| 6 | import cuchaz.enigma.utils.Utils; | ||
| 6 | 7 | ||
| 7 | import java.io.File; | 8 | import java.io.IOException; |
| 8 | import java.nio.file.Path; | 9 | import java.nio.file.Path; |
| 9 | import java.util.Locale; | 10 | import java.nio.file.Paths; |
| 10 | 11 | ||
| 11 | public class ConvertMappingsCommand extends Command { | 12 | public class ConvertMappingsCommand extends Command { |
| 12 | 13 | public ConvertMappingsCommand() { | |
| 13 | public ConvertMappingsCommand() { | 14 | super("convert-mappings"); |
| 14 | super("convertmappings"); | 15 | } |
| 15 | } | 16 | |
| 16 | 17 | @Override | |
| 17 | @Override | 18 | public String getUsage() { |
| 18 | public String getUsage() { | 19 | return "<source-format> <source> <result-format> <result>"; |
| 19 | return "<enigma mappings> <converted mappings> <ENIGMA_FILE|ENIGMA_DIRECTORY|SRG_FILE>"; | 20 | } |
| 20 | } | 21 | |
| 21 | 22 | @Override | |
| 22 | @Override | 23 | public boolean isValidArgument(int length) { |
| 23 | public boolean isValidArgument(int length) { | 24 | return length == 4; |
| 24 | return length == 3; | 25 | } |
| 25 | } | 26 | |
| 26 | 27 | @Override | |
| 27 | @Override | 28 | public void run(String... args) throws IOException, MappingParseException { |
| 28 | public void run(String... args) throws Exception { | 29 | EntryTree<EntryMapping> mappings = MappingCommandsUtil.read(args[0], Paths.get(args[1])); |
| 29 | Path fileMappings = getReadablePath(getArg(args, 0, "enigma mappings", true)); | 30 | |
| 30 | File result = getWritableFile(getArg(args, 1, "converted mappings", true)); | 31 | Path output = Paths.get(args[3]); |
| 31 | String name = getArg(args, 2, "format desc", true); | 32 | Utils.delete(output); |
| 32 | MappingFormat saveFormat; | 33 | MappingCommandsUtil.write(mappings, args[2], output); |
| 33 | try { | 34 | } |
| 34 | saveFormat = MappingFormat.valueOf(name.toUpperCase(Locale.ROOT)); | ||
| 35 | } catch (IllegalArgumentException e) { | ||
| 36 | throw new IllegalArgumentException(name + "is not a valid mapping format!"); | ||
| 37 | } | ||
| 38 | |||
| 39 | System.out.println("Reading mappings..."); | ||
| 40 | |||
| 41 | MappingFormat readFormat = chooseEnigmaFormat(fileMappings); | ||
| 42 | EntryTree<EntryMapping> mappings = readFormat.read(fileMappings, new ConsoleProgressListener()); | ||
| 43 | System.out.println("Saving new mappings..."); | ||
| 44 | |||
| 45 | saveFormat.write(mappings, result.toPath(), new ConsoleProgressListener()); | ||
| 46 | } | ||
| 47 | } | 35 | } |
diff --git a/src/main/java/cuchaz/enigma/command/InvertMappingsCommand.java b/src/main/java/cuchaz/enigma/command/InvertMappingsCommand.java new file mode 100644 index 0000000..bfe8308 --- /dev/null +++ b/src/main/java/cuchaz/enigma/command/InvertMappingsCommand.java | |||
| @@ -0,0 +1,36 @@ | |||
| 1 | package cuchaz.enigma.command; | ||
| 2 | |||
| 3 | import cuchaz.enigma.throwables.MappingParseException; | ||
| 4 | import cuchaz.enigma.translation.mapping.EntryMapping; | ||
| 5 | import cuchaz.enigma.translation.mapping.tree.EntryTree; | ||
| 6 | import cuchaz.enigma.utils.Utils; | ||
| 7 | |||
| 8 | import java.io.IOException; | ||
| 9 | import java.nio.file.Path; | ||
| 10 | import java.nio.file.Paths; | ||
| 11 | |||
| 12 | public class InvertMappingsCommand extends Command { | ||
| 13 | public InvertMappingsCommand() { | ||
| 14 | super("invert-mappings"); | ||
| 15 | } | ||
| 16 | |||
| 17 | @Override | ||
| 18 | public String getUsage() { | ||
| 19 | return "<source-format> <source> <result-format> <result>"; | ||
| 20 | } | ||
| 21 | |||
| 22 | @Override | ||
| 23 | public boolean isValidArgument(int length) { | ||
| 24 | return length == 4; | ||
| 25 | } | ||
| 26 | |||
| 27 | @Override | ||
| 28 | public void run(String... args) throws IOException, MappingParseException { | ||
| 29 | EntryTree<EntryMapping> source = MappingCommandsUtil.read(args[0], Paths.get(args[1])); | ||
| 30 | EntryTree<EntryMapping> result = MappingCommandsUtil.invert(source); | ||
| 31 | |||
| 32 | Path output = Paths.get(args[3]); | ||
| 33 | Utils.delete(output); | ||
| 34 | MappingCommandsUtil.write(result, args[2], output); | ||
| 35 | } | ||
| 36 | } | ||
diff --git a/src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java b/src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java new file mode 100644 index 0000000..4679575 --- /dev/null +++ b/src/main/java/cuchaz/enigma/command/MappingCommandsUtil.java | |||
| @@ -0,0 +1,152 @@ | |||
| 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.VoidEntryResolver; | ||
| 9 | import cuchaz.enigma.translation.mapping.serde.*; | ||
| 10 | import cuchaz.enigma.translation.mapping.tree.EntryTree; | ||
| 11 | import cuchaz.enigma.translation.mapping.tree.EntryTreeNode; | ||
| 12 | import cuchaz.enigma.translation.mapping.tree.HashEntryTree; | ||
| 13 | import cuchaz.enigma.translation.representation.entry.ClassEntry; | ||
| 14 | import cuchaz.enigma.translation.representation.entry.Entry; | ||
| 15 | import cuchaz.enigma.translation.representation.entry.FieldEntry; | ||
| 16 | import cuchaz.enigma.translation.representation.entry.MethodEntry; | ||
| 17 | |||
| 18 | import java.io.IOException; | ||
| 19 | import java.nio.file.Path; | ||
| 20 | import java.util.HashMap; | ||
| 21 | import java.util.HashSet; | ||
| 22 | import java.util.Map; | ||
| 23 | import java.util.Set; | ||
| 24 | |||
| 25 | public final class MappingCommandsUtil { | ||
| 26 | public static void main(String[] args) throws Exception { | ||
| 27 | new InvertMappingsCommand().run( | ||
| 28 | "enigma", | ||
| 29 | "D:\\IdeaProjects\\yarn\\mappings", | ||
| 30 | "enigma", | ||
| 31 | "D:\\IdeaProjects\\Enigma\\converted"); | ||
| 32 | } | ||
| 33 | |||
| 34 | private MappingCommandsUtil() {} | ||
| 35 | |||
| 36 | public static EntryTree<EntryMapping> invert(EntryTree<EntryMapping> mappings) { | ||
| 37 | Translator translator = new MappingTranslator(mappings, VoidEntryResolver.INSTANCE); | ||
| 38 | EntryTree<EntryMapping> result = new HashEntryTree<>(); | ||
| 39 | |||
| 40 | for (EntryTreeNode<EntryMapping> node : mappings) { | ||
| 41 | Entry<?> leftEntry = node.getEntry(); | ||
| 42 | EntryMapping leftMapping = node.getValue(); | ||
| 43 | |||
| 44 | if (!(leftEntry instanceof ClassEntry || leftEntry instanceof MethodEntry || leftEntry instanceof FieldEntry)) { | ||
| 45 | result.insert(translator.translate(leftEntry), leftMapping); | ||
| 46 | continue; | ||
| 47 | } | ||
| 48 | |||
| 49 | Entry<?> rightEntry = translator.translate(leftEntry); | ||
| 50 | |||
| 51 | result.insert(rightEntry, leftMapping == null ? null : new EntryMapping(leftEntry.getName())); // TODO: leftMapping.withName once javadoc PR is merged | ||
| 52 | } | ||
| 53 | |||
| 54 | return result; | ||
| 55 | } | ||
| 56 | |||
| 57 | @SuppressWarnings("unchecked") | ||
| 58 | public static EntryTree<EntryMapping> compose(EntryTree<EntryMapping> left, EntryTree<EntryMapping> right, boolean keepLeftOnly, boolean keepRightOnly) { | ||
| 59 | Translator leftTranslator = new MappingTranslator(left, VoidEntryResolver.INSTANCE); | ||
| 60 | EntryTree<EntryMapping> result = new HashEntryTree<>(); | ||
| 61 | Map<Entry<?>, Entry<?>> rightToLeft = new HashMap<>(); | ||
| 62 | Set<Entry<?>> addedMappings = new HashSet<>(); | ||
| 63 | |||
| 64 | for (EntryTreeNode<EntryMapping> node : left) { | ||
| 65 | Entry<?> leftEntry = node.getEntry(); | ||
| 66 | EntryMapping leftMapping = node.getValue(); | ||
| 67 | |||
| 68 | Entry<?> rightEntry = leftTranslator.translate(leftEntry); | ||
| 69 | rightToLeft.put(rightEntry, leftEntry); | ||
| 70 | |||
| 71 | EntryMapping rightMapping = right.get(rightEntry); | ||
| 72 | if (rightMapping != null) { | ||
| 73 | result.insert(leftEntry, rightMapping); | ||
| 74 | addedMappings.add(rightEntry); | ||
| 75 | } else if (keepLeftOnly) { | ||
| 76 | result.insert(leftEntry, leftMapping); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | if (keepRightOnly) { | ||
| 81 | for (EntryTreeNode<EntryMapping> node : right) { | ||
| 82 | Entry<?> rightEntry = node.getEntry(); | ||
| 83 | EntryMapping rightMapping = node.getValue(); | ||
| 84 | |||
| 85 | if (addedMappings.contains(rightEntry)) { | ||
| 86 | continue; | ||
| 87 | } | ||
| 88 | |||
| 89 | Entry<?> parent = rightEntry.getParent(); | ||
| 90 | Entry<?> correctEntry = rightEntry; | ||
| 91 | if (rightToLeft.containsKey(parent)) { | ||
| 92 | correctEntry = ((Entry<Entry<?>>) rightEntry).withParent(rightToLeft.get(parent)); | ||
| 93 | } | ||
| 94 | |||
| 95 | result.insert(correctEntry, rightMapping); | ||
| 96 | rightToLeft.put(rightEntry, correctEntry); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | return result; | ||
| 100 | } | ||
| 101 | |||
| 102 | public static EntryTree<EntryMapping> read(String type, Path path) throws MappingParseException, IOException { | ||
| 103 | if (type.equals("enigma")) { | ||
| 104 | return EnigmaMappingsReader.DIRECTORY.read(path, ProgressListener.none()); | ||
| 105 | } | ||
| 106 | |||
| 107 | if (type.equals("tiny")) { | ||
| 108 | return TinyMappingsReader.INSTANCE.read(path, ProgressListener.none()); | ||
| 109 | } | ||
| 110 | |||
| 111 | MappingFormat format = null; | ||
| 112 | try { | ||
| 113 | format = MappingFormat.valueOf(type.toUpperCase()); | ||
| 114 | } catch (IllegalArgumentException ignored) {} | ||
| 115 | |||
| 116 | if (format != null) { | ||
| 117 | return format.getReader().read(path, ProgressListener.none()); | ||
| 118 | } | ||
| 119 | |||
| 120 | throw new IllegalArgumentException("no reader for " + type); | ||
| 121 | } | ||
| 122 | |||
| 123 | public static void write(EntryTree<EntryMapping> mappings, String type, Path path) { | ||
| 124 | if (type.equals("enigma")) { | ||
| 125 | EnigmaMappingsWriter.DIRECTORY.write(mappings, path, ProgressListener.none()); | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | if (type.startsWith("tiny")) { | ||
| 130 | String[] split = type.split(":"); | ||
| 131 | |||
| 132 | if (split.length != 3) { | ||
| 133 | throw new IllegalArgumentException("specify column names as 'tiny:from_column:to_column'"); | ||
| 134 | } | ||
| 135 | |||
| 136 | new TinyMappingsWriter(split[1], split[2]).write(mappings, path, ProgressListener.none()); | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | MappingFormat format = null; | ||
| 141 | try { | ||
| 142 | format = MappingFormat.valueOf(type.toUpperCase()); | ||
| 143 | } catch (IllegalArgumentException ignored) {} | ||
| 144 | |||
| 145 | if (format != null) { | ||
| 146 | format.getWriter().write(mappings, path, ProgressListener.none()); | ||
| 147 | return; | ||
| 148 | } | ||
| 149 | |||
| 150 | throw new IllegalArgumentException("no writer for " + type); | ||
| 151 | } | ||
| 152 | } | ||