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