blob: 1d63ec1cc1bed8751ec55f260f9468aac0b29851 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/*******************************************************************************
* Copyright (c) 2015 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public
* License v3.0 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
* <p>
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma;
import cuchaz.enigma.gui.Gui;
import cuchaz.enigma.gui.GuiController;
import cuchaz.enigma.translation.mapping.serde.MappingFormat;
import joptsimple.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import com.google.common.io.MoreFiles;
public class Main {
public static void main(String[] args) throws IOException {
OptionParser parser = new OptionParser();
OptionSpec<Path> jar = parser.accepts("jar", "Jar file to open at startup")
.withRequiredArg()
.withValuesConvertedBy(PathConverter.INSTANCE);
OptionSpec<Path> mappings = parser.accepts("mappings", "Mappings file to open at startup")
.withRequiredArg()
.withValuesConvertedBy(PathConverter.INSTANCE);
OptionSpec<Path> profile = parser.accepts("profile", "Profile json to apply at startup")
.withRequiredArg()
.withValuesConvertedBy(PathConverter.INSTANCE);
parser.accepts("help", "Displays help information");
try {
OptionSet options = parser.parse(args);
if (options.has("help")) {
parser.printHelpOn(System.out);
return;
}
EnigmaProfile parsedProfile;
if (options.has(profile)) {
Path profilePath = options.valueOf(profile);
try (BufferedReader reader = Files.newBufferedReader(profilePath)) {
parsedProfile = EnigmaProfile.parse(reader);
}
} else {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream("/profile.json"), StandardCharsets.UTF_8))){
parsedProfile = EnigmaProfile.parse(reader);
} catch (IOException ex) {
System.out.println("Failed to load default profile, will use empty profile: " + ex.getMessage());
parsedProfile = EnigmaProfile.EMPTY;
}
}
Gui gui = new Gui(parsedProfile);
GuiController controller = gui.getController();
if (options.has(jar)) {
Path jarPath = options.valueOf(jar);
controller.openJar(jarPath)
.whenComplete((v, t) -> {
if (options.has(mappings)) {
Path mappingsPath = options.valueOf(mappings);
if (Files.isDirectory(mappingsPath)) {
controller.openMappings(MappingFormat.ENIGMA_DIRECTORY, mappingsPath);
} else if ("zip".equalsIgnoreCase(MoreFiles.getFileExtension(mappingsPath))) {
controller.openMappings(MappingFormat.ENIGMA_ZIP, mappingsPath);
} else {
controller.openMappings(MappingFormat.ENIGMA_FILE, mappingsPath);
}
}
});
}
} catch (OptionException e) {
System.out.println("Invalid arguments: " + e.getMessage());
System.out.println();
parser.printHelpOn(System.out);
}
}
private static class PathConverter implements ValueConverter<Path> {
static final ValueConverter<Path> INSTANCE = new PathConverter();
PathConverter() {
}
@Override
public Path convert(String path) {
// expand ~ to the home dir
if (path.startsWith("~")) {
// get the home dir
Path dirHome = Paths.get(System.getProperty("user.home"));
// is the path just ~/ or is it ~user/ ?
if (path.startsWith("~/")) {
return dirHome.resolve(path.substring(2));
} else {
return dirHome.getParent().resolve(path.substring(1));
}
}
return Paths.get(path);
}
@Override
public Class<? extends Path> valueType() {
return Path.class;
}
@Override
public String valuePattern() {
return "path";
}
}
}
|