Skip to content

Commit

Permalink
i think its basically done now!
Browse files Browse the repository at this point in the history
  • Loading branch information
not-coded committed Oct 22, 2023
1 parent 7dbbc87 commit b6e4731
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 22 deletions.
33 changes: 33 additions & 0 deletions src/main/java/com/nexia/installer/util/HttpAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.nexia.installer.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpAPI {

public static String userAgent = "Mozilla/5.0 (compatible; combat-test-installer; +https://github.com/nexia-cts/combat-test-installer)";

public static String get(String url) {
try {
URL apiUrl = new URL(url);

HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();

connection.setRequestProperty("User-Agent", userAgent);
connection.setRequestMethod("GET");

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuilder response = new StringBuilder();
while ((line = reader.readLine()) != null) {
response.append(line);
}

reader.close();
connection.disconnect();

return response.toString();
} catch (Exception ignored) { return null; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import com.nexia.installer.InstallerGUI;
import com.nexia.installer.Main;
import com.nexia.installer.util.InstallerHelper;
import com.nexia.installer.util.InstallerUtils;
import com.nexia.installer.util.ProfileInstaller;
import com.nexia.installer.util.Utils;
import com.nexia.installer.util.*;
import mjson.Json;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
Expand Down Expand Up @@ -80,13 +79,6 @@ public JPanel setPanel(InstallerGUI gui) {
}
buttonFabric.setEnabled(true);
} catch (IOException ex) {
try {
getJarFile().delete();
} catch (IOException exc) {
// what the fuck
exc.printStackTrace();
}

buttonFabric.setEnabled(false);
}
});
Expand Down Expand Up @@ -130,21 +122,38 @@ public void launch() throws IOException {
}

System.out.println("Installing Fabric " + gameVersion.getVersion() + " (" + gameVersion.getCodeName() + ")");
String[] cmd2 = new String[]{"java", "-jar", "cache/" + getJarFile().getName(), "client", "-dir" + "\"" + mcPath.toAbsolutePath() + "\"", "-mcversion", gameVersion.codeName, launcherThing};


// help - Opens this menu
//client -dir <install dir> -mcversion <minecraft version, default latest> -loader <loader version, default latest> -launcher [win32, microsoft_store]
//server -dir <install dir, default current dir> -mcversion <minecraft version, default latest> -loader <loader version, default latest> -downloadMinecraft
try {
Process process = Runtime.getRuntime().exec(cmd2);

String[] cmd2 = new String[]{"java", "-jar", "cache/" + getJarFile().getName(), "client", "-dir" + "\"" + mcPath.toAbsolutePath() + "\"", "-mcversion", gameVersion.codeName, launcherThing};
BufferedInputStream successBufferedInputStream = new BufferedInputStream(process.getInputStream());
BufferedInputStream errorBufferedInputStream = new BufferedInputStream(process.getErrorStream());
synchronized (process) {
process.waitFor();
}

boolean hasError = false;

if (errorBufferedInputStream.available() != 0) {
errorBufferedInputStream.close();
hasError = true;
}

if (process.exitValue() != 0) hasError = true;
if (successBufferedInputStream.available() == 0) hasError = true;

Process process = Runtime.getRuntime().exec(cmd2);
// instead of checking if is done, ykyk
// lets just show them its already done!
// who would notice!!!
if(hasError) {
this.error();
} else {
this.showDone(gameVersion);
}

} catch (Exception ignored) {
this.error();
}

this.showDone(gameVersion);
buttonInstall.setEnabled(true);
}

Expand All @@ -160,15 +169,18 @@ private File getJarFile() throws IOException {
Files.createDirectories(cacheDir);
Utils.downloadFile(url, cacheDir.resolve(fileName));
return new File(cacheDir.toFile(), fileName);
}

if(cacheDir.toFile().listFiles().length == 0) {
Files.delete(cacheDir);
return getJarFile();
}

for(File file : cacheDir.toFile().listFiles()) {
if(file.getName().equals(fileName)) {
return file;
} else {
file.delete();
Files.createDirectory(cacheDir);
Utils.downloadFile(url, cacheDir.resolve(fileName));
return new File(cacheDir.toFile(), fileName);
}
Expand All @@ -178,7 +190,18 @@ private File getJarFile() throws IOException {
}

private String getFabricVersion() {
return "0.11.1";

String version = "0.11.1";

try {
String response = HttpAPI.get("https://api.github.com/repos/rizecookey/fabric-installer/releases/latest");
Json jsonObject = Json.read(response);
Json jsonVersion = jsonObject.at("tag_name");

if(jsonVersion == null || !jsonVersion.isString()) return version;

return jsonVersion.asString();
} catch (Exception ignored) { return version; }
}

private void showDone(FabricVersionHandler.GameVersion gameVersion) {
Expand All @@ -195,5 +218,29 @@ private void showDone(FabricVersionHandler.GameVersion gameVersion) {

if(result == JOptionPane.NO_OPTION) InstallerGUI.instance.pane.setSelectedComponent(InstallerGUI.instance.vanilla);
}

private void error() {
Object[] options = {"OK", "Cancel"};
int result = JOptionPane.showOptionDialog(null,
MessageFormat.format(Main.BUNDLE.getString("installer.prompt.install.error"), ""),
Main.BUNDLE.getString("installer.title"),
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.ERROR_MESSAGE,
null,
options,
options[0]
);

if(result == JOptionPane.OK_OPTION) {
try {
InstallerGUI.instance.dispose();
Main.main(new String[]{});
} catch (Exception ignored) {
System.exit(0);
}
}

buttonInstall.setEnabled(true);
}
}

2 changes: 2 additions & 0 deletions src/main/resources/lang/installer.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ installer.prompt.launcher.type.win32=Standalone (Win32)
installer.title=Combat Test Installer
installer.option.create.profile=Create profile

installer.prompt.install.error=An error has occurred, click "OK" to restart the program or click "Cancel" to continue.

installer.button.fabric=Open the Fabric Installer
installer.tab.vanilla=Vanilla
installer.tab.fabric=Fabric

0 comments on commit b6e4731

Please sign in to comment.