diff options
| author | 2016-06-30 00:49:21 +1000 | |
|---|---|---|
| committer | 2016-06-30 00:49:21 +1000 | |
| commit | 4be005617b3b8c3578cca07c5d085d12916f0d1d (patch) | |
| tree | db163431f38703e26da417ef05eaea2b27a498b9 /src/main/java/cuchaz/enigma/CommandMain.java | |
| parent | Some small changes to fix idea importing (diff) | |
| download | enigma-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/main/java/cuchaz/enigma/CommandMain.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/CommandMain.java | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/CommandMain.java b/src/main/java/cuchaz/enigma/CommandMain.java new file mode 100644 index 0000000..69f5684 --- /dev/null +++ b/src/main/java/cuchaz/enigma/CommandMain.java | |||
| @@ -0,0 +1,186 @@ | |||
| 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.FileReader; | ||
| 15 | import java.util.jar.JarFile; | ||
| 16 | |||
| 17 | import cuchaz.enigma.Deobfuscator.ProgressListener; | ||
| 18 | import cuchaz.enigma.mapping.Mappings; | ||
| 19 | import cuchaz.enigma.mapping.MappingsReader; | ||
| 20 | |||
| 21 | public class CommandMain { | ||
| 22 | |||
| 23 | public static class ConsoleProgressListener implements ProgressListener { | ||
| 24 | |||
| 25 | private static final int ReportTime = 5000; // 5s | ||
| 26 | |||
| 27 | private int m_totalWork; | ||
| 28 | private long m_startTime; | ||
| 29 | private long m_lastReportTime; | ||
| 30 | |||
| 31 | @Override | ||
| 32 | public void init(int totalWork, String title) { | ||
| 33 | m_totalWork = totalWork; | ||
| 34 | m_startTime = System.currentTimeMillis(); | ||
| 35 | m_lastReportTime = m_startTime; | ||
| 36 | System.out.println(title); | ||
| 37 | } | ||
| 38 | |||
| 39 | @Override | ||
| 40 | public void onProgress(int numDone, String message) { | ||
| 41 | |||
| 42 | long now = System.currentTimeMillis(); | ||
| 43 | boolean isLastUpdate = numDone == m_totalWork; | ||
| 44 | boolean shouldReport = isLastUpdate || now - m_lastReportTime > ReportTime; | ||
| 45 | |||
| 46 | if (shouldReport) { | ||
| 47 | int percent = numDone * 100 / m_totalWork; | ||
| 48 | System.out.println(String.format("\tProgress: %3d%%", percent)); | ||
| 49 | m_lastReportTime = now; | ||
| 50 | } | ||
| 51 | if (isLastUpdate) { | ||
| 52 | double elapsedSeconds = (now - m_startTime) / 1000; | ||
| 53 | System.out.println(String.format("Finished in %.1f seconds", elapsedSeconds)); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | public static void main(String[] args) | ||
| 59 | throws Exception { | ||
| 60 | |||
| 61 | try { | ||
| 62 | |||
| 63 | // process the command | ||
| 64 | String command = getArg(args, 0, "command", true); | ||
| 65 | if (command.equalsIgnoreCase("deobfuscate")) { | ||
| 66 | deobfuscate(args); | ||
| 67 | } else if (command.equalsIgnoreCase("decompile")) { | ||
| 68 | decompile(args); | ||
| 69 | } else if (command.equalsIgnoreCase("protectify")) { | ||
| 70 | protectify(args); | ||
| 71 | } else if (command.equalsIgnoreCase("publify")) { | ||
| 72 | publify(args); | ||
| 73 | } else { | ||
| 74 | throw new IllegalArgumentException("Command not recognized: " + command); | ||
| 75 | } | ||
| 76 | } catch (IllegalArgumentException ex) { | ||
| 77 | System.out.println(ex.getMessage()); | ||
| 78 | printHelp(); | ||
| 79 | } | ||
| 80 | } | ||
| 81 | |||
| 82 | private static void printHelp() { | ||
| 83 | System.out.println(String.format("%s - %s", Constants.Name, Constants.Version)); | ||
| 84 | System.out.println("Usage:"); | ||
| 85 | System.out.println("\tjava -cp enigma.jar cuchaz.enigma.CommandMain <command>"); | ||
| 86 | System.out.println("\twhere <command> is one of:"); | ||
| 87 | System.out.println("\t\tdeobfuscate <in jar> <out jar> [<mappings file>]"); | ||
| 88 | System.out.println("\t\tdecompile <in jar> <out folder> [<mappings file>]"); | ||
| 89 | System.out.println("\t\tprotectify <in jar> <out jar>"); | ||
| 90 | } | ||
| 91 | |||
| 92 | private static void decompile(String[] args) | ||
| 93 | throws Exception { | ||
| 94 | File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); | ||
| 95 | File fileJarOut = getWritableFolder(getArg(args, 2, "out folder", true)); | ||
| 96 | File fileMappings = getReadableFile(getArg(args, 3, "mappings file", false)); | ||
| 97 | Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn)); | ||
| 98 | deobfuscator.writeSources(fileJarOut, new ConsoleProgressListener()); | ||
| 99 | } | ||
| 100 | |||
| 101 | private static void deobfuscate(String[] args) | ||
| 102 | throws Exception { | ||
| 103 | File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); | ||
| 104 | File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true)); | ||
| 105 | File fileMappings = getReadableFile(getArg(args, 3, "mappings file", false)); | ||
| 106 | Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn)); | ||
| 107 | deobfuscator.writeJar(fileJarOut, new ConsoleProgressListener()); | ||
| 108 | } | ||
| 109 | |||
| 110 | private static void protectify(String[] args) | ||
| 111 | throws Exception { | ||
| 112 | File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); | ||
| 113 | File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true)); | ||
| 114 | Deobfuscator deobfuscator = getDeobfuscator(null, new JarFile(fileJarIn)); | ||
| 115 | deobfuscator.protectifyJar(fileJarOut, new ConsoleProgressListener()); | ||
| 116 | } | ||
| 117 | |||
| 118 | private static void publify(String[] args) | ||
| 119 | throws Exception { | ||
| 120 | File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); | ||
| 121 | File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true)); | ||
| 122 | Deobfuscator deobfuscator = getDeobfuscator(null, new JarFile(fileJarIn)); | ||
| 123 | deobfuscator.publifyJar(fileJarOut, new ConsoleProgressListener()); | ||
| 124 | } | ||
| 125 | |||
| 126 | private static Deobfuscator getDeobfuscator(File fileMappings, JarFile jar) | ||
| 127 | throws Exception { | ||
| 128 | System.out.println("Reading jar..."); | ||
| 129 | Deobfuscator deobfuscator = new Deobfuscator(jar); | ||
| 130 | if (fileMappings != null) { | ||
| 131 | System.out.println("Reading mappings..."); | ||
| 132 | Mappings mappings = new MappingsReader().read(fileMappings); | ||
| 133 | deobfuscator.setMappings(mappings); | ||
| 134 | } | ||
| 135 | return deobfuscator; | ||
| 136 | } | ||
| 137 | |||
| 138 | private static String getArg(String[] args, int i, String name, boolean required) { | ||
| 139 | if (i >= args.length) { | ||
| 140 | if (required) { | ||
| 141 | throw new IllegalArgumentException(name + " is required"); | ||
| 142 | } else { | ||
| 143 | return null; | ||
| 144 | } | ||
| 145 | } | ||
| 146 | return args[i]; | ||
| 147 | } | ||
| 148 | |||
| 149 | private static File getWritableFile(String path) { | ||
| 150 | if (path == null) { | ||
| 151 | return null; | ||
| 152 | } | ||
| 153 | File file = new File(path).getAbsoluteFile(); | ||
| 154 | File dir = file.getParentFile(); | ||
| 155 | if (dir == null) { | ||
| 156 | throw new IllegalArgumentException("Cannot write to folder: " + dir); | ||
| 157 | } | ||
| 158 | // quick fix to avoid stupid stuff in Gradle code | ||
| 159 | if (!dir.isDirectory()) { | ||
| 160 | dir.mkdirs(); | ||
| 161 | } | ||
| 162 | return file; | ||
| 163 | } | ||
| 164 | |||
| 165 | private static File getWritableFolder(String path) { | ||
| 166 | if (path == null) { | ||
| 167 | return null; | ||
| 168 | } | ||
| 169 | File dir = new File(path).getAbsoluteFile(); | ||
| 170 | if (!dir.exists()) { | ||
| 171 | throw new IllegalArgumentException("Cannot write to folder: " + dir); | ||
| 172 | } | ||
| 173 | return dir; | ||
| 174 | } | ||
| 175 | |||
| 176 | private static File getReadableFile(String path) { | ||
| 177 | if (path == null) { | ||
| 178 | return null; | ||
| 179 | } | ||
| 180 | File file = new File(path).getAbsoluteFile(); | ||
| 181 | if (!file.exists()) { | ||
| 182 | throw new IllegalArgumentException("Cannot find file: " + file.getAbsolutePath()); | ||
| 183 | } | ||
| 184 | return file; | ||
| 185 | } | ||
| 186 | } | ||