Skip to content

Commit 82f6fbf

Browse files
committed
🔨 chore: minor code changes
Signed-off-by: xtrm <oss@xtrm.me>
1 parent 0502bcd commit 82f6fbf

File tree

4 files changed

+63
-43
lines changed

4 files changed

+63
-43
lines changed

common/src/main/java/org/polyfrost/oneconfig/loader/base/Capabilities.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ interface GameMetadata {
5252
String getGameVersion();
5353
String getLoaderName();
5454

55+
default boolean mayRequireRelaunch() {
56+
return false;
57+
}
58+
5559
default @NotNull String getTargetSpecifier() {
5660
return String.format("%s-%s", getGameVersion(), getLoaderName());
5761
}

stage0/src/launchwrapper/java/org/polyfrost/oneconfig/loader/stage0/LaunchWrapperGameMetadata.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class LaunchWrapperGameMetadata implements Capabilities.GameMetadata {
2323

2424
private final String loaderName;
2525
private final String gameVersion;
26+
private final boolean isFabric;
2627

2728
LaunchWrapperGameMetadata() {
2829
boolean isFabric = false;
@@ -32,9 +33,10 @@ public class LaunchWrapperGameMetadata implements Capabilities.GameMetadata {
3233
isFabric = true;
3334
} catch (Throwable ignored) {
3435
}
36+
this.isFabric = isFabric;
3537
this.loaderName = isFabric ? "fabric" : "forge";
3638
try {
37-
this.gameVersion = fetchGameVersion(isFabric);
39+
this.gameVersion = fetchGameVersion();
3840
} catch (Throwable t) {
3941
throw new RuntimeException("Failed to fetch game version", t);
4042
}
@@ -45,7 +47,12 @@ public Path getGameDir() {
4547
return Launch.minecraftHome.toPath();
4648
}
4749

48-
private static String fetchGameVersion(boolean isFabric) {
50+
@Override
51+
public boolean mayRequireRelaunch() {
52+
return !this.isFabric;
53+
}
54+
55+
private String fetchGameVersion() {
4956
if (isFabric) {
5057
FabricLoader loader = FabricLoader.getInstance();
5158
ModContainer container = loader.getModContainer("minecraft").orElseThrow(() -> new RuntimeException("Failed to find 'minecraft' fabric mod container"));

stage1/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ sourceSets {
1212
dependencies {
1313
implementation(projects.common)
1414
include("org.polyfrost:polyio:0.1.0")
15+
include("me.xtrm:propy:0.0.5")
1516

1617
"legacyCompileOnly"("net.minecraft:launchwrapper:1.12")
1718
}

stage1/src/main/java/org/polyfrost/oneconfig/loader/stage1/Stage1Loader.java

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.polyfrost.oneconfig.loader.stage1;
22

3+
import static me.xtrm.propy.Property.propertyOf;
4+
35
import java.io.FileOutputStream;
46
import java.io.InputStream;
57
import java.io.OutputStream;
@@ -12,14 +14,14 @@
1214
import java.nio.file.Path;
1315
import java.nio.file.Paths;
1416
import java.util.List;
15-
import java.util.jar.JarEntry;
1617
import java.util.jar.JarInputStream;
1718
import java.util.zip.ZipEntry;
1819

1920
import com.google.gson.Gson;
2021
import com.google.gson.reflect.TypeToken;
2122
import lombok.SneakyThrows;
2223
import lombok.extern.log4j.Log4j2;
24+
import me.xtrm.propy.Property;
2325

2426
import org.polyfrost.oneconfig.loader.base.Capabilities;
2527
import org.polyfrost.oneconfig.loader.base.LoaderBase;
@@ -36,12 +38,17 @@
3638
*/
3739
@Log4j2
3840
public class Stage1Loader extends LoaderBase {
41+
//FIXME: Detect this from the target artifacts
3942
private static final String ONECONFIG_MAIN_CLASS = "org.polyfrost.oneconfig.internal.bootstrap.Bootstrap";
4043

41-
private static final String ARTIFACT_SNAPSHOTS = "oneconfig.loader.stage1.snapshots";
42-
private static final String STAGE1_ARTIFACT_SNAPSHOTS = "oneconfig.loader.stage1.update.snapshots";
43-
private static final String RELAUNCH_ARTIFACT_SNAPSHOTS = "oneconfig.loader.stage1.relaunch.snapshots";
44-
private static final String ONECONFIG_ARTIFACT_SNAPSHOTS = "oneconfig.loader.stage1.oneconfig.snapshots";
44+
private static final Property<Boolean> ARTIFACT_SNAPSHOTS =
45+
propertyOf("oneconfig.loader.stage1.snapshots", false);
46+
private static final Property<Boolean> STAGE1_ARTIFACT_SNAPSHOTS =
47+
propertyOf("oneconfig.loader.stage1.update.snapshots", false);
48+
private static final Property<Boolean> RELAUNCH_ARTIFACT_SNAPSHOTS =
49+
propertyOf("oneconfig.loader.stage1.relaunch.snapshots", false);
50+
private static final Property<Boolean> ONECONFIG_ARTIFACT_SNAPSHOTS =
51+
propertyOf("oneconfig.loader.stage1.oneconfig.snapshots", false);
4552

4653
private Class<?> oneconfigMainClass;
4754
private Object oneconfigMainInstance;
@@ -56,15 +63,16 @@ public Stage1Loader(Capabilities capabilities) {
5663

5764
@Override
5865
public void load() {
59-
log.info("Creating UI");
60-
LoaderFrame loaderFrame = new LoaderFrame();
61-
6266
log.info("Loading stage1...");
6367
Capabilities capabilities = getCapabilities();
6468
Capabilities.RuntimeAccess runtimeAccess = capabilities.getRuntimeAccess();
6569
Capabilities.GameMetadata gameMetadata = capabilities.getGameMetadata();
6670

67-
log.info("Displaying UI");
71+
String targetSpecifier = gameMetadata.getTargetSpecifier();
72+
log.info("Target specifier: {}", targetSpecifier);
73+
74+
log.info("Creating UI");
75+
LoaderFrame loaderFrame = new LoaderFrame();
6876
loaderFrame.display();
6977

7078
checkForUpdates(loaderFrame);
@@ -74,9 +82,6 @@ public void load() {
7482
// Close our updater window, we're done
7583
loaderFrame.destroy();
7684

77-
String targetSpecifier = gameMetadata.getTargetSpecifier();
78-
log.info("Target specifier: {}", targetSpecifier);
79-
8085
try {
8186
ClassLoader classLoader = runtimeAccess.getClassLoader();
8287
log.info("Bootstrapping OneConfig...");
@@ -105,7 +110,7 @@ public void postLoad() {
105110
}
106111

107112
private void checkForUpdates(LoaderFrame loaderFrame) {
108-
boolean usingUpdateSnapshots = "true".equals(System.getProperty(ARTIFACT_SNAPSHOTS)) || "true".equals(System.getProperty(STAGE1_ARTIFACT_SNAPSHOTS));
113+
boolean usingUpdateSnapshots = shouldUseSnapshots(STAGE1_ARTIFACT_SNAPSHOTS);
109114
BackendArtifact stage1Artifact = readArtifactAt("https://api.polyfrost.org/v1/artifacts/stage1?snapshots=" + usingUpdateSnapshots);
110115
if (stage1Artifact == null) {
111116
// Retry with the opposite snapshot setting
@@ -135,16 +140,11 @@ private void maybeDownloadRelaunch(LoaderFrame loaderFrame) {
135140
Capabilities.RuntimeAccess runtimeAccess = capabilities.getRuntimeAccess();
136141

137142
Capabilities.GameMetadata gameMetadata = capabilities.getGameMetadata();
138-
if (!"forge".equalsIgnoreCase(gameMetadata.getLoaderName())) {
143+
if (!gameMetadata.mayRequireRelaunch()) {
139144
return;
140145
}
141146

142-
String gameVersion = gameMetadata.getGameVersion();
143-
if (!"1.8.9".equals(gameVersion) && !"1.12.2".equals(gameVersion)) {
144-
return;
145-
}
146-
147-
boolean usingRelaunchSnapshots = "true".equals(System.getProperty(ARTIFACT_SNAPSHOTS)) || "true".equals(System.getProperty(RELAUNCH_ARTIFACT_SNAPSHOTS));
147+
boolean usingRelaunchSnapshots = shouldUseSnapshots(RELAUNCH_ARTIFACT_SNAPSHOTS);
148148
BackendArtifact relaunchArtifact = readArtifactAt("https://api.polyfrost.org/v1/artifacts/relaunch?snapshots=" + usingRelaunchSnapshots);
149149
if (relaunchArtifact == null) {
150150
// Retry with the opposite snapshot setting
@@ -183,27 +183,29 @@ private void downloadOneConfigArtifacts(LoaderFrame loaderFrame) {
183183
.resolve("data");
184184
Path artifactCacheFile = dataDir.resolve("artifact-cache.json");
185185

186-
boolean usingSnapshots = "true".equals(System.getProperty(ARTIFACT_SNAPSHOTS)) || "true".equals(System.getProperty(ONECONFIG_ARTIFACT_SNAPSHOTS));
186+
boolean usingSnapshots = shouldUseSnapshots(ONECONFIG_ARTIFACT_SNAPSHOTS);
187187
List<BackendArtifact> artifacts = readArtifactsAt("https://api.polyfrost.org/v1/artifacts/oneconfig?version=" + gameVersion + "&loader=" + loaderName + "&snapshots=" + usingSnapshots);
188188

189189
String dummyArtifactsPath = System.getProperty("oneconfig.loader.stage1.dummyArtifacts");
190190
if (dummyArtifactsPath != null) {
191-
artifacts = readArtifactsFrom(Files.newInputStream(Paths.get(dummyArtifactsPath)));
191+
Path dummyArtifactsPathObj = Paths.get(dummyArtifactsPath);
192+
if (Files.exists(dummyArtifactsPathObj)) {
193+
artifacts = readArtifactsFrom(Files.newInputStream(dummyArtifactsPathObj));
194+
}
192195
}
193196

194197
if (artifacts == null) {
195198
// Retry with the opposite snapshot setting
196199
artifacts = readArtifactsAt("https://api.polyfrost.org/v1/artifacts/oneconfig?version=" + gameVersion + "&loader=" + loaderName + "&snapshots=" + !usingSnapshots);
197200
if (artifacts == null) {
198-
artifacts = readArtifactsFrom(Files.newInputStream(artifactCacheFile));
199-
if (artifacts == null) {
200-
throw new RuntimeException("Failed to fetch OneConfig artifacts");
201+
if (Files.exists(artifactCacheFile)) {
202+
artifacts = readArtifactsFrom(Files.newInputStream(artifactCacheFile));
201203
}
202204
}
203205
}
204206

205-
if (artifacts.isEmpty()) {
206-
throw new RuntimeException("No artifacts found for OneConfig");
207+
if (artifacts == null || artifacts.isEmpty()) {
208+
throw new RuntimeException("Failed to fetch OneConfig artifacts");
207209
}
208210

209211
// Compare all the hashes of any known artifacts and download any that are missing or have changed
@@ -262,15 +264,14 @@ private void downloadOneConfigArtifacts(LoaderFrame loaderFrame) {
262264
@SneakyThrows
263265
private BackendArtifact readArtifactAt(String url) {
264266
URLConnection connection = getRequestHelper().establishConnection(URI.create(url).toURL());
265-
if (!(connection instanceof HttpURLConnection)) {
266-
throw new IllegalArgumentException("Connection is not an HTTP connection");
267-
}
268267

269-
HttpURLConnection httpConnection = (HttpURLConnection) connection;
270-
httpConnection.connect();
268+
connection.connect();
271269

272-
if (httpConnection.getResponseCode() != 200) {
273-
return null;
270+
if (connection instanceof HttpURLConnection) {
271+
HttpURLConnection httpConnection = (HttpURLConnection) connection;
272+
if (httpConnection.getResponseCode() != 200) {
273+
return null;
274+
}
274275
}
275276

276277
try (InputStream inputStream = connection.getInputStream()) {
@@ -288,22 +289,29 @@ private List<BackendArtifact> readArtifactsFrom(InputStream inputStream) {
288289
@SneakyThrows
289290
private List<BackendArtifact> readArtifactsAt(String url) {
290291
URLConnection connection = getRequestHelper().establishConnection(URI.create(url).toURL());
291-
if (!(connection instanceof HttpURLConnection)) {
292-
throw new IllegalArgumentException("Connection is not an HTTP connection");
293-
}
292+
log.info("Fetching artifacts from {}", url);
294293

295-
HttpURLConnection httpConnection = (HttpURLConnection) connection;
296-
httpConnection.connect();
294+
connection.connect();
297295

298-
if (httpConnection.getResponseCode() != 200) {
299-
return null;
296+
if (connection instanceof HttpURLConnection) {
297+
HttpURLConnection httpConnection = (HttpURLConnection) connection;
298+
if (httpConnection.getResponseCode() != 200) {
299+
return null;
300+
}
300301
}
301302

302303
try (InputStream inputStream = connection.getInputStream()) {
303304
return readArtifactsFrom(inputStream);
304305
} catch (Exception e) {
306+
log.error("Failed to read artifacts from {}", url, e);
305307
return null;
306308
}
307309
}
308310

311+
private static boolean shouldUseSnapshots(Property<Boolean> property) {
312+
if (ARTIFACT_SNAPSHOTS.get()) {
313+
return true;
314+
}
315+
return property.get();
316+
}
309317
}

0 commit comments

Comments
 (0)