-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMain.java
127 lines (109 loc) · 5.13 KB
/
Main.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
package me.earth.headlessmc.launcher;
import lombok.CustomLog;
import lombok.experimental.UtilityClass;
import lombok.val;
import me.earth.headlessmc.HeadlessMcImpl;
import me.earth.headlessmc.auth.AbstractLoginCommand;
import me.earth.headlessmc.command.line.CommandLineImpl;
import me.earth.headlessmc.config.HmcProperties;
import me.earth.headlessmc.launcher.auth.*;
import me.earth.headlessmc.launcher.command.LaunchContext;
import me.earth.headlessmc.launcher.files.*;
import me.earth.headlessmc.launcher.java.JavaService;
import me.earth.headlessmc.launcher.launch.ProcessFactory;
import me.earth.headlessmc.launcher.os.OSFactory;
import me.earth.headlessmc.launcher.util.UuidUtil;
import me.earth.headlessmc.launcher.version.VersionService;
import me.earth.headlessmc.launcher.version.VersionUtil;
import me.earth.headlessmc.logging.LogLevelUtil;
import me.earth.headlessmc.logging.LoggingHandler;
import me.earth.headlessmc.logging.SimpleLog;
import java.io.IOException;
@CustomLog
@UtilityClass
public final class Main {
public static void main(String[] args) {
Throwable throwable = null;
try {
runHeadlessMc(args);
} catch (Throwable t) {
throwable = t;
} finally {
/*
These "System.exit()" calls are here because of the LoginCommands
-webview option. It seems that after closing the JFrame there is
still either the AWT, Webview or Javafx thread running, keeping the
program alive. I played around with the code of the OpenAuth lib and
could not find a good solution. E.g. LoginFrame DISPOSE_ON_CLOSE
prevents further LoginFrames from getting displayed. The only ok
option I found was to make the LoginFrame a Singleton and dispose it
manually at the end but that prevents multiple LoginFrames at the
same time.
*/
try {
if (throwable == null) {
System.exit(0);
} else {
log.error(throwable);
System.exit(-1);
}
} catch (Throwable exitThrowable) {
// it is possible, if we launch in memory, that forge prevents us from calling System.exit through their SecurityManager
if (throwable != null && exitThrowable.getClass() == throwable.getClass()) { // we have logged FMLSecurityManager$ExitTrappedException before
log.error("Failed to exit!", exitThrowable);
}
// TODO: exit gracefully, try to call Forge to exit
}
}
}
private void runHeadlessMc(String... args) throws IOException, AuthException {
LoggingHandler.apply();
AbstractLoginCommand.replaceLogger();
if (Main.class.getClassLoader() == ClassLoader.getSystemClassLoader()) {
log.warn("You are not running from headlessmc-launcher-wrapper. Some things will not work properly!");
}
val files = FileManager.mkdir("HeadlessMC");
AutoConfiguration.runAutoConfiguration(files);
val configs = Service.refresh(new ConfigService(files));
LogLevelUtil.trySetLevel(
configs.getConfig().get(HmcProperties.LOGLEVEL, "INFO"));
val in = new CommandLineImpl();
val hmc = new HeadlessMcImpl(new SimpleLog(), configs, in);
val os = OSFactory.detect(configs.getConfig());
val mcFiles = MinecraftFinder.find(configs.getConfig(), os);
val versions = Service.refresh(new VersionService(mcFiles));
val javas = Service.refresh(new JavaService(configs));
val accountStore = new AccountStore(files, configs);
val accounts = new AccountManager(new AccountValidator(), new OfflineChecker(configs), accountStore);
accounts.load(configs.getConfig());
val launcher = new Launcher(hmc, versions, mcFiles, files,
new ProcessFactory(mcFiles, configs, os), configs,
javas, accounts);
LauncherApi.setLauncher(launcher);
deleteOldFiles(launcher);
versions.refresh();
hmc.setCommandContext(new LaunchContext(launcher));
if (!QuickExitCliHandler.checkQuickExit(launcher, in, args)) {
log.info(String.format("Detected: %s", os));
log.info(String.format("Minecraft Dir: %s", mcFiles.getBase()));
hmc.log(VersionUtil.makeTable(VersionUtil.releases(versions)));
in.listen(hmc);
}
}
private void deleteOldFiles(Launcher launcher) {
if (launcher.getConfig().get(LauncherProperties.KEEP_FILES, false)) {
return;
}
for (val file : launcher.getFileManager().listFiles()) {
if (file.isDirectory() && UuidUtil.isUuid(file.getName())) {
try {
log.debug("Deleting " + file.getAbsolutePath());
FileUtil.delete(file);
} catch (IOException ioe) {
log.error("Couldn't delete " + file.getName()
+ " : " + ioe.getMessage());
}
}
}
}
}