-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathProcessFactory.java
171 lines (150 loc) · 6.59 KB
/
ProcessFactory.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
package me.earth.headlessmc.launcher.launch;
import lombok.CustomLog;
import lombok.RequiredArgsConstructor;
import lombok.val;
import me.earth.headlessmc.api.config.HasConfig;
import me.earth.headlessmc.launcher.LauncherProperties;
import me.earth.headlessmc.launcher.auth.AuthException;
import me.earth.headlessmc.launcher.files.FileManager;
import me.earth.headlessmc.launcher.instrumentation.Instrumentation;
import me.earth.headlessmc.launcher.instrumentation.InstrumentationHelper;
import me.earth.headlessmc.launcher.instrumentation.Target;
import me.earth.headlessmc.launcher.os.OS;
import me.earth.headlessmc.launcher.util.IOUtil;
import me.earth.headlessmc.launcher.version.Features;
import me.earth.headlessmc.launcher.version.Rule;
import me.earth.headlessmc.launcher.version.Version;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.zip.ZipFile;
@CustomLog
@RequiredArgsConstructor
public class ProcessFactory {
private final FileManager files;
private final HasConfig config;
private final OS os;
public @Nullable Process run(LaunchOptions options)
throws LaunchException, AuthException, IOException {
val instrumentation = InstrumentationHelper.create(options);
return run(options, instrumentation);
}
public @Nullable Process run(LaunchOptions options, Instrumentation instrumentation)
throws IOException, LaunchException, AuthException {
val launcher = options.getLauncher();
val version = new VersionMerger(options.getVersion());
if (version.getArguments() == null) {
throw new LaunchException(
version.getName() + ": Version file and its parents" +
" didn't contain arguments.");
}
val dlls = options.getFiles().createRelative("extracted");
val targets = processLibraries(version, dlls);
addGameJar(version, targets);
val commandBuilder = Command.builder()
.account(options.getAccount())
.classpath(instrumentation.instrument(targets))
.os(os)
.jvmArgs(options.getAdditionalJvmArgs())
.natives(dlls.getBase().getAbsolutePath())
.runtime(options.isRuntime())
.version(version)
.launcher(launcher)
.inMemory(options.isInMemory())
.lwjgl(options.isLwjgl())
.build();
val command = commandBuilder.build();
downloadAssets(files, version);
log.debug(command.toString());
val dir = new File(launcher.getConfig().get(
LauncherProperties.GAME_DIR, launcher.getMcFiles().getPath()));
log.info("Game will run in " + dir);
//noinspection ResultOfMethodCallIgnored
dir.mkdirs();
if (options.isInMemory()) {
new InMemoryLauncher(options, commandBuilder, version, launcher.getJavaService().getCurrent()).launch();
return null;
}
return this.run(new ProcessBuilder()
.command(command)
.directory(dir)
.redirectError(options.isNoOut()
? ProcessBuilder.Redirect.PIPE
: ProcessBuilder.Redirect.INHERIT)
.redirectOutput(options.isNoOut()
? ProcessBuilder.Redirect.PIPE
: ProcessBuilder.Redirect.INHERIT)
.redirectInput(options.isNoIn()
? ProcessBuilder.Redirect.PIPE
: ProcessBuilder.Redirect.INHERIT));
}
private void addGameJar(Version version, List<Target> targets)
throws IOException {
File gameJar = new File(version.getFolder(), version.getName() + ".jar");
log.debug("GameJar: " + gameJar.getAbsolutePath());
if (!gameJar.exists() || !checkZipIntact(gameJar) && gameJar.delete()) {
log.info("Downloading " + version.getName() + " from "
+ version.getClientDownload());
download(version.getClientDownload(), gameJar.getAbsolutePath());
}
targets.add(new Target(true, gameJar.getAbsolutePath()));
}
private List<Target> processLibraries(Version version, FileManager dlls)
throws IOException {
// TODO: proper features
val features = Features.EMPTY;
val targets = new ArrayList<Target>(version.getLibraries().size());
Set<String> libPaths = new HashSet<>();
for (val library : version.getLibraries()) {
if (library.getRule().apply(os, features) == Rule.Action.ALLOW) {
log.debug("Checking: " + library);
String libPath = library.getPath(os);
if (!libPaths.add(libPath)) {
continue;
}
val path = files.getDir("libraries") + File.separator + libPath;
if (!new File(path).exists()) {
String url = library.getUrl(libPath);
log.info(libPath + " is missing, downloading from " + url);
download(url, path);
}
library.getExtractor().extract(path, dlls);
if (!library.isNativeLibrary()) {
targets.add(new Target(false, path));
}
} else {
log.debug("Ignoring: " + library.getName());
}
}
return targets;
}
protected boolean checkZipIntact(File file) {
val name = file.getName();
boolean result = true;
if (name.endsWith(".jar") || name.endsWith(".zip")) {
try {
val zipFile = new ZipFile(file);
zipFile.close();
} catch (IOException e) {
log.error("Couldn't read " + name + " : " + e.getMessage());
result = false;
}
}
return result;
}
protected Process run(ProcessBuilder builder) throws IOException {
return builder.start();
}
protected void downloadAssets(FileManager files, Version version)
throws IOException {
new AssetsDownloader(files, config, version.getAssetsUrl(), version.getAssets())
.download();
}
protected void download(String from, String to) throws IOException {
IOUtil.download(from, to);
}
}