1
1
package org .polyfrost .oneconfig .loader .stage1 ;
2
2
3
+ import static me .xtrm .propy .Property .propertyOf ;
4
+
3
5
import java .io .FileOutputStream ;
4
6
import java .io .InputStream ;
5
7
import java .io .OutputStream ;
12
14
import java .nio .file .Path ;
13
15
import java .nio .file .Paths ;
14
16
import java .util .List ;
15
- import java .util .jar .JarEntry ;
16
17
import java .util .jar .JarInputStream ;
17
18
import java .util .zip .ZipEntry ;
18
19
19
20
import com .google .gson .Gson ;
20
21
import com .google .gson .reflect .TypeToken ;
21
22
import lombok .SneakyThrows ;
22
23
import lombok .extern .log4j .Log4j2 ;
24
+ import me .xtrm .propy .Property ;
23
25
24
26
import org .polyfrost .oneconfig .loader .base .Capabilities ;
25
27
import org .polyfrost .oneconfig .loader .base .LoaderBase ;
36
38
*/
37
39
@ Log4j2
38
40
public class Stage1Loader extends LoaderBase {
41
+ //FIXME: Detect this from the target artifacts
39
42
private static final String ONECONFIG_MAIN_CLASS = "org.polyfrost.oneconfig.internal.bootstrap.Bootstrap" ;
40
43
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 );
45
52
46
53
private Class <?> oneconfigMainClass ;
47
54
private Object oneconfigMainInstance ;
@@ -56,15 +63,16 @@ public Stage1Loader(Capabilities capabilities) {
56
63
57
64
@ Override
58
65
public void load () {
59
- log .info ("Creating UI" );
60
- LoaderFrame loaderFrame = new LoaderFrame ();
61
-
62
66
log .info ("Loading stage1..." );
63
67
Capabilities capabilities = getCapabilities ();
64
68
Capabilities .RuntimeAccess runtimeAccess = capabilities .getRuntimeAccess ();
65
69
Capabilities .GameMetadata gameMetadata = capabilities .getGameMetadata ();
66
70
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 ();
68
76
loaderFrame .display ();
69
77
70
78
checkForUpdates (loaderFrame );
@@ -74,9 +82,6 @@ public void load() {
74
82
// Close our updater window, we're done
75
83
loaderFrame .destroy ();
76
84
77
- String targetSpecifier = gameMetadata .getTargetSpecifier ();
78
- log .info ("Target specifier: {}" , targetSpecifier );
79
-
80
85
try {
81
86
ClassLoader classLoader = runtimeAccess .getClassLoader ();
82
87
log .info ("Bootstrapping OneConfig..." );
@@ -105,7 +110,7 @@ public void postLoad() {
105
110
}
106
111
107
112
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 );
109
114
BackendArtifact stage1Artifact = readArtifactAt ("https://api.polyfrost.org/v1/artifacts/stage1?snapshots=" + usingUpdateSnapshots );
110
115
if (stage1Artifact == null ) {
111
116
// Retry with the opposite snapshot setting
@@ -135,16 +140,11 @@ private void maybeDownloadRelaunch(LoaderFrame loaderFrame) {
135
140
Capabilities .RuntimeAccess runtimeAccess = capabilities .getRuntimeAccess ();
136
141
137
142
Capabilities .GameMetadata gameMetadata = capabilities .getGameMetadata ();
138
- if (!"forge" . equalsIgnoreCase ( gameMetadata .getLoaderName () )) {
143
+ if (!gameMetadata .mayRequireRelaunch ( )) {
139
144
return ;
140
145
}
141
146
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 );
148
148
BackendArtifact relaunchArtifact = readArtifactAt ("https://api.polyfrost.org/v1/artifacts/relaunch?snapshots=" + usingRelaunchSnapshots );
149
149
if (relaunchArtifact == null ) {
150
150
// Retry with the opposite snapshot setting
@@ -183,27 +183,29 @@ private void downloadOneConfigArtifacts(LoaderFrame loaderFrame) {
183
183
.resolve ("data" );
184
184
Path artifactCacheFile = dataDir .resolve ("artifact-cache.json" );
185
185
186
- boolean usingSnapshots = "true" . equals ( System . getProperty ( ARTIFACT_SNAPSHOTS )) || "true" . equals ( System . getProperty ( ONECONFIG_ARTIFACT_SNAPSHOTS ) );
186
+ boolean usingSnapshots = shouldUseSnapshots ( ONECONFIG_ARTIFACT_SNAPSHOTS );
187
187
List <BackendArtifact > artifacts = readArtifactsAt ("https://api.polyfrost.org/v1/artifacts/oneconfig?version=" + gameVersion + "&loader=" + loaderName + "&snapshots=" + usingSnapshots );
188
188
189
189
String dummyArtifactsPath = System .getProperty ("oneconfig.loader.stage1.dummyArtifacts" );
190
190
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
+ }
192
195
}
193
196
194
197
if (artifacts == null ) {
195
198
// Retry with the opposite snapshot setting
196
199
artifacts = readArtifactsAt ("https://api.polyfrost.org/v1/artifacts/oneconfig?version=" + gameVersion + "&loader=" + loaderName + "&snapshots=" + !usingSnapshots );
197
200
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 ));
201
203
}
202
204
}
203
205
}
204
206
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 " );
207
209
}
208
210
209
211
// 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) {
262
264
@ SneakyThrows
263
265
private BackendArtifact readArtifactAt (String url ) {
264
266
URLConnection connection = getRequestHelper ().establishConnection (URI .create (url ).toURL ());
265
- if (!(connection instanceof HttpURLConnection )) {
266
- throw new IllegalArgumentException ("Connection is not an HTTP connection" );
267
- }
268
267
269
- HttpURLConnection httpConnection = (HttpURLConnection ) connection ;
270
- httpConnection .connect ();
268
+ connection .connect ();
271
269
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
+ }
274
275
}
275
276
276
277
try (InputStream inputStream = connection .getInputStream ()) {
@@ -288,22 +289,29 @@ private List<BackendArtifact> readArtifactsFrom(InputStream inputStream) {
288
289
@ SneakyThrows
289
290
private List <BackendArtifact > readArtifactsAt (String url ) {
290
291
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 );
294
293
295
- HttpURLConnection httpConnection = (HttpURLConnection ) connection ;
296
- httpConnection .connect ();
294
+ connection .connect ();
297
295
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
+ }
300
301
}
301
302
302
303
try (InputStream inputStream = connection .getInputStream ()) {
303
304
return readArtifactsFrom (inputStream );
304
305
} catch (Exception e ) {
306
+ log .error ("Failed to read artifacts from {}" , url , e );
305
307
return null ;
306
308
}
307
309
}
308
310
311
+ private static boolean shouldUseSnapshots (Property <Boolean > property ) {
312
+ if (ARTIFACT_SNAPSHOTS .get ()) {
313
+ return true ;
314
+ }
315
+ return property .get ();
316
+ }
309
317
}
0 commit comments