-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathLaunchCommand.java
196 lines (172 loc) · 8.07 KB
/
LaunchCommand.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
package me.earth.headlessmc.launcher.command;
import lombok.CustomLog;
import lombok.val;
import me.earth.headlessmc.api.command.CommandException;
import me.earth.headlessmc.api.command.CommandUtil;
import me.earth.headlessmc.api.command.ParseUtil;
import me.earth.headlessmc.launcher.Launcher;
import me.earth.headlessmc.launcher.LauncherProperties;
import me.earth.headlessmc.launcher.auth.AuthException;
import me.earth.headlessmc.launcher.auth.LaunchAccount;
import me.earth.headlessmc.launcher.auth.ValidatedAccount;
import me.earth.headlessmc.launcher.files.FileManager;
import me.earth.headlessmc.launcher.launch.LaunchException;
import me.earth.headlessmc.launcher.launch.LaunchOptions;
import me.earth.headlessmc.launcher.version.Version;
import java.io.IOException;
import java.util.UUID;
import java.util.logging.Level;
import static me.earth.headlessmc.api.command.CommandUtil.flag;
import static me.earth.headlessmc.launcher.LauncherProperties.RE_THROW_LAUNCH_EXCEPTIONS;
@CustomLog
public class LaunchCommand extends AbstractVersionCommand {
public LaunchCommand(Launcher launcher) {
super(launcher, "launch", "Launches the game.");
args.put("<version/id>", "Name or id of the version to launch. If you use the id you need to use the -id flag as well.");
args.put("-id", "Use if you specified an id instead of a version name.");
args.put("-commands", "Starts the game with the built-in command line support.");
args.put("-lwjgl", "Removes lwjgl code, causing Minecraft not to render anything.");
args.put("-inmemory", "Launches the game in the same JVM headlessmc is running in.");
args.put("-jndi", "Patches the Log4J vulnerability.");
args.put("-lookup", "Patches the Log4J vulnerability even harder.");
args.put("-paulscode", "Removes some error messages from the PaulsCode library which may annoy you if you started the game with the -lwjgl flag.");
args.put("-noout", "Doesn't print Minecrafts output to the console."); // TODO: is this really necessary?
args.put("-quit", "Quit HeadlessMc after launching the game.");
args.put("--jvm", "Jvm args to use.");
args.put("--retries", "The amount of times you want to retry running Minecraft.");
}
@Override
public void execute(Version version, String... args) throws CommandException {
boolean prepare = CommandUtil.hasFlag("-prepare", args);
val uuid = UUID.fromString(ctx.getConfig().get(LauncherProperties.EXTRACTED_FILE_CACHE_UUID, UUID.randomUUID().toString()));
ctx.log((prepare ? "Preparing" : "Launching") + " version " + version.getName() + ", " + uuid);
ctx.getLoggingService().setLevel(Level.INFO, true);
val files = ctx.getFileManager().createRelative(uuid.toString());
boolean quit = flag(ctx, "-quit", LauncherProperties.INVERT_QUIT_FLAG, LauncherProperties.ALWAYS_QUIT_FLAG, args);
int status = 0;
try {
status = runProcess(version, files, quit, prepare, args);
} catch (LaunchException | AuthException e) {
status = -1;
log.error(e);
ctx.log(String.format("Couldn't launch %s: %s", version.getName(), e.getMessage()));
if (ctx.getConfig().get(RE_THROW_LAUNCH_EXCEPTIONS, false)) {
throw new IllegalStateException(e);
}
} catch (Throwable t) {
status = -1;
val msg = String.format("Couldn't launch %s: %s", version.getName(), t.getMessage());
log.error(msg, t);
ctx.log(msg);
throw t;
} finally {
cleanup(files, args);
if (!prepare && !CommandUtil.hasFlag("-stay", args)) {
ctx.getExitManager().exit(status);
}
}
if (status != 0 && ctx.getConfig().get(RE_THROW_LAUNCH_EXCEPTIONS, false)) {
throw new IllegalStateException("Minecraft exited with code " + status);
}
if (!prepare) {
try {
ctx.getCommandLine().open(ctx);
} catch (IOException ioException) {
throw new IllegalStateException("Failed to reopen HeadlessMc CommandLineReader", ioException);
}
}
}
private int runProcess(Version version, FileManager files, boolean quit, boolean prepare, String... args)
throws CommandException, LaunchException, AuthException {
int status = 0;
LaunchAccount account = getAccount();
String retriesOption = CommandUtil.getOption("--retries", args);
int retries = 0;
if (retriesOption != null) {
retries = ParseUtil.parseI(retriesOption);
}
Throwable throwable = null;
for (int i = 0; i < retries + 1; i++) {
if (i > 0) {
log.warn("Retrying to launch Minecraft: " + i);
}
try {
Process process = ctx.getProcessFactory().run(
LaunchOptions.builder()
.account(account)
.version(version)
.launcher(ctx)
.files(files)
.closeCommandLine(!prepare)
.parseFlags(ctx, quit, args)
.prepare(prepare)
.build()
);
if (prepare) {
return 0;
}
if (process == null) {
ctx.log("InMemory main thread ended.");
}
if (quit || process == null) {
cleanup(files, args);
ctx.getExitManager().exit(0);
return 0;
}
try {
status = process.waitFor();
ctx.log("Minecraft exited with code: " + status);
} catch (InterruptedException ie) {
ctx.log("Launcher has been interrupted...");
Thread.currentThread().interrupt();
}
if (status == 0) {
break;
}
} catch (Throwable t) {
status = -1;
log.error("Failed to start Minecraft on try " + i, t);
if (throwable == null) {
throwable = t;
} else {
throwable.addSuppressed(t);
}
}
}
if (status != 0 && throwable != null) {
if (throwable instanceof RuntimeException) {
throw (RuntimeException) throwable;
}
throw new LaunchException(throwable);
}
return status;
}
private void cleanup(FileManager files, String... args) {
// for some reason both ShutdownHooks and File.deleteOnExit are
// not really working, that's why we Main.deleteOldFiles, too.
if (!CommandUtil.hasFlag("-keep", args) && !ctx.getConfig().get(LauncherProperties.KEEP_FILES, false)) {
try {
log.info("Deleting " + files.getBase().getName());
ctx.getFileManager().delete(files.getBase());
} catch (IOException e) {
log.error("Couldn't delete files of game " + files.getBase().getName(), e);
}
}
}
protected LaunchAccount getAccount() throws CommandException {
try {
ValidatedAccount account = ctx.getAccountManager().getPrimaryAccount();
if (account == null) {
if (ctx.getAccountManager().getOfflineChecker().isOffline()) {
return ctx.getAccountManager().getOfflineAccount(ctx.getConfig());
}
throw new AuthException("You can't play the game without an account! Please use the login command.");
} else {
account = ctx.getAccountManager().refreshAccount(account);
return account.toLaunchAccount();
}
} catch (AuthException e) {
throw new CommandException(e.getMessage());
}
}
}