diff options
Diffstat (limited to 'src/main/java/cuchaz/enigma/Main.java')
| -rw-r--r-- | src/main/java/cuchaz/enigma/Main.java | 115 |
1 files changed, 86 insertions, 29 deletions
diff --git a/src/main/java/cuchaz/enigma/Main.java b/src/main/java/cuchaz/enigma/Main.java index ccfc51f..1f2cb84 100644 --- a/src/main/java/cuchaz/enigma/Main.java +++ b/src/main/java/cuchaz/enigma/Main.java | |||
| @@ -12,49 +12,106 @@ | |||
| 12 | package cuchaz.enigma; | 12 | package cuchaz.enigma; |
| 13 | 13 | ||
| 14 | import cuchaz.enigma.gui.Gui; | 14 | import cuchaz.enigma.gui.Gui; |
| 15 | import cuchaz.enigma.gui.GuiController; | ||
| 15 | import cuchaz.enigma.translation.mapping.serde.MappingFormat; | 16 | import cuchaz.enigma.translation.mapping.serde.MappingFormat; |
| 17 | import joptsimple.*; | ||
| 16 | 18 | ||
| 17 | import java.io.File; | 19 | import java.io.BufferedReader; |
| 20 | import java.io.IOException; | ||
| 18 | import java.nio.file.Files; | 21 | import java.nio.file.Files; |
| 19 | import java.nio.file.Path; | 22 | import java.nio.file.Path; |
| 20 | import java.util.jar.JarFile; | 23 | import java.nio.file.Paths; |
| 21 | 24 | ||
| 22 | public class Main { | 25 | public class Main { |
| 23 | 26 | ||
| 24 | public static void main(String[] args) throws Exception { | 27 | public static void main(String[] args) throws IOException { |
| 25 | Gui gui = new Gui(); | 28 | OptionParser parser = new OptionParser(); |
| 26 | 29 | ||
| 27 | // parse command-line args | 30 | OptionSpec<Path> jar = parser.accepts("jar", "Jar file to open at startup") |
| 28 | if (args.length >= 1) { | 31 | .withRequiredArg() |
| 29 | gui.getController().openJar(new JarFile(getFile(args[0]))); | 32 | .withValuesConvertedBy(PathConverter.INSTANCE); |
| 30 | } | 33 | |
| 31 | if (args.length >= 2) { | 34 | OptionSpec<Path> mappings = parser.accepts("mappings", "Mappings file to open at startup") |
| 32 | Path mappingsFile = getFile(args[1]).toPath(); | 35 | .withRequiredArg() |
| 33 | if (Files.isDirectory(mappingsFile)) { | 36 | .withValuesConvertedBy(PathConverter.INSTANCE); |
| 34 | gui.getController().openMappings(MappingFormat.ENIGMA_DIRECTORY, mappingsFile); | 37 | |
| 35 | } else { | 38 | OptionSpec<Path> profile = parser.accepts("profile", "Profile json to apply at startup") |
| 36 | gui.getController().openMappings(MappingFormat.ENIGMA_FILE, mappingsFile); | 39 | .withRequiredArg() |
| 40 | .withValuesConvertedBy(PathConverter.INSTANCE); | ||
| 41 | |||
| 42 | parser.accepts("help", "Displays help information"); | ||
| 43 | |||
| 44 | try { | ||
| 45 | OptionSet options = parser.parse(args); | ||
| 46 | |||
| 47 | if (options.has("help")) { | ||
| 48 | parser.printHelpOn(System.out); | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | |||
| 52 | EnigmaProfile parsedProfile = EnigmaProfile.EMPTY; | ||
| 53 | if (options.has(profile)) { | ||
| 54 | Path profilePath = options.valueOf(profile); | ||
| 55 | try (BufferedReader reader = Files.newBufferedReader(profilePath)) { | ||
| 56 | parsedProfile = EnigmaProfile.parse(reader); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | Gui gui = new Gui(parsedProfile); | ||
| 61 | GuiController controller = gui.getController(); | ||
| 62 | |||
| 63 | if (options.has(jar)) { | ||
| 64 | Path jarPath = options.valueOf(jar); | ||
| 65 | controller.openJar(jarPath); | ||
| 37 | } | 66 | } |
| 38 | } | ||
| 39 | 67 | ||
| 40 | // DEBUG | 68 | if (options.has(mappings)) { |
| 41 | //gui.getController().openDeclaration(new ClassEntry("none/byp")); | 69 | Path mappingsPath = options.valueOf(mappings); |
| 70 | if (Files.isDirectory(mappingsPath)) { | ||
| 71 | controller.openMappings(MappingFormat.ENIGMA_DIRECTORY, mappingsPath); | ||
| 72 | } else { | ||
| 73 | controller.openMappings(MappingFormat.ENIGMA_FILE, mappingsPath); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } catch (OptionException e) { | ||
| 77 | System.out.println("Invalid arguments: " + e.getMessage()); | ||
| 78 | System.out.println(); | ||
| 79 | parser.printHelpOn(System.out); | ||
| 80 | } | ||
| 42 | } | 81 | } |
| 43 | 82 | ||
| 44 | private static File getFile(String path) { | 83 | private static class PathConverter implements ValueConverter<Path> { |
| 45 | // expand ~ to the home dir | 84 | static final ValueConverter<Path> INSTANCE = new PathConverter(); |
| 46 | if (path.startsWith("~")) { | 85 | |
| 47 | // get the home dir | 86 | PathConverter() { |
| 48 | File dirHome = new File(System.getProperty("user.home")); | 87 | } |
| 49 | 88 | ||
| 50 | // is the path just ~/ or is it ~user/ ? | 89 | @Override |
| 51 | if (path.startsWith("~/")) { | 90 | public Path convert(String path) { |
| 52 | return new File(dirHome, path.substring(2)); | 91 | // expand ~ to the home dir |
| 53 | } else { | 92 | if (path.startsWith("~")) { |
| 54 | return new File(dirHome.getParentFile(), path.substring(1)); | 93 | // get the home dir |
| 94 | Path dirHome = Paths.get(System.getProperty("user.home")); | ||
| 95 | |||
| 96 | // is the path just ~/ or is it ~user/ ? | ||
| 97 | if (path.startsWith("~/")) { | ||
| 98 | return dirHome.resolve(path.substring(2)); | ||
| 99 | } else { | ||
| 100 | return dirHome.getParent().resolve(path.substring(1)); | ||
| 101 | } | ||
| 55 | } | 102 | } |
| 103 | |||
| 104 | return Paths.get(path); | ||
| 56 | } | 105 | } |
| 57 | 106 | ||
| 58 | return new File(path); | 107 | @Override |
| 108 | public Class<? extends Path> valueType() { | ||
| 109 | return Path.class; | ||
| 110 | } | ||
| 111 | |||
| 112 | @Override | ||
| 113 | public String valuePattern() { | ||
| 114 | return "path"; | ||
| 115 | } | ||
| 59 | } | 116 | } |
| 60 | } | 117 | } |