diff options
| author | 2021-05-15 15:23:52 +0300 | |
|---|---|---|
| committer | 2021-05-15 14:23:52 +0200 | |
| commit | 6a6b5878e1ff3c06abe26cf33bea49d44a78420a (patch) | |
| tree | e15dd3398101a98dd27de453aef704070c996f13 | |
| parent | Bump version (diff) | |
| download | enigma-6a6b5878e1ff3c06abe26cf33bea49d44a78420a.tar.gz enigma-6a6b5878e1ff3c06abe26cf33bea49d44a78420a.tar.xz enigma-6a6b5878e1ff3c06abe26cf33bea49d44a78420a.zip | |
Add EnigmaMappingsReader.readFiles (#381)
| -rw-r--r-- | enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/enigma/EnigmaMappingsReader.java | 55 |
1 files changed, 43 insertions, 12 deletions
diff --git a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/enigma/EnigmaMappingsReader.java b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/enigma/EnigmaMappingsReader.java index 30de85f0..ca8e4e11 100644 --- a/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/enigma/EnigmaMappingsReader.java +++ b/enigma/src/main/java/cuchaz/enigma/translation/mapping/serde/enigma/EnigmaMappingsReader.java | |||
| @@ -77,7 +77,38 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 77 | } | 77 | } |
| 78 | }; | 78 | }; |
| 79 | 79 | ||
| 80 | protected void readFile(Path path, EntryTree<EntryMapping> mappings) throws IOException, MappingParseException { | 80 | /** |
| 81 | * Reads multiple Enigma mapping files. | ||
| 82 | * | ||
| 83 | * @param progress the progress listener | ||
| 84 | * @param paths the Enigma files to read; cannot be empty | ||
| 85 | * @return the parsed mappings | ||
| 86 | * @throws MappingParseException if a mapping file cannot be parsed | ||
| 87 | * @throws IOException if an IO error occurs | ||
| 88 | * @throws IllegalArgumentException if there are no paths to read | ||
| 89 | */ | ||
| 90 | public static EntryTree<EntryMapping> readFiles(ProgressListener progress, Path... paths) throws MappingParseException, IOException { | ||
| 91 | EntryTree<EntryMapping> mappings = new HashEntryTree<>(); | ||
| 92 | |||
| 93 | if (paths.length == 0) { | ||
| 94 | throw new IllegalArgumentException("No paths to read mappings from"); | ||
| 95 | } | ||
| 96 | |||
| 97 | progress.init(paths.length, I18n.translate("progress.mappings.enigma_directory.loading")); | ||
| 98 | int step = 0; | ||
| 99 | |||
| 100 | for (Path file : paths) { | ||
| 101 | progress.step(step++, paths.toString()); | ||
| 102 | if (Files.isHidden(file)) { | ||
| 103 | continue; | ||
| 104 | } | ||
| 105 | readFile(file, mappings); | ||
| 106 | } | ||
| 107 | |||
| 108 | return mappings; | ||
| 109 | } | ||
| 110 | |||
| 111 | private static void readFile(Path path, EntryTree<EntryMapping> mappings) throws IOException, MappingParseException { | ||
| 81 | List<String> lines = Files.readAllLines(path, Charsets.UTF_8); | 112 | List<String> lines = Files.readAllLines(path, Charsets.UTF_8); |
| 82 | Deque<MappingPair<?, RawEntryMapping>> mappingStack = new ArrayDeque<>(); | 113 | Deque<MappingPair<?, RawEntryMapping>> mappingStack = new ArrayDeque<>(); |
| 83 | 114 | ||
| @@ -110,7 +141,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 110 | cleanMappingStack(0, mappingStack, mappings); | 141 | cleanMappingStack(0, mappingStack, mappings); |
| 111 | } | 142 | } |
| 112 | 143 | ||
| 113 | private void cleanMappingStack(int indentation, Deque<MappingPair<?, RawEntryMapping>> mappingStack, EntryTree<EntryMapping> mappings) { | 144 | private static void cleanMappingStack(int indentation, Deque<MappingPair<?, RawEntryMapping>> mappingStack, EntryTree<EntryMapping> mappings) { |
| 114 | while (indentation < mappingStack.size()) { | 145 | while (indentation < mappingStack.size()) { |
| 115 | MappingPair<?, RawEntryMapping> pair = mappingStack.pop(); | 146 | MappingPair<?, RawEntryMapping> pair = mappingStack.pop(); |
| 116 | if (pair.getMapping() != null) { | 147 | if (pair.getMapping() != null) { |
| @@ -120,7 +151,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 120 | } | 151 | } |
| 121 | 152 | ||
| 122 | @Nullable | 153 | @Nullable |
| 123 | private String formatLine(String line) { | 154 | private static String formatLine(String line) { |
| 124 | line = stripComment(line); | 155 | line = stripComment(line); |
| 125 | line = line.trim(); | 156 | line = line.trim(); |
| 126 | 157 | ||
| @@ -131,7 +162,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 131 | return line; | 162 | return line; |
| 132 | } | 163 | } |
| 133 | 164 | ||
| 134 | private String stripComment(String line) { | 165 | private static String stripComment(String line) { |
| 135 | //Dont support comments on javadoc lines | 166 | //Dont support comments on javadoc lines |
| 136 | if (line.trim().startsWith(EnigmaFormat.COMMENT)) { | 167 | if (line.trim().startsWith(EnigmaFormat.COMMENT)) { |
| 137 | return line; | 168 | return line; |
| @@ -144,7 +175,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 144 | return line; | 175 | return line; |
| 145 | } | 176 | } |
| 146 | 177 | ||
| 147 | private int countIndentation(String line) { | 178 | private static int countIndentation(String line) { |
| 148 | int indent = 0; | 179 | int indent = 0; |
| 149 | for (int i = 0; i < line.length(); i++) { | 180 | for (int i = 0; i < line.length(); i++) { |
| 150 | if (line.charAt(i) != '\t') { | 181 | if (line.charAt(i) != '\t') { |
| @@ -155,7 +186,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 155 | return indent; | 186 | return indent; |
| 156 | } | 187 | } |
| 157 | 188 | ||
| 158 | private MappingPair<?, RawEntryMapping> parseLine(@Nullable MappingPair<?, RawEntryMapping> parent, String line) { | 189 | private static MappingPair<?, RawEntryMapping> parseLine(@Nullable MappingPair<?, RawEntryMapping> parent, String line) { |
| 159 | String[] tokens = line.trim().split("\\s"); | 190 | String[] tokens = line.trim().split("\\s"); |
| 160 | String keyToken = tokens[0].toUpperCase(Locale.ROOT); | 191 | String keyToken = tokens[0].toUpperCase(Locale.ROOT); |
| 161 | Entry<?> parentEntry = parent == null ? null : parent.getEntry(); | 192 | Entry<?> parentEntry = parent == null ? null : parent.getEntry(); |
| @@ -177,7 +208,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 177 | } | 208 | } |
| 178 | } | 209 | } |
| 179 | 210 | ||
| 180 | private void readJavadoc(MappingPair<?, RawEntryMapping> parent, String[] tokens) { | 211 | private static void readJavadoc(MappingPair<?, RawEntryMapping> parent, String[] tokens) { |
| 181 | if (parent == null) | 212 | if (parent == null) |
| 182 | throw new IllegalStateException("Javadoc has no parent!"); | 213 | throw new IllegalStateException("Javadoc has no parent!"); |
| 183 | // Empty string to concat | 214 | // Empty string to concat |
| @@ -188,7 +219,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 188 | parent.getMapping().addJavadocLine(MappingHelper.unescape(jdLine)); | 219 | parent.getMapping().addJavadocLine(MappingHelper.unescape(jdLine)); |
| 189 | } | 220 | } |
| 190 | 221 | ||
| 191 | private MappingPair<ClassEntry, RawEntryMapping> parseClass(@Nullable Entry<?> parent, String[] tokens) { | 222 | private static MappingPair<ClassEntry, RawEntryMapping> parseClass(@Nullable Entry<?> parent, String[] tokens) { |
| 192 | String obfuscatedName = ClassEntry.getInnerName(tokens[1]); | 223 | String obfuscatedName = ClassEntry.getInnerName(tokens[1]); |
| 193 | ClassEntry obfuscatedEntry; | 224 | ClassEntry obfuscatedEntry; |
| 194 | if (parent instanceof ClassEntry) { | 225 | if (parent instanceof ClassEntry) { |
| @@ -220,7 +251,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 220 | } | 251 | } |
| 221 | } | 252 | } |
| 222 | 253 | ||
| 223 | private MappingPair<FieldEntry, RawEntryMapping> parseField(@Nullable Entry<?> parent, String[] tokens) { | 254 | private static MappingPair<FieldEntry, RawEntryMapping> parseField(@Nullable Entry<?> parent, String[] tokens) { |
| 224 | if (!(parent instanceof ClassEntry)) { | 255 | if (!(parent instanceof ClassEntry)) { |
| 225 | throw new RuntimeException("Field must be a child of a class!"); | 256 | throw new RuntimeException("Field must be a child of a class!"); |
| 226 | } | 257 | } |
| @@ -260,7 +291,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 260 | } | 291 | } |
| 261 | } | 292 | } |
| 262 | 293 | ||
| 263 | private MappingPair<MethodEntry, RawEntryMapping> parseMethod(@Nullable Entry<?> parent, String[] tokens) { | 294 | private static MappingPair<MethodEntry, RawEntryMapping> parseMethod(@Nullable Entry<?> parent, String[] tokens) { |
| 264 | if (!(parent instanceof ClassEntry)) { | 295 | if (!(parent instanceof ClassEntry)) { |
| 265 | throw new RuntimeException("Method must be a child of a class!"); | 296 | throw new RuntimeException("Method must be a child of a class!"); |
| 266 | } | 297 | } |
| @@ -300,7 +331,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 300 | } | 331 | } |
| 301 | } | 332 | } |
| 302 | 333 | ||
| 303 | private MappingPair<LocalVariableEntry, RawEntryMapping> parseArgument(@Nullable Entry<?> parent, String[] tokens) { | 334 | private static MappingPair<LocalVariableEntry, RawEntryMapping> parseArgument(@Nullable Entry<?> parent, String[] tokens) { |
| 304 | if (!(parent instanceof MethodEntry)) { | 335 | if (!(parent instanceof MethodEntry)) { |
| 305 | throw new RuntimeException("Method arg must be a child of a method!"); | 336 | throw new RuntimeException("Method arg must be a child of a method!"); |
| 306 | } | 337 | } |
| @@ -313,7 +344,7 @@ public enum EnigmaMappingsReader implements MappingsReader { | |||
| 313 | } | 344 | } |
| 314 | 345 | ||
| 315 | @Nullable | 346 | @Nullable |
| 316 | private AccessModifier parseModifier(String token) { | 347 | private static AccessModifier parseModifier(String token) { |
| 317 | if (token.startsWith("ACC:")) { | 348 | if (token.startsWith("ACC:")) { |
| 318 | return AccessModifier.valueOf(token.substring(4)); | 349 | return AccessModifier.valueOf(token.substring(4)); |
| 319 | } | 350 | } |