-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathLaunchCommand.java
139 lines (125 loc) · 5.76 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
package me.earth.headlessmc.launcher.command;
import lombok.CustomLog;
import lombok.val;
import me.earth.headlessmc.api.command.CommandException;
import me.earth.headlessmc.api.config.Property;
import me.earth.headlessmc.command.CommandUtil;
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.FileUtil;
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 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.");
// TODO: is this really necessary?
args.put("-noout", "Doesn't print Minecrafts output to the console.");
args.put("-quit", "Quit HeadlessMc after launching the game.");
args.put("--jvm", "Jvm args to use.");
}
@Override
public void execute(Version version, String... args) throws CommandException {
val uuid = UUID.randomUUID();
ctx.log("Launching version " + version.getName() + ", " + uuid);
val files = ctx.getFileManager().createRelative(uuid.toString());
boolean quit = flag("-quit", LauncherProperties.INVERT_QUIT_FLAG, args);
int status = 0;
try {
val process = ctx.getProcessFactory().run(
LaunchOptions.builder()
.account(getAccount())
.version(version)
.launcher(ctx)
.files(files)
.parseFlags(ctx, quit, args)
.build());
if (process == null) {
ctx.log("InMemory main thread ended.");
}
if (quit || process == null) {
System.exit(0);
return;
}
try {
status = process.waitFor();
ctx.log("Minecraft exited with code: " + status);
} catch (InterruptedException ie) {
ctx.log("Launcher has been interrupted...");
Thread.currentThread().interrupt();
}
} catch (IOException | 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 {
// 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());
FileUtil.delete(files.getBase());
} catch (IOException e) {
log.error("Couldn't delete files of game "
+ files.getBase().getName()
+ ": " + e.getMessage());
}
}
if (!CommandUtil.hasFlag("-stay", args)) {
System.exit(status);
}
}
}
private boolean flag(String flg, Property<Boolean> inv, String... args) {
return CommandUtil.hasFlag(flg, args) ^ ctx.getConfig().get(inv, false);
}
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());
}
}
}