summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/ConvertMain.java
diff options
context:
space:
mode:
authorGravatar lclc982016-06-30 00:49:21 +1000
committerGravatar GitHub2016-06-30 00:49:21 +1000
commit4be005617b3b8c3578cca07c5d085d12916f0d1d (patch)
treedb163431f38703e26da417ef05eaea2b27a498b9 /src/main/java/cuchaz/enigma/ConvertMain.java
parentSome small changes to fix idea importing (diff)
downloadenigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.gz
enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.tar.xz
enigma-fork-4be005617b3b8c3578cca07c5d085d12916f0d1d.zip
Json format (#2)
* Added new format * Fixed bug * Updated Version
Diffstat (limited to 'src/main/java/cuchaz/enigma/ConvertMain.java')
-rw-r--r--src/main/java/cuchaz/enigma/ConvertMain.java362
1 files changed, 362 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/ConvertMain.java b/src/main/java/cuchaz/enigma/ConvertMain.java
new file mode 100644
index 0000000..409a269
--- /dev/null
+++ b/src/main/java/cuchaz/enigma/ConvertMain.java
@@ -0,0 +1,362 @@
1/*******************************************************************************
2 * Copyright (c) 2015 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser General Public
5 * License v3.0 which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 * <p>
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma;
12
13import java.io.File;
14import java.io.FileReader;
15import java.io.FileWriter;
16import java.io.IOException;
17import java.util.jar.JarFile;
18
19import cuchaz.enigma.convert.*;
20import cuchaz.enigma.gui.ClassMatchingGui;
21import cuchaz.enigma.gui.MemberMatchingGui;
22import cuchaz.enigma.mapping.*;
23
24
25public class ConvertMain {
26
27 public static void main(String[] args)
28 throws IOException, MappingParseException {
29 try {
30 //Get all are args
31 String JarOld = getArg(args, 1, "Path to Old Jar", true);
32 String JarNew = getArg(args, 2, "Path to New Jar", true);
33 String OldMappings = getArg(args, 3, "Path to old .mappings file", true);
34 String NewMappings = getArg(args, 4, "Path to new .mappings file", true);
35 String ClassMatches = getArg(args, 5, "Path to Class .matches file", true);
36 String FieldMatches = getArg(args, 6, "Path to Field .matches file", true);
37 String MethodMatches = getArg(args, 7, "Path to Method .matches file", true);
38 //OldJar
39 JarFile sourceJar = new JarFile(new File(JarOld));
40 //NewJar
41 JarFile destJar = new JarFile(new File(JarNew));
42 //Get the mapping files
43 File inMappingsFile = new File(OldMappings);
44 File outMappingsFile = new File(NewMappings);
45 Mappings mappings = new MappingsReader().read( inMappingsFile);
46 //Make the Match Files..
47 File classMatchesFile = new File(ClassMatches);
48 File fieldMatchesFile = new File(FieldMatches);
49 File methodMatchesFile = new File(MethodMatches);
50
51 String command = getArg(args, 0, "command", true);
52
53 if (command.equalsIgnoreCase("computeClassMatches")) {
54 computeClassMatches(classMatchesFile, sourceJar, destJar, mappings);
55 convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile);
56 } else if (command.equalsIgnoreCase("editClassMatches")) {
57 editClasssMatches(classMatchesFile, sourceJar, destJar, mappings);
58 convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile);
59 } else if (command.equalsIgnoreCase("computeFieldMatches")) {
60 computeFieldMatches(fieldMatchesFile, destJar, outMappingsFile, classMatchesFile);
61 convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile, fieldMatchesFile);
62 } else if (command.equalsIgnoreCase("editFieldMatches")) {
63 editFieldMatches(sourceJar, destJar, outMappingsFile, mappings, classMatchesFile, fieldMatchesFile);
64 convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile, fieldMatchesFile);
65 } else if (command.equalsIgnoreCase("computeMethodMatches")) {
66 computeMethodMatches(methodMatchesFile, destJar, outMappingsFile, classMatchesFile);
67 convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile, fieldMatchesFile, methodMatchesFile);
68 } else if (command.equalsIgnoreCase("editMethodMatches")) {
69 editMethodMatches(sourceJar, destJar, outMappingsFile, mappings, classMatchesFile, methodMatchesFile);
70 convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile, fieldMatchesFile, methodMatchesFile);
71 } else if (command.equalsIgnoreCase("convertMappings")) {
72 convertMappings(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile, fieldMatchesFile, methodMatchesFile);
73 }
74 } catch (IllegalArgumentException ex) {
75 System.out.println(ex.getMessage());
76 printHelp();
77 }
78 }
79
80 private static void printHelp() {
81 System.out.println(String.format("%s - %s", Constants.Name, Constants.Version));
82 System.out.println("Usage:");
83 System.out.println("\tjava -cp enigma.jar cuchaz.enigma.ConvertMain <command> <old-jar> <new-jar> <old-mappings> <new-mappings> <class-matches> <field-matches> <method-matches>");
84 System.out.println("\tWhere <command> is one of:");
85 System.out.println("\t\tcomputeClassMatches");
86 System.out.println("\t\teditClassMatches");
87 System.out.println("\t\tcomputeFieldMatches");
88 System.out.println("\t\teditFieldMatches");
89 System.out.println("\t\teditMethodMatches");
90 System.out.println("\t\tconvertMappings");
91 System.out.println("\tWhere <old-jar> is the already mapped jar.");
92 System.out.println("\tWhere <new-jar> is the unmapped jar.");
93 System.out.println("\tWhere <old-mappings> is the path to the mappings for the old jar.");
94 System.out.println("\tWhere <new-mappings> is the new mappings. (Where you want to save them and there name)");
95 System.out.println("\tWhere <class-matches> is the class matches file.");
96 System.out.println("\tWhere <field-matches> is the field matches file.");
97 System.out.println("\tWhere <method-matches> is the method matches file.");
98 }
99
100 //Copy of getArg from CommandMain.... Should make a utils class.
101 private static String getArg(String[] args, int i, String name, boolean required) {
102 if (i >= args.length) {
103 if (required) {
104 throw new IllegalArgumentException(name + " is required");
105 } else {
106 return null;
107 }
108 }
109 return args[i];
110 }
111
112 private static void computeClassMatches(File classMatchesFile, JarFile sourceJar, JarFile destJar, Mappings mappings)
113 throws IOException {
114 ClassMatches classMatches = MappingsConverter.computeClassMatches(sourceJar, destJar, mappings);
115 MatchesWriter.writeClasses(classMatches, classMatchesFile);
116 System.out.println("Wrote:\n\t" + classMatchesFile.getAbsolutePath());
117 }
118
119 private static void editClasssMatches(final File classMatchesFile, JarFile sourceJar, JarFile destJar, Mappings mappings)
120 throws IOException {
121 System.out.println("Reading class matches...");
122 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
123 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
124 deobfuscators.source.setMappings(mappings);
125 System.out.println("Starting GUI...");
126 new ClassMatchingGui(classMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new ClassMatchingGui.SaveListener() {
127 @Override
128 public void save(ClassMatches matches) {
129 try {
130 MatchesWriter.writeClasses(matches, classMatchesFile);
131 } catch (IOException ex) {
132 throw new Error(ex);
133 }
134 }
135 });
136 }
137
138 @SuppressWarnings("unused")
139 private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile)
140 throws IOException {
141 System.out.println("Reading class matches...");
142 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
143 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
144 deobfuscators.source.setMappings(mappings);
145
146 Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
147 new MappingsWriter().write(outMappingsFile, newMappings);
148 System.out.println("Write converted mappings to: " + outMappingsFile.getAbsolutePath());
149 }
150
151 private static void computeFieldMatches(File memberMatchesFile, JarFile destJar, File destMappingsFile, File classMatchesFile)
152 throws IOException, MappingParseException {
153
154 System.out.println("Reading class matches...");
155 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
156 System.out.println("Reading mappings...");
157 Mappings destMappings = new MappingsReader().read(destMappingsFile);
158 System.out.println("Indexing dest jar...");
159 Deobfuscator destDeobfuscator = new Deobfuscator(destJar);
160
161 System.out.println("Writing matches...");
162
163 // get the matched and unmatched mappings
164 MemberMatches<FieldEntry> fieldMatches = MappingsConverter.computeMemberMatches(
165 destDeobfuscator,
166 destMappings,
167 classMatches,
168 MappingsConverter.getFieldDoer()
169 );
170
171 MatchesWriter.writeMembers(fieldMatches, memberMatchesFile);
172 System.out.println("Wrote:\n\t" + memberMatchesFile.getAbsolutePath());
173 }
174
175 private static void editFieldMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchesFile, final File fieldMatchesFile)
176 throws IOException, MappingParseException {
177
178 System.out.println("Reading matches...");
179 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
180 MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
181
182 // prep deobfuscators
183 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
184 deobfuscators.source.setMappings(sourceMappings);
185 Mappings destMappings = new MappingsReader().read(destMappingsFile);
186 MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
187 checker.dropBrokenMappings(destMappings);
188 deobfuscators.dest.setMappings(destMappings);
189
190 new MemberMatchingGui<FieldEntry>(classMatches, fieldMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new MemberMatchingGui.SaveListener<FieldEntry>() {
191 @Override
192 public void save(MemberMatches<FieldEntry> matches) {
193 try {
194 MatchesWriter.writeMembers(matches, fieldMatchesFile);
195 } catch (IOException ex) {
196 throw new Error(ex);
197 }
198 }
199 });
200 }
201
202 @SuppressWarnings("unused")
203 private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile, File fieldMatchesFile)
204 throws IOException {
205
206 System.out.println("Reading matches...");
207 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
208 MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
209
210 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
211 deobfuscators.source.setMappings(mappings);
212
213 // apply matches
214 Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
215 MappingsConverter.applyMemberMatches(newMappings, classMatches, fieldMatches, MappingsConverter.getFieldDoer());
216
217 // write out the converted mappings
218
219 new MappingsWriter().write(outMappingsFile, newMappings);
220 System.out.println("Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath());
221 }
222
223
224 private static void computeMethodMatches(File methodMatchesFile, JarFile destJar, File destMappingsFile, File classMatchesFile)
225 throws IOException, MappingParseException {
226
227 System.out.println("Reading class matches...");
228 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
229 System.out.println("Reading mappings...");
230 Mappings destMappings = new MappingsReader().read(destMappingsFile);
231 System.out.println("Indexing dest jar...");
232 Deobfuscator destDeobfuscator = new Deobfuscator(destJar);
233
234 System.out.println("Writing method matches...");
235
236 // get the matched and unmatched mappings
237 MemberMatches<BehaviorEntry> methodMatches = MappingsConverter.computeMemberMatches(
238 destDeobfuscator,
239 destMappings,
240 classMatches,
241 MappingsConverter.getMethodDoer()
242 );
243
244 MatchesWriter.writeMembers(methodMatches, methodMatchesFile);
245 System.out.println("Wrote:\n\t" + methodMatchesFile.getAbsolutePath());
246 }
247
248 private static void editMethodMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchesFile, final File methodMatchesFile)
249 throws IOException, MappingParseException {
250
251 System.out.println("Reading matches...");
252 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
253 MemberMatches<BehaviorEntry> methodMatches = MatchesReader.readMembers(methodMatchesFile);
254
255 // prep deobfuscators
256 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
257 deobfuscators.source.setMappings(sourceMappings);
258 Mappings destMappings = new MappingsReader().read(destMappingsFile);
259 MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
260 checker.dropBrokenMappings(destMappings);
261 deobfuscators.dest.setMappings(destMappings);
262
263 new MemberMatchingGui<BehaviorEntry>(classMatches, methodMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new MemberMatchingGui.SaveListener<BehaviorEntry>() {
264 @Override
265 public void save(MemberMatches<BehaviorEntry> matches) {
266 try {
267 MatchesWriter.writeMembers(matches, methodMatchesFile);
268 } catch (IOException ex) {
269 throw new Error(ex);
270 }
271 }
272 });
273 }
274
275 private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile, File fieldMatchesFile, File methodMatchesFile)
276 throws IOException {
277
278 System.out.println("Reading matches...");
279 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
280 MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
281 MemberMatches<BehaviorEntry> methodMatches = MatchesReader.readMembers(methodMatchesFile);
282
283 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
284 deobfuscators.source.setMappings(mappings);
285
286 // apply matches
287 Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
288 MappingsConverter.applyMemberMatches(newMappings, classMatches, fieldMatches, MappingsConverter.getFieldDoer());
289 MappingsConverter.applyMemberMatches(newMappings, classMatches, methodMatches, MappingsConverter.getMethodDoer());
290
291 // check the final mappings
292 MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
293 checker.dropBrokenMappings(newMappings);
294
295 for (java.util.Map.Entry<ClassEntry, ClassMapping> mapping : checker.getDroppedClassMappings().entrySet()) {
296 System.out.println("WARNING: Broken class entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
297 }
298 for (java.util.Map.Entry<ClassEntry, ClassMapping> mapping : checker.getDroppedInnerClassMappings().entrySet()) {
299 System.out.println("WARNING: Broken inner class entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
300 }
301 for (java.util.Map.Entry<FieldEntry, FieldMapping> mapping : checker.getDroppedFieldMappings().entrySet()) {
302 System.out.println("WARNING: Broken field entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
303 }
304 for (java.util.Map.Entry<BehaviorEntry, MethodMapping> mapping : checker.getDroppedMethodMappings().entrySet()) {
305 System.out.println("WARNING: Broken behavior entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
306 }
307
308 //TODO Fix
309 // write out the converted mappings
310// try (FileWriter out = new FileWriter(outMappingsFile)) {
311// new MappingsWriter().write(out, newMappings);
312// }
313 System.out.println("Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath());
314 }
315
316 private static class Deobfuscators {
317
318 public Deobfuscator source;
319 public Deobfuscator dest;
320
321 public Deobfuscators(JarFile sourceJar, JarFile destJar) {
322 System.out.println("Indexing source jar...");
323 IndexerThread sourceIndexer = new IndexerThread(sourceJar);
324 sourceIndexer.start();
325 System.out.println("Indexing dest jar...");
326 IndexerThread destIndexer = new IndexerThread(destJar);
327 destIndexer.start();
328 sourceIndexer.joinOrBail();
329 destIndexer.joinOrBail();
330 source = sourceIndexer.deobfuscator;
331 dest = destIndexer.deobfuscator;
332 }
333 }
334
335 private static class IndexerThread extends Thread {
336
337 private JarFile m_jarFile;
338 public Deobfuscator deobfuscator;
339
340 public IndexerThread(JarFile jarFile) {
341 m_jarFile = jarFile;
342 deobfuscator = null;
343 }
344
345 public void joinOrBail() {
346 try {
347 join();
348 } catch (InterruptedException ex) {
349 throw new Error(ex);
350 }
351 }
352
353 @Override
354 public void run() {
355 try {
356 deobfuscator = new Deobfuscator(m_jarFile);
357 } catch (IOException ex) {
358 throw new Error(ex);
359 }
360 }
361 }
362} \ No newline at end of file