summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/ConvertMain.java
diff options
context:
space:
mode:
authorGravatar Thog2017-05-16 00:24:29 +0200
committerGravatar Thog2017-05-16 00:24:29 +0200
commitb280104d2f926ab74772cef2bf1602663cefa312 (patch)
treed130c86a30ec5df37b3a9c4bab576e971ae2e664 /src/main/java/cuchaz/enigma/ConvertMain.java
parentAdd offset for Enum constructor arguments (Fix #58) (diff)
downloadenigma-fork-b280104d2f926ab74772cef2bf1602663cefa312.tar.gz
enigma-fork-b280104d2f926ab74772cef2bf1602663cefa312.tar.xz
enigma-fork-b280104d2f926ab74772cef2bf1602663cefa312.zip
Remove the converter + some reorganization
Diffstat (limited to 'src/main/java/cuchaz/enigma/ConvertMain.java')
-rw-r--r--src/main/java/cuchaz/enigma/ConvertMain.java357
1 files changed, 0 insertions, 357 deletions
diff --git a/src/main/java/cuchaz/enigma/ConvertMain.java b/src/main/java/cuchaz/enigma/ConvertMain.java
deleted file mode 100644
index 3d58f57..0000000
--- a/src/main/java/cuchaz/enigma/ConvertMain.java
+++ /dev/null
@@ -1,357 +0,0 @@
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 ******************************************************************************/
11
12package cuchaz.enigma;
13
14import cuchaz.enigma.convert.*;
15import cuchaz.enigma.gui.ClassMatchingGui;
16import cuchaz.enigma.gui.MemberMatchingGui;
17import cuchaz.enigma.mapping.*;
18import cuchaz.enigma.throwables.MappingConflict;
19import cuchaz.enigma.throwables.MappingParseException;
20
21import java.io.File;
22import java.io.IOException;
23import java.util.jar.JarFile;
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 MappingsEnigmaReader().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(outMappingsFile, sourceJar, destJar, mappings, classMatchesFile, fieldMatchesFile, methodMatchesFile);
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 (MappingConflict ex) {
75 System.out.println(ex.getMessage());
76 ex.printStackTrace();
77 } catch (IllegalArgumentException ex) {
78 System.out.println(ex.getMessage());
79 printHelp();
80 }
81 }
82
83 private static void printHelp() {
84 System.out.println(String.format("%s - %s", Constants.NAME, Constants.VERSION));
85 System.out.println("Usage:");
86 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>");
87 System.out.println("\tWhere <command> is one of:");
88 System.out.println("\t\tcomputeClassMatches");
89 System.out.println("\t\teditClassMatches");
90 System.out.println("\t\tcomputeFieldMatches");
91 System.out.println("\t\teditFieldMatches");
92 System.out.println("\t\teditMethodMatches");
93 System.out.println("\t\tconvertMappings");
94 System.out.println("\tWhere <old-jar> is the already mapped jar.");
95 System.out.println("\tWhere <new-jar> is the unmapped jar.");
96 System.out.println("\tWhere <old-mappings> is the path to the mappings for the old jar.");
97 System.out.println("\tWhere <new-mappings> is the new mappings. (Where you want to save them and there name)");
98 System.out.println("\tWhere <class-matches> is the class matches file.");
99 System.out.println("\tWhere <field-matches> is the field matches file.");
100 System.out.println("\tWhere <method-matches> is the method matches file.");
101 }
102
103 //Copy of getArg from CommandMain.... Should make a utils class.
104 private static String getArg(String[] args, int i, String name, boolean required) {
105 if (i >= args.length) {
106 if (required) {
107 throw new IllegalArgumentException(name + " is required");
108 } else {
109 return null;
110 }
111 }
112 return args[i];
113 }
114
115 private static void computeClassMatches(File classMatchesFile, JarFile sourceJar, JarFile destJar, Mappings mappings)
116 throws IOException {
117 ClassMatches classMatches = MappingsConverter.computeClassMatches(sourceJar, destJar, mappings);
118 MatchesWriter.writeClasses(classMatches, classMatchesFile);
119 System.out.println("Wrote:\n\t" + classMatchesFile.getAbsolutePath());
120 }
121
122 private static void editClasssMatches(final File classMatchesFile, JarFile sourceJar, JarFile destJar, Mappings mappings)
123 throws IOException {
124 System.out.println("Reading class matches...");
125 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
126 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
127 deobfuscators.source.setMappings(mappings);
128 System.out.println("Starting GUI...");
129 new ClassMatchingGui(classMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(matches ->
130 {
131 try {
132 MatchesWriter.writeClasses(matches, classMatchesFile);
133 } catch (IOException ex) {
134 throw new Error(ex);
135 }
136 });
137 }
138
139 @SuppressWarnings("unused")
140 private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile)
141 throws IOException, MappingConflict {
142 System.out.println("Reading class matches...");
143 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
144 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
145 deobfuscators.source.setMappings(mappings);
146
147 Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
148 new MappingsEnigmaWriter().write(outMappingsFile, newMappings, true);
149 System.out.println("Write converted mappings to: " + outMappingsFile.getAbsolutePath());
150 }
151
152 private static void computeFieldMatches(File memberMatchesFile, JarFile destJar, File destMappingsFile, File classMatchesFile)
153 throws IOException, MappingParseException {
154
155 System.out.println("Reading class matches...");
156 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
157 System.out.println("Reading mappings...");
158 Mappings destMappings = new MappingsEnigmaReader().read(destMappingsFile);
159 System.out.println("Indexing dest jar...");
160 Deobfuscator destDeobfuscator = new Deobfuscator(destJar);
161
162 System.out.println("Writing matches...");
163
164 // get the matched and unmatched mappings
165 MemberMatches<FieldEntry> fieldMatches = MappingsConverter.computeMemberMatches(
166 destDeobfuscator,
167 destMappings,
168 classMatches,
169 MappingsConverter.getFieldDoer()
170 );
171
172 MatchesWriter.writeMembers(fieldMatches, memberMatchesFile);
173 System.out.println("Wrote:\n\t" + memberMatchesFile.getAbsolutePath());
174 }
175
176 private static void editFieldMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchesFile, final File fieldMatchesFile)
177 throws IOException, MappingParseException {
178
179 System.out.println("Reading matches...");
180 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
181 MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
182
183 // prep deobfuscators
184 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
185 deobfuscators.source.setMappings(sourceMappings);
186 Mappings destMappings = new MappingsEnigmaReader().read(destMappingsFile);
187 MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
188 checker.dropBrokenMappings(destMappings);
189 deobfuscators.dest.setMappings(destMappings);
190
191 new MemberMatchingGui<>(classMatches, fieldMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(
192 matches ->
193 {
194 try {
195 MatchesWriter.writeMembers(matches, fieldMatchesFile);
196 } catch (IOException ex) {
197 throw new Error(ex);
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, MappingConflict {
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 MappingsEnigmaWriter().write(outMappingsFile, newMappings, true);
220 System.out.println("Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath());
221 }
222
223 private static void computeMethodMatches(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings sourceMappings, File classMatchesFile, File fieldMatchesFile, File methodMatchesFile)
224 throws IOException, MappingParseException {
225
226 System.out.println("Reading class matches...");
227 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
228 System.out.println("Reading dest mappings...");
229 Mappings destMappings = new MappingsEnigmaReader().read(outMappingsFile);
230 System.out.println("Indexing dest jar...");
231 Deobfuscator destDeobfuscator = new Deobfuscator(destJar);
232 System.out.println("Indexing source jar...");
233 Deobfuscator sourceDeobfuscator = new Deobfuscator(sourceJar);
234
235 System.out.println("Writing method matches...");
236
237 // get the matched and unmatched mappings
238 MemberMatches<BehaviorEntry> methodMatches = MappingsConverter.computeMethodsMatches(
239 destDeobfuscator,
240 destMappings,
241 sourceDeobfuscator,
242 sourceMappings,
243 classMatches,
244 MappingsConverter.getMethodDoer()
245 );
246
247 MatchesWriter.writeMembers(methodMatches, methodMatchesFile);
248 System.out.println("Wrote:\n\t" + methodMatchesFile.getAbsolutePath());
249 }
250
251 private static void editMethodMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchesFile, final File methodMatchesFile)
252 throws IOException, MappingParseException {
253
254 System.out.println("Reading matches...");
255 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
256 MemberMatches<BehaviorEntry> methodMatches = MatchesReader.readMembers(methodMatchesFile);
257
258 // prep deobfuscators
259 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
260 deobfuscators.source.setMappings(sourceMappings);
261 Mappings destMappings = new MappingsEnigmaReader().read(destMappingsFile);
262 MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
263 checker.dropBrokenMappings(destMappings);
264 deobfuscators.dest.setMappings(destMappings);
265
266 new MemberMatchingGui<>(classMatches, methodMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(
267 matches ->
268 {
269 try {
270 MatchesWriter.writeMembers(matches, methodMatchesFile);
271 } catch (IOException ex) {
272 throw new Error(ex);
273 }
274 });
275 }
276
277 private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile, File fieldMatchesFile, File methodMatchesFile)
278 throws IOException, MappingConflict {
279
280 System.out.println("Reading matches...");
281 ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile);
282 MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile);
283 MemberMatches<BehaviorEntry> methodMatches = MatchesReader.readMembers(methodMatchesFile);
284
285 Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar);
286 deobfuscators.source.setMappings(mappings);
287
288 // apply matches
289 Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest);
290 MappingsConverter.applyMemberMatches(newMappings, classMatches, fieldMatches, MappingsConverter.getFieldDoer());
291 MappingsConverter.applyMemberMatches(newMappings, classMatches, methodMatches, MappingsConverter.getMethodDoer());
292
293 // check the final mappings
294 MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex());
295 checker.dropBrokenMappings(newMappings);
296
297 for (java.util.Map.Entry<ClassEntry, ClassMapping> mapping : checker.getDroppedClassMappings().entrySet()) {
298 System.out.println("WARNING: Broken class entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
299 }
300 for (java.util.Map.Entry<ClassEntry, ClassMapping> mapping : checker.getDroppedInnerClassMappings().entrySet()) {
301 System.out.println("WARNING: Broken inner class entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
302 }
303 for (java.util.Map.Entry<FieldEntry, FieldMapping> mapping : checker.getDroppedFieldMappings().entrySet()) {
304 System.out.println("WARNING: Broken field entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
305 }
306 for (java.util.Map.Entry<BehaviorEntry, MethodMapping> mapping : checker.getDroppedMethodMappings().entrySet()) {
307 System.out.println("WARNING: Broken behavior entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")");
308 }
309
310 // write out the converted mappings
311 new MappingsEnigmaWriter().write(outMappingsFile, newMappings, true);
312 System.out.println("Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath());
313 }
314
315 private static class Deobfuscators {
316
317 public Deobfuscator source;
318 public Deobfuscator dest;
319
320 public Deobfuscators(JarFile sourceJar, JarFile destJar) {
321 System.out.println("Indexing source jar...");
322 IndexerThread sourceIndexer = new IndexerThread(sourceJar);
323 sourceIndexer.start();
324 System.out.println("Indexing dest jar...");
325 IndexerThread destIndexer = new IndexerThread(destJar);
326 destIndexer.start();
327 sourceIndexer.joinOrBail();
328 destIndexer.joinOrBail();
329 source = sourceIndexer.deobfuscator;
330 dest = destIndexer.deobfuscator;
331 }
332 }
333
334 private static class IndexerThread extends Thread {
335
336 public Deobfuscator deobfuscator;
337 private JarFile jarFile;
338
339 public IndexerThread(JarFile jarFile) {
340 this.jarFile = jarFile;
341 deobfuscator = null;
342 }
343
344 public void joinOrBail() {
345 try {
346 join();
347 } catch (InterruptedException ex) {
348 throw new Error(ex);
349 }
350 }
351
352 @Override
353 public void run() {
354 deobfuscator = new Deobfuscator(jarFile);
355 }
356 }
357} \ No newline at end of file