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