Fix NPE when launching Forge 1.13.2.

This commit is contained in:
ZekerZhayard 2020-02-09 12:22:30 +08:00
parent 9f06151fca
commit 7114b3e0d5
3 changed files with 22 additions and 32 deletions

View file

@ -3,6 +3,7 @@ package io.github.zekerzhayard.forgewrapper;
import java.io.File; import java.io.File;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -11,6 +12,7 @@ import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import cpw.mods.modlauncher.Launcher; import cpw.mods.modlauncher.Launcher;
import io.github.zekerzhayard.forgewrapper.converter.Converter;
import io.github.zekerzhayard.forgewrapper.installer.Download; import io.github.zekerzhayard.forgewrapper.installer.Download;
public class Main { public class Main {
@ -18,12 +20,14 @@ public class Main {
URL[] urls = Utils.getURLs(new ArrayList<>()); URL[] urls = Utils.getURLs(new ArrayList<>());
Pattern pattern = Pattern.compile("forge-(?<mcVersion>[0-9.]+)-(?<forgeVersion>[0-9.]+)\\.jar"); Pattern pattern = Pattern.compile("forge-(?<mcVersion>[0-9.]+)-(?<forgeVersion>[0-9.]+)\\.jar");
String version = ""; String version = "";
String installerFileStr = "";
for (URL url : urls) { for (URL url : urls) {
Matcher matcher = pattern.matcher(url.getFile()); Matcher matcher = pattern.matcher(url.getFile());
if (matcher.find() && url.getFile().endsWith(matcher.group(0))) { if (matcher.find() && url.getFile().endsWith(matcher.group(0))) {
version = matcher.group("mcVersion") + "-" + matcher.group("forgeVersion"); version = matcher.group("mcVersion") + "-" + matcher.group("forgeVersion");
String installerUrl = String.format("https://files.minecraftforge.net/maven/net/minecraftforge/forge/%s/forge-%s-installer.jar", version, version); String installerUrl = String.format("https://files.minecraftforge.net/maven/net/minecraftforge/forge/%s/forge-%s-installer.jar", version, version);
Download.download(installerUrl, String.format("./.forgewrapper/forge-%s-installer.jar", version)); installerFileStr = String.format("./.forgewrapper/forge-%s-installer.jar", version);
Download.download(installerUrl, installerFileStr);
break; break;
} }
} }
@ -31,7 +35,7 @@ public class Main {
URLClassLoader ucl = URLClassLoader.newInstance(new URL[] { URLClassLoader ucl = URLClassLoader.newInstance(new URL[] {
Main.class.getProtectionDomain().getCodeSource().getLocation(), Main.class.getProtectionDomain().getCodeSource().getLocation(),
Launcher.class.getProtectionDomain().getCodeSource().getLocation(), Launcher.class.getProtectionDomain().getCodeSource().getLocation(),
new File(String.format("./.forgewrapper/forge-%s-installer.jar", version)).toURI().toURL() new File(installerFileStr).toURI().toURL()
}, null); }, null);
Class<?> installer = ucl.loadClass("io.github.zekerzhayard.forgewrapper.installer.Installer"); Class<?> installer = ucl.loadClass("io.github.zekerzhayard.forgewrapper.installer.Installer");
@ -39,16 +43,7 @@ public class Main {
return; return;
} }
List<String> argsList = Stream.of(args).collect(Collectors.toList()); List<String> argsList = Stream.of(args).collect(Collectors.toList());
argsList.add("--launchTarget"); argsList.addAll(Converter.getAdditionalArgs(Paths.get(installerFileStr)));
argsList.add("fmlclient");
argsList.add("--fml.forgeVersion");
argsList.add((String) installer.getMethod("getForgeVersion").invoke(null));
argsList.add("--fml.mcVersion");
argsList.add((String) installer.getMethod("getMcVersion").invoke(null));
argsList.add("--fml.forgeGroup");
argsList.add("net.minecraftforge");
argsList.add("--fml.mcpVersion");
argsList.add((String) installer.getMethod("getMcpVersion").invoke(null));
Launcher.main(argsList.toArray(new String[0])); Launcher.main(argsList.toArray(new String[0]));
} }

View file

@ -10,6 +10,7 @@ import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption; import java.nio.file.StandardOpenOption;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
@ -24,10 +25,7 @@ import com.google.gson.JsonParser;
public class Converter { public class Converter {
public static void convert(Path installerPath, Path targetDir) throws Exception { public static void convert(Path installerPath, Path targetDir) throws Exception {
JsonObject installer = getInstallerJson(installerPath); JsonObject installer = getInstallerJson(installerPath);
List<String> arguments = getAdditionalArgs(installer);
ArrayList<String> arguments = new ArrayList<>();
getElement(installer.getAsJsonObject("arguments"), "game").getAsJsonArray().iterator().forEachRemaining(je -> arguments.add(je.getAsString()));
String mcVersion = arguments.get(arguments.indexOf("--fml.mcVersion") + 1); String mcVersion = arguments.get(arguments.indexOf("--fml.mcVersion") + 1);
String forgeVersion = arguments.get(arguments.indexOf("--fml.forgeVersion") + 1); String forgeVersion = arguments.get(arguments.indexOf("--fml.forgeVersion") + 1);
String forgeFullVersion = "forge-" + mcVersion + "-" + forgeVersion; String forgeFullVersion = "forge-" + mcVersion + "-" + forgeVersion;
@ -60,7 +58,18 @@ public class Converter {
Files.copy(installerPath, forgeWrapperPath.resolve(forgeFullVersion + "-installer.jar"), StandardCopyOption.REPLACE_EXISTING); Files.copy(installerPath, forgeWrapperPath.resolve(forgeFullVersion + "-installer.jar"), StandardCopyOption.REPLACE_EXISTING);
} }
private static JsonObject getInstallerJson(Path installerPath) { public static List<String> getAdditionalArgs(Path installerPath) {
JsonObject installer = getInstallerJson(installerPath);
return getAdditionalArgs(installer);
}
public static List<String> getAdditionalArgs(JsonObject installer) {
List<String> args = new ArrayList<>();
getElement(installer.getAsJsonObject("arguments"), "game").getAsJsonArray().iterator().forEachRemaining(je -> args.add(je.getAsString()));
return args;
}
public static JsonObject getInstallerJson(Path installerPath) {
try { try {
ZipFile zf = new ZipFile(installerPath.toFile()); ZipFile zf = new ZipFile(installerPath.toFile());
ZipEntry versionFile = zf.getEntry("version.json"); ZipEntry versionFile = zf.getEntry("version.json");

View file

@ -5,11 +5,9 @@ import net.minecraftforge.installer.json.Install;
import net.minecraftforge.installer.json.Util; import net.minecraftforge.installer.json.Util;
public class Installer { public class Installer {
private static Install install;
public static boolean install() { public static boolean install() {
ProgressCallback monitor = ProgressCallback.withOutputs(System.out); ProgressCallback monitor = ProgressCallback.withOutputs(System.out);
install = Util.loadInstallProfile(); Install install = Util.loadInstallProfile();
if (System.getProperty("java.net.preferIPv4Stack") == null) { if (System.getProperty("java.net.preferIPv4Stack") == null) {
System.setProperty("java.net.preferIPv4Stack", "true"); System.setProperty("java.net.preferIPv4Stack", "true");
} }
@ -20,16 +18,4 @@ public class Installer {
monitor.message("java.net.preferIPv4Stack=" + System.getProperty("java.net.preferIPv4Stack")); monitor.message("java.net.preferIPv4Stack=" + System.getProperty("java.net.preferIPv4Stack"));
return new ClientInstall4MultiMC(install, monitor).run(null, input -> true); return new ClientInstall4MultiMC(install, monitor).run(null, input -> true);
} }
public static String getForgeVersion() {
return install.getVersion().substring(install.getVersion().lastIndexOf("-") + 1);
}
public static String getMcVersion() {
return install.getMinecraft();
}
public static String getMcpVersion() {
return install.getData(true).get("MCP_VERSION").replace("'", "");
}
} }