Skip to content

Commit

Permalink
listen, it's scuffed but it works 👍 (fabric)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-coded committed Oct 21, 2023
1 parent ed51370 commit 7dbbc87
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 21 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store


### cache ###
cache/
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

group = 'com.nexia'
version = '1.0.1'
version = '1.0.2'

repositories {
mavenCentral()
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/com/nexia/installer/InstallerGUI.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.nexia.installer;

import com.nexia.installer.util.InstallerHelper;
import com.nexia.installer.util.fabric.FabricInstallerHelper;

import javax.swing.*;
import java.awt.*;
Expand All @@ -11,13 +12,18 @@
public class InstallerGUI extends JFrame {
public static InstallerGUI instance;

public JPanel vanilla;

public JPanel fabric;

public JTabbedPane pane;

public InstallerGUI() {
InstallerHelper helper = new InstallerHelper();
JPanel panel = helper.setPanel(this);
this.vanilla = new InstallerHelper().setPanel(this);
this.fabric = new FabricInstallerHelper().setPanel(this);

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

add(panel);
instance = this;
}

Expand All @@ -43,14 +49,23 @@ public static void load() throws UnsupportedLookAndFeelException, ClassNotFoundE
}

InstallerGUI gui = new InstallerGUI();

gui.pane = new JTabbedPane(JTabbedPane.TOP);

gui.pane.addTab(Main.BUNDLE.getString("installer.tab.vanilla"), gui.vanilla);
gui.pane.addTab(Main.BUNDLE.getString("installer.tab.fabric"), gui.fabric);

gui.setContentPane(gui.pane);


gui.updateSize(true);
gui.setTitle(Main.BUNDLE.getString("installer.title"));
gui.setIconImage(Main.icon);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
}

public void updateSize(boolean updateMinimum) {
private void updateSize(boolean updateMinimum) {
if (updateMinimum) setMinimumSize(null);
setPreferredSize(null);
pack();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/nexia/installer/util/InstallerHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public JPanel setPanel(InstallerGUI gui) {
buttonInstall.addActionListener(e -> {
buttonInstall.setEnabled(false);
try {
install();
launch();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
Expand Down Expand Up @@ -107,7 +107,7 @@ public void addRow(Container parent, GridBagConstraints c, boolean last, String
c.gridx = 0;
}

private void install() throws IOException {
public void launch() throws IOException {

String stringGameVersion = (String) gameVersionComboBox.getSelectedItem();
VersionHandler.GameVersion gameVersion = VersionHandler.identifyGameVersion(stringGameVersion);
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/nexia/installer/util/InstallerUtils.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.nexia.installer.util;

import com.nexia.installer.InstallerGUI;
import com.nexia.installer.Main;
import com.nexia.installer.game.VersionHandler;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -115,7 +113,7 @@ public static void install(Path mcDir, VersionHandler.GameVersion gameVersion) {
}).start();
}

private static ProfileInstaller.LauncherType showLauncherTypeSelection() {
public static ProfileInstaller.LauncherType showLauncherTypeSelection() {
Object[] options = { Main.BUNDLE.getString("installer.prompt.launcher.type.xbox"), Main.BUNDLE.getString("installer.prompt.launcher.type.win32")};

int result = JOptionPane.showOptionDialog(null,
Expand All @@ -135,7 +133,7 @@ private static ProfileInstaller.LauncherType showLauncherTypeSelection() {
return result == JOptionPane.YES_OPTION ? ProfileInstaller.LauncherType.MICROSOFT_STORE : ProfileInstaller.LauncherType.WIN32;
}

private static void showDone(VersionHandler.GameVersion gameVersion) throws URISyntaxException, IOException {
private static void showDone(VersionHandler.GameVersion gameVersion) {
Object[] options = {"OK", "Install Fabric"};
int result = JOptionPane.showOptionDialog(null,
MessageFormat.format(Main.BUNDLE.getString("installer.prompt.install.done"), gameVersion.getVersion()),
Expand All @@ -147,6 +145,6 @@ private static void showDone(VersionHandler.GameVersion gameVersion) throws URIS
options[0]
);

if(result == JOptionPane.NO_OPTION && Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) Desktop.getDesktop().browse(new URI("https://github.com/rizecookey/fabric-installer/releases"));
if(result == JOptionPane.NO_OPTION) InstallerGUI.instance.pane.setSelectedComponent(InstallerGUI.instance.fabric);
}
}
10 changes: 7 additions & 3 deletions src/main/java/com/nexia/installer/util/ProfileInstaller.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,17 @@ private static Json createProfile(String name) {
}

public enum LauncherType {
WIN32("launcher_profiles.json"),
MICROSOFT_STORE("launcher_profiles_microsoft_store.json");
WIN32("win32", "launcher_profiles.json"),
MICROSOFT_STORE("microsoft_store", "launcher_profiles_microsoft_store.json");

public final String profileJsonName;

LauncherType(String profileJsonName) {
public final String name;

LauncherType(String name, String profileJsonName) {
this.name = name;
this.profileJsonName = profileJsonName;
}

}
}
6 changes: 2 additions & 4 deletions src/main/java/com/nexia/installer/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Utils {
public static void extractZip(Path file, Path path) throws IOException {
ZipInputStream zipIn = new ZipInputStream(Files.newInputStream(Paths.get(file.toString())));
ZipEntry entry = zipIn.getNextEntry();
String filePath = "";
String filePath;
// iterates over entries in the zip file
while (entry != null) {
filePath = path + File.separator + entry.getName();
Expand Down Expand Up @@ -136,9 +136,7 @@ public static String getProfileIcon() {

return "data:image/png;base64," + Base64.getEncoder().encodeToString(Arrays.copyOf(ret, offset));
} catch (IOException e) {
e.printStackTrace();
return "furnace"; // Fallback to furnace icon if we cant load Nexia icon.
}

return "furnace"; // Fallback to furnace icon if we cant load CTS icon.
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package com.nexia.installer.util.fabric;

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 javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.List;

public class FabricInstallerHelper extends InstallerHelper {
public static JButton buttonInstall;

public static JButton buttonFabric;
public JComboBox<String> gameVersionComboBox;
public JTextField installLocation;
public JButton selectFolderButton;

public static JCheckBox createProfile;

@Override
public JPanel setPanel(InstallerGUI gui) {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(new EmptyBorder(4, 4, 4, 4));

GridBagConstraints c = new GridBagConstraints();
c.insets = new Insets(6, 4, 6, 4);
c.gridx = c.gridy = 0;

addRow(panel, c, "installer.prompt.game.version",
gameVersionComboBox = new JComboBox<>(),
createSpacer()
);

for(FabricVersionHandler.GameVersion version : FabricVersionHandler.versions) {
gameVersionComboBox.addItem(version.getVersion());
}

addRow(panel, c, "installer.prompt.select.location",
installLocation = new JTextField(20),
selectFolderButton = new JButton());
selectFolderButton.setText("...");
selectFolderButton.setPreferredSize(new Dimension(installLocation.getPreferredSize().height, installLocation.getPreferredSize().height));
selectFolderButton.addActionListener(e -> InstallerGUI.selectInstallLocation(() -> installLocation.getText(), s -> installLocation.setText(s)));

addRow(panel, c, null,
createProfile = new JCheckBox(Main.BUNDLE.getString("installer.option.create.profile"), true));

installLocation.setText(InstallerUtils.findDefaultInstallDir().toString());

addLastRow(panel, c,
buttonInstall = new JButton(Main.BUNDLE.getString("installer.button.install")));
buttonInstall.addActionListener(e -> {
buttonInstall.setEnabled(false);
try {
launch();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});

addLastRow(panel, c,
buttonFabric = new JButton(Main.BUNDLE.getString("installer.button.fabric")));
buttonFabric.addActionListener(e -> {
try {
Process process = Runtime.getRuntime().exec("java -jar cache/" + getJarFile().getName());
while(process.isAlive()) {
buttonFabric.setEnabled(false);
}
buttonFabric.setEnabled(true);
} catch (IOException ex) {
try {
getJarFile().delete();
} catch (IOException exc) {
// what the fuck
exc.printStackTrace();
}

buttonFabric.setEnabled(false);
}
});

return panel;
}

@Override
public void launch() throws IOException {

String stringGameVersion = (String) gameVersionComboBox.getSelectedItem();
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"));
}

final ProfileInstaller profileInstaller = new ProfileInstaller(mcPath);
ProfileInstaller.LauncherType launcherType;
String launcherThing = "";

// can you tell i have no fucking idea what im doing?
if(InstallerHelper.createProfile.isSelected()) {
List<ProfileInstaller.LauncherType> types = profileInstaller.getInstalledLauncherTypes();

if (types.isEmpty()) {
throw new RuntimeException(Main.BUNDLE.getString("installer.exception.no.launcher.profile"));
} else {
launcherType = InstallerUtils.showLauncherTypeSelection();

if (launcherType == null) {
return;
}

launcherThing = "-launcher " + launcherType.name;
}
}

System.out.println("Installing Fabric " + gameVersion.getVersion() + " (" + gameVersion.getCodeName() + ")");


// 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

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


Process process = Runtime.getRuntime().exec(cmd2);
// instead of checking if is done, ykyk
// lets just show them its already done!
// who would notice!!!

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


private File getJarFile() throws IOException {
Path currentDir = new File("").toPath();
Path cacheDir = currentDir.resolve("cache");

String fileName = "fabric-installer-" + getFabricVersion() + ".jar";
URL url = new URL("https://github.com/rizecookey/fabric-installer/releases/latest/download/" + fileName);

if(!Files.exists(cacheDir)) {
Files.createDirectories(cacheDir);
Utils.downloadFile(url, cacheDir.resolve(fileName));
return new File(cacheDir.toFile(), fileName);

}

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);
}
}

return null;
}

private String getFabricVersion() {
return "0.11.1";
}

private void showDone(FabricVersionHandler.GameVersion gameVersion) {
Object[] options = {"OK", "Install Vanilla"};
int result = JOptionPane.showOptionDialog(null,
MessageFormat.format(Main.BUNDLE.getString("installer.prompt.install.done.fabric"), gameVersion.getVersion()),
Main.BUNDLE.getString("installer.title"),
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null,
options,
options[0]
);

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

Loading

0 comments on commit 7dbbc87

Please sign in to comment.