summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/CommandMain.java
diff options
context:
space:
mode:
authorGravatar Thog2017-03-08 08:17:04 +0100
committerGravatar Thog2017-03-08 08:17:04 +0100
commit6e464ea251cab63c776ece0b2a356f1498ffa294 (patch)
tree5ed30c03f5ac4cd2d6877874f5ede576049954f7 /src/main/java/cuchaz/enigma/CommandMain.java
parentDrop unix case style and implement hashCode when equals is overrided (diff)
downloadenigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.gz
enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.tar.xz
enigma-fork-6e464ea251cab63c776ece0b2a356f1498ffa294.zip
Follow Fabric guidelines
Diffstat (limited to 'src/main/java/cuchaz/enigma/CommandMain.java')
-rw-r--r--src/main/java/cuchaz/enigma/CommandMain.java375
1 files changed, 186 insertions, 189 deletions
diff --git a/src/main/java/cuchaz/enigma/CommandMain.java b/src/main/java/cuchaz/enigma/CommandMain.java
index b0a4107..f546eb1 100644
--- a/src/main/java/cuchaz/enigma/CommandMain.java
+++ b/src/main/java/cuchaz/enigma/CommandMain.java
@@ -8,201 +8,198 @@
8 * Contributors: 8 * Contributors:
9 * Jeff Martin - initial API and implementation 9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/ 10 ******************************************************************************/
11package cuchaz.enigma;
12 11
13import java.io.File; 12package cuchaz.enigma;
14import java.util.jar.JarFile;
15 13
16import cuchaz.enigma.Deobfuscator.ProgressListener; 14import cuchaz.enigma.Deobfuscator.ProgressListener;
17import cuchaz.enigma.mapping.Mappings; 15import cuchaz.enigma.mapping.Mappings;
18import cuchaz.enigma.mapping.MappingsEnigmaReader; 16import cuchaz.enigma.mapping.MappingsEnigmaReader;
19 17
18import java.io.File;
19import java.util.jar.JarFile;
20
20public class CommandMain { 21public class CommandMain {
21 22
22 public static class ConsoleProgressListener implements ProgressListener { 23 public static void main(String[] args) throws Exception {
23 24 try {
24 private static final int ReportTime = 5000; // 5s 25 // process the command
25 26 String command = getArg(args, 0, "command", true);
26 private int totalWork; 27 if (command.equalsIgnoreCase("deobfuscate")) {
27 private long startTime; 28 deobfuscate(args);
28 private long lastReportTime; 29 } else if (command.equalsIgnoreCase("decompile")) {
29 30 decompile(args);
30 @Override 31 } else if (command.equalsIgnoreCase("protectify")) {
31 public void init(int totalWork, String title) { 32 protectify(args);
32 this.totalWork = totalWork; 33 } else if (command.equalsIgnoreCase("publify")) {
33 this.startTime = System.currentTimeMillis(); 34 publify(args);
34 this.lastReportTime = this.startTime; 35 } else if (command.equalsIgnoreCase("convertmappings")) {
35 System.out.println(title); 36 convertMappings(args);
36 } 37 } else {
37 38 throw new IllegalArgumentException("Command not recognized: " + command);
38 @Override 39 }
39 public void onProgress(int numDone, String message) { 40 } catch (IllegalArgumentException ex) {
40 long now = System.currentTimeMillis(); 41 System.out.println(ex.getMessage());
41 boolean isLastUpdate = numDone == this.totalWork; 42 printHelp();
42 boolean shouldReport = isLastUpdate || now - this.lastReportTime > ReportTime; 43 }
43 44 }
44 if (shouldReport) { 45
45 int percent = numDone * 100 / this.totalWork; 46 private static void printHelp() {
46 System.out.println(String.format("\tProgress: %3d%%", percent)); 47 System.out.println(String.format("%s - %s", Constants.NAME, Constants.VERSION));
47 this.lastReportTime = now; 48 System.out.println("Usage:");
48 } 49 System.out.println("\tjava -cp enigma.jar cuchaz.enigma.CommandMain <command>");
49 if (isLastUpdate) { 50 System.out.println("\twhere <command> is one of:");
50 double elapsedSeconds = (now - this.startTime) / 1000.0; 51 System.out.println("\t\tdeobfuscate <in jar> <out jar> [<mappings file>]");
51 System.out.println(String.format("Finished in %.1f seconds", elapsedSeconds)); 52 System.out.println("\t\tdecompile <in jar> <out folder> [<mappings file>]");
52 } 53 System.out.println("\t\tprotectify <in jar> <out jar>");
53 } 54 System.out.println("\t\tpublify <in jar> <out jar>");
54 } 55 System.out.println("\t\tconvertmappings <enigma mappings> <converted mappings> <ENIGMA_FILE|ENIGMA_DIRECTORY|SRG_FILE>");
55 56 }
56 public static void main(String[] args) throws Exception { 57
57 try { 58 private static void decompile(String[] args) throws Exception {
58 // process the command 59 File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true));
59 String command = getArg(args, 0, "command", true); 60 File fileJarOut = getWritableFolder(getArg(args, 2, "out folder", true));
60 if (command.equalsIgnoreCase("deobfuscate")) { 61 File fileMappings = getReadableFile(getArg(args, 3, "mappings file", false));
61 deobfuscate(args); 62 Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn));
62 } else if (command.equalsIgnoreCase("decompile")) { 63 deobfuscator.writeSources(fileJarOut, new ConsoleProgressListener());
63 decompile(args); 64 }
64 } else if (command.equalsIgnoreCase("protectify")) { 65
65 protectify(args); 66 private static void deobfuscate(String[] args) throws Exception {
66 } else if (command.equalsIgnoreCase("publify")) { 67 File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true));
67 publify(args); 68 File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true));
68 } else if (command.equalsIgnoreCase("convertmappings")) { 69 File fileMappings = getReadableFile(getArg(args, 3, "mappings file", false));
69 convertMappings(args); 70 Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn));
70 } 71 deobfuscator.writeJar(fileJarOut, new ConsoleProgressListener());
71 else { 72 }
72 throw new IllegalArgumentException("Command not recognized: " + command); 73
73 } 74 private static void protectify(String[] args) throws Exception {
74 } catch (IllegalArgumentException ex) { 75 File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true));
75 System.out.println(ex.getMessage()); 76 File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true));
76 printHelp(); 77 Deobfuscator deobfuscator = getDeobfuscator(null, new JarFile(fileJarIn));
77 } 78 deobfuscator.protectifyJar(fileJarOut, new ConsoleProgressListener());
78 } 79 }
79 80
80 private static void printHelp() { 81 private static void publify(String[] args) throws Exception {
81 System.out.println(String.format("%s - %s", Constants.NAME, Constants.VERSION)); 82 File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true));
82 System.out.println("Usage:"); 83 File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true));
83 System.out.println("\tjava -cp enigma.jar cuchaz.enigma.CommandMain <command>"); 84 Deobfuscator deobfuscator = getDeobfuscator(null, new JarFile(fileJarIn));
84 System.out.println("\twhere <command> is one of:"); 85 deobfuscator.publifyJar(fileJarOut, new ConsoleProgressListener());
85 System.out.println("\t\tdeobfuscate <in jar> <out jar> [<mappings file>]"); 86 }
86 System.out.println("\t\tdecompile <in jar> <out folder> [<mappings file>]"); 87
87 System.out.println("\t\tprotectify <in jar> <out jar>"); 88 private static Deobfuscator getDeobfuscator(File fileMappings, JarFile jar) throws Exception {
88 System.out.println("\t\tpublify <in jar> <out jar>"); 89 System.out.println("Reading jar...");
89 System.out.println("\t\tconvertmappings <enigma mappings> <converted mappings> <ENIGMA_FILE|ENIGMA_DIRECTORY|SRG_FILE>"); 90 Deobfuscator deobfuscator = new Deobfuscator(jar);
90 } 91 if (fileMappings != null) {
91 92 System.out.println("Reading mappings...");
92 private static void decompile(String[] args) throws Exception { 93 Mappings mappings = new MappingsEnigmaReader().read(fileMappings);
93 File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); 94 deobfuscator.setMappings(mappings);
94 File fileJarOut = getWritableFolder(getArg(args, 2, "out folder", true)); 95 }
95 File fileMappings = getReadableFile(getArg(args, 3, "mappings file", false)); 96 return deobfuscator;
96 Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn)); 97 }
97 deobfuscator.writeSources(fileJarOut, new ConsoleProgressListener()); 98
98 } 99 private static void convertMappings(String[] args) throws Exception {
99 100 File fileMappings = getReadableFile(getArg(args, 1, "enigma mapping", true));
100 private static void deobfuscate(String[] args) throws Exception { 101 File result = getWritableFile(getArg(args, 2, "enigma mapping", true));
101 File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); 102 String name = getArg(args, 3, "format type", true);
102 File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true)); 103 Mappings.FormatType formatType;
103 File fileMappings = getReadableFile(getArg(args, 3, "mappings file", false)); 104 try {
104 Deobfuscator deobfuscator = getDeobfuscator(fileMappings, new JarFile(fileJarIn)); 105 formatType = Mappings.FormatType.valueOf(name.toUpperCase());
105 deobfuscator.writeJar(fileJarOut, new ConsoleProgressListener()); 106 } catch (IllegalArgumentException e) {
106 } 107 throw new IllegalArgumentException(name + "is not a valid mapping format!");
107 108 }
108 private static void protectify(String[] args) throws Exception { 109
109 File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); 110 System.out.println("Reading mappings...");
110 File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true)); 111 Mappings mappings = new MappingsEnigmaReader().read(fileMappings);
111 Deobfuscator deobfuscator = getDeobfuscator(null, new JarFile(fileJarIn)); 112 System.out.println("Saving new mappings...");
112 deobfuscator.protectifyJar(fileJarOut, new ConsoleProgressListener()); 113 switch (formatType) {
113 } 114 case SRG_FILE:
114 115 mappings.saveSRGMappings(result);
115 private static void publify(String[] args) throws Exception { 116 break;
116 File fileJarIn = getReadableFile(getArg(args, 1, "in jar", true)); 117 default:
117 File fileJarOut = getWritableFile(getArg(args, 2, "out jar", true)); 118 mappings.saveEnigmaMappings(result, Mappings.FormatType.ENIGMA_FILE != formatType);
118 Deobfuscator deobfuscator = getDeobfuscator(null, new JarFile(fileJarIn)); 119 break;
119 deobfuscator.publifyJar(fileJarOut, new ConsoleProgressListener()); 120 }
120 } 121 }
121 122
122 private static Deobfuscator getDeobfuscator(File fileMappings, JarFile jar) throws Exception { 123 private static String getArg(String[] args, int i, String name, boolean required) {
123 System.out.println("Reading jar..."); 124 if (i >= args.length) {
124 Deobfuscator deobfuscator = new Deobfuscator(jar); 125 if (required) {
125 if (fileMappings != null) { 126 throw new IllegalArgumentException(name + " is required");
126 System.out.println("Reading mappings..."); 127 } else {
127 Mappings mappings = new MappingsEnigmaReader().read(fileMappings); 128 return null;
128 deobfuscator.setMappings(mappings); 129 }
129 } 130 }
130 return deobfuscator; 131 return args[i];
131 } 132 }
132 133
133 private static void convertMappings(String[] args) throws Exception { 134 private static File getWritableFile(String path) {
134 File fileMappings = getReadableFile(getArg(args, 1, "enigma mapping", true)); 135 if (path == null) {
135 File result = getWritableFile(getArg(args, 2, "enigma mapping", true)); 136 return null;
136 String name = getArg(args, 3, "format type", true); 137 }
137 Mappings.FormatType formatType; 138 File file = new File(path).getAbsoluteFile();
138 try 139 File dir = file.getParentFile();
139 { 140 if (dir == null) {
140 formatType = Mappings.FormatType.valueOf(name.toUpperCase()); 141 throw new IllegalArgumentException("Cannot write file: " + path);
141 } catch (IllegalArgumentException e) 142 }
142 { 143 // quick fix to avoid stupid stuff in Gradle code
143 throw new IllegalArgumentException(name + "is not a valid mapping format!"); 144 if (!dir.isDirectory()) {
144 } 145 dir.mkdirs();
145 146 }
146 System.out.println("Reading mappings..."); 147 return file;
147 Mappings mappings = new MappingsEnigmaReader().read(fileMappings); 148 }
148 System.out.println("Saving new mappings..."); 149
149 switch (formatType) 150 private static File getWritableFolder(String path) {
150 { 151 if (path == null) {
151 case SRG_FILE: 152 return null;
152 mappings.saveSRGMappings(result); 153 }
153 break; 154 File dir = new File(path).getAbsoluteFile();
154 default: 155 if (!dir.exists()) {
155 mappings.saveEnigmaMappings(result, Mappings.FormatType.ENIGMA_FILE != formatType); 156 throw new IllegalArgumentException("Cannot write to folder: " + dir);
156 break; 157 }
157 } 158 return dir;
158 } 159 }
159 160
160 private static String getArg(String[] args, int i, String name, boolean required) { 161 private static File getReadableFile(String path) {
161 if (i >= args.length) { 162 if (path == null) {
162 if (required) { 163 return null;
163 throw new IllegalArgumentException(name + " is required"); 164 }
164 } else { 165 File file = new File(path).getAbsoluteFile();
165 return null; 166 if (!file.exists()) {
166 } 167 throw new IllegalArgumentException("Cannot find file: " + file.getAbsolutePath());
167 } 168 }
168 return args[i]; 169 return file;
169 } 170 }
170 171
171 private static File getWritableFile(String path) { 172 public static class ConsoleProgressListener implements ProgressListener {
172 if (path == null) { 173
173 return null; 174 private static final int ReportTime = 5000; // 5s
174 } 175
175 File file = new File(path).getAbsoluteFile(); 176 private int totalWork;
176 File dir = file.getParentFile(); 177 private long startTime;
177 if (dir == null) { 178 private long lastReportTime;
178 throw new IllegalArgumentException("Cannot write file: " + path); 179
179 } 180 @Override
180 // quick fix to avoid stupid stuff in Gradle code 181 public void init(int totalWork, String title) {
181 if (!dir.isDirectory()) { 182 this.totalWork = totalWork;
182 dir.mkdirs(); 183 this.startTime = System.currentTimeMillis();
183 } 184 this.lastReportTime = this.startTime;
184 return file; 185 System.out.println(title);
185 } 186 }
186 187
187 private static File getWritableFolder(String path) { 188 @Override
188 if (path == null) { 189 public void onProgress(int numDone, String message) {
189 return null; 190 long now = System.currentTimeMillis();
190 } 191 boolean isLastUpdate = numDone == this.totalWork;
191 File dir = new File(path).getAbsoluteFile(); 192 boolean shouldReport = isLastUpdate || now - this.lastReportTime > ReportTime;
192 if (!dir.exists()) { 193
193 throw new IllegalArgumentException("Cannot write to folder: " + dir); 194 if (shouldReport) {
194 } 195 int percent = numDone * 100 / this.totalWork;
195 return dir; 196 System.out.println(String.format("\tProgress: %3d%%", percent));
196 } 197 this.lastReportTime = now;
197 198 }
198 private static File getReadableFile(String path) { 199 if (isLastUpdate) {
199 if (path == null) { 200 double elapsedSeconds = (now - this.startTime) / 1000.0;
200 return null; 201 System.out.println(String.format("Finished in %.1f seconds", elapsedSeconds));
201 } 202 }
202 File file = new File(path).getAbsoluteFile(); 203 }
203 if (!file.exists()) { 204 }
204 throw new IllegalArgumentException("Cannot find file: " + file.getAbsolutePath());
205 }
206 return file;
207 }
208} 205}