Skip to content

Commit

Permalink
fix not being able to download fabric from the launcher (technically)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-coded committed Aug 7, 2024
1 parent 76852b0 commit 9efd960
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 54 deletions.
65 changes: 36 additions & 29 deletions src/main/java/com/nexia/installer/util/InstallerUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}


Expand Down

0 comments on commit 9efd960

Please sign in to comment.