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
320
321
322
323
324
|
/*******************************************************************************
* Copyright (c) 2015 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public
* License v3.0 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
*
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.jar.JarFile;
import cuchaz.enigma.convert.ClassMatches;
import cuchaz.enigma.convert.MappingsConverter;
import cuchaz.enigma.convert.MatchesReader;
import cuchaz.enigma.convert.MatchesWriter;
import cuchaz.enigma.convert.MemberMatches;
import cuchaz.enigma.gui.ClassMatchingGui;
import cuchaz.enigma.gui.MemberMatchingGui;
import cuchaz.enigma.mapping.BehaviorEntry;
import cuchaz.enigma.mapping.ClassEntry;
import cuchaz.enigma.mapping.ClassMapping;
import cuchaz.enigma.mapping.FieldEntry;
import cuchaz.enigma.mapping.FieldMapping;
import cuchaz.enigma.mapping.MappingParseException;
import cuchaz.enigma.mapping.Mappings;
import cuchaz.enigma.mapping.MappingsChecker;
import cuchaz.enigma.mapping.MappingsReader;
import cuchaz.enigma.mapping.MappingsWriter;
import cuchaz.enigma.mapping.MethodMapping;
public class ConvertMain {
public static void main(String[] args)
throws IOException, MappingParseException {
// init files
String inVer = "1.8.3";
String outVer = "1.8.8";
File home = new File(System.getProperty("user.home"));
JarFile sourceJar = new JarFile(new File(home, ".minecraft/versions/" + inVer + "/" + inVer + ".jar"));
JarFile destJar = new JarFile(new File(home, ".minecraft/versions/" + outVer + "/" + outVer + ".jar"));
File inMappingsFile = new File("../minecraft-mappings/" + inVer + ".mappings");
File outMappingsFile = new File("../minecraft-mappings/" + outVer + ".mappings");
Mappings mappings = new MappingsReader().read(new FileReader(inMappingsFile));
File classMatchesFile = new File(inVer + "to" + outVer + ".class.matches");
File fieldMatchesFile = new File(inVer + "to" + outVer + ".field.matches");
File methodMatchesFile = new File(inVer + "to" + outVer + ".method.matches");
// match classes
//computeClassMatches(classMatchesFile, sourceJar, destJar, mappings);
editClasssMatches(classMatchesFile, sourceJar, destJar, mappings);
//convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile);
// match fields
//computeFieldMatches(fieldMatchesFile, destJar, outMappingsFile, classMatchesFile);
//editFieldMatches(sourceJar, destJar, outMappingsFile, mappings, classMatchesFile, fieldMatchesFile);
//convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile, fieldMatchesFile);
// match methods/constructors
//computeMethodMatches(methodMatchesFile, destJar, outMappingsFile, classMatchesFile);
//editMethodMatches(sourceJar, destJar, outMappingsFile, mappings, classMatchesFile, methodMatchesFile);
//convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile, fieldMatchesFile, methodMatchesFile);
}
private static void computeClassMatches(File classMatchesFile, JarFile sourceJar, JarFile destJar, Mappings mappings)
throws IOException {
ClassMatches classMatches = MappingsConverter.computeClassMatches(sourceJar, destJar, mappings);
MatchesWriter.writeClasses(classMatches, classMatchesFile);
System.out.println("Wrote:\n\t" + classMatchesFile.getAbsolutePath());
}
private static void editClasssMatches(final File classMatchesFile, JarFile sourceJar, JarFile destJar, Mappings mappings)
throws IOException {
System.out.println("Reading class matches...");
ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
deobfuscators.source.setMappings(mappings);
System.out.println("Starting GUI...");
new ClassMatchingGui(classMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new ClassMatchingGui.SaveListener() {
@Override
public void save(ClassMatches matches) {
try {
MatchesWriter.writeClasses(matches, classMatchesFile);
} catch (IOException ex) {
throw new Error(ex);
}
}
});
}
private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile)
throws IOException {
System.out.println("Reading class matches...");
ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
deobfuscators.source.setMappings(mappings);
Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
try (FileWriter out = new FileWriter(outMappingsFile)) {
new MappingsWriter().write(out, newMappings);
}
System.out.println("Write converted mappings to: " + outMappingsFile.getAbsolutePath());
}
private static void computeFieldMatches(File memberMatchesFile, JarFile destJar, File destMappingsFile, File classMatchesFile)
throws IOException, MappingParseException {
System.out.println("Reading class matches...");
ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
System.out.println("Reading mappings...");
Mappings destMappings = new MappingsReader().read(new FileReader(destMappingsFile));
System.out.println("Indexing dest jar...");
Deobfuscator destDeobfuscator = new Deobfuscator(destJar);
System.out.println("Writing matches...");
// get the matched and unmatched mappings
MemberMatches<FieldEntry> fieldMatches = MappingsConverter.computeMemberMatches(
destDeobfuscator,
destMappings,
classMatches,
MappingsConverter.getFieldDoer()
);
MatchesWriter.writeMembers(fieldMatches, memberMatchesFile);
System.out.println("Wrote:\n\t" + memberMatchesFile.getAbsolutePath());
}
private static void editFieldMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchesFile, final File fieldMatchesFile)
throws IOException, MappingParseException {
System.out.println("Reading matches...");
ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
// prep deobfuscators
Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
deobfuscators.source.setMappings(sourceMappings);
Mappings destMappings = new MappingsReader().read(new FileReader(destMappingsFile));
MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
checker.dropBrokenMappings(destMappings);
deobfuscators.dest.setMappings(destMappings);
new MemberMatchingGui<FieldEntry>(classMatches, fieldMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new MemberMatchingGui.SaveListener<FieldEntry>() {
@Override
public void save(MemberMatches<FieldEntry> matches) {
try {
MatchesWriter.writeMembers(matches, fieldMatchesFile);
} catch (IOException ex) {
throw new Error(ex);
}
}
});
}
private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile, File fieldMatchesFile)
throws IOException {
System.out.println("Reading matches...");
ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
deobfuscators.source.setMappings(mappings);
// apply matches
Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
MappingsConverter.applyMemberMatches(newMappings, classMatches, fieldMatches, MappingsConverter.getFieldDoer());
// write out the converted mappings
try (FileWriter out = new FileWriter(outMappingsFile)) {
new MappingsWriter().write(out, newMappings);
}
System.out.println("Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath());
}
private static void computeMethodMatches(File methodMatchesFile, JarFile destJar, File destMappingsFile, File classMatchesFile)
throws IOException, MappingParseException {
System.out.println("Reading class matches...");
ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
System.out.println("Reading mappings...");
Mappings destMappings = new MappingsReader().read(new FileReader(destMappingsFile));
System.out.println("Indexing dest jar...");
Deobfuscator destDeobfuscator = new Deobfuscator(destJar);
System.out.println("Writing method matches...");
// get the matched and unmatched mappings
MemberMatches<BehaviorEntry> methodMatches = MappingsConverter.computeMemberMatches(
destDeobfuscator,
destMappings,
classMatches,
MappingsConverter.getMethodDoer()
);
MatchesWriter.writeMembers(methodMatches, methodMatchesFile);
System.out.println("Wrote:\n\t" + methodMatchesFile.getAbsolutePath());
}
private static void editMethodMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchesFile, final File methodMatchesFile)
throws IOException, MappingParseException {
System.out.println("Reading matches...");
ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
MemberMatches<BehaviorEntry> methodMatches = MatchesReader.readMembers(methodMatchesFile);
// prep deobfuscators
Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
deobfuscators.source.setMappings(sourceMappings);
Mappings destMappings = new MappingsReader().read(new FileReader(destMappingsFile));
MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
checker.dropBrokenMappings(destMappings);
deobfuscators.dest.setMappings(destMappings);
new MemberMatchingGui<BehaviorEntry>(classMatches, methodMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new MemberMatchingGui.SaveListener<BehaviorEntry>() {
@Override
public void save(MemberMatches<BehaviorEntry> matches) {
try {
MatchesWriter.writeMembers(matches, methodMatchesFile);
} catch (IOException ex) {
throw new Error(ex);
}
}
});
}
private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile, File fieldMatchesFile, File methodMatchesFile)
throws IOException {
System.out.println("Reading matches...");
ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
MemberMatches<BehaviorEntry> methodMatches = MatchesReader.readMembers(methodMatchesFile);
Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
deobfuscators.source.setMappings(mappings);
// apply matches
Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
MappingsConverter.applyMemberMatches(newMappings, classMatches, fieldMatches, MappingsConverter.getFieldDoer());
MappingsConverter.applyMemberMatches(newMappings, classMatches, methodMatches, MappingsConverter.getMethodDoer());
// check the final mappings
MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
checker.dropBrokenMappings(newMappings);
for (java.util.Map.Entry<ClassEntry,ClassMapping> mapping : checker.getDroppedClassMappings().entrySet()) {
System.out.println("WARNING: Broken class entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
}
for (java.util.Map.Entry<ClassEntry,ClassMapping> mapping : checker.getDroppedInnerClassMappings().entrySet()) {
System.out.println("WARNING: Broken inner class entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
}
for (java.util.Map.Entry<FieldEntry,FieldMapping> mapping : checker.getDroppedFieldMappings().entrySet()) {
System.out.println("WARNING: Broken field entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
}
for (java.util.Map.Entry<BehaviorEntry,MethodMapping> mapping : checker.getDroppedMethodMappings().entrySet()) {
System.out.println("WARNING: Broken behavior entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
}
// write out the converted mappings
try (FileWriter out = new FileWriter(outMappingsFile)) {
new MappingsWriter().write(out, newMappings);
}
System.out.println("Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath());
}
private static class Deobfuscators {
public Deobfuscator source;
public Deobfuscator dest;
public Deobfuscators(JarFile sourceJar, JarFile destJar) {
System.out.println("Indexing source jar...");
IndexerThread sourceIndexer = new IndexerThread(sourceJar);
sourceIndexer.start();
System.out.println("Indexing dest jar...");
IndexerThread destIndexer = new IndexerThread(destJar);
destIndexer.start();
sourceIndexer.joinOrBail();
destIndexer.joinOrBail();
source = sourceIndexer.deobfuscator;
dest = destIndexer.deobfuscator;
}
}
private static class IndexerThread extends Thread {
private JarFile m_jarFile;
public Deobfuscator deobfuscator;
public IndexerThread(JarFile jarFile) {
m_jarFile = jarFile;
deobfuscator = null;
}
public void joinOrBail() {
try {
join();
} catch (InterruptedException ex) {
throw new Error(ex);
}
}
@Override
public void run() {
try {
deobfuscator = new Deobfuscator(m_jarFile);
} catch (IOException ex) {
throw new Error(ex);
}
}
}
}
|