From ba67f6c0231157c0b07b37fe0a09fca381bb37d9 Mon Sep 17 00:00:00 2001 From: jeff Date: Sat, 17 Jan 2015 16:20:15 -0500 Subject: added command-line interface for scriptable awesome --- build.py | 13 ++-- readme.gui.txt | 19 ------ readme.txt | 28 ++++++++ src/cuchaz/enigma/CommandMain.java | 134 +++++++++++++++++++++++++++++++------ 4 files changed, 149 insertions(+), 45 deletions(-) delete mode 100644 readme.gui.txt create mode 100644 readme.txt diff --git a/build.py b/build.py index 69182514..62b96d11 100644 --- a/build.py +++ b/build.py @@ -12,8 +12,11 @@ dirBuild = "build" dirTemp = os.path.join(dirBuild, "tmp") -def getJarFullName(name) : - return "%s-%s-%s.jar" % (projectName, name, version) +def getJarFullName(name=None) : + if name is not None: + return "%s-%s-%s.jar" % (projectName, name, version) + else: + return "%s-%s.jar" % (projectName, version) def buildGuiJar(): jarName = "gui" @@ -25,9 +28,9 @@ def buildGuiJar(): ssjb.delete(os.path.join(dirTemp, "LICENSE.txt")) ssjb.copyFile(dirTemp, "license.APL2.txt") ssjb.copyFile(dirTemp, "license.GPL3.txt") - ssjb.copyFile(dirTemp, "readme.gui.txt", renameTo="readme.txt") - manifest = ssjb.buildManifest("%s-%s" % (projectName, jarName), version, author, "cuchaz.enigma.Main") - ssjb.jar(os.path.join(dirBuild, getJarFullName(jarName)), dirTemp, manifest=manifest) + ssjb.copyFile(dirTemp, "readme.txt") + manifest = ssjb.buildManifest(projectName, version, author, "cuchaz.enigma.Main") + ssjb.jar(os.path.join(dirBuild, getJarFullName()), dirTemp, manifest=manifest) ssjb.delete(dirTemp) def buildTranslateJar(): diff --git a/readme.gui.txt b/readme.gui.txt deleted file mode 100644 index 81f985a0..00000000 --- a/readme.gui.txt +++ /dev/null @@ -1,19 +0,0 @@ - -Enigma v0.6 beta -A tool for deobfuscation of Java bytecode - -Copyright Jeff Martin, 2014 - - -LICENSE - -Enigma is distributed under the GNU General Public license version 3 - -Enigma includes a modified version of Procyon which is distributed under the Apache license version 2. Procyon is copyrighted by Mike Strobel, 2013 - -Enigma includes unmodified versions of the following libraries which are also released under the Apache license version 2. - Guava - Javassist - JSyntaxPane - -Copies of the GNU General Public license verion 3 and the Apache license v2 have been included in this distribution. diff --git a/readme.txt b/readme.txt new file mode 100644 index 00000000..3844f54e --- /dev/null +++ b/readme.txt @@ -0,0 +1,28 @@ + +Enigma v0.6 beta +A tool for deobfuscation of Java bytecode + +Copyright Jeff Martin, 2014 + + +LICENSE + +Enigma is distributed under the GNU General Public license version 3 + +Enigma includes a modified version of Procyon which is distributed under the Apache license version 2. Procyon is copyrighted by Mike Strobel, 2013 + +Enigma includes unmodified versions of the following libraries which are also released under the Apache license version 2. + Guava + Javassist + JSyntaxPane + +Copies of the GNU General Public license verion 3 and the Apache license v2 have been included in this distribution. + + +USING ENIGMA + +Launch the GUI: + java -jar enigma.jar + +Use Enigma on the command line: + java -cp enigma.jar cuchaz.enigma.CommandMain diff --git a/src/cuchaz/enigma/CommandMain.java b/src/cuchaz/enigma/CommandMain.java index 6a01661b..74bd4991 100644 --- a/src/cuchaz/enigma/CommandMain.java +++ b/src/cuchaz/enigma/CommandMain.java @@ -1,23 +1,65 @@ package cuchaz.enigma; +import java.io.File; +import java.io.FileReader; + +import cuchaz.enigma.Deobfuscator.ProgressListener; +import cuchaz.enigma.mapping.Mappings; +import cuchaz.enigma.mapping.MappingsReader; + public class CommandMain { - public static void main(String[] args) { + public static class ConsoleProgressListener implements ProgressListener { - // parse the args - if (args.length < 1) { - printHelp(); - return; + private static final int ReportTime = 5000; // 5s + + private int m_totalWork; + private long m_startTime; + private long m_lastReportTime; + + @Override + public void init(int totalWork, String title) { + m_totalWork = totalWork; + m_startTime = System.currentTimeMillis(); + m_lastReportTime = m_startTime; + System.out.println(title); + } + + @Override + public void onProgress(int numDone, String message) { + + long now = System.currentTimeMillis(); + boolean isLastUpdate = numDone == m_totalWork; + boolean shouldReport = isLastUpdate || now - m_lastReportTime > ReportTime; + + if (shouldReport) { + int percent = numDone*100/m_totalWork; + System.out.println(String.format("\tProgress: %3d%%", percent)); + m_lastReportTime = now; + } + if (isLastUpdate) { + double elapsedSeconds = (now - m_startTime)/1000; + System.out.println(String.format("Finished in %.1f seconds", elapsedSeconds)); + } } + } + + public static void main(String[] args) + throws Exception { - // process the command - String command = args[0]; - if (command.equalsIgnoreCase("deobfuscate")) { - deobfuscate(args); - } else if(command.equalsIgnoreCase("decompile")) { - decompile(args); - } else { - System.out.println("Command not recognized: " + args[0]); + try { + + // process the command + String command = getArg(args, 0, "command"); + if (command.equalsIgnoreCase("deobfuscate")) { + deobfuscate(args); + } else if(command.equalsIgnoreCase("decompile")) { + decompile(args); + } else { + throw new IllegalArgumentException("Command not recognized: " + command); + } + } catch (IllegalArgumentException ex) { + System.out.println(ex.getMessage()); printHelp(); } } @@ -25,19 +67,69 @@ public class CommandMain { private static void printHelp() { System.out.println(String.format("%s - %s", Constants.Name, Constants.Version)); System.out.println("Usage:"); - System.out.println("\tjava -jar enigma.jar cuchaz.enigma.CommandMain "); + System.out.println("\tjava -cp enigma.jar cuchaz.enigma.CommandMain "); System.out.println("\twhere is one of:"); System.out.println("\t\tdeobfuscate "); - System.out.println("\t\tdecompile "); + System.out.println("\t\tdecompile "); } - private static void decompile(String[] args) { - // TODO - throw new Error("Not implemented yet"); + private static void decompile(String[] args) + throws Exception { + File fileMappings = getReadableFile(getArg(args, 1, "mappings file")); + File fileJarIn = getReadableFile(getArg(args, 2, "in jar")); + File fileJarOut = getWritableFolder(getArg(args, 3, "out folder")); + Deobfuscator deobfuscator = getDeobfuscator(fileMappings, fileJarIn); + deobfuscator.writeSources(fileJarOut, new ConsoleProgressListener()); } - private static void deobfuscate(String[] args) { - // TODO - throw new Error("Not implemented yet"); + private static void deobfuscate(String[] args) + throws Exception { + File fileMappings = getReadableFile(getArg(args, 1, "mappings file")); + File fileJarIn = getReadableFile(getArg(args, 2, "in jar")); + File fileJarOut = getWritableFile(getArg(args, 3, "out jar")); + Deobfuscator deobfuscator = getDeobfuscator(fileMappings, fileJarIn); + deobfuscator.writeJar(fileJarOut, new ConsoleProgressListener()); + } + + private static Deobfuscator getDeobfuscator(File fileMappings, File fileJar) + throws Exception { + System.out.println("Reading mappings..."); + Mappings mappings = new MappingsReader().read(new FileReader(fileMappings)); + System.out.println("Reading jar..."); + Deobfuscator deobfuscator = new Deobfuscator(fileJar); + deobfuscator.setMappings(mappings); + return deobfuscator; + } + + private static String getArg(String[] args, int i, String name) { + if (i >= args.length) { + throw new IllegalArgumentException(name + " is required"); + } + return args[i]; + } + + private static File getWritableFile(String path) { + File file = new File(path).getAbsoluteFile(); + File dir = file.getParentFile(); + if (dir == null || !dir.exists()) { + throw new IllegalArgumentException("Cannot write to folder: " + file); + } + return file; + } + + private static File getWritableFolder(String path) { + File dir = new File(path).getAbsoluteFile(); + if (!dir.exists()) { + throw new IllegalArgumentException("Cannot write to folder: " + dir); + } + return dir; + } + + private static File getReadableFile(String path) { + File file = new File(path).getAbsoluteFile(); + if (!file.exists()) { + throw new IllegalArgumentException("Cannot find file: " + file.getAbsolutePath()); + } + return file; } } -- cgit v1.2.3