diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/ConvertMain.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/ConvertMain.java | 361 |
1 files changed, 361 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..4c6be7f --- /dev/null +++ b/src/main/java/cuchaz/enigma/ConvertMain.java | |||
| @@ -0,0 +1,361 @@ | |||
| 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 | package cuchaz.enigma; | ||
| 12 | |||
| 13 | import java.io.File; | ||
| 14 | import java.io.IOException; | ||
| 15 | import java.util.jar.JarFile; | ||
| 16 | |||
| 17 | import cuchaz.enigma.convert.*; | ||
| 18 | import cuchaz.enigma.gui.ClassMatchingGui; | ||
| 19 | import cuchaz.enigma.gui.MemberMatchingGui; | ||
| 20 | import cuchaz.enigma.mapping.*; | ||
| 21 | import cuchaz.enigma.throwables.MappingConflict; | ||
| 22 | import cuchaz.enigma.throwables.MappingParseException; | ||
| 23 | |||
| 24 | |||
| 25 | public 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(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 (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(new ClassMatchingGui.SaveListener() { | ||
| 130 | @Override | ||
| 131 | public void save(ClassMatches matches) { | ||
| 132 | try { | ||
| 133 | MatchesWriter.writeClasses(matches, classMatchesFile); | ||
| 134 | } catch (IOException ex) { | ||
| 135 | throw new Error(ex); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | }); | ||
| 139 | } | ||
| 140 | |||
| 141 | @SuppressWarnings("unused") | ||
| 142 | private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile) | ||
| 143 | throws IOException, MappingConflict { | ||
| 144 | System.out.println("Reading class matches..."); | ||
| 145 | ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile); | ||
| 146 | Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar); | ||
| 147 | deobfuscators.source.setMappings(mappings); | ||
| 148 | |||
| 149 | Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest); | ||
| 150 | new MappingsEnigmaWriter().write(outMappingsFile, newMappings, true); | ||
| 151 | System.out.println("Write converted mappings to: " + outMappingsFile.getAbsolutePath()); | ||
| 152 | } | ||
| 153 | |||
| 154 | private static void computeFieldMatches(File memberMatchesFile, JarFile destJar, File destMappingsFile, File classMatchesFile) | ||
| 155 | throws IOException, MappingParseException { | ||
| 156 | |||
| 157 | System.out.println("Reading class matches..."); | ||
| 158 | ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile); | ||
| 159 | System.out.println("Reading mappings..."); | ||
| 160 | Mappings destMappings = new MappingsEnigmaReader().read(destMappingsFile); | ||
| 161 | System.out.println("Indexing dest jar..."); | ||
| 162 | Deobfuscator destDeobfuscator = new Deobfuscator(destJar); | ||
| 163 | |||
| 164 | System.out.println("Writing matches..."); | ||
| 165 | |||
| 166 | // get the matched and unmatched mappings | ||
| 167 | MemberMatches<FieldEntry> fieldMatches = MappingsConverter.computeMemberMatches( | ||
| 168 | destDeobfuscator, | ||
| 169 | destMappings, | ||
| 170 | classMatches, | ||
| 171 | MappingsConverter.getFieldDoer() | ||
| 172 | ); | ||
| 173 | |||
| 174 | MatchesWriter.writeMembers(fieldMatches, memberMatchesFile); | ||
| 175 | System.out.println("Wrote:\n\t" + memberMatchesFile.getAbsolutePath()); | ||
| 176 | } | ||
| 177 | |||
| 178 | private static void editFieldMatches(JarFile sourceJar, JarFile destJar, File destMappingsFile, Mappings sourceMappings, File classMatchesFile, final File fieldMatchesFile) | ||
| 179 | throws IOException, MappingParseException { | ||
| 180 | |||
| 181 | System.out.println("Reading matches..."); | ||
| 182 | ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile); | ||
| 183 | MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile); | ||
| 184 | |||
| 185 | // prep deobfuscators | ||
| 186 | Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar); | ||
| 187 | deobfuscators.source.setMappings(sourceMappings); | ||
| 188 | Mappings destMappings = new MappingsEnigmaReader().read(destMappingsFile); | ||
| 189 | MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex()); | ||
| 190 | checker.dropBrokenMappings(destMappings); | ||
| 191 | deobfuscators.dest.setMappings(destMappings); | ||
| 192 | |||
| 193 | new MemberMatchingGui<>(classMatches, fieldMatches, deobfuscators.source, deobfuscators.dest).setSaveListener(new MemberMatchingGui.SaveListener<FieldEntry>() { | ||
| 194 | @Override | ||
| 195 | public void save(MemberMatches<FieldEntry> matches) { | ||
| 196 | try { | ||
| 197 | MatchesWriter.writeMembers(matches, fieldMatchesFile); | ||
| 198 | } catch (IOException ex) { | ||
| 199 | throw new Error(ex); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | }); | ||
| 203 | } | ||
| 204 | |||
| 205 | @SuppressWarnings("unused") | ||
| 206 | private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile, File fieldMatchesFile) | ||
| 207 | throws IOException, MappingConflict { | ||
| 208 | |||
| 209 | System.out.println("Reading matches..."); | ||
| 210 | ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile); | ||
| 211 | MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile); | ||
| 212 | |||
| 213 | Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar); | ||
| 214 | deobfuscators.source.setMappings(mappings); | ||
| 215 | |||
| 216 | // apply matches | ||
| 217 | Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest); | ||
| 218 | MappingsConverter.applyMemberMatches(newMappings, classMatches, fieldMatches, MappingsConverter.getFieldDoer()); | ||
| 219 | |||
| 220 | // write out the converted mappings | ||
| 221 | |||
| 222 | new MappingsEnigmaWriter().write(outMappingsFile, newMappings, true); | ||
| 223 | System.out.println("Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath()); | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | private static void computeMethodMatches(File methodMatchesFile, JarFile destJar, File destMappingsFile, File classMatchesFile) | ||
| 228 | throws IOException, MappingParseException { | ||
| 229 | |||
| 230 | System.out.println("Reading class matches..."); | ||
| 231 | ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile); | ||
| 232 | System.out.println("Reading mappings..."); | ||
| 233 | Mappings destMappings = new MappingsEnigmaReader().read(destMappingsFile); | ||
| 234 | System.out.println("Indexing dest jar..."); | ||
| 235 | Deobfuscator destDeobfuscator = new Deobfuscator(destJar); | ||
| 236 | |||
| 237 | System.out.println("Writing method matches..."); | ||
| 238 | |||
| 239 | // get the matched and unmatched mappings | ||
| 240 | MemberMatches<BehaviorEntry> methodMatches = MappingsConverter.computeMemberMatches( | ||
| 241 | destDeobfuscator, | ||
| 242 | destMappings, | ||
| 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(new MemberMatchingGui.SaveListener<BehaviorEntry>() { | ||
| 267 | @Override | ||
| 268 | public void save(MemberMatches<BehaviorEntry> matches) { | ||
| 269 | try { | ||
| 270 | MatchesWriter.writeMembers(matches, methodMatchesFile); | ||
| 271 | } catch (IOException ex) { | ||
| 272 | throw new Error(ex); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | }); | ||
| 276 | } | ||
| 277 | |||
| 278 | private static void convertMappings(File outMappingsFile, JarFile sourceJar, JarFile destJar, Mappings mappings, File classMatchesFile, File fieldMatchesFile, File methodMatchesFile) | ||
| 279 | throws IOException, MappingConflict { | ||
| 280 | |||
| 281 | System.out.println("Reading matches..."); | ||
| 282 | ClassMatches classMatches = MatchesReader.readClasses(classMatchesFile); | ||
| 283 | MemberMatches<FieldEntry> fieldMatches = MatchesReader.readMembers(fieldMatchesFile); | ||
| 284 | MemberMatches<BehaviorEntry> methodMatches = MatchesReader.readMembers(methodMatchesFile); | ||
| 285 | |||
| 286 | Deobfuscators deobfuscators = new Deobfuscators(sourceJar, destJar); | ||
| 287 | deobfuscators.source.setMappings(mappings); | ||
| 288 | |||
| 289 | // apply matches | ||
| 290 | Mappings newMappings = MappingsConverter.newMappings(classMatches, mappings, deobfuscators.source, deobfuscators.dest); | ||
| 291 | MappingsConverter.applyMemberMatches(newMappings, classMatches, fieldMatches, MappingsConverter.getFieldDoer()); | ||
| 292 | MappingsConverter.applyMemberMatches(newMappings, classMatches, methodMatches, MappingsConverter.getMethodDoer()); | ||
| 293 | |||
| 294 | // check the final mappings | ||
| 295 | MappingsChecker checker = new MappingsChecker(deobfuscators.dest.getJarIndex()); | ||
| 296 | checker.dropBrokenMappings(newMappings); | ||
| 297 | |||
| 298 | for (java.util.Map.Entry<ClassEntry, ClassMapping> mapping : checker.getDroppedClassMappings().entrySet()) { | ||
| 299 | System.out.println("WARNING: Broken class entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")"); | ||
| 300 | } | ||
| 301 | for (java.util.Map.Entry<ClassEntry, ClassMapping> mapping : checker.getDroppedInnerClassMappings().entrySet()) { | ||
| 302 | System.out.println("WARNING: Broken inner class entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")"); | ||
| 303 | } | ||
| 304 | for (java.util.Map.Entry<FieldEntry, FieldMapping> mapping : checker.getDroppedFieldMappings().entrySet()) { | ||
| 305 | System.out.println("WARNING: Broken field entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")"); | ||
| 306 | } | ||
| 307 | for (java.util.Map.Entry<BehaviorEntry, MethodMapping> mapping : checker.getDroppedMethodMappings().entrySet()) { | ||
| 308 | System.out.println("WARNING: Broken behavior entry " + mapping.getKey() + " (" + mapping.getValue().getDeobfName() + ")"); | ||
| 309 | } | ||
| 310 | |||
| 311 | //TODO Fix | ||
| 312 | // write out the converted mappings | ||
| 313 | // try (FileWriter out = new FileWriter(outMappingsFile)) { | ||
| 314 | // new MappingsWriter().write(out, newMappings); | ||
| 315 | // } | ||
| 316 | System.out.println("Wrote converted mappings to:\n\t" + outMappingsFile.getAbsolutePath()); | ||
| 317 | } | ||
| 318 | |||
| 319 | private static class Deobfuscators { | ||
| 320 | |||
| 321 | public Deobfuscator source; | ||
| 322 | public Deobfuscator dest; | ||
| 323 | |||
| 324 | public Deobfuscators(JarFile sourceJar, JarFile destJar) { | ||
| 325 | System.out.println("Indexing source jar..."); | ||
| 326 | IndexerThread sourceIndexer = new IndexerThread(sourceJar); | ||
| 327 | sourceIndexer.start(); | ||
| 328 | System.out.println("Indexing dest jar..."); | ||
| 329 | IndexerThread destIndexer = new IndexerThread(destJar); | ||
| 330 | destIndexer.start(); | ||
| 331 | sourceIndexer.joinOrBail(); | ||
| 332 | destIndexer.joinOrBail(); | ||
| 333 | source = sourceIndexer.deobfuscator; | ||
| 334 | dest = destIndexer.deobfuscator; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | private static class IndexerThread extends Thread { | ||
| 339 | |||
| 340 | private JarFile m_jarFile; | ||
| 341 | public Deobfuscator deobfuscator; | ||
| 342 | |||
| 343 | public IndexerThread(JarFile jarFile) { | ||
| 344 | m_jarFile = jarFile; | ||
| 345 | deobfuscator = null; | ||
| 346 | } | ||
| 347 | |||
| 348 | public void joinOrBail() { | ||
| 349 | try { | ||
| 350 | join(); | ||
| 351 | } catch (InterruptedException ex) { | ||
| 352 | throw new Error(ex); | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | @Override | ||
| 357 | public void run() { | ||
| 358 | deobfuscator = new Deobfuscator(m_jarFile); | ||
| 359 | } | ||
| 360 | } | ||
| 361 | } \ No newline at end of file | ||