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