diff options
| author | 2017-03-08 08:17:04 +0100 | |
|---|---|---|
| committer | 2017-03-08 08:17:04 +0100 | |
| commit | 6e464ea251cab63c776ece0b2a356f1498ffa294 (patch) | |
| tree | 5ed30c03f5ac4cd2d6877874f5ede576049954f7 /src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java | |
| parent | Drop unix case style and implement hashCode when equals is overrided (diff) | |
| download | enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.gz enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.xz enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.zip | |
Follow Fabric guidelines
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java | 340 |
1 files changed, 166 insertions, 174 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java index cdfed72..a0d4313 100644 --- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java +++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java | |||
| @@ -8,178 +8,170 @@ import cuchaz.enigma.throwables.MappingParseException; | |||
| 8 | import java.io.*; | 8 | import java.io.*; |
| 9 | import java.util.Deque; | 9 | import java.util.Deque; |
| 10 | 10 | ||
| 11 | public class MappingsEnigmaReader | 11 | public class MappingsEnigmaReader { |
| 12 | { | 12 | |
| 13 | 13 | public Mappings read(File file) throws IOException, MappingParseException { | |
| 14 | public Mappings read(File file) throws IOException, MappingParseException { | 14 | Mappings mappings; |
| 15 | Mappings mappings; | 15 | |
| 16 | 16 | // Multiple file | |
| 17 | // Multiple file | 17 | if (file.isDirectory()) { |
| 18 | if (file.isDirectory()) | 18 | mappings = new Mappings(Mappings.FormatType.ENIGMA_DIRECTORY); |
| 19 | { | 19 | readDirectory(mappings, file); |
| 20 | mappings = new Mappings(Mappings.FormatType.ENIGMA_DIRECTORY); | 20 | } else { |
| 21 | readDirectory(mappings, file); | 21 | mappings = new Mappings(); |
| 22 | } | 22 | readFile(mappings, file); |
| 23 | else | 23 | } |
| 24 | { | 24 | return mappings; |
| 25 | mappings = new Mappings(); | 25 | } |
| 26 | readFile(mappings, file); | 26 | |
| 27 | } | 27 | public void readDirectory(Mappings mappings, File directory) throws IOException, MappingParseException { |
| 28 | return mappings; | 28 | File[] files = directory.listFiles(); |
| 29 | } | 29 | if (files != null) { |
| 30 | 30 | for (File file : files) { | |
| 31 | public void readDirectory(Mappings mappings, File directory) throws IOException, MappingParseException { | 31 | if (file.isFile() && !file.getName().startsWith(".") && file.getName().endsWith(".mapping")) |
| 32 | File[] files = directory.listFiles(); | 32 | readFile(mappings, file); |
| 33 | if (files != null) { | 33 | else if (file.isDirectory()) |
| 34 | for (File file : files) { | 34 | readDirectory(mappings, file.getAbsoluteFile()); |
| 35 | if (file.isFile() && !file.getName().startsWith(".") && file.getName().endsWith(".mapping")) | 35 | } |
| 36 | readFile(mappings, file); | 36 | mappings.savePreviousState(); |
| 37 | else if (file.isDirectory()) | 37 | } else |
| 38 | readDirectory(mappings, file.getAbsoluteFile()); | 38 | throw new IOException("Cannot access directory" + directory.getAbsolutePath()); |
| 39 | } | 39 | } |
| 40 | mappings.savePreviousState(); | 40 | |
| 41 | } | 41 | public Mappings readFile(Mappings mappings, File file) throws IOException, MappingParseException { |
| 42 | else | 42 | |
| 43 | throw new IOException("Cannot access directory" + directory.getAbsolutePath()); | 43 | BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8)); |
| 44 | } | 44 | Deque<Object> mappingStack = Queues.newArrayDeque(); |
| 45 | 45 | ||
| 46 | public Mappings readFile(Mappings mappings, File file) throws IOException, MappingParseException { | 46 | int lineNumber = 0; |
| 47 | 47 | String line; | |
| 48 | BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8)); | 48 | while ((line = in.readLine()) != null) { |
| 49 | Deque<Object> mappingStack = Queues.newArrayDeque(); | 49 | lineNumber++; |
| 50 | 50 | ||
| 51 | int lineNumber = 0; | 51 | // strip comments |
| 52 | String line; | 52 | int commentPos = line.indexOf('#'); |
| 53 | while ((line = in.readLine()) != null) { | 53 | if (commentPos >= 0) { |
| 54 | lineNumber++; | 54 | line = line.substring(0, commentPos); |
| 55 | 55 | } | |
| 56 | // strip comments | 56 | |
| 57 | int commentPos = line.indexOf('#'); | 57 | // skip blank lines |
| 58 | if (commentPos >= 0) { | 58 | if (line.trim().length() <= 0) { |
| 59 | line = line.substring(0, commentPos); | 59 | continue; |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | // skip blank lines | 62 | // get the indent of this line |
| 63 | if (line.trim().length() <= 0) { | 63 | int indent = 0; |
| 64 | continue; | 64 | for (int i = 0; i < line.length(); i++) { |
| 65 | } | 65 | if (line.charAt(i) != '\t') { |
| 66 | 66 | break; | |
| 67 | // get the indent of this line | 67 | } |
| 68 | int indent = 0; | 68 | indent++; |
| 69 | for (int i = 0; i < line.length(); i++) { | 69 | } |
| 70 | if (line.charAt(i) != '\t') { | 70 | |
| 71 | break; | 71 | // handle stack pops |
| 72 | } | 72 | while (indent < mappingStack.size()) { |
| 73 | indent++; | 73 | mappingStack.pop(); |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | // handle stack pops | 76 | String[] parts = line.trim().split("\\s"); |
| 77 | while (indent < mappingStack.size()) { | 77 | try { |
| 78 | mappingStack.pop(); | 78 | // read the first token |
| 79 | } | 79 | String token = parts[0]; |
| 80 | 80 | ||
| 81 | String[] parts = line.trim().split("\\s"); | 81 | if (token.equalsIgnoreCase("CLASS")) { |
| 82 | try { | 82 | ClassMapping classMapping; |
| 83 | // read the first token | 83 | if (indent <= 0) { |
| 84 | String token = parts[0]; | 84 | // outer class |
| 85 | 85 | classMapping = readClass(parts, false); | |
| 86 | if (token.equalsIgnoreCase("CLASS")) { | 86 | mappings.addClassMapping(classMapping); |
| 87 | ClassMapping classMapping; | 87 | } else { |
| 88 | if (indent <= 0) { | 88 | |
| 89 | // outer class | 89 | // inner class |
| 90 | classMapping = readClass(parts, false); | 90 | if (!(mappingStack.peek() instanceof ClassMapping)) { |
| 91 | mappings.addClassMapping(classMapping); | 91 | throw new MappingParseException(file, lineNumber, "Unexpected CLASS entry here!"); |
| 92 | } else { | 92 | } |
| 93 | 93 | ||
| 94 | // inner class | 94 | classMapping = readClass(parts, true); |
| 95 | if (!(mappingStack.peek() instanceof ClassMapping)) { | 95 | ((ClassMapping) mappingStack.peek()).addInnerClassMapping(classMapping); |
| 96 | throw new MappingParseException(file, lineNumber, "Unexpected CLASS entry here!"); | 96 | } |
| 97 | } | 97 | mappingStack.push(classMapping); |
| 98 | 98 | } else if (token.equalsIgnoreCase("FIELD")) { | |
| 99 | classMapping = readClass(parts, true); | 99 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { |
| 100 | ((ClassMapping) mappingStack.peek()).addInnerClassMapping(classMapping); | 100 | throw new MappingParseException(file, lineNumber, "Unexpected FIELD entry here!"); |
| 101 | } | 101 | } |
| 102 | mappingStack.push(classMapping); | 102 | ((ClassMapping) mappingStack.peek()).addFieldMapping(readField(parts)); |
| 103 | } else if (token.equalsIgnoreCase("FIELD")) { | 103 | } else if (token.equalsIgnoreCase("METHOD")) { |
| 104 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { | 104 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { |
| 105 | throw new MappingParseException(file, lineNumber, "Unexpected FIELD entry here!"); | 105 | throw new MappingParseException(file, lineNumber, "Unexpected METHOD entry here!"); |
| 106 | } | 106 | } |
| 107 | ((ClassMapping) mappingStack.peek()).addFieldMapping(readField(parts)); | 107 | MethodMapping methodMapping = readMethod(parts); |
| 108 | } else if (token.equalsIgnoreCase("METHOD")) { | 108 | ((ClassMapping) mappingStack.peek()).addMethodMapping(methodMapping); |
| 109 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { | 109 | mappingStack.push(methodMapping); |
| 110 | throw new MappingParseException(file, lineNumber, "Unexpected METHOD entry here!"); | 110 | } else if (token.equalsIgnoreCase("ARG")) { |
| 111 | } | 111 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof MethodMapping)) { |
| 112 | MethodMapping methodMapping = readMethod(parts); | 112 | throw new MappingParseException(file, lineNumber, "Unexpected ARG entry here!"); |
| 113 | ((ClassMapping) mappingStack.peek()).addMethodMapping(methodMapping); | 113 | } |
| 114 | mappingStack.push(methodMapping); | 114 | ((MethodMapping) mappingStack.peek()).addArgumentMapping(readArgument(parts)); |
| 115 | } else if (token.equalsIgnoreCase("ARG")) { | 115 | } |
| 116 | if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof MethodMapping)) { | 116 | } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) { |
| 117 | throw new MappingParseException(file, lineNumber, "Unexpected ARG entry here!"); | 117 | throw new MappingParseException(file, lineNumber, "Malformed line:\n" + line); |
| 118 | } | 118 | } catch (MappingConflict e) { |
| 119 | ((MethodMapping) mappingStack.peek()).addArgumentMapping(readArgument(parts)); | 119 | throw new MappingParseException(file, lineNumber, e.getMessage()); |
| 120 | } | 120 | } |
| 121 | } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) { | 121 | } |
| 122 | throw new MappingParseException(file, lineNumber, "Malformed line:\n" + line); | 122 | in.close(); |
| 123 | } catch (MappingConflict e) { | 123 | return mappings; |
| 124 | throw new MappingParseException(file, lineNumber, e.getMessage()); | 124 | } |
| 125 | } | 125 | |
| 126 | } | 126 | private ArgumentMapping readArgument(String[] parts) { |
| 127 | in.close(); | 127 | return new ArgumentMapping(Integer.parseInt(parts[1]), parts[2]); |
| 128 | return mappings; | 128 | } |
| 129 | } | 129 | |
| 130 | 130 | private ClassMapping readClass(String[] parts, boolean makeSimple) { | |
| 131 | private ArgumentMapping readArgument(String[] parts) { | 131 | if (parts.length == 2) { |
| 132 | return new ArgumentMapping(Integer.parseInt(parts[1]), parts[2]); | 132 | return new ClassMapping(parts[1]); |
| 133 | } | 133 | } else if (parts.length == 3) { |
| 134 | 134 | boolean access = parts[2].startsWith("ACC:"); | |
| 135 | private ClassMapping readClass(String[] parts, boolean makeSimple) { | 135 | ClassMapping mapping; |
| 136 | if (parts.length == 2) { | 136 | if (access) |
| 137 | return new ClassMapping(parts[1]); | 137 | mapping = new ClassMapping(parts[1], null, Mappings.EntryModifier.valueOf(parts[2].substring(4))); |
| 138 | } else if (parts.length == 3) { | 138 | else |
| 139 | boolean access = parts[2].startsWith("ACC:"); | 139 | mapping = new ClassMapping(parts[1], parts[2]); |
| 140 | ClassMapping mapping; | 140 | |
| 141 | if (access) | 141 | return mapping; |
| 142 | mapping = new ClassMapping(parts[1], null, Mappings.EntryModifier.valueOf(parts[2].substring(4))); | 142 | } else if (parts.length == 4) |
| 143 | else | 143 | return new ClassMapping(parts[1], parts[2], Mappings.EntryModifier.valueOf(parts[3].substring(4))); |
| 144 | mapping = new ClassMapping(parts[1], parts[2]); | 144 | return null; |
| 145 | 145 | } | |
| 146 | return mapping; | 146 | |
| 147 | } else if (parts.length == 4) | 147 | /* TEMP */ |
| 148 | return new ClassMapping(parts[1], parts[2], Mappings.EntryModifier.valueOf(parts[3].substring(4))); | 148 | protected FieldMapping readField(String[] parts) { |
| 149 | return null; | 149 | FieldMapping mapping = null; |
| 150 | } | 150 | if (parts.length == 4) { |
| 151 | 151 | boolean access = parts[3].startsWith("ACC:"); | |
| 152 | /* TEMP */ | 152 | if (access) |
| 153 | protected FieldMapping readField(String[] parts) { | 153 | mapping = new FieldMapping(parts[1], new Type(parts[2]), null, |
| 154 | FieldMapping mapping = null; | 154 | Mappings.EntryModifier.valueOf(parts[3].substring(4))); |
| 155 | if (parts.length == 4) | 155 | else |
| 156 | { | 156 | mapping = new FieldMapping(parts[1], new Type(parts[3]), parts[2], Mappings.EntryModifier.UNCHANGED); |
| 157 | boolean access = parts[3].startsWith("ACC:"); | 157 | } else if (parts.length == 5) |
| 158 | if (access) | 158 | mapping = new FieldMapping(parts[1], new Type(parts[3]), parts[2], Mappings.EntryModifier.valueOf(parts[4].substring(4))); |
| 159 | mapping = new FieldMapping(parts[1], new Type(parts[2]), null, | 159 | return mapping; |
| 160 | Mappings.EntryModifier.valueOf(parts[3].substring(4))); | 160 | } |
| 161 | else | 161 | |
| 162 | mapping = new FieldMapping(parts[1], new Type(parts[3]), parts[2], Mappings.EntryModifier.UNCHANGED); | 162 | private MethodMapping readMethod(String[] parts) { |
| 163 | } | 163 | MethodMapping mapping = null; |
| 164 | else if (parts.length == 5) | 164 | if (parts.length == 3) |
| 165 | mapping = new FieldMapping(parts[1], new Type(parts[3]), parts[2], Mappings.EntryModifier.valueOf(parts[4].substring(4))); | 165 | mapping = new MethodMapping(parts[1], new Signature(parts[2])); |
| 166 | return mapping; | 166 | else if (parts.length == 4) { |
| 167 | } | 167 | boolean access = parts[3].startsWith("ACC:"); |
| 168 | 168 | if (access) | |
| 169 | private MethodMapping readMethod(String[] parts) { | 169 | mapping = new MethodMapping(parts[1], new Signature(parts[2]), null, Mappings.EntryModifier.valueOf(parts[3].substring(4))); |
| 170 | MethodMapping mapping = null; | 170 | else |
| 171 | if (parts.length == 3) | 171 | mapping = new MethodMapping(parts[1], new Signature(parts[3]), parts[2]); |
| 172 | mapping = new MethodMapping(parts[1], new Signature(parts[2])); | 172 | } else if (parts.length == 5) |
| 173 | else if (parts.length == 4){ | 173 | mapping = new MethodMapping(parts[1], new Signature(parts[3]), parts[2], |
| 174 | boolean access = parts[3].startsWith("ACC:"); | 174 | Mappings.EntryModifier.valueOf(parts[4].substring(4))); |
| 175 | if (access) | 175 | return mapping; |
| 176 | mapping = new MethodMapping(parts[1], new Signature(parts[2]), null, Mappings.EntryModifier.valueOf(parts[3].substring(4))); | 176 | } |
| 177 | else | ||
| 178 | mapping = new MethodMapping(parts[1], new Signature(parts[3]), parts[2]); | ||
| 179 | } | ||
| 180 | else if (parts.length == 5) | ||
| 181 | mapping = new MethodMapping(parts[1], new Signature(parts[3]), parts[2], | ||
| 182 | Mappings.EntryModifier.valueOf(parts[4].substring(4))); | ||
| 183 | return mapping; | ||
| 184 | } | ||
| 185 | } | 177 | } |