diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/CommandMain.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/CommandMain.java | 57 |
1 files changed, 38 insertions, 19 deletions
diff --git a/src/main/java/cuchaz/enigma/CommandMain.java b/src/main/java/cuchaz/enigma/CommandMain.java index 6c8caa4..a84cd5e 100644 --- a/src/main/java/cuchaz/enigma/CommandMain.java +++ b/src/main/java/cuchaz/enigma/CommandMain.java | |||
| @@ -11,11 +11,15 @@ | |||
| 11 | 11 | ||
| 12 | package cuchaz.enigma; | 12 | package cuchaz.enigma; |
| 13 | 13 | ||
| 14 | import cuchaz.enigma.Deobfuscator.ProgressListener; | 14 | import cuchaz.enigma.translation.mapping.EntryMapping; |
| 15 | import cuchaz.enigma.mapping.Mappings; | 15 | import cuchaz.enigma.translation.mapping.serde.MappingFormat; |
| 16 | import cuchaz.enigma.mapping.MappingsEnigmaReader; | 16 | import cuchaz.enigma.translation.mapping.tree.EntryTree; |
| 17 | 17 | ||
| 18 | import java.io.File; | 18 | import java.io.File; |
| 19 | import java.nio.file.Files; | ||
| 20 | import java.nio.file.Path; | ||
| 21 | import java.nio.file.Paths; | ||
| 22 | import java.util.Locale; | ||
| 19 | import java.util.jar.JarFile; | 23 | import java.util.jar.JarFile; |
| 20 | 24 | ||
| 21 | public class CommandMain { | 25 | public class CommandMain { |
| @@ -52,7 +56,7 @@ public class CommandMain { | |||
| 52 | private static void decompile(String[] args) throws Exception { | 56 | private static void decompile(String[] args) throws Exception { |
| 53 | File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); | 57 | File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); |
| 54 | File fileJarOut = getWritableFolder(getArg(args, 2, "out folder", true)); | 58 | File fileJarOut = getWritableFolder(getArg(args, 2, "out folder", true)); |
| 55 | File fileMappings = getReadableFile(getArg(args, 3, "mappings file", false)); | 59 | Path fileMappings = getReadablePath(getArg(args, 3, "mappings file", false)); |
| 56 | Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn)); | 60 | Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn)); |
| 57 | deobfuscator.writeSources(fileJarOut, new ConsoleProgressListener()); | 61 | deobfuscator.writeSources(fileJarOut, new ConsoleProgressListener()); |
| 58 | } | 62 | } |
| @@ -60,43 +64,47 @@ public class CommandMain { | |||
| 60 | private static void deobfuscate(String[] args) throws Exception { | 64 | private static void deobfuscate(String[] args) throws Exception { |
| 61 | File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); | 65 | File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); |
| 62 | File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true)); | 66 | File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true)); |
| 63 | File fileMappings = getReadableFile(getArg(args, 3, "mappings file", false)); | 67 | Path fileMappings = getReadablePath(getArg(args, 3, "mappings file", false)); |
| 64 | Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn)); | 68 | Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn)); |
| 65 | deobfuscator.writeJar(fileJarOut, new ConsoleProgressListener()); | 69 | deobfuscator.writeJar(fileJarOut, new ConsoleProgressListener()); |
| 66 | } | 70 | } |
| 67 | 71 | ||
| 68 | private static Deobfuscator getDeobfuscator(File fileMappings, JarFile jar) throws Exception { | 72 | private static Deobfuscator getDeobfuscator(Path fileMappings, JarFile jar) throws Exception { |
| 69 | System.out.println("Reading jar..."); | 73 | System.out.println("Reading jar..."); |
| 70 | Deobfuscator deobfuscator = new Deobfuscator(jar); | 74 | Deobfuscator deobfuscator = new Deobfuscator(jar); |
| 71 | if (fileMappings != null) { | 75 | if (fileMappings != null) { |
| 72 | System.out.println("Reading mappings..."); | 76 | System.out.println("Reading mappings..."); |
| 73 | Mappings mappings = new MappingsEnigmaReader().read(fileMappings); | 77 | EntryTree<EntryMapping> mappings = chooseEnigmaFormat(fileMappings).read(fileMappings); |
| 74 | deobfuscator.setMappings(mappings); | 78 | deobfuscator.setMappings(mappings); |
| 75 | } | 79 | } |
| 76 | return deobfuscator; | 80 | return deobfuscator; |
| 77 | } | 81 | } |
| 78 | 82 | ||
| 79 | private static void convertMappings(String[] args) throws Exception { | 83 | private static void convertMappings(String[] args) throws Exception { |
| 80 | File fileMappings = getReadableFile(getArg(args, 1, "enigma mapping", true)); | 84 | Path fileMappings = getReadablePath(getArg(args, 1, "enigma mapping", true)); |
| 81 | File result = getWritableFile(getArg(args, 2, "enigma mapping", true)); | 85 | File result = getWritableFile(getArg(args, 2, "enigma mapping", true)); |
| 82 | String name = getArg(args, 3, "format desc", true); | 86 | String name = getArg(args, 3, "format desc", true); |
| 83 | Mappings.FormatType formatType; | 87 | MappingFormat saveFormat; |
| 84 | try { | 88 | try { |
| 85 | formatType = Mappings.FormatType.valueOf(name.toUpperCase()); | 89 | saveFormat = MappingFormat.valueOf(name.toUpperCase(Locale.ROOT)); |
| 86 | } catch (IllegalArgumentException e) { | 90 | } catch (IllegalArgumentException e) { |
| 87 | throw new IllegalArgumentException(name + "is not a valid mapping format!"); | 91 | throw new IllegalArgumentException(name + "is not a valid mapping format!"); |
| 88 | } | 92 | } |
| 89 | 93 | ||
| 90 | System.out.println("Reading mappings..."); | 94 | System.out.println("Reading mappings..."); |
| 91 | Mappings mappings = new MappingsEnigmaReader().read(fileMappings); | 95 | |
| 96 | MappingFormat readFormat = chooseEnigmaFormat(fileMappings); | ||
| 97 | EntryTree<EntryMapping> mappings = readFormat.read(fileMappings); | ||
| 92 | System.out.println("Saving new mappings..."); | 98 | System.out.println("Saving new mappings..."); |
| 93 | switch (formatType) { | 99 | |
| 94 | case SRG_FILE: | 100 | saveFormat.write(mappings, result.toPath(), new ConsoleProgressListener()); |
| 95 | mappings.saveSRGMappings(result); | 101 | } |
| 96 | break; | 102 | |
| 97 | default: | 103 | private static MappingFormat chooseEnigmaFormat(Path path) { |
| 98 | mappings.saveEnigmaMappings(result, Mappings.FormatType.ENIGMA_FILE != formatType); | 104 | if (Files.isDirectory(path)) { |
| 99 | break; | 105 | return MappingFormat.ENIGMA_DIRECTORY; |
| 106 | } else { | ||
| 107 | return MappingFormat.ENIGMA_FILE; | ||
| 100 | } | 108 | } |
| 101 | } | 109 | } |
| 102 | 110 | ||
| @@ -149,6 +157,17 @@ public class CommandMain { | |||
| 149 | return file; | 157 | return file; |
| 150 | } | 158 | } |
| 151 | 159 | ||
| 160 | private static Path getReadablePath(String path) { | ||
| 161 | if (path == null) { | ||
| 162 | return null; | ||
| 163 | } | ||
| 164 | Path file = Paths.get(path).toAbsolutePath(); | ||
| 165 | if (!Files.exists(file)) { | ||
| 166 | throw new IllegalArgumentException("Cannot find file: " + file.toString()); | ||
| 167 | } | ||
| 168 | return file; | ||
| 169 | } | ||
| 170 | |||
| 152 | public static class ConsoleProgressListener implements ProgressListener { | 171 | public static class ConsoleProgressListener implements ProgressListener { |
| 153 | 172 | ||
| 154 | private static final int ReportTime = 5000; // 5s | 173 | private static final int ReportTime = 5000; // 5s |
| @@ -166,7 +185,7 @@ public class CommandMain { | |||
| 166 | } | 185 | } |
| 167 | 186 | ||
| 168 | @Override | 187 | @Override |
| 169 | public void onProgress(int numDone, String message) { | 188 | public void step(int numDone, String message) { |
| 170 | long now = System.currentTimeMillis(); | 189 | long now = System.currentTimeMillis(); |
| 171 | boolean isLastUpdate = numDone == this.totalWork; | 190 | boolean isLastUpdate = numDone == this.totalWork; |
| 172 | boolean shouldReport = isLastUpdate || now - this.lastReportTime > ReportTime; | 191 | boolean shouldReport = isLastUpdate || now - this.lastReportTime > ReportTime; |