summaryrefslogtreecommitdiff
path: root/src/cuchaz/enigma/CommandMain.java
diff options
context:
space:
mode:
authorGravatar jeff2015-02-03 22:00:53 -0500
committerGravatar jeff2015-02-03 22:00:53 -0500
commit52ab426d8fad3dbee7e728f523a35af94facebda (patch)
tree146fadfd8e639a909d6c1d6a193e7eddeab0be4a /src/cuchaz/enigma/CommandMain.java
downloadenigma-fork-52ab426d8fad3dbee7e728f523a35af94facebda.tar.gz
enigma-fork-52ab426d8fad3dbee7e728f523a35af94facebda.tar.xz
enigma-fork-52ab426d8fad3dbee7e728f523a35af94facebda.zip
oops, don't depend on local procyon project
Diffstat (limited to 'src/cuchaz/enigma/CommandMain.java')
-rw-r--r--src/cuchaz/enigma/CommandMain.java136
1 files changed, 136 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/CommandMain.java b/src/cuchaz/enigma/CommandMain.java
new file mode 100644
index 0000000..1ec2ad2
--- /dev/null
+++ b/src/cuchaz/enigma/CommandMain.java
@@ -0,0 +1,136 @@
1package cuchaz.enigma;
2
3import java.io.File;
4import java.io.FileReader;
5import java.util.jar.JarFile;
6
7import cuchaz.enigma.Deobfuscator.ProgressListener;
8import cuchaz.enigma.mapping.Mappings;
9import cuchaz.enigma.mapping.MappingsReader;
10
11public class CommandMain {
12
13 public static class ConsoleProgressListener implements ProgressListener {
14
15 private static final int ReportTime = 5000; // 5s
16
17 private int m_totalWork;
18 private long m_startTime;
19 private long m_lastReportTime;
20
21 @Override
22 public void init(int totalWork, String title) {
23 m_totalWork = totalWork;
24 m_startTime = System.currentTimeMillis();
25 m_lastReportTime = m_startTime;
26 System.out.println(title);
27 }
28
29 @Override
30 public void onProgress(int numDone, String message) {
31
32 long now = System.currentTimeMillis();
33 boolean isLastUpdate = numDone == m_totalWork;
34 boolean shouldReport = isLastUpdate || now - m_lastReportTime > ReportTime;
35
36 if (shouldReport) {
37 int percent = numDone*100/m_totalWork;
38 System.out.println(String.format("\tProgress: %3d%%", percent));
39 m_lastReportTime = now;
40 }
41 if (isLastUpdate) {
42 double elapsedSeconds = (now - m_startTime)/1000;
43 System.out.println(String.format("Finished in %.1f seconds", elapsedSeconds));
44 }
45 }
46 }
47
48 public static void main(String[] args)
49 throws Exception {
50
51 try {
52
53 // process the command
54 String command = getArg(args, 0, "command");
55 if (command.equalsIgnoreCase("deobfuscate")) {
56 deobfuscate(args);
57 } else if(command.equalsIgnoreCase("decompile")) {
58 decompile(args);
59 } else {
60 throw new IllegalArgumentException("Command not recognized: " + command);
61 }
62 } catch (IllegalArgumentException ex) {
63 System.out.println(ex.getMessage());
64 printHelp();
65 }
66 }
67
68 private static void printHelp() {
69 System.out.println(String.format("%s - %s", Constants.Name, Constants.Version));
70 System.out.println("Usage:");
71 System.out.println("\tjava -cp enigma.jar cuchaz.enigma.CommandMain <command>");
72 System.out.println("\twhere <command> is one of:");
73 System.out.println("\t\tdeobfuscate <mappings file> <in jar> <out jar>");
74 System.out.println("\t\tdecompile <mappings file> <in jar> <out folder>");
75 }
76
77 private static void decompile(String[] args)
78 throws Exception {
79 File fileMappings = getReadableFile(getArg(args, 1, "mappings file"));
80 File fileJarIn = getReadableFile(getArg(args, 2, "in jar"));
81 File fileJarOut = getWritableFolder(getArg(args, 3, "out folder"));
82 Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn));
83 deobfuscator.writeSources(fileJarOut, new ConsoleProgressListener());
84 }
85
86 private static void deobfuscate(String[] args)
87 throws Exception {
88 File fileMappings = getReadableFile(getArg(args, 1, "mappings file"));
89 File fileJarIn = getReadableFile(getArg(args, 2, "in jar"));
90 File fileJarOut = getWritableFile(getArg(args, 3, "out jar"));
91 Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn));
92 deobfuscator.writeJar(fileJarOut, new ConsoleProgressListener());
93 }
94
95 private static Deobfuscator getDeobfuscator(File fileMappings, JarFile jar)
96 throws Exception {
97 System.out.println("Reading mappings...");
98 Mappings mappings = new MappingsReader().read(new FileReader(fileMappings));
99 System.out.println("Reading jar...");
100 Deobfuscator deobfuscator = new Deobfuscator(jar);
101 deobfuscator.setMappings(mappings);
102 return deobfuscator;
103 }
104
105 private static String getArg(String[] args, int i, String name) {
106 if (i >= args.length) {
107 throw new IllegalArgumentException(name + " is required");
108 }
109 return args[i];
110 }
111
112 private static File getWritableFile(String path) {
113 File file = new File(path).getAbsoluteFile();
114 File dir = file.getParentFile();
115 if (dir == null || !dir.exists()) {
116 throw new IllegalArgumentException("Cannot write to folder: " + file);
117 }
118 return file;
119 }
120
121 private static File getWritableFolder(String path) {
122 File dir = new File(path).getAbsoluteFile();
123 if (!dir.exists()) {
124 throw new IllegalArgumentException("Cannot write to folder: " + dir);
125 }
126 return dir;
127 }
128
129 private static File getReadableFile(String path) {
130 File file = new File(path).getAbsoluteFile();
131 if (!file.exists()) {
132 throw new IllegalArgumentException("Cannot find file: " + file.getAbsolutePath());
133 }
134 return file;
135 }
136}