summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/command/Command.java
diff options
context:
space:
mode:
authorGravatar liach2019-05-15 22:03:13 -0700
committerGravatar Gegy2019-05-16 07:03:13 +0200
commitcb8823eb0b446d5c1b9b580e5578866e691771d8 (patch)
tree1e8c1a5b981f3ad42c393f5d7cb75754f25f51ba /src/main/java/cuchaz/enigma/command/Command.java
parentcheckmappings command (#137) (diff)
downloadenigma-fork-cb8823eb0b446d5c1b9b580e5578866e691771d8.tar.gz
enigma-fork-cb8823eb0b446d5c1b9b580e5578866e691771d8.tar.xz
enigma-fork-cb8823eb0b446d5c1b9b580e5578866e691771d8.zip
Feature/weave (#138)
* Add weave/stitch style command system to enigma Also fixed divide by zero stupidity Signed-off-by: liach <liach@users.noreply.github.com> * Add tests for package access index and command Signed-off-by: liach <liach@users.noreply.github.com> * Minor tweaks Signed-off-by: liach <liach@users.noreply.github.com>
Diffstat (limited to 'src/main/java/cuchaz/enigma/command/Command.java')
-rw-r--r--src/main/java/cuchaz/enigma/command/Command.java140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/main/java/cuchaz/enigma/command/Command.java b/src/main/java/cuchaz/enigma/command/Command.java
new file mode 100644
index 0000000..b107fb6
--- /dev/null
+++ b/src/main/java/cuchaz/enigma/command/Command.java
@@ -0,0 +1,140 @@
1package cuchaz.enigma.command;
2
3import cuchaz.enigma.Deobfuscator;
4import cuchaz.enigma.ProgressListener;
5import cuchaz.enigma.translation.mapping.EntryMapping;
6import cuchaz.enigma.translation.mapping.serde.MappingFormat;
7import cuchaz.enigma.translation.mapping.tree.EntryTree;
8
9import java.io.File;
10import java.nio.file.Files;
11import java.nio.file.Path;
12import java.nio.file.Paths;
13import java.util.jar.JarFile;
14
15public abstract class Command {
16 public final String name;
17
18 protected Command(String name) {
19 this.name = name;
20 }
21
22 public abstract String getUsage();
23
24 public abstract boolean isValidArgument(int length);
25
26 public abstract void run(String... args) throws Exception;
27
28 protected static Deobfuscator getDeobfuscator(Path fileMappings, JarFile jar) throws Exception {
29 System.out.println("Reading jar...");
30 Deobfuscator deobfuscator = new Deobfuscator(jar);
31 if (fileMappings != null) {
32 System.out.println("Reading mappings...");
33 EntryTree<EntryMapping> mappings = chooseEnigmaFormat(fileMappings).read(fileMappings, new ConsoleProgressListener());
34 deobfuscator.setMappings(mappings);
35 }
36 return deobfuscator;
37 }
38
39 protected static MappingFormat chooseEnigmaFormat(Path path) {
40 if (Files.isDirectory(path)) {
41 return MappingFormat.ENIGMA_DIRECTORY;
42 } else {
43 return MappingFormat.ENIGMA_FILE;
44 }
45 }
46
47 protected static File getWritableFile(String path) {
48 if (path == null) {
49 return null;
50 }
51 File file = new File(path).getAbsoluteFile();
52 File dir = file.getParentFile();
53 if (dir == null) {
54 throw new IllegalArgumentException("Cannot write file: " + path);
55 }
56 // quick fix to avoid stupid stuff in Gradle code
57 if (!dir.isDirectory()) {
58 dir.mkdirs();
59 }
60 return file;
61 }
62
63 protected static File getWritableFolder(String path) {
64 if (path == null) {
65 return null;
66 }
67 File dir = new File(path).getAbsoluteFile();
68 if (!dir.exists()) {
69 throw new IllegalArgumentException("Cannot write to folder: " + dir);
70 }
71 return dir;
72 }
73
74 protected static File getReadableFile(String path) {
75 if (path == null) {
76 return null;
77 }
78 File file = new File(path).getAbsoluteFile();
79 if (!file.exists()) {
80 throw new IllegalArgumentException("Cannot find file: " + file.getAbsolutePath());
81 }
82 return file;
83 }
84
85 protected static Path getReadablePath(String path) {
86 if (path == null) {
87 return null;
88 }
89 Path file = Paths.get(path).toAbsolutePath();
90 if (!Files.exists(file)) {
91 throw new IllegalArgumentException("Cannot find file: " + file.toString());
92 }
93 return file;
94 }
95
96 protected static String getArg(String[] args, int i, String name, boolean required) {
97 if (i >= args.length) {
98 if (required) {
99 throw new IllegalArgumentException(name + " is required");
100 } else {
101 return null;
102 }
103 }
104 return args[i];
105 }
106
107 public static class ConsoleProgressListener implements ProgressListener {
108
109 private static final int ReportTime = 5000; // 5s
110
111 private int totalWork;
112 private long startTime;
113 private long lastReportTime;
114
115 @Override
116 public void init(int totalWork, String title) {
117 this.totalWork = totalWork;
118 this.startTime = System.currentTimeMillis();
119 this.lastReportTime = this.startTime;
120 System.out.println(title);
121 }
122
123 @Override
124 public void step(int numDone, String message) {
125 long now = System.currentTimeMillis();
126 boolean isLastUpdate = numDone == this.totalWork;
127 boolean shouldReport = isLastUpdate || now - this.lastReportTime > ReportTime;
128
129 if (shouldReport) {
130 int percent = numDone * 100 / this.totalWork;
131 System.out.println(String.format("\tProgress: %3d%%", percent));
132 this.lastReportTime = now;
133 }
134 if (isLastUpdate) {
135 double elapsedSeconds = (now - this.startTime) / 1000.0;
136 System.out.println(String.format("Finished in %.1f seconds", elapsedSeconds));
137 }
138 }
139 }
140}