Skip to content

Commit 71ac683

Browse files
committed
Use a better way to determine the installer version.
1 parent f0a1b27 commit 71ac683

File tree

5 files changed

+20
-31
lines changed

5 files changed

+20
-31
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
org.gradle.daemon = false
33

4-
fw_version = 1.5.4
4+
fw_version = 1.5.5
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-all.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
package io.github.zekerzhayard.forgewrapper.installer;
22

33
import java.io.File;
4-
import java.lang.reflect.Method;
54

65
import io.github.zekerzhayard.forgewrapper.installer.util.AbstractInstaller;
76
import io.github.zekerzhayard.forgewrapper.installer.util.InstallerV0;
87
import io.github.zekerzhayard.forgewrapper.installer.util.InstallerV1;
98
import net.minecraftforge.installer.actions.ProgressCallback;
109
import net.minecraftforge.installer.json.Install;
10+
import net.minecraftforge.installer.json.InstallV1;
1111
import net.minecraftforge.installer.json.Util;
1212

1313
public class Installer {
14-
public static boolean install(File libraryDir, File minecraftJar, File installerJar, int installerSpec) {
15-
AbstractInstaller installer = getInstaller(installerSpec);
14+
public static boolean install(File libraryDir, File minecraftJar, File installerJar) {
15+
AbstractInstaller installer = createInstaller();
1616
ProgressCallback monitor = ProgressCallback.withOutputs(System.out);
1717
Install profile = installer.loadInstallProfile();
1818
if (System.getProperty("java.net.preferIPv4Stack") == null) {
@@ -26,22 +26,18 @@ public static boolean install(File libraryDir, File minecraftJar, File installer
2626
return installer.runClientInstall(profile, monitor, libraryDir, minecraftJar, installerJar);
2727
}
2828

29-
private static AbstractInstaller getInstaller(int installerSpec) {
30-
switch (installerSpec) {
31-
case 0: {
32-
Boolean isV1 = false;
33-
Method[] methods = Util.class.getDeclaredMethods();
34-
for (Method method: methods) {
35-
String methodName = method.toString();
36-
if (methodName.contains("InstallV1") && methodName.contains("loadInstallProfile")) {
37-
isV1 = true;
38-
break;
39-
}
40-
}
41-
return isV1 ? new InstallerV1() : new InstallerV0();
29+
private static AbstractInstaller createInstaller() {
30+
try {
31+
Class<?> installerClass = Util.class.getMethod("loadInstallProfile").getReturnType();
32+
if (installerClass.equals(Install.class)) {
33+
return new InstallerV0();
34+
} else if (installerClass.equals(InstallV1.class)) {
35+
return new InstallerV1();
36+
} else {
37+
throw new IllegalArgumentException("Unable to determine the installer version. (" + installerClass + ")");
4238
}
43-
case 1: return new InstallerV1();
44-
default: throw new IllegalArgumentException("Invalid installer profile spec: " + installerSpec);
39+
} catch (Throwable t) {
40+
throw new RuntimeException(t);
4541
}
4642
}
4743
}

src/main/java/io/github/zekerzhayard/forgewrapper/installer/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static void main(String[] args) throws Throwable {
4747
installerJar.toUri().toURL()
4848
}, ModuleUtil.getPlatformClassLoader())) {
4949
Class<?> installer = ucl.loadClass("io.github.zekerzhayard.forgewrapper.installer.Installer");
50-
if (!(boolean) installer.getMethod("install", File.class, File.class, File.class, int.class).invoke(null, detector.getLibraryDir().toFile(), minecraftJar.toFile(), installerJar.toFile(), detector.getInstallProfileSpec(forgeFullVersion))) {
50+
if (!(boolean) installer.getMethod("install", File.class, File.class, File.class).invoke(null, detector.getLibraryDir().toFile(), minecraftJar.toFile(), installerJar.toFile())) {
5151
return;
5252
}
5353
}

src/main/java/io/github/zekerzhayard/forgewrapper/installer/detector/IFileDetector.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,6 @@ default String getMainClass(String forgeFullVersion) {
107107
return this.getDataFromInstaller(forgeFullVersion, "version.json", e -> e.getAsJsonObject().getAsJsonPrimitive("mainClass").getAsString());
108108
}
109109

110-
/**
111-
* @param forgeFullVersion Forge full version (e.g. 1.14.4-28.2.0).
112-
* @return The installer specification version.
113-
*/
114-
default int getInstallProfileSpec(String forgeFullVersion) {
115-
return this.getDataFromInstaller(forgeFullVersion, "install_profile.json", e -> e.getAsJsonObject().getAsJsonPrimitive("spec").getAsInt());
116-
}
117-
118110
/**
119111
* @param forgeFullVersion Forge full version (e.g. 1.14.4-28.2.0).
120112
* @return The json object in the-installer-jar-->install_profile.json-->data-->xxx-->client.
@@ -123,6 +115,7 @@ default JsonObject getInstallProfileExtraData(String forgeFullVersion) {
123115
return this.getDataFromInstaller(forgeFullVersion, "install_profile.json", e -> e.getAsJsonObject().getAsJsonObject("data"));
124116
}
125117

118+
@SuppressWarnings("deprecation")
126119
default <R> R getDataFromInstaller(String forgeFullVersion, String entry, Function<JsonElement, R> function) {
127120
Path installer = this.getInstallerJar(forgeFullVersion);
128121
if (isFile(installer)) {
@@ -186,7 +179,7 @@ default boolean checkExtraFiles(String forgeFullVersion) {
186179
// Check all cached libraries.
187180
boolean checked = true;
188181
for (Map.Entry<String, Path> entry : libsMap.entrySet()) {
189-
checked = this.checkExtraFile(entry.getValue(), hashMap.get(entry.getKey() + "_SHA"));
182+
checked = checkExtraFile(entry.getValue(), hashMap.get(entry.getKey() + "_SHA"));
190183
if (!checked) {
191184
System.out.println("Missing: " + entry.getValue());
192185
break;
@@ -204,7 +197,7 @@ default boolean checkExtraFiles(String forgeFullVersion) {
204197
* @param sha1 The sha1 defined in installer.
205198
* @return True represents the file is ready.
206199
*/
207-
default boolean checkExtraFile(Path path, String sha1) {
200+
static boolean checkExtraFile(Path path, String sha1) {
208201
return sha1 == null || sha1.equals("") || (isFile(path) && sha1.toLowerCase(Locale.ENGLISH).equals(getFileSHA1(path)));
209202
}
210203

0 commit comments

Comments
 (0)