-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathSimpleInstaller.java
More file actions
256 lines (225 loc) · 10.8 KB
/
SimpleInstaller.java
File metadata and controls
256 lines (225 loc) · 10.8 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright (c) Forge Development LLC
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.installer;
import java.awt.HeadlessException;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import net.minecraftforge.installer.actions.Actions;
import net.minecraftforge.installer.actions.OfflineAction;
import net.minecraftforge.installer.actions.ProgressCallback;
import net.minecraftforge.installer.json.InstallV1;
import net.minecraftforge.installer.json.Util;
public class SimpleInstaller {
public static boolean headless = false;
public static boolean debug = false;
public static URL mirror = null;
public static void main(String[] args) throws IOException, URISyntaxException {
ProgressCallback monitor;
try {
monitor = ProgressCallback.withOutputs(System.out, getLog());
} catch (FileNotFoundException e) {
e.printStackTrace();
monitor = ProgressCallback.withOutputs(System.out);
}
hookStdOut(monitor);
if (System.getProperty("java.net.preferIPv4Stack") == null) //This is a dirty hack, but screw it, i'm hoping this as default will fix more things then it breaks.
System.setProperty("java.net.preferIPv4Stack", "true");
String vendor = System.getProperty("java.vendor", "missing vendor");
String javaVersion = System.getProperty("java.version", "missing java version");
String jvmVersion = System.getProperty("java.vm.version", "missing jvm version");
monitor.message(String.format("JVM info: %s - %s - %s", vendor, javaVersion, jvmVersion));
monitor.message("Multi-Release: " + MRTest.getVersion());
monitor.message("java.net.preferIPv4Stack=" + System.getProperty("java.net.preferIPv4Stack"));
monitor.message("Current Time: " + new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").format(new Date()));
File installer = new File(SimpleInstaller.class.getProtectionDomain().getCodeSource().getLocation().toURI());
if (installer.getAbsolutePath().contains("!/")) {
monitor.stage("Due to java limitation, please do not run this jar in a folder ending with !");
monitor.message(installer.getAbsolutePath());
return;
}
OptionParser parser = new OptionParser();
Map<Actions, OptionSpec<File>> actions = new LinkedHashMap<>();
actions.put(Actions.SERVER, action(parser, "installServer", "Install a server to the specified directory"));
actions.put(Actions.CLIENT, action(parser, "installClient", "Install the client files to the specified directory"));
actions.put(Actions.EXTRACT, action(parser, "extract", "Extract the contained jar file to the specified directory"));
actions.put(Actions.OFFLINE, action(parser, "makeOffline", "Creates a offline installer at the specified path"));
OptionSpec<Void> helpOption = parser.acceptsAll(Arrays.asList("h", "help"),"Help with this installer");
OptionSpec<Void> offlineOption = parser.accepts("offline", "Don't attempt any network calls");
OptionSpec<Void> debugOption = parser.accepts("debug", "Run in debug mode -- don't delete any files");
OptionSpec<URL> mirrorOption = parser.accepts("mirror", "Use a specific mirror URL").withRequiredArg().ofType(URL.class);
OptionSet optionSet = parser.parse(args);
if (optionSet.has(helpOption)) {
parser.printHelpOn(System.out);
return;
}
debug = optionSet.has(debugOption);
if (optionSet.has(mirrorOption))
mirror = optionSet.valueOf(mirrorOption);
String badCerts = "";
if (optionSet.has(offlineOption) || SimpleInstaller.class.getResource("/" + OfflineAction.OFFLINE_FLAG) != null) {
DownloadUtils.OFFLINE_MODE = true;
monitor.message("ENABLING OFFLINE MODE");
} else {
for(String host : new String[] {
"files.minecraftforge.net",
"maven.minecraftforge.net",
"libraries.minecraft.net",
"launchermeta.mojang.com",
"piston-meta.mojang.com",
"sessionserver.mojang.com",
"authserver.mojang.com",
}) {
monitor.message("Host: " + host + " [" + DownloadUtils.getIpString(host) + "]");
}
for (String host : new String[] {
"https://files.minecraftforge.net/",
"https://launchermeta.mojang.com/"
}) {
if (DownloadUtils.checkCertificate(host))
continue;
if (!badCerts.isEmpty())
badCerts += ", ";
badCerts += host;
}
}
boolean didWork = false;
for (Actions action : actions.keySet()) {
OptionSpec<File> option = actions.get(action);
if (!optionSet.has(option))
continue;
try {
if (!badCerts.isEmpty()) {
monitor.message("Failed to validate certificates for " + badCerts + " - this typically means you have an outdated Java.");
monitor.message("If installation fails, try updating your Java!");
return;
}
File target = optionSet.valueOf(option);
SimpleInstaller.headless = true;
monitor.message("Target Directory: " + target);
InstallV1 install = Util.loadInstallProfile();
if (install.getMirror() != null)
monitor.stage(String.format("Data kindly mirrored by %s at %s", install.getMirror().getName(), install.getMirror().getHomepage()));
if (!action.getAction(install, monitor).run(target, installer)) {
monitor.stage("There was an error during installation");
System.exit(1);
} else {
monitor.message(action.getSuccess());
monitor.stage("You can delete this installer file now if you wish");
}
System.exit(0);
} catch (Throwable e) {
monitor.stage("A problem installing was detected, install cannot continue");
System.exit(1);
}
}
if (!didWork)
launchGui(monitor, installer, badCerts, parser);
}
private static OptionSpec<File> action(OptionParser parser, String arg, String desc) {
return parser.accepts(arg, desc).withOptionalArg().ofType(File.class).defaultsTo(new File("."));
}
public static File getMCDir() {
String userHomeDir = System.getProperty("user.home", ".");
String osType = System.getProperty("os.name").toLowerCase(Locale.ENGLISH);
String mcDir = ".minecraft";
if (osType.contains("win") && System.getenv("APPDATA") != null)
return new File(System.getenv("APPDATA"), mcDir);
else if (osType.contains("mac"))
return new File(userHomeDir, "Library/Application Support/minecraft");
return new File(userHomeDir, mcDir);
}
// Bouncer method, just in case
private static void launchGui(ProgressCallback monitor, File installer, String badCerts) {
launchGui(monitor, installer, badCerts, null);
}
private static void launchGui(ProgressCallback monitor, File installer, String badCerts, OptionParser parser) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (HeadlessException headless) {
// if ran in a headless CLI environment with no args, show some help text and exit gracefully
if (parser == null) {
System.out.println("No options found. Try running with --help for a list of options.");
System.exit(0);
return;
}
try {
parser.printHelpOn(System.out);
} catch (IOException ioException) {
sneak(ioException);
}
System.exit(0);
return;
} catch (Exception ignored) {}
try {
InstallV1 profile = Util.loadInstallProfile();
if (profile.getMirror() != null)
monitor.stage(String.format("Data kindly mirrored by %s at %s", profile.getMirror().getName(), profile.getMirror().getHomepage()));
InstallerPanel panel = new InstallerPanel(getMCDir(), profile, installer, badCerts);
panel.run(monitor);
} catch (Throwable e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null,
"Something went wrong while installing: " + e.toString() + "\n" +
"Check log for more details.",
"Error",
JOptionPane.ERROR_MESSAGE
);
}
}
private static OutputStream getLog() throws FileNotFoundException {
File f = new File(SimpleInstaller.class.getProtectionDomain().getCodeSource().getLocation().getFile());
File output;
if (f.isFile()) output = new File(f.getName() + ".log");
else output = new File("installer.log");
return new BufferedOutputStream(new FileOutputStream(output));
}
static void hookStdOut(ProgressCallback monitor) {
final Pattern endingWhitespace = Pattern.compile("\\r?\\n$");
final OutputStream monitorStream = new OutputStream() {
@Override
public void write(byte[] buf, int off, int len) {
byte[] toWrite = new byte[len];
System.arraycopy(buf, off, toWrite, 0, len);
write(toWrite);
}
@Override
public void write(byte[] b) {
String toWrite = new String(b);
toWrite = endingWhitespace.matcher(toWrite).replaceAll("");
if (!toWrite.isEmpty())
monitor.message(toWrite);
}
@Override
public void write(int b) {
write(new byte[] { (byte) b });
}
};
System.setOut(new PrintStream(monitorStream));
System.setErr(new PrintStream(monitorStream));
}
@SuppressWarnings("unchecked")
private static <T extends Throwable> void sneak(Throwable t) throws T {
throw (T) t;
}
}