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