From 9efd960153ae9c8887856416a3aa7f544b05ecc9 Mon Sep 17 00:00:00 2001 From: NotCoded <66999075+not-coded@users.noreply.github.com> Date: Wed, 7 Aug 2024 17:58:11 +0200 Subject: [PATCH] fix not being able to download fabric from the launcher (technically) --- .../nexia/installer/util/InstallerUtils.java | 65 ++++++++++--------- .../util/fabric/FabricInstallerHelper.java | 62 +++++++++++------- 2 files changed, 73 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/nexia/installer/util/InstallerUtils.java b/src/main/java/com/nexia/installer/util/InstallerUtils.java index 80fc73c..03cfdeb 100644 --- a/src/main/java/com/nexia/installer/util/InstallerUtils.java +++ b/src/main/java/com/nexia/installer/util/InstallerUtils.java @@ -6,6 +6,7 @@ import javax.swing.*; import java.io.File; +import java.io.IOException; import java.net.URI; import java.nio.file.*; import java.text.MessageFormat; @@ -40,6 +41,40 @@ public static Path findDefaultInstallDir() { return dir.toAbsolutePath().normalize(); } + public static void downloadVanilla(Path mcDir, VersionHandler.GameVersion gameVersion) throws IOException { + String alternativeCodeName = gameVersion.getCodeName().replaceAll("\\.", "_"); + + System.out.println("Installing " + gameVersion.getVersion() + " (" + gameVersion.getCodeName() + ")"); + + Path versionsDir = mcDir.resolve("versions"); + if(!Files.exists(versionsDir)) Files.createDirectories(versionsDir); + + Path profileDir = versionsDir.resolve(gameVersion.getCodeName()); + Path profileJson = profileDir.resolve(gameVersion.getCodeName() + ".json"); + + Path aProfileDir = versionsDir.resolve(alternativeCodeName); + Path aProfileJson = aProfileDir.resolve(alternativeCodeName + ".json"); + + if(!Files.exists(profileDir)) Files.createDirectory(profileDir); + if(!Files.exists(profileJson)) Files.createFile(profileJson); + + if(!Files.exists(aProfileDir)) Files.createDirectory(aProfileDir); + if(!Files.exists(aProfileJson)) Files.createFile(aProfileJson); + + File zipFile = new File(versionsDir + "/" + gameVersion.getCodeName() + ".zip"); + + Utils.downloadFile(URI.create(gameVersion.getDownload().url).toURL(), zipFile.toPath()); + Utils.extractZip(zipFile.toPath(), versionsDir); + + Files.copy(aProfileJson, profileJson, StandardCopyOption.REPLACE_EXISTING); + + //System.out.println(Utils.sha1String(zipFile.toPath()).equalsIgnoreCase(gameVersion.getDownload().sha1)); + + aProfileJson.toFile().delete(); + aProfileDir.toFile().delete(); + zipFile.delete(); + } + public static void install(Path mcDir, VersionHandler.GameVersion gameVersion) { if(mcDir == null || gameVersion == null) return; @@ -65,35 +100,7 @@ public static void install(Path mcDir, VersionHandler.GameVersion gameVersion) { } } - String alternativeCodeName = gameVersion.getCodeName().replaceAll("\\.", "_"); - - System.out.println("Installing " + gameVersion.getVersion() + " (" + gameVersion.getCodeName() + ")"); - - Path versionsDir = mcDir.resolve("versions"); - Path profileDir = versionsDir.resolve(gameVersion.getCodeName()); - Path profileJson = profileDir.resolve(gameVersion.getCodeName() + ".json"); - - Path aProfileDir = versionsDir.resolve(alternativeCodeName); - Path aProfileJson = aProfileDir.resolve(alternativeCodeName + ".json"); - - if(!Files.exists(profileDir)) Files.createDirectory(profileDir); - if(!Files.exists(profileJson)) Files.createFile(profileJson); - - if(!Files.exists(aProfileDir)) Files.createDirectory(aProfileDir); - if(!Files.exists(aProfileJson)) Files.createFile(aProfileJson); - - File zipFile = new File(versionsDir + "/" + gameVersion.getCodeName() + ".zip"); - - Utils.downloadFile(URI.create(gameVersion.getDownload().url).toURL(), zipFile.toPath()); - Utils.extractZip(zipFile.toPath(), versionsDir); - - Files.copy(aProfileJson, profileJson, StandardCopyOption.REPLACE_EXISTING); - - //System.out.println(Utils.sha1String(zipFile.toPath()).equalsIgnoreCase(gameVersion.getDownload().sha1)); - - aProfileJson.toFile().delete(); - aProfileDir.toFile().delete(); - zipFile.delete(); + downloadVanilla(mcDir, gameVersion); if (InstallerHelper.createProfile.isSelected()) { if (launcherType == null) { diff --git a/src/main/java/com/nexia/installer/util/fabric/FabricInstallerHelper.java b/src/main/java/com/nexia/installer/util/fabric/FabricInstallerHelper.java index ff8b8df..a3f541c 100644 --- a/src/main/java/com/nexia/installer/util/fabric/FabricInstallerHelper.java +++ b/src/main/java/com/nexia/installer/util/fabric/FabricInstallerHelper.java @@ -2,6 +2,7 @@ import com.nexia.installer.InstallerGUI; import com.nexia.installer.Main; +import com.nexia.installer.game.VersionHandler; import com.nexia.installer.util.HttpAPI; import com.nexia.installer.util.InstallerHelper; import com.nexia.installer.util.InstallerUtils; @@ -99,47 +100,58 @@ public void launch() throws IOException { FabricVersionHandler.GameVersion gameVersion = FabricVersionHandler.identifyGameVersion(stringGameVersion); if(gameVersion == null) return; - Path mcPath = Paths.get(installLocation.getText()); if (!Files.exists(mcPath)) { throw new RuntimeException(Main.BUNDLE.getString("installer.exception.no.launcher.directory")); } + VersionHandler.GameVersion vanillaGameVersion = VersionHandler.identifyGameVersion(stringGameVersion); + if(vanillaGameVersion != null) { + // Fix not being able to download fabric from the launcher + Path versionsPath = mcPath.resolve("versions").resolve(vanillaGameVersion.getCodeName()); + if(!Files.exists(versionsPath)) { + try { + InstallerUtils.downloadVanilla(mcPath, vanillaGameVersion); + } catch (Exception ignored) { } + } + } + System.out.println("Installing Fabric " + gameVersion.getVersion() + " (" + gameVersion.getCodeName() + ")"); String[] cmd2 = new String[]{"java", "-jar", "cache/" + Objects.requireNonNull(getJarFile()).getName(), "client", "-dir" + "\"" + mcPath.toAbsolutePath() + "\"", "-mcversion", gameVersion.codeName}; + new Thread(() -> { + try { + Process process = Runtime.getRuntime().exec(cmd2); - try { - Process process = Runtime.getRuntime().exec(cmd2); + BufferedInputStream successBufferedInputStream = new BufferedInputStream(process.getInputStream()); + BufferedInputStream errorBufferedInputStream = new BufferedInputStream(process.getErrorStream()); + synchronized (process) { + process.waitFor(); + } - BufferedInputStream successBufferedInputStream = new BufferedInputStream(process.getInputStream()); - BufferedInputStream errorBufferedInputStream = new BufferedInputStream(process.getErrorStream()); - synchronized (process) { - process.waitFor(); - } + boolean hasError = false; - boolean hasError = false; + if (errorBufferedInputStream.available() != 0) { + errorBufferedInputStream.close(); + hasError = true; + } - if (errorBufferedInputStream.available() != 0) { - errorBufferedInputStream.close(); - hasError = true; - } + if (process.exitValue() != 0) hasError = true; + if (successBufferedInputStream.available() == 0) hasError = true; - if (process.exitValue() != 0) hasError = true; - if (successBufferedInputStream.available() == 0) hasError = true; + if(hasError) { + InstallerUtils.showError("The Fabric Installer has had an unknown error."); + } else { + this.showDone(gameVersion); + } - if(hasError) { - InstallerUtils.showError("The Fabric installer has had an unknown error."); - } else { - this.showDone(gameVersion); + } catch (Exception e) { + InstallerUtils.showError(e); + } finally { + buttonInstall.setEnabled(true); } - - } catch (Exception e) { - InstallerUtils.showError(e); - } - - buttonInstall.setEnabled(true); + }).start(); }