diff options
| author | 2020-06-03 13:39:42 -0400 | |
|---|---|---|
| committer | 2020-06-03 18:39:42 +0100 | |
| commit | 0f47403d0220757fed189b76e2071e25b1025cb8 (patch) | |
| tree | 879bf72c4476f0a5e0d82da99d7ff2b2276bcaca /src/main/java/cuchaz/enigma/command/Command.java | |
| parent | Fix search dialog hanging for a short time sometimes (#250) (diff) | |
| download | enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.gz enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.tar.xz enigma-fork-0f47403d0220757fed189b76e2071e25b1025cb8.zip | |
Split GUI code to separate module (#242)
* Split into modules
* Post merge compile fixes
Co-authored-by: modmuss50 <modmuss50@gmail.com>
Diffstat (limited to 'src/main/java/cuchaz/enigma/command/Command.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/command/Command.java | 154 |
1 files changed, 0 insertions, 154 deletions
diff --git a/src/main/java/cuchaz/enigma/command/Command.java b/src/main/java/cuchaz/enigma/command/Command.java deleted file mode 100644 index 09dd321..0000000 --- a/src/main/java/cuchaz/enigma/command/Command.java +++ /dev/null | |||
| @@ -1,154 +0,0 @@ | |||
| 1 | package cuchaz.enigma.command; | ||
| 2 | |||
| 3 | import cuchaz.enigma.Enigma; | ||
| 4 | import cuchaz.enigma.EnigmaProject; | ||
| 5 | import cuchaz.enigma.ProgressListener; | ||
| 6 | import cuchaz.enigma.translation.mapping.EntryMapping; | ||
| 7 | import cuchaz.enigma.translation.mapping.MappingSaveParameters; | ||
| 8 | import cuchaz.enigma.translation.mapping.serde.MappingFormat; | ||
| 9 | import cuchaz.enigma.translation.mapping.tree.EntryTree; | ||
| 10 | |||
| 11 | import java.io.File; | ||
| 12 | import java.nio.file.Files; | ||
| 13 | import java.nio.file.Path; | ||
| 14 | import java.nio.file.Paths; | ||
| 15 | |||
| 16 | import com.google.common.io.MoreFiles; | ||
| 17 | |||
| 18 | public abstract class Command { | ||
| 19 | public final String name; | ||
| 20 | |||
| 21 | protected Command(String name) { | ||
| 22 | this.name = name; | ||
| 23 | } | ||
| 24 | |||
| 25 | public abstract String getUsage(); | ||
| 26 | |||
| 27 | public abstract boolean isValidArgument(int length); | ||
| 28 | |||
| 29 | public abstract void run(String... args) throws Exception; | ||
| 30 | |||
| 31 | protected static EnigmaProject openProject(Path fileJarIn, Path fileMappings) throws Exception { | ||
| 32 | ProgressListener progress = new ConsoleProgressListener(); | ||
| 33 | |||
| 34 | Enigma enigma = Enigma.create(); | ||
| 35 | |||
| 36 | System.out.println("Reading jar..."); | ||
| 37 | EnigmaProject project = enigma.openJar(fileJarIn, progress); | ||
| 38 | |||
| 39 | if (fileMappings != null) { | ||
| 40 | System.out.println("Reading mappings..."); | ||
| 41 | |||
| 42 | MappingSaveParameters saveParameters = enigma.getProfile().getMappingSaveParameters(); | ||
| 43 | EntryTree<EntryMapping> mappings = chooseEnigmaFormat(fileMappings).read(fileMappings, progress, saveParameters); | ||
| 44 | |||
| 45 | project.setMappings(mappings); | ||
| 46 | } | ||
| 47 | |||
| 48 | return project; | ||
| 49 | } | ||
| 50 | |||
| 51 | protected static MappingFormat chooseEnigmaFormat(Path path) { | ||
| 52 | if (Files.isDirectory(path)) { | ||
| 53 | return MappingFormat.ENIGMA_DIRECTORY; | ||
| 54 | } else if ("zip".equalsIgnoreCase(MoreFiles.getFileExtension(path))) { | ||
| 55 | return MappingFormat.ENIGMA_ZIP; | ||
| 56 | } else { | ||
| 57 | return MappingFormat.ENIGMA_FILE; | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | protected static File getWritableFile(String path) { | ||
| 62 | if (path == null) { | ||
| 63 | return null; | ||
| 64 | } | ||
| 65 | File file = new File(path).getAbsoluteFile(); | ||
| 66 | File dir = file.getParentFile(); | ||
| 67 | if (dir == null) { | ||
| 68 | throw new IllegalArgumentException("Cannot write file: " + path); | ||
| 69 | } | ||
| 70 | // quick fix to avoid stupid stuff in Gradle code | ||
| 71 | if (!dir.isDirectory()) { | ||
| 72 | dir.mkdirs(); | ||
| 73 | } | ||
| 74 | return file; | ||
| 75 | } | ||
| 76 | |||
| 77 | protected static File getWritableFolder(String path) { | ||
| 78 | if (path == null) { | ||
| 79 | return null; | ||
| 80 | } | ||
| 81 | File dir = new File(path).getAbsoluteFile(); | ||
| 82 | if (!dir.exists()) { | ||
| 83 | throw new IllegalArgumentException("Cannot write to folder: " + dir); | ||
| 84 | } | ||
| 85 | return dir; | ||
| 86 | } | ||
| 87 | |||
| 88 | protected static File getReadableFile(String path) { | ||
| 89 | if (path == null) { | ||
| 90 | return null; | ||
| 91 | } | ||
| 92 | File file = new File(path).getAbsoluteFile(); | ||
| 93 | if (!file.exists()) { | ||
| 94 | throw new IllegalArgumentException("Cannot find file: " + file.getAbsolutePath()); | ||
| 95 | } | ||
| 96 | return file; | ||
| 97 | } | ||
| 98 | |||
| 99 | protected static Path getReadablePath(String path) { | ||
| 100 | if (path == null) { | ||
| 101 | return null; | ||
| 102 | } | ||
| 103 | Path file = Paths.get(path).toAbsolutePath(); | ||
| 104 | if (!Files.exists(file)) { | ||
| 105 | throw new IllegalArgumentException("Cannot find file: " + file.toString()); | ||
| 106 | } | ||
| 107 | return file; | ||
| 108 | } | ||
| 109 | |||
| 110 | protected static String getArg(String[] args, int i, String name, boolean required) { | ||
| 111 | if (i >= args.length) { | ||
| 112 | if (required) { | ||
| 113 | throw new IllegalArgumentException(name + " is required"); | ||
| 114 | } else { | ||
| 115 | return null; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | return args[i]; | ||
| 119 | } | ||
| 120 | |||
| 121 | public static class ConsoleProgressListener implements ProgressListener { | ||
| 122 | |||
| 123 | private static final int ReportTime = 5000; // 5s | ||
| 124 | |||
| 125 | private int totalWork; | ||
| 126 | private long startTime; | ||
| 127 | private long lastReportTime; | ||
| 128 | |||
| 129 | @Override | ||
| 130 | public void init(int totalWork, String title) { | ||
| 131 | this.totalWork = totalWork; | ||
| 132 | this.startTime = System.currentTimeMillis(); | ||
| 133 | this.lastReportTime = this.startTime; | ||
| 134 | System.out.println(title); | ||
| 135 | } | ||
| 136 | |||
| 137 | @Override | ||
| 138 | public void step(int numDone, String message) { | ||
| 139 | long now = System.currentTimeMillis(); | ||
| 140 | boolean isLastUpdate = numDone == this.totalWork; | ||
| 141 | boolean shouldReport = isLastUpdate || now - this.lastReportTime > ReportTime; | ||
| 142 | |||
| 143 | if (shouldReport) { | ||
| 144 | int percent = numDone * 100 / this.totalWork; | ||
| 145 | System.out.println(String.format("\tProgress: %3d%%", percent)); | ||
| 146 | this.lastReportTime = now; | ||
| 147 | } | ||
| 148 | if (isLastUpdate) { | ||
| 149 | double elapsedSeconds = (now - this.startTime) / 1000.0; | ||
| 150 | System.out.println(String.format("Finished in %.1f seconds", elapsedSeconds)); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||