Skip to content

Commit

Permalink
Use a better way to determine the installer version.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZekerZhayard committed Dec 22, 2021
1 parent f0a1b27 commit 71ac683
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 31 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

org.gradle.daemon = false

fw_version = 1.5.4
fw_version = 1.5.5
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package io.github.zekerzhayard.forgewrapper.installer;

import java.io.File;
import java.lang.reflect.Method;

import io.github.zekerzhayard.forgewrapper.installer.util.AbstractInstaller;
import io.github.zekerzhayard.forgewrapper.installer.util.InstallerV0;
import io.github.zekerzhayard.forgewrapper.installer.util.InstallerV1;
import net.minecraftforge.installer.actions.ProgressCallback;
import net.minecraftforge.installer.json.Install;
import net.minecraftforge.installer.json.InstallV1;
import net.minecraftforge.installer.json.Util;

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

private static AbstractInstaller getInstaller(int installerSpec) {
switch (installerSpec) {
case 0: {
Boolean isV1 = false;
Method[] methods = Util.class.getDeclaredMethods();
for (Method method: methods) {
String methodName = method.toString();
if (methodName.contains("InstallV1") && methodName.contains("loadInstallProfile")) {
isV1 = true;
break;
}
}
return isV1 ? new InstallerV1() : new InstallerV0();
private static AbstractInstaller createInstaller() {
try {
Class<?> installerClass = Util.class.getMethod("loadInstallProfile").getReturnType();
if (installerClass.equals(Install.class)) {
return new InstallerV0();
} else if (installerClass.equals(InstallV1.class)) {
return new InstallerV1();
} else {
throw new IllegalArgumentException("Unable to determine the installer version. (" + installerClass + ")");
}
case 1: return new InstallerV1();
default: throw new IllegalArgumentException("Invalid installer profile spec: " + installerSpec);
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static void main(String[] args) throws Throwable {
installerJar.toUri().toURL()
}, ModuleUtil.getPlatformClassLoader())) {
Class<?> installer = ucl.loadClass("io.github.zekerzhayard.forgewrapper.installer.Installer");
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))) {
if (!(boolean) installer.getMethod("install", File.class, File.class, File.class).invoke(null, detector.getLibraryDir().toFile(), minecraftJar.toFile(), installerJar.toFile())) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,6 @@ default String getMainClass(String forgeFullVersion) {
return this.getDataFromInstaller(forgeFullVersion, "version.json", e -> e.getAsJsonObject().getAsJsonPrimitive("mainClass").getAsString());
}

/**
* @param forgeFullVersion Forge full version (e.g. 1.14.4-28.2.0).
* @return The installer specification version.
*/
default int getInstallProfileSpec(String forgeFullVersion) {
return this.getDataFromInstaller(forgeFullVersion, "install_profile.json", e -> e.getAsJsonObject().getAsJsonPrimitive("spec").getAsInt());
}

/**
* @param forgeFullVersion Forge full version (e.g. 1.14.4-28.2.0).
* @return The json object in the-installer-jar-->install_profile.json-->data-->xxx-->client.
Expand All @@ -123,6 +115,7 @@ default JsonObject getInstallProfileExtraData(String forgeFullVersion) {
return this.getDataFromInstaller(forgeFullVersion, "install_profile.json", e -> e.getAsJsonObject().getAsJsonObject("data"));
}

@SuppressWarnings("deprecation")
default <R> R getDataFromInstaller(String forgeFullVersion, String entry, Function<JsonElement, R> function) {
Path installer = this.getInstallerJar(forgeFullVersion);
if (isFile(installer)) {
Expand Down Expand Up @@ -186,7 +179,7 @@ default boolean checkExtraFiles(String forgeFullVersion) {
// Check all cached libraries.
boolean checked = true;
for (Map.Entry<String, Path> entry : libsMap.entrySet()) {
checked = this.checkExtraFile(entry.getValue(), hashMap.get(entry.getKey() + "_SHA"));
checked = checkExtraFile(entry.getValue(), hashMap.get(entry.getKey() + "_SHA"));
if (!checked) {
System.out.println("Missing: " + entry.getValue());
break;
Expand All @@ -204,7 +197,7 @@ default boolean checkExtraFiles(String forgeFullVersion) {
* @param sha1 The sha1 defined in installer.
* @return True represents the file is ready.
*/
default boolean checkExtraFile(Path path, String sha1) {
static boolean checkExtraFile(Path path, String sha1) {
return sha1 == null || sha1.equals("") || (isFile(path) && sha1.toLowerCase(Locale.ENGLISH).equals(getFileSHA1(path)));
}

Expand Down

0 comments on commit 71ac683

Please sign in to comment.