1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
package cuchaz.enigma.translation.mapping.serde;
import com.google.common.base.Charsets;
import cuchaz.enigma.ProgressListener;
import cuchaz.enigma.throwables.MappingParseException;
import cuchaz.enigma.translation.mapping.AccessModifier;
import cuchaz.enigma.translation.mapping.EntryMapping;
import cuchaz.enigma.translation.mapping.MappingPair;
import cuchaz.enigma.translation.mapping.MappingSaveParameters;
import cuchaz.enigma.translation.mapping.tree.EntryTree;
import cuchaz.enigma.translation.mapping.tree.HashEntryTree;
import cuchaz.enigma.translation.representation.MethodDescriptor;
import cuchaz.enigma.translation.representation.TypeDescriptor;
import cuchaz.enigma.translation.representation.entry.*;
import cuchaz.enigma.utils.I18n;
import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
public enum EnigmaMappingsReader implements MappingsReader {
FILE {
@Override
public EntryTree<EntryMapping> read(Path path, ProgressListener progress, MappingSaveParameters saveParameters) throws IOException, MappingParseException {
progress.init(1, I18n.translate("progress.mappings.enigma_file.loading"));
EntryTree<EntryMapping> mappings = new HashEntryTree<>();
readFile(path, mappings);
progress.step(1, I18n.translate("progress.mappings.enigma_file.done"));
return mappings;
}
},
DIRECTORY {
@Override
public EntryTree<EntryMapping> read(Path root, ProgressListener progress, MappingSaveParameters saveParameters) throws IOException, MappingParseException {
EntryTree<EntryMapping> mappings = new HashEntryTree<>();
List<Path> files = Files.walk(root)
.filter(f -> !Files.isDirectory(f))
.filter(f -> f.toString().endsWith(".mapping"))
.collect(Collectors.toList());
progress.init(files.size(), I18n.translate("progress.mappings.enigma_directory.loading"));
int step = 0;
for (Path file : files) {
progress.step(step++, root.relativize(file).toString());
if (Files.isHidden(file)) {
continue;
}
readFile(file, mappings);
}
return mappings;
}
},
ZIP {
@Override
public EntryTree<EntryMapping> read(Path zip, ProgressListener progress, MappingSaveParameters saveParameters) throws MappingParseException, IOException {
try (FileSystem fs = FileSystems.newFileSystem(zip, (ClassLoader) null)) {
return DIRECTORY.read(fs.getPath("/"), progress, saveParameters);
}
}
};
protected void readFile(Path path, EntryTree<EntryMapping> mappings) throws IOException, MappingParseException {
List<String> lines = Files.readAllLines(path, Charsets.UTF_8);
Deque<MappingPair<?, RawEntryMapping>> mappingStack = new ArrayDeque<>();
for (int lineNumber = 0; lineNumber < lines.size(); lineNumber++) {
String line = lines.get(lineNumber);
int indentation = countIndentation(line);
line = formatLine(line);
if (line == null) {
continue;
}
cleanMappingStack(indentation, mappingStack, mappings);
try {
MappingPair<?, RawEntryMapping> pair = parseLine(mappingStack.peek(), line);
if (pair != null) {
mappingStack.push(pair);
if (pair.getMapping() != null) {
}
}
} catch (Throwable t) {
t.printStackTrace();
throw new MappingParseException(path::toString, lineNumber, t.toString());
}
}
// Clean up rest
cleanMappingStack(0, mappingStack, mappings);
}
private void cleanMappingStack(int indentation, Deque<MappingPair<?, RawEntryMapping>> mappingStack, EntryTree<EntryMapping> mappings) {
while (indentation < mappingStack.size()) {
MappingPair<?, RawEntryMapping> pair = mappingStack.pop();
if (pair.getMapping() != null) {
mappings.insert(pair.getEntry(), pair.getMapping().bake());
}
}
}
@Nullable
private String formatLine(String line) {
line = stripComment(line);
line = line.trim();
if (line.isEmpty()) {
return null;
}
return line;
}
private String stripComment(String line) {
//Dont support comments on javadoc lines
if (line.trim().startsWith(EnigmaFormat.COMMENT)) {
return line;
}
int commentPos = line.indexOf('#');
if (commentPos >= 0) {
return line.substring(0, commentPos);
}
return line;
}
private int countIndentation(String line) {
int indent = 0;
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) != '\t') {
break;
}
indent++;
}
return indent;
}
private MappingPair<?, RawEntryMapping> parseLine(@Nullable MappingPair<?, RawEntryMapping> parent, String line) {
String[] tokens = line.trim().split("\\s");
String keyToken = tokens[0].toUpperCase(Locale.ROOT);
Entry<?> parentEntry = parent == null ? null : parent.getEntry();
switch (keyToken) {
case EnigmaFormat.CLASS:
return parseClass(parentEntry, tokens);
case EnigmaFormat.FIELD:
return parseField(parentEntry, tokens);
case EnigmaFormat.METHOD:
return parseMethod(parentEntry, tokens);
case EnigmaFormat.PARAMETER:
return parseArgument(parentEntry, tokens);
case EnigmaFormat.COMMENT:
readJavadoc(parent, tokens);
return null;
default:
throw new RuntimeException("Unknown token '" + keyToken + "'");
}
}
private void readJavadoc(MappingPair<?, RawEntryMapping> parent, String[] tokens) {
if (parent == null)
throw new IllegalStateException("Javadoc has no parent!");
// Empty string to concat
String jdLine = tokens.length > 1 ? String.join(" ", Arrays.copyOfRange(tokens,1,tokens.length)) : "";
if (parent.getMapping() == null) {
parent.setMapping(new RawEntryMapping(parent.getEntry().getName(), AccessModifier.UNCHANGED));
}
parent.getMapping().addJavadocLine(MappingHelper.unescape(jdLine));
}
private MappingPair<ClassEntry, RawEntryMapping> parseClass(@Nullable Entry<?> parent, String[] tokens) {
String obfuscatedName = ClassEntry.getInnerName(tokens[1]);
ClassEntry obfuscatedEntry;
if (parent instanceof ClassEntry) {
obfuscatedEntry = new ClassEntry((ClassEntry) parent, obfuscatedName);
} else {
obfuscatedEntry = new ClassEntry(obfuscatedName);
}
String mapping = null;
AccessModifier modifier = AccessModifier.UNCHANGED;
if (tokens.length == 3) {
AccessModifier parsedModifier = parseModifier(tokens[2]);
if (parsedModifier != null) {
modifier = parsedModifier;
mapping = obfuscatedName;
} else {
mapping = tokens[2];
}
} else if (tokens.length == 4) {
mapping = tokens[2];
modifier = parseModifier(tokens[3]);
}
if (mapping != null) {
return new MappingPair<>(obfuscatedEntry, new RawEntryMapping(mapping, modifier));
} else {
return new MappingPair<>(obfuscatedEntry);
}
}
private MappingPair<FieldEntry, RawEntryMapping> parseField(@Nullable Entry<?> parent, String[] tokens) {
if (!(parent instanceof ClassEntry)) {
throw new RuntimeException("Field must be a child of a class!");
}
ClassEntry ownerEntry = (ClassEntry) parent;
String obfuscatedName = tokens[1];
String mapping = obfuscatedName;
AccessModifier modifier = AccessModifier.UNCHANGED;
TypeDescriptor descriptor;
if (tokens.length == 3) {
mapping = tokens[1];
descriptor = new TypeDescriptor(tokens[2]);
} else if (tokens.length == 4) {
AccessModifier parsedModifier = parseModifier(tokens[3]);
if (parsedModifier != null) {
descriptor = new TypeDescriptor(tokens[2]);
modifier = parsedModifier;
} else {
mapping = tokens[2];
descriptor = new TypeDescriptor(tokens[3]);
}
} else if (tokens.length == 5) {
descriptor = new TypeDescriptor(tokens[3]);
mapping = tokens[2];
modifier = parseModifier(tokens[4]);
} else {
throw new RuntimeException("Invalid field declaration");
}
FieldEntry obfuscatedEntry = new FieldEntry(ownerEntry, obfuscatedName, descriptor);
if (mapping != null) {
return new MappingPair<>(obfuscatedEntry, new RawEntryMapping(mapping, modifier));
} else {
return new MappingPair<>(obfuscatedEntry);
}
}
private MappingPair<MethodEntry, RawEntryMapping> parseMethod(@Nullable Entry<?> parent, String[] tokens) {
if (!(parent instanceof ClassEntry)) {
throw new RuntimeException("Method must be a child of a class!");
}
ClassEntry ownerEntry = (ClassEntry) parent;
String obfuscatedName = tokens[1];
String mapping = null;
AccessModifier modifier = AccessModifier.UNCHANGED;
MethodDescriptor descriptor;
if (tokens.length == 3) {
descriptor = new MethodDescriptor(tokens[2]);
} else if (tokens.length == 4) {
AccessModifier parsedModifier = parseModifier(tokens[3]);
if (parsedModifier != null) {
modifier = parsedModifier;
mapping = obfuscatedName;
descriptor = new MethodDescriptor(tokens[2]);
} else {
mapping = tokens[2];
descriptor = new MethodDescriptor(tokens[3]);
}
} else if (tokens.length == 5) {
mapping = tokens[2];
modifier = parseModifier(tokens[4]);
descriptor = new MethodDescriptor(tokens[3]);
} else {
throw new RuntimeException("Invalid method declaration");
}
MethodEntry obfuscatedEntry = new MethodEntry(ownerEntry, obfuscatedName, descriptor);
if (mapping != null) {
return new MappingPair<>(obfuscatedEntry, new RawEntryMapping(mapping, modifier));
} else {
return new MappingPair<>(obfuscatedEntry);
}
}
private MappingPair<LocalVariableEntry, RawEntryMapping> parseArgument(@Nullable Entry<?> parent, String[] tokens) {
if (!(parent instanceof MethodEntry)) {
throw new RuntimeException("Method arg must be a child of a method!");
}
MethodEntry ownerEntry = (MethodEntry) parent;
LocalVariableEntry obfuscatedEntry = new LocalVariableEntry(ownerEntry, Integer.parseInt(tokens[1]), "", true, null);
String mapping = tokens[2];
return new MappingPair<>(obfuscatedEntry, new RawEntryMapping(mapping));
}
@Nullable
private AccessModifier parseModifier(String token) {
if (token.startsWith("ACC:")) {
return AccessModifier.valueOf(token.substring(4));
}
return null;
}
}
|