Skip to content

Commit 9efd960

Browse files
committed
fix not being able to download fabric from the launcher (technically)
1 parent 76852b0 commit 9efd960

File tree

2 files changed

+73
-54
lines changed

2 files changed

+73
-54
lines changed

src/main/java/com/nexia/installer/util/InstallerUtils.java

+36-29
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import javax.swing.*;
88
import java.io.File;
9+
import java.io.IOException;
910
import java.net.URI;
1011
import java.nio.file.*;
1112
import java.text.MessageFormat;
@@ -40,6 +41,40 @@ public static Path findDefaultInstallDir() {
4041
return dir.toAbsolutePath().normalize();
4142
}
4243

44+
public static void downloadVanilla(Path mcDir, VersionHandler.GameVersion gameVersion) throws IOException {
45+
String alternativeCodeName = gameVersion.getCodeName().replaceAll("\\.", "_");
46+
47+
System.out.println("Installing " + gameVersion.getVersion() + " (" + gameVersion.getCodeName() + ")");
48+
49+
Path versionsDir = mcDir.resolve("versions");
50+
if(!Files.exists(versionsDir)) Files.createDirectories(versionsDir);
51+
52+
Path profileDir = versionsDir.resolve(gameVersion.getCodeName());
53+
Path profileJson = profileDir.resolve(gameVersion.getCodeName() + ".json");
54+
55+
Path aProfileDir = versionsDir.resolve(alternativeCodeName);
56+
Path aProfileJson = aProfileDir.resolve(alternativeCodeName + ".json");
57+
58+
if(!Files.exists(profileDir)) Files.createDirectory(profileDir);
59+
if(!Files.exists(profileJson)) Files.createFile(profileJson);
60+
61+
if(!Files.exists(aProfileDir)) Files.createDirectory(aProfileDir);
62+
if(!Files.exists(aProfileJson)) Files.createFile(aProfileJson);
63+
64+
File zipFile = new File(versionsDir + "/" + gameVersion.getCodeName() + ".zip");
65+
66+
Utils.downloadFile(URI.create(gameVersion.getDownload().url).toURL(), zipFile.toPath());
67+
Utils.extractZip(zipFile.toPath(), versionsDir);
68+
69+
Files.copy(aProfileJson, profileJson, StandardCopyOption.REPLACE_EXISTING);
70+
71+
//System.out.println(Utils.sha1String(zipFile.toPath()).equalsIgnoreCase(gameVersion.getDownload().sha1));
72+
73+
aProfileJson.toFile().delete();
74+
aProfileDir.toFile().delete();
75+
zipFile.delete();
76+
}
77+
4378
public static void install(Path mcDir, VersionHandler.GameVersion gameVersion) {
4479
if(mcDir == null || gameVersion == null) return;
4580

@@ -65,35 +100,7 @@ public static void install(Path mcDir, VersionHandler.GameVersion gameVersion) {
65100
}
66101
}
67102

68-
String alternativeCodeName = gameVersion.getCodeName().replaceAll("\\.", "_");
69-
70-
System.out.println("Installing " + gameVersion.getVersion() + " (" + gameVersion.getCodeName() + ")");
71-
72-
Path versionsDir = mcDir.resolve("versions");
73-
Path profileDir = versionsDir.resolve(gameVersion.getCodeName());
74-
Path profileJson = profileDir.resolve(gameVersion.getCodeName() + ".json");
75-
76-
Path aProfileDir = versionsDir.resolve(alternativeCodeName);
77-
Path aProfileJson = aProfileDir.resolve(alternativeCodeName + ".json");
78-
79-
if(!Files.exists(profileDir)) Files.createDirectory(profileDir);
80-
if(!Files.exists(profileJson)) Files.createFile(profileJson);
81-
82-
if(!Files.exists(aProfileDir)) Files.createDirectory(aProfileDir);
83-
if(!Files.exists(aProfileJson)) Files.createFile(aProfileJson);
84-
85-
File zipFile = new File(versionsDir + "/" + gameVersion.getCodeName() + ".zip");
86-
87-
Utils.downloadFile(URI.create(gameVersion.getDownload().url).toURL(), zipFile.toPath());
88-
Utils.extractZip(zipFile.toPath(), versionsDir);
89-
90-
Files.copy(aProfileJson, profileJson, StandardCopyOption.REPLACE_EXISTING);
91-
92-
//System.out.println(Utils.sha1String(zipFile.toPath()).equalsIgnoreCase(gameVersion.getDownload().sha1));
93-
94-
aProfileJson.toFile().delete();
95-
aProfileDir.toFile().delete();
96-
zipFile.delete();
103+
downloadVanilla(mcDir, gameVersion);
97104

98105
if (InstallerHelper.createProfile.isSelected()) {
99106
if (launcherType == null) {

src/main/java/com/nexia/installer/util/fabric/FabricInstallerHelper.java

+37-25
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.nexia.installer.InstallerGUI;
44
import com.nexia.installer.Main;
5+
import com.nexia.installer.game.VersionHandler;
56
import com.nexia.installer.util.HttpAPI;
67
import com.nexia.installer.util.InstallerHelper;
78
import com.nexia.installer.util.InstallerUtils;
@@ -99,47 +100,58 @@ public void launch() throws IOException {
99100
FabricVersionHandler.GameVersion gameVersion = FabricVersionHandler.identifyGameVersion(stringGameVersion);
100101
if(gameVersion == null) return;
101102

102-
103103
Path mcPath = Paths.get(installLocation.getText());
104104

105105
if (!Files.exists(mcPath)) {
106106
throw new RuntimeException(Main.BUNDLE.getString("installer.exception.no.launcher.directory"));
107107
}
108108

109+
VersionHandler.GameVersion vanillaGameVersion = VersionHandler.identifyGameVersion(stringGameVersion);
110+
if(vanillaGameVersion != null) {
111+
// Fix not being able to download fabric from the launcher
112+
Path versionsPath = mcPath.resolve("versions").resolve(vanillaGameVersion.getCodeName());
113+
if(!Files.exists(versionsPath)) {
114+
try {
115+
InstallerUtils.downloadVanilla(mcPath, vanillaGameVersion);
116+
} catch (Exception ignored) { }
117+
}
118+
}
119+
109120
System.out.println("Installing Fabric " + gameVersion.getVersion() + " (" + gameVersion.getCodeName() + ")");
110121
String[] cmd2 = new String[]{"java", "-jar", "cache/" + Objects.requireNonNull(getJarFile()).getName(), "client", "-dir" + "\"" + mcPath.toAbsolutePath() + "\"", "-mcversion", gameVersion.codeName};
111122

123+
new Thread(() -> {
124+
try {
125+
Process process = Runtime.getRuntime().exec(cmd2);
112126

113-
try {
114-
Process process = Runtime.getRuntime().exec(cmd2);
127+
BufferedInputStream successBufferedInputStream = new BufferedInputStream(process.getInputStream());
128+
BufferedInputStream errorBufferedInputStream = new BufferedInputStream(process.getErrorStream());
129+
synchronized (process) {
130+
process.waitFor();
131+
}
115132

116-
BufferedInputStream successBufferedInputStream = new BufferedInputStream(process.getInputStream());
117-
BufferedInputStream errorBufferedInputStream = new BufferedInputStream(process.getErrorStream());
118-
synchronized (process) {
119-
process.waitFor();
120-
}
133+
boolean hasError = false;
121134

122-
boolean hasError = false;
135+
if (errorBufferedInputStream.available() != 0) {
136+
errorBufferedInputStream.close();
137+
hasError = true;
138+
}
123139

124-
if (errorBufferedInputStream.available() != 0) {
125-
errorBufferedInputStream.close();
126-
hasError = true;
127-
}
140+
if (process.exitValue() != 0) hasError = true;
141+
if (successBufferedInputStream.available() == 0) hasError = true;
128142

129-
if (process.exitValue() != 0) hasError = true;
130-
if (successBufferedInputStream.available() == 0) hasError = true;
143+
if(hasError) {
144+
InstallerUtils.showError("The Fabric Installer has had an unknown error.");
145+
} else {
146+
this.showDone(gameVersion);
147+
}
131148

132-
if(hasError) {
133-
InstallerUtils.showError("The Fabric installer has had an unknown error.");
134-
} else {
135-
this.showDone(gameVersion);
149+
} catch (Exception e) {
150+
InstallerUtils.showError(e);
151+
} finally {
152+
buttonInstall.setEnabled(true);
136153
}
137-
138-
} catch (Exception e) {
139-
InstallerUtils.showError(e);
140-
}
141-
142-
buttonInstall.setEnabled(true);
154+
}).start();
143155
}
144156

145157

0 commit comments

Comments
 (0)