-
-
Notifications
You must be signed in to change notification settings - Fork 424
/
Bootstrap.java
219 lines (179 loc) 路 7.23 KB
/
Bootstrap.java
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
/*
* SK's Minecraft Launcher
* Copyright (C) 2010-2014 Albert Pham <http://www.sk89q.com> and contributors
* Please see LICENSE.txt for license information.
*/
package com.skcraft.launcher;
import com.skcraft.launcher.bootstrap.*;
import lombok.Getter;
import lombok.extern.java.Log;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.*;
import java.util.logging.Level;
import static com.skcraft.launcher.bootstrap.SharedLocale.tr;
@Log
public class Bootstrap {
private static final int BOOTSTRAP_VERSION = 1;
@Getter private final File baseDir;
@Getter private final boolean portable;
@Getter private final File binariesDir;
@Getter private final Properties properties;
private final String[] originalArgs;
public static void main(String[] args) throws Throwable {
SimpleLogFormatter.configureGlobalLogger();
SharedLocale.loadBundle("com.skcraft.launcher.lang.Bootstrap", Locale.getDefault());
boolean portable = isPortableMode();
Bootstrap bootstrap = new Bootstrap(portable, args);
try {
bootstrap.cleanup();
bootstrap.launch();
} catch (Throwable t) {
Bootstrap.log.log(Level.WARNING, "Error", t);
Bootstrap.setSwingLookAndFeel();
SwingHelper.showErrorDialog(null, tr("errors.bootstrapError"), tr("errorTitle"), t);
}
}
public Bootstrap(boolean portable, String[] args) throws IOException {
this.properties = BootstrapUtils.loadProperties(Bootstrap.class, "bootstrap.properties");
File baseDir = portable ? new File(".") : getUserLauncherDir();
this.baseDir = baseDir;
this.portable = portable;
this.binariesDir = new File(baseDir, "launcher");
this.originalArgs = args;
binariesDir.mkdirs();
}
public void cleanup() {
File[] files = binariesDir.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".tmp");
}
});
if (files != null) {
for (File file : files) {
file.delete();
}
}
}
public void launch() throws Throwable {
File[] files = binariesDir.listFiles(new LauncherBinary.Filter());
List<LauncherBinary> binaries = new ArrayList<LauncherBinary>();
if (files != null) {
for (File file : files) {
Bootstrap.log.info("Found " + file.getAbsolutePath() + "...");
binaries.add(new LauncherBinary(file));
}
}
if (!binaries.isEmpty()) {
launchExisting(binaries, true);
} else {
launchInitial();
}
}
public void launchInitial() throws Exception {
Bootstrap.log.info("Downloading the launcher...");
Thread thread = new Thread(new Downloader(this));
thread.start();
}
public void launchExisting(List<LauncherBinary> binaries, boolean redownload) throws Exception {
Collections.sort(binaries);
LauncherBinary working = null;
Class<?> clazz = null;
for (LauncherBinary binary : binaries) {
File testFile = binary.getPath();
try {
testFile = binary.getExecutableJar();
Bootstrap.log.info("Trying " + testFile.getAbsolutePath() + "...");
clazz = load(testFile);
Bootstrap.log.info("Launcher loaded successfully.");
working = binary;
break;
} catch (Throwable t) {
Bootstrap.log.log(Level.WARNING, "Failed to load " + testFile.getAbsoluteFile(), t);
}
}
if (working != null) {
for (LauncherBinary binary : binaries) {
if (working != binary) {
log.info("Removing " + binary.getPath() + "...");
binary.remove();
}
}
execute(clazz);
} else {
if (redownload) {
launchInitial();
} else {
throw new IOException("Failed to find launchable .jar");
}
}
}
public void execute(Class<?> clazz) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
Method method = clazz.getDeclaredMethod("main", String[].class);
String[] launcherArgs;
if (portable) {
launcherArgs = new String[] {
"--portable",
"--dir",
baseDir.getAbsolutePath(),
"--bootstrap-version",
String.valueOf(BOOTSTRAP_VERSION) };
} else {
launcherArgs = new String[] {
"--dir",
baseDir.getAbsolutePath(),
"--bootstrap-version",
String.valueOf(BOOTSTRAP_VERSION) };
}
String[] args = new String[originalArgs.length + launcherArgs.length];
System.arraycopy(launcherArgs, 0, args, 0, launcherArgs.length);
System.arraycopy(originalArgs, 0, args, launcherArgs.length, originalArgs.length);
log.info("Launching with arguments " + Arrays.toString(args));
method.invoke(null, new Object[] { args });
}
public Class<?> load(File jarFile) throws MalformedURLException, ClassNotFoundException {
URL[] urls = new URL[] { jarFile.toURI().toURL() };
URLClassLoader child = new URLClassLoader(urls, this.getClass().getClassLoader());
Class<?> clazz = Class.forName(getProperties().getProperty("launcherClass"), true, child);
return clazz;
}
public static void setSwingLookAndFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Throwable e) {
}
}
private static File getFileChooseDefaultDir() {
JFileChooser chooser = new JFileChooser();
FileSystemView fsv = chooser.getFileSystemView();
return fsv.getDefaultDirectory();
}
private File getUserLauncherDir() {
String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("win")) {
return new File(getFileChooseDefaultDir(), getProperties().getProperty("homeFolderWindows"));
}
File dotFolder = new File(System.getProperty("user.home"), getProperties().getProperty("homeFolder"));
String xdgFolderName = getProperties().getProperty("homeFolderLinux");
if (osName.contains("linux") && !dotFolder.exists() && !xdgFolderName.isEmpty()) {
String xdgDataHome = System.getenv("XDG_DATA_HOME");
if (xdgDataHome.isEmpty()) {
xdgDataHome = System.getProperty("user.home") + "/.local/share";
}
return new File(xdgDataHome, xdgFolderName);
}
return dotFolder;
}
private static boolean isPortableMode() {
return new File("portable.txt").exists();
}
}