summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java
diff options
context:
space:
mode:
authorGravatar gegy10002018-07-05 13:02:28 +0200
committerGravatar gegy10002018-07-05 13:02:28 +0200
commit6aafa87757ab80a164a30f3601bcfb83ae48f559 (patch)
tree86f6d7d2de7b28f43908994313390cb5fea99f31 /src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java
parentFix old mappings not properly being removed (diff)
downloadenigma-fork-6aafa87757ab80a164a30f3601bcfb83ae48f559.tar.gz
enigma-fork-6aafa87757ab80a164a30f3601bcfb83ae48f559.tar.xz
enigma-fork-6aafa87757ab80a164a30f3601bcfb83ae48f559.zip
Fix old mappings not properly being removed
Diffstat (limited to 'src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java')
-rw-r--r--src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java143
1 files changed, 71 insertions, 72 deletions
diff --git a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java
index d1d5634..a53793d 100644
--- a/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java
+++ b/src/main/java/cuchaz/enigma/mapping/MappingsEnigmaReader.java
@@ -39,88 +39,87 @@ public class MappingsEnigmaReader {
39 } 39 }
40 40
41 public Mappings readFile(Mappings mappings, File file) throws IOException, MappingParseException { 41 public Mappings readFile(Mappings mappings, File file) throws IOException, MappingParseException {
42 try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8))) {
43 Deque<Object> mappingStack = Queues.newArrayDeque();
44
45 int lineNumber = 0;
46 String line;
47 while ((line = in.readLine()) != null) {
48 lineNumber++;
49
50 // strip comments
51 int commentPos = line.indexOf('#');
52 if (commentPos >= 0) {
53 line = line.substring(0, commentPos);
54 }
42 55
43 BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), Charsets.UTF_8)); 56 // skip blank lines
44 Deque<Object> mappingStack = Queues.newArrayDeque(); 57 if (line.trim().length() <= 0) {
45 58 continue;
46 int lineNumber = 0; 59 }
47 String line;
48 while ((line = in.readLine()) != null) {
49 lineNumber++;
50
51 // strip comments
52 int commentPos = line.indexOf('#');
53 if (commentPos >= 0) {
54 line = line.substring(0, commentPos);
55 }
56
57 // skip blank lines
58 if (line.trim().length() <= 0) {
59 continue;
60 }
61 60
62 // get the indent of this line 61 // get the indent of this line
63 int indent = 0; 62 int indent = 0;
64 for (int i = 0; i < line.length(); i++) { 63 for (int i = 0; i < line.length(); i++) {
65 if (line.charAt(i) != '\t') { 64 if (line.charAt(i) != '\t') {
66 break; 65 break;
66 }
67 indent++;
67 } 68 }
68 indent++;
69 }
70 69
71 // handle stack pops 70 // handle stack pops
72 while (indent < mappingStack.size()) { 71 while (indent < mappingStack.size()) {
73 mappingStack.pop(); 72 mappingStack.pop();
74 } 73 }
75 74
76 String[] parts = line.trim().split("\\s"); 75 String[] parts = line.trim().split("\\s");
77 try { 76 try {
78 // read the first token 77 // read the first token
79 String token = parts[0]; 78 String token = parts[0];
80 79
81 if (token.equalsIgnoreCase("CLASS")) { 80 if (token.equalsIgnoreCase("CLASS")) {
82 ClassMapping classMapping; 81 ClassMapping classMapping;
83 if (indent <= 0) { 82 if (indent <= 0) {
84 // outer class 83 // outer class
85 classMapping = readClass(parts, false); 84 classMapping = readClass(parts, false);
86 mappings.addClassMapping(classMapping); 85 mappings.addClassMapping(classMapping);
87 } else { 86 } else {
88 87
89 // inner class 88 // inner class
90 if (!(mappingStack.peek() instanceof ClassMapping)) { 89 if (!(mappingStack.peek() instanceof ClassMapping)) {
91 throw new MappingParseException(file, lineNumber, "Unexpected CLASS entry here!"); 90 throw new MappingParseException(file, lineNumber, "Unexpected CLASS entry here!");
91 }
92
93 classMapping = readClass(parts, true);
94 ((ClassMapping) mappingStack.peek()).addInnerClassMapping(classMapping);
92 } 95 }
93 96 mappingStack.push(classMapping);
94 classMapping = readClass(parts, true); 97 } else if (token.equalsIgnoreCase("FIELD")) {
95 ((ClassMapping) mappingStack.peek()).addInnerClassMapping(classMapping); 98 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) {
96 } 99 throw new MappingParseException(file, lineNumber, "Unexpected FIELD entry here!");
97 mappingStack.push(classMapping); 100 }
98 } else if (token.equalsIgnoreCase("FIELD")) { 101 ((ClassMapping) mappingStack.peek()).addFieldMapping(readField(parts));
99 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { 102 } else if (token.equalsIgnoreCase("METHOD")) {
100 throw new MappingParseException(file, lineNumber, "Unexpected FIELD entry here!"); 103 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) {
101 } 104 throw new MappingParseException(file, lineNumber, "Unexpected METHOD entry here!");
102 ((ClassMapping) mappingStack.peek()).addFieldMapping(readField(parts)); 105 }
103 } else if (token.equalsIgnoreCase("METHOD")) { 106 MethodMapping methodMapping = readMethod(parts);
104 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof ClassMapping)) { 107 ((ClassMapping) mappingStack.peek()).addMethodMapping(methodMapping);
105 throw new MappingParseException(file, lineNumber, "Unexpected METHOD entry here!"); 108 mappingStack.push(methodMapping);
106 } 109 } else if (token.equalsIgnoreCase("ARG")) {
107 MethodMapping methodMapping = readMethod(parts); 110 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof MethodMapping)) {
108 ((ClassMapping) mappingStack.peek()).addMethodMapping(methodMapping); 111 throw new MappingParseException(file, lineNumber, "Unexpected ARG entry here!");
109 mappingStack.push(methodMapping); 112 }
110 } else if (token.equalsIgnoreCase("ARG")) { 113 ((MethodMapping) mappingStack.peek()).addArgumentMapping(readArgument(parts));
111 if (mappingStack.isEmpty() || !(mappingStack.peek() instanceof MethodMapping)) {
112 throw new MappingParseException(file, lineNumber, "Unexpected ARG entry here!");
113 } 114 }
114 ((MethodMapping) mappingStack.peek()).addArgumentMapping(readArgument(parts)); 115 } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) {
116 throw new MappingParseException(file, lineNumber, "Malformed line:\n" + line);
117 } catch (MappingConflict e) {
118 throw new MappingParseException(file, lineNumber, e.getMessage());
115 } 119 }
116 } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException ex) {
117 throw new MappingParseException(file, lineNumber, "Malformed line:\n" + line);
118 } catch (MappingConflict e) {
119 throw new MappingParseException(file, lineNumber, e.getMessage());
120 } 120 }
121 return mappings;
121 } 122 }
122 in.close();
123 return mappings;
124 } 123 }
125 124
126 private LocalVariableMapping readArgument(String[] parts) { 125 private LocalVariableMapping readArgument(String[] parts) {