diff --git a/pom.xml b/pom.xml index 5902d47..20d8303 100644 --- a/pom.xml +++ b/pom.xml @@ -45,7 +45,7 @@ false - net.kyrptonaught.ToolBox.Main + net.kyrptonaught.ToolBoxBootstrap.Main diff --git a/src/main/java/net/kyrptonaught/ToolBox/AutoHash.java b/src/main/java/net/kyrptonaught/ToolBox/AutoHash.java deleted file mode 100644 index ad38337..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/AutoHash.java +++ /dev/null @@ -1,39 +0,0 @@ -package net.kyrptonaught.ToolBox; - -import com.google.gson.JsonObject; -import net.kyrptonaught.ToolBox.IO.ConfigLoader; -import net.kyrptonaught.ToolBox.IO.FileHelper; -import net.kyrptonaught.ToolBox.IO.GithubHelper; -import net.kyrptonaught.ToolBox.configs.BranchConfig; - -import java.io.InputStream; -import java.nio.file.Path; - -public class AutoHash { - - public static void autoHash() { - String toolboxJson = FileHelper.readFile(Path.of("toolbox.json")); - BranchConfig config = ConfigLoader.parseToolboxConfig(toolboxJson); - - for (BranchConfig.Dependency dependency : config.dependencies) { - if (dependency.gitRepo) { - String apiCall = GithubHelper.convertRepoToApiCall(dependency.url); - JsonObject response = FileHelper.download(apiCall, JsonObject.class); - String hash = response.getAsJsonObject("commit").getAsJsonPrimitive("sha").getAsString(); - dependency.autoGeneratedHash = hash; - System.out.println("Generated Git Hash for " + dependency.getDisplayName() + ": " + hash); - } else { - try (InputStream in = FileHelper.openFileOrURL(dependency.url)) { - byte[] data = in.readAllBytes(); - String hash = FileHelper.hashFile(data); - dependency.autoGeneratedHash = hash; - System.out.println("Generated Hash for " + dependency.getDisplayName() + ": " + hash); - } catch (Exception e) { - e.printStackTrace(); - } - } - } - - FileHelper.writeFile(Path.of("toolbox.json"), ConfigLoader.serializeToolboxInstall(config)); - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/Automation.java b/src/main/java/net/kyrptonaught/ToolBox/Automation.java deleted file mode 100644 index 719ba47..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/Automation.java +++ /dev/null @@ -1,87 +0,0 @@ -package net.kyrptonaught.ToolBox; - -import net.kyrptonaught.ToolBox.IO.ConfigLoader; -import net.kyrptonaught.ToolBox.IO.EulaChecker; -import net.kyrptonaught.ToolBox.IO.FileHelper; -import net.kyrptonaught.ToolBox.IO.GithubHelper; -import net.kyrptonaught.ToolBox.Menu.State; -import net.kyrptonaught.ToolBox.configs.BranchConfig; -import net.kyrptonaught.ToolBox.configs.BranchesConfig; -import net.kyrptonaught.ToolBox.holders.InstalledServerInfo; - -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.nio.file.Path; - -public class Automation { - static BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); - - public static void run() { - if (CMDArgsParser.installServer()) { - installServer(); - return; - } - - if (CMDArgsParser.updateServer() || CMDArgsParser.launchServer()) { - InstalledServerInfo server = Menu.getServerFromName(CMDArgsParser.getTargetServer()); - - if (server == null) { - System.out.println(); - System.out.println("This server is invalid."); - System.out.println("Returning to menu."); - Menu.pressEnterToCont(input); - return; - } - - Menu.setState(Menu.State.EXISTING_INSTALL, server); - - if (CMDArgsParser.updateServer()) - Executer.updateServer(server); - - if (CMDArgsParser.launchServer()) - Executer.startServer(server); - } - } - - public static void installServer() { - BranchesConfig.BranchInfo branchInfo = CMDArgsParser.getNewServerBranch(); - - String url = GithubHelper.convertRepoToToolboxConfig(branchInfo.url); - BranchConfig branch = ConfigLoader.parseToolboxConfig(FileHelper.download(url)); - - if (branch == null) { - System.out.println(); - System.out.println("This branch is invalid."); - System.out.println("Returning to menu."); - Menu.pressEnterToCont(input); - return; - } - - InstalledServerInfo serverInfo = new InstalledServerInfo(branch, branchInfo); - - String name = CMDArgsParser.getTargetServer(); - if (name != null) serverInfo.setName(name); - serverInfo.setPath(); - - int allocatedRam = CMDArgsParser.getNewServerRam(); - if (allocatedRam < 1) allocatedRam = 3; - serverInfo.setRAMArgs(allocatedRam); - - Installer.installAndCheckForUpdates(serverInfo); - if (CMDArgsParser.doesNewServerAgreeToEULA()) { - Path eulaFile = serverInfo.getPath().resolve("eula.txt"); - - EulaChecker.agreeToEula(eulaFile); - } else if (!CMDArgsParser.unattendedInstall()) { - Menu.checkEula(input, serverInfo); - } else System.out.println("Did not agree to Mojang's EULA"); - - if (CMDArgsParser.unattendedInstall()) { - Menu.setState(State.EXIT); - } else { - Menu.pressEnterToCont(input); - - Menu.setState(State.EXISTING_INSTALL, serverInfo); - } - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/CMDArgsParser.java b/src/main/java/net/kyrptonaught/ToolBox/CMDArgsParser.java deleted file mode 100644 index 8b0b6d5..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/CMDArgsParser.java +++ /dev/null @@ -1,114 +0,0 @@ -package net.kyrptonaught.ToolBox; - -import net.kyrptonaught.ToolBox.IO.ConfigLoader; -import net.kyrptonaught.ToolBox.IO.FileHelper; -import net.kyrptonaught.ToolBox.configs.BranchesConfig; - -public class CMDArgsParser { - - public static String[] args; - - public static void setArgs(String[] args) { - CMDArgsParser.args = args; - } - - public static boolean containsArgs(String arg) { - for (String str : args) { - if (str.equalsIgnoreCase(arg)) return true; - } - return false; - } - - public static boolean containsOrBeginsArgs(String arg) { - for (String str : args) { - if (str.equalsIgnoreCase(arg) || str.startsWith(arg)) return true; - } - return false; - } - - public static boolean containsServer() { - return containsArgs("--server"); - } - - public static String getTargetServer() { - for (int i = 0; i < args.length - 1; i++) { - if (args[i].equalsIgnoreCase("--server") && !args[i + 1].startsWith("--")) { - return args[i + 1]; - } - } - return null; - } - - public static boolean autoUpdateToolbox() {return containsArgs("--autoUpdateToolbox");} - - public static boolean updateServer() { - return containsArgs("--updateServer"); - } - - public static boolean launchServer() { - return containsArgs("--launchServer"); - } - - public static boolean skipSplash() { - return containsArgs("--skipSplash"); - } - - public static boolean autoExit() { - return containsArgs("--autoExit"); - } - - public static boolean autoRestart() { - return containsArgs("--autoRestart"); - } - - public static boolean updateAll() { - return containsArgs("--updateAll"); - } - - public static boolean installServer() { - return containsArgs("--installServer"); - } - - public static int getNewServerRam() { - for (int i = 0; i < args.length - 1; i++) { - if (args[i].equalsIgnoreCase("--newServerRam") && !args[i + 1].startsWith("--")) { - return Integer.parseInt(args[i + 1]); - } - } - return 3; - } - - public static BranchesConfig.BranchInfo getNewServerBranch() { - BranchesConfig availableBranches = ConfigLoader.parseBranches(FileHelper.download("https://raw.githubusercontent.com/Legacy-Edition-Minigames/ToolBox/java/testConfigs/TestBranches.json")); - - for (int i = 0; i < args.length - 1; i++) { - if (args[i].equalsIgnoreCase("--newServerBranch") && !args[i + 1].startsWith("--")) { - String branchURL = args[i + 1]; - - for (BranchesConfig.BranchInfo branch : availableBranches.branches) { - if (branchURL.equals(branch.url)) { - return branch; - } - } - } - } - System.out.println("Using default branch"); - - BranchesConfig.BranchInfo defaultBranch = availableBranches.branches.get(0); - - return defaultBranch; - } - - public static boolean doesNewServerAgreeToEULA() { - for (int i = 0; i < args.length - 1; i++) { - if (args[i].equalsIgnoreCase("--newServerEULA") && !args[i + 1].startsWith("--")) { - return Boolean.parseBoolean(args[i + 1]); - } - } - return false; - } - - public static boolean unattendedInstall() { - return containsArgs("--unattendedInstall"); - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/Executer.java b/src/main/java/net/kyrptonaught/ToolBox/Executer.java deleted file mode 100644 index 85cc2ed..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/Executer.java +++ /dev/null @@ -1,41 +0,0 @@ -package net.kyrptonaught.ToolBox; - -import net.kyrptonaught.ToolBox.IO.ConfigLoader; -import net.kyrptonaught.ToolBox.IO.FileHelper; -import net.kyrptonaught.ToolBox.IO.GithubHelper; -import net.kyrptonaught.ToolBox.configs.BranchConfig; -import net.kyrptonaught.ToolBox.holders.InstalledServerInfo; -import net.kyrptonaught.ToolBox.holders.RunningServer; - -public class Executer { - - public static void startServer(InstalledServerInfo serverInfo) { - System.out.println("Starting server: " + serverInfo.getLaunchCMD()); - RunningServer runningServer = ServerRunner.runServer(serverInfo); - - System.out.println(); - if (runningServer.isRunning()) { - System.out.println("Server backgrounded..."); - - } else { - System.out.println("Server stopped..."); - - if (CMDArgsParser.autoRestart()) { - startServer(serverInfo); - } - - if (CMDArgsParser.autoExit()) { - Menu.setState(Menu.State.EXIT); - return; - } - } - System.out.println(); - } - - public static void updateServer(InstalledServerInfo serverInfo) { - String url = GithubHelper.convertRepoToToolboxConfig(serverInfo.getBranchInfo().url); - BranchConfig branch = ConfigLoader.parseToolboxConfig(FileHelper.download(url)); - serverInfo.updateBranchConfig(branch); - Installer.installAndCheckForUpdates(serverInfo); - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/IO/ConfigLoader.java b/src/main/java/net/kyrptonaught/ToolBox/IO/ConfigLoader.java deleted file mode 100644 index 926b482..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/IO/ConfigLoader.java +++ /dev/null @@ -1,35 +0,0 @@ -package net.kyrptonaught.ToolBox.IO; - -import com.google.gson.Gson; -import net.kyrptonaught.ToolBox.configs.BranchConfig; -import net.kyrptonaught.ToolBox.configs.BranchesConfig; -import net.kyrptonaught.ToolBox.holders.InstalledDependencyInfo; -import net.kyrptonaught.ToolBox.holders.InstalledServerInfo; - -public class ConfigLoader { - public static Gson gson = new Gson().newBuilder() - .setLenient() - .disableHtmlEscaping() - .setPrettyPrinting() - .create(); - - public static BranchesConfig parseBranches(String json) { - return gson.fromJson(json, BranchesConfig.class); - } - - public static BranchConfig parseToolboxConfig(String json) { - return gson.fromJson(json, BranchConfig.class); - } - - public static InstalledDependencyInfo parseInstalledDependency(String json) { - return gson.fromJson(json, InstalledDependencyInfo.class); - } - - public static InstalledServerInfo parseToolboxInstall(String json) { - return gson.fromJson(json, InstalledServerInfo.class); - } - - public static String serializeToolboxInstall(Object config) { - return gson.toJson(config); - } -} \ No newline at end of file diff --git a/src/main/java/net/kyrptonaught/ToolBox/IO/EulaChecker.java b/src/main/java/net/kyrptonaught/ToolBox/IO/EulaChecker.java deleted file mode 100644 index 939f3ec..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/IO/EulaChecker.java +++ /dev/null @@ -1,29 +0,0 @@ -package net.kyrptonaught.ToolBox.IO; - -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.Properties; - -public class EulaChecker { - public static boolean checkEulaAgreement(Path eulaFile) { - try (InputStream inputStream = Files.newInputStream(eulaFile)) { - Properties properties = new Properties(); - properties.load(inputStream); - return Boolean.parseBoolean(properties.getProperty("eula", "false")); - } catch (Exception exception) { - } - return false; - } - - public static void agreeToEula(Path eulaFile) { - try (OutputStream outputStream = Files.newOutputStream(eulaFile)) { - Properties properties = new Properties(); - properties.setProperty("eula", "true"); - properties.store(outputStream, "By changing the setting below to TRUE you are indicating your agreement to our EULA (https://aka.ms/MinecraftEULA)."); - } catch (Exception exception) { - exception.printStackTrace(); - } - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/IO/GithubHelper.java b/src/main/java/net/kyrptonaught/ToolBox/IO/GithubHelper.java deleted file mode 100644 index 0e4bf96..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/IO/GithubHelper.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.kyrptonaught.ToolBox.IO; - -public class GithubHelper { - public static String convertRepoToZipball(String repo) { - return repo.replace("/tree/", "/archive/refs/heads/") + ".zip"; - } - - public static String convertRepoToToolboxConfig(String repo) { - return repo.replace("github.com", "raw.githubusercontent.com").replace("tree/", "") + "/.toolbox/toolbox.json"; - } - - public static String convertRepoToApiCall(String repo) { - return repo.replace("github.com", "api.github.com/repos").replace("/tree/", "/branches/"); - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/Installer.java b/src/main/java/net/kyrptonaught/ToolBox/Installer.java deleted file mode 100644 index beefaf4..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/Installer.java +++ /dev/null @@ -1,207 +0,0 @@ -package net.kyrptonaught.ToolBox; - -import net.kyrptonaught.ToolBox.IO.ConfigLoader; -import net.kyrptonaught.ToolBox.IO.FileHelper; -import net.kyrptonaught.ToolBox.IO.GithubHelper; -import net.kyrptonaught.ToolBox.configs.BranchConfig; -import net.kyrptonaught.ToolBox.holders.InstalledDependencyInfo; -import net.kyrptonaught.ToolBox.holders.InstalledServerInfo; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.List; -import java.util.stream.Stream; - -public class Installer { - - public static void saveInstalledServerInfo(InstalledServerInfo serverInfo) { - FileHelper.writeFile(serverInfo.getMetaPath().resolve("toolbox.json"), ConfigLoader.serializeToolboxInstall(serverInfo)); - } - - public static List detectInstalls() { - Path installPath = Path.of("installs"); - - List configs = new ArrayList<>(); - try (Stream files = Files.walk(installPath, 1)) { - files.forEach(path -> { - if (Files.isDirectory(path) && Files.exists(path.resolve(".toolbox").resolve("meta").resolve("toolbox.json"))) { - InstalledServerInfo serverInfo = ConfigLoader.parseToolboxInstall(FileHelper.readFile(path.resolve(".toolbox").resolve("meta").resolve("toolbox.json"))); - serverInfo.setPath(path); - configs.add(serverInfo); - } - }); - } catch (IOException ignored) { - } - - return configs; - } - - public static void installAndCheckForUpdates(InstalledServerInfo serverInfo) { - FileHelper.createDir(serverInfo.getPath()); - - FileHelper.createDir(serverInfo.getMetaPath()); - FileHelper.createDir(serverInfo.getDownloadPath()); - FileHelper.createDir(serverInfo.getInstalledDependencyPath()); - - System.out.println("Checking dependencies..."); - installDependencies(serverInfo); - - saveInstalledServerInfo(serverInfo); - System.out.println("Dependencies done"); - } - - public static void verifyInstall(InstalledServerInfo serverInfo) { - for (BranchConfig.Dependency dependency : serverInfo.getDependencies()) { - System.out.println("Checking files from dependency: " + dependency.getDisplayName()); - InstalledDependencyInfo installedDependency = getInstalledDependency(serverInfo, dependency); - if (installedDependency.installedFiles != null) - for (String file : installedDependency.installedFiles) - if (!FileHelper.exists(serverInfo.getPath().resolve(file))) { - replaceMissingFiles(serverInfo, installedDependency); - break; - } - } - } - - public static void packageInstall(InstalledServerInfo serverInfo) { - FileHelper.zipDirectory(serverInfo.getPath(), Path.of("packaged/" + serverInfo.getName() + ".toolbox")); - } - - public static String installPackage(Path path) { - InstalledServerInfo serverInfo = ConfigLoader.parseToolboxInstall(FileHelper.readFileFromZip(path, ".toolbox\\meta\\toolbox.json")); - String name = serverInfo.getName() + " (Imported)"; - - //todo improve this conflict logic - while (Menu.getServerFromName(name) != null) { - name += " (duplicate)"; - } - serverInfo.setName(name); - serverInfo.setPath(); - - FileHelper.createDir(serverInfo.getPath()); - FileHelper.unzipFile(path, serverInfo.getPath(), false); - saveInstalledServerInfo(serverInfo); - - return name; - } - - private static void installDependencies(InstalledServerInfo serverInfo) { - for (BranchConfig.Dependency dependency : serverInfo.getDependencies()) { - - if (dependency.location.startsWith("/")) - dependency.location = dependency.location.substring(1); - - System.out.print("Checking " + dependency.getDisplayName() + "..."); - - InstalledDependencyInfo installedDependency = getInstalledDependency(serverInfo, dependency); - - String hash = dependency.autoGeneratedHash; - String existingHash = installedDependency.hash; - - if (hash != null && !hash.equals(existingHash)) { - System.out.print("downloading..."); - installFile(serverInfo, installedDependency, hash); - System.out.println("installed"); - } else { - System.out.println("Already exists"); - } - } - detectRemovedDependencies(serverInfo); - } - - private static void installFile(InstalledServerInfo serverInfo, InstalledDependencyInfo dependency, String hash) { - Path downloadPath = serverInfo.getDownloadPath(dependency); - Path destination = serverInfo.getDependencyInstallPath(dependency); - FileHelper.createDir(destination); - - if (dependency.gitRepo) { - FileHelper.download(GithubHelper.convertRepoToZipball(dependency.url), downloadPath); - } else { - FileHelper.download(dependency.url, downloadPath); - } - - clearOldFiles(serverInfo, dependency.installedFiles); - - List installedFiles; - if (dependency.unzip) { - installedFiles = FileHelper.unzipFile(downloadPath, destination, true); - } else { - installedFiles = FileHelper.copyFile(downloadPath, destination.resolve(dependency.name)); - } - - dependency.installedFiles = installedFiles; - dependency.hash = hash; - FileHelper.writeFile(serverInfo.getInstalledDependencyPath(dependency), ConfigLoader.serializeToolboxInstall(dependency)); - } - - private static void replaceMissingFiles(InstalledServerInfo serverInfo, InstalledDependencyInfo dependency) { - Path downloadPath = serverInfo.getDownloadPath(dependency); - Path destination = serverInfo.getDependencyInstallPath(dependency); - FileHelper.createDir(destination); - - if (dependency.unzip) { - List installedFiles = new ArrayList<>(List.copyOf(dependency.installedFiles)); - if (installedFiles != null) { - Path unzipPath = serverInfo.getTempPath(dependency); - FileHelper.unzipFile(downloadPath, unzipPath, true); - installedFiles.sort(Comparator.naturalOrder()); - for (String file : installedFiles) { - Path path = serverInfo.getPath().resolve(file); - if (!FileHelper.exists(path)) { - System.out.println("Replacing file: " + file); - - if (Files.isDirectory(path)) { - FileHelper.createDir(path); - } else { - FileHelper.copyFile(unzipPath.resolve(file), path); - } - } - } - FileHelper.deleteDirectory(serverInfo.getTempPath()); - } - } else { - System.out.println("Replacing file: " + destination.resolve(dependency.name)); - FileHelper.copyFile(downloadPath, destination.resolve(dependency.name)); - } - } - - private static void detectRemovedDependencies(InstalledServerInfo serverInfo) { - try (Stream dependencyFiles = Files.list(serverInfo.getInstalledDependencyPath())) { - for (Path dependencyPath : dependencyFiles.toList()) { - InstalledDependencyInfo installedDependency = ConfigLoader.parseInstalledDependency(FileHelper.readFile(dependencyPath)); - boolean found = false; - for (BranchConfig.Dependency dependency : serverInfo.getDependencies()) { - if (dependency.name.equals(installedDependency.name)) { - found = true; - break; - } - } - if (!found) { - System.out.println("Removing deleted dependency: " + installedDependency.getDisplayName()); - clearOldFiles(serverInfo, installedDependency.installedFiles); - FileHelper.delete(dependencyPath); - } - } - } catch (Exception e) { - e.printStackTrace(); - } - } - - private static void clearOldFiles(InstalledServerInfo serverInfo, List previousInstalledFiles) { - if (previousInstalledFiles != null) { - for (String string : previousInstalledFiles) { - FileHelper.delete(serverInfo.getPath().resolve(string)); - } - } - } - - private static InstalledDependencyInfo getInstalledDependency(InstalledServerInfo serverInfo, BranchConfig.Dependency dependency) { - if (FileHelper.exists(serverInfo.getInstalledDependencyPath(dependency))) { - return ConfigLoader.parseInstalledDependency(FileHelper.readFile(serverInfo.getInstalledDependencyPath(dependency))); - } - return new InstalledDependencyInfo(dependency); - } -} \ No newline at end of file diff --git a/src/main/java/net/kyrptonaught/ToolBox/Main.java b/src/main/java/net/kyrptonaught/ToolBox/Main.java deleted file mode 100644 index f280f97..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/Main.java +++ /dev/null @@ -1,19 +0,0 @@ -package net.kyrptonaught.ToolBox; - -import java.io.BufferedReader; -import java.io.InputStreamReader; - -public class Main { - //Γûä - public static void main(String[] args) { - if (args.length > 0 && args[0].equals("--autoHash")) { - AutoHash.autoHash(); - } else if (args.length > 0 && args[0].equals("--runToolbox")) { - Menu.startStateMachine(args); - } else { - BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); - CMDArgsParser.setArgs(args); - Menu.checkForUpdate(input); - } - } -} \ No newline at end of file diff --git a/src/main/java/net/kyrptonaught/ToolBox/Menu.java b/src/main/java/net/kyrptonaught/ToolBox/Menu.java deleted file mode 100644 index b827951..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/Menu.java +++ /dev/null @@ -1,646 +0,0 @@ -package net.kyrptonaught.ToolBox; - -import net.kyrptonaught.ToolBox.IO.*; -import net.kyrptonaught.ToolBox.configs.BranchConfig; -import net.kyrptonaught.ToolBox.configs.BranchesConfig; -import net.kyrptonaught.ToolBox.holders.InstalledServerInfo; -import net.kyrptonaught.ToolBox.holders.RunningServer; - -import java.awt.*; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URI; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.HashMap; -import java.util.List; -import java.util.stream.Stream; - -public class Menu { - public static State state; - public static Object stateData; - - public static void startStateMachine(String[] args) { - Runtime.getRuntime().addShutdownHook(new Thread(() -> { - System.out.println(); - System.out.println(); - System.out.println("SHUTTING DOWN"); - System.out.println("Attempting to stop running servers"); - ServerRunner.exit(); - })); - BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); - - CMDArgsParser.setArgs(args); - - setState(CMDArgsParser.skipSplash() ? State.MENU : State.SPLASH); - - Automation.run(); - - clearConsole(); - - while (true) { - clearConsole(); - switch (state) { - case SPLASH -> splashState(input); - case MENU -> menuState(input); - case EXISTING_INSTALL -> existingMenu(input); - case RUNNING_INSTALL -> runningMenu(input); - case INSTALLER -> installMenu(input); - case IMPORT -> importMenu(input); - case EXIT -> System.exit(0); - } - } - } - - public static void splashState(BufferedReader input) { - System.out.println(" ▄▄▄ ▄▄▄▄▄▄▄ ▄▄ ▄▄ ▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄▄▄ ▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄▄ ▄▄ "); - System.out.println("█ █ █ █ █▄█ █ █ █ █ █ █ █ ▄ █ █ █▄█ █"); - System.out.println("█ █ █ ▄▄▄█ █ █▄ ▄█ ▄ █ ▄ █ █ █ █▄█ █ ▄ █ █"); - System.out.println("█ █ █ █▄▄▄█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █"); - System.out.println("█ █▄▄▄█ ▄▄▄█ █ █ █ █ █▄█ █ █▄█ █ █▄▄▄█ ▄ ██ █▄█ ██ █ "); - System.out.println("█ █ █▄▄▄█ ██▄██ █ █ █ █ █ █ █ █▄█ █ █ ▄ █"); - System.out.println("█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄█ █▄█ █▄▄▄█ █▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄▄▄▄▄▄█▄▄█ █▄▄█"); - System.out.println(); - System.out.println("Welcome to LEM-ToolBox!"); - System.out.println(); - System.out.println("LEM-ToolBox is a tool designed to make it easy for users to install, update and customize their own LEM instance."); - System.out.println(); - System.out.println("If you encounter any problem, try performing a clean reinstall or contact us on our Discord."); - System.out.println("LEM-ToolBox 2.0 created by Kyrptonaught"); - System.out.println("Legacy Edition Minigames created by DBTDerpbox & Kyrptonaught"); - System.out.println("Consider donating at Patreon! www.legacyminigames.net/patreon"); - System.out.println(); - System.out.println("Have fun!"); - System.out.println(); - - pressEnterToCont(input); - - setState(State.MENU); - } - - public static void menuState(BufferedReader input) { - System.out.println("Loading Servers..."); - System.out.println(); - - List installedServers = Installer.detectInstalls(); - List runningServers = ServerRunner.getRunningServers(); - BranchesConfig branches = ConfigLoader.parseBranches(FileHelper.download("https://raw.githubusercontent.com/Legacy-Edition-Minigames/ToolBox/java/testConfigs/TestBranches.json")); - - HashMap options = new HashMap<>(); - - clearConsole(); - - System.out.println("Select the server you would like to use"); - int serverOptions = 0; - - System.out.println(); - System.out.println("Installed Servers"); - if (!installedServers.isEmpty()) { - for (InstalledServerInfo serverInfo : installedServers) { - if (runningServers.stream().anyMatch(runningServer -> runningServer.serverInfo.getName().equals(serverInfo.getName()))) - continue; - serverOptions++; - System.out.println(serverOptions + ". " + serverInfo.getName() + " (" + serverInfo.getBranchInfo().name + ")"); - options.put(serverOptions, () -> setState(State.EXISTING_INSTALL, serverInfo)); - } - } - - if (serverOptions == 0) { - System.out.println("--NONE--"); - } - - System.out.println(); - System.out.println("Running Servers"); - if (!runningServers.isEmpty()) { - for (RunningServer runningServer : runningServers) { - serverOptions++; - System.out.println(serverOptions + ". " + runningServer.serverInfo.getName() + " (" + runningServer.serverInfo.getBranchInfo().name + ")"); - options.put(serverOptions, () -> setState(State.RUNNING_INSTALL, runningServer)); - } - } else { - System.out.println("--NONE--"); - } - - System.out.println(); - System.out.println("New Servers"); - if (!branches.branches.isEmpty()) { - for (BranchesConfig.BranchInfo branch : branches.branches) { - serverOptions++; - System.out.println(serverOptions + ". " + branch.name + " : " + branch.desc); - options.put(serverOptions, () -> setState(State.INSTALLER, branch)); - } - } else { - System.out.println("--NONE--"); - } - - System.out.println(); - System.out.println("Import Servers"); - System.out.println(++serverOptions + ". Import a packaged .toolbox server"); - options.put(serverOptions, () -> setState(State.IMPORT)); - - System.out.println(); - System.out.println("Other Options"); - System.out.println("0. Exit"); - - options.put(0, () -> setState(State.EXIT)); - - System.out.println(); - - System.out.print("Select Server: "); - int selection = readInt(input); - - - if (options.containsKey(selection)) { - options.get(selection).run(); - } - //will loop back into this menu - } - - public static void existingMenu(BufferedReader input) { - InstalledServerInfo serverInfo = (InstalledServerInfo) stateData; - - BranchesConfig.BranchInfo info = serverInfo.getBranchInfo(); - System.out.println("Server Selected: "); - System.out.println(); - System.out.println(serverInfo.getName() + " (" + info.name + ")"); - System.out.println(info.desc); - System.out.println(info.url); - System.out.println(); - System.out.println(""" - Choose an action below: - - 1. Start Server - 2. Check for Updates - 3. Verify Integrity - 4. Share Install - 5. Accept EULA - 6. Rename Server - 7. Reinstall - 8. Delete - - 0. Back - """); - - System.out.print("Action: "); - int selectedAction = readInt(input); - System.out.println(); - - if (selectedAction == 0) { - setState(State.MENU); - } else if (selectedAction == 1) { - clearConsole(); - Executer.startServer(serverInfo); - setState(State.MENU); - pressEnterToCont(input); - } else if (selectedAction == 2) { - Executer.updateServer(serverInfo); - System.out.println(); - System.out.println("Server updated."); - System.out.println(); - pressEnterToCont(input); - } else if (selectedAction == 3) { - System.out.println("Verifying install..."); - System.out.println(); - Installer.verifyInstall(serverInfo); - System.out.println(); - - System.out.println("All checks passed, server install is intact"); - System.out.println(); - pressEnterToCont(input); - } else if (selectedAction == 4) { - System.out.println("Packaging install..."); - System.out.println(); - FileHelper.createDir(Path.of("packaged")); - Installer.packageInstall(serverInfo); - System.out.println("Placed packaged server in: " + Path.of("packaged/" + serverInfo.getName() + ".toolbox")); - System.out.println(); - pressEnterToCont(input); - } else if (selectedAction == 5) { - System.out.println("Checking EULA..."); - System.out.println(); - checkEula(input, serverInfo); - } else if (selectedAction == 6) { - String newName = askServerName(input, serverInfo.getName()); - - System.out.println(); - System.out.println("Renaming to " + newName); - FileHelper.renameDirectory(serverInfo.getPath(), Path.of("installs/" + newName)); - serverInfo.setName(newName); - serverInfo.setPath(); - Installer.saveInstalledServerInfo(serverInfo); - System.out.println("Complete"); - System.out.println(); - } else if (selectedAction == 7) { - System.out.println("This server and all data associated with it will be permanently deleted before being reinstalled."); - System.out.println("This is irreversible."); - System.out.println(); - System.out.print("Are you sure you want to reinstall this server? (Y/N): "); - String deleteAgree = readLine(input); - System.out.println(); - - if (!deleteAgree.isEmpty() && deleteAgree.substring(0, 1).equalsIgnoreCase("Y")) { - FileHelper.deleteDirectory(serverInfo.getPath()); - System.out.println("Server deleted...reinstalling..."); - System.out.println(); - Installer.installAndCheckForUpdates(serverInfo); - System.out.println(); - checkEula(input, serverInfo); - System.out.println("Server reinstalled."); - System.out.println(); - pressEnterToCont(input); - } - } else if (selectedAction == 8) { - System.out.println("This server and all data associated with it will be permanently deleted."); - System.out.println("This is irreversible."); - System.out.println(); - System.out.print("Are you sure you want to delete this server? (Y/N): "); - String deleteAgree = readLine(input); - System.out.println(); - - if (!deleteAgree.isEmpty() && deleteAgree.substring(0, 1).equalsIgnoreCase("Y")) { - FileHelper.deleteDirectory(serverInfo.getPath()); - System.out.println("Server deleted."); - System.out.println(); - pressEnterToCont(input); - setState(State.MENU); - } - } - //will loop back into this menu - } - - public static void runningMenu(BufferedReader input) { - RunningServer runningServer = (RunningServer) stateData; - - BranchesConfig.BranchInfo info = runningServer.serverInfo.getBranchInfo(); - System.out.println("Server Selected: "); - System.out.println(); - System.out.println(runningServer.serverInfo.getName() + " (" + info.name + ")"); - System.out.println(info.desc); - System.out.println(info.url); - System.out.println(); - System.out.println(""" - Choose an action below: - - 1. Open Console - 2. Stop Server - - 0. Back - """); - - System.out.print("Action: "); - int selectedAction = readInt(input); - System.out.println(); - - if (selectedAction == 0) { - setState(State.MENU); - } else if (selectedAction == 1) { - clearConsole(); - System.out.println("Resuming server"); - ServerRunner.resumeServer(runningServer); - - System.out.println(); - if (runningServer.isRunning()) { - System.out.println("Server backgrounded..."); - - } else { - System.out.println("Server stopped..."); - } - System.out.println(); - - setState(State.MENU); - pressEnterToCont(input); - } else if (selectedAction == 2) { - System.out.println(); - System.out.println("Stopping Server..."); - - ServerRunner.stopServer(runningServer); - System.out.println(); - System.out.println("Server Stopped..."); - System.out.println(); - setState(State.MENU); - pressEnterToCont(input); - } - } - - public static void installMenu(BufferedReader input) { - BranchesConfig.BranchInfo branchInfo = (BranchesConfig.BranchInfo) stateData; - - System.out.println("Loading branch: " + branchInfo.name + " (" + branchInfo.url + ")"); - System.out.println(); - - String url = GithubHelper.convertRepoToToolboxConfig(branchInfo.url); - BranchConfig branch = ConfigLoader.parseToolboxConfig(FileHelper.download(url)); - - if (branch == null) { - System.out.println(); - System.out.println("This branch is invalid."); - System.out.println("Returning to menu."); - pressEnterToCont(input); - - setState(State.MENU); - return; - } - - System.out.println("Configuring server"); - System.out.println(); - - String enteredServerName = askServerName(input, branch.name); - System.out.println(); - - System.out.println("How much RAM do you want to allocate to the server?"); - System.out.println("It's recommended to use at least 3GB of RAM to ensure LEM will work as intended."); - System.out.println(); - System.out.print("RAM Allocation (GB): "); - int allocatedRam = readInt(input); - System.out.println(); - - InstalledServerInfo serverInfo = new InstalledServerInfo(branch, branchInfo); - serverInfo.setName(enteredServerName); - serverInfo.setPath(); - if (allocatedRam < 1) allocatedRam = 3; - serverInfo.setRAMArgs(allocatedRam); - - System.out.println("Creating toolbox instance in " + serverInfo.getPath()); - Installer.installAndCheckForUpdates(serverInfo); - System.out.println("Finished"); - System.out.println(); - checkEula(input, serverInfo); - pressEnterToCont(input); - - setState(State.EXISTING_INSTALL, serverInfo); - - //will loop back into this menu - } - - public static void importMenu(BufferedReader input) { - System.out.println("Import a packaged .toolbox server"); - System.out.println(); - - System.out.println(""" - Choose how to import: - - 1. Discover in /install folder - 2. Select from local file system - 3. Download from URL - - 0. Back - """); - - System.out.print("Option: "); - int selectedAction = readInt(input); - System.out.println(); - - if (selectedAction == 0) { - setState(State.MENU); - } else if (selectedAction == 1) { - System.out.println("Detecting .toolbox packages in /installs..."); - System.out.println(); - - Path installPath = Path.of("installs"); - try (Stream files = Files.walk(installPath, 1)) { - files.forEach(path -> { - if (!Files.isDirectory(path) && path.toString().endsWith(".toolbox")) { - System.out.println("Installing: " + path.getFileName().toString()); - String importedName = Installer.installPackage(path); - FileHelper.delete(path); - System.out.println("Installed as: " + importedName); - System.out.println(); - } - }); - } catch (IOException ignored) { - } - System.out.println(); - System.out.println("Finished importing"); - pressEnterToCont(input); - setState(State.MENU); - } else if (selectedAction == 2) { - System.out.println("Please enter the path of the .toolbox file (You can also drag and drop the file here)"); - System.out.print("Path: "); - String path = readLine(input).trim(); - path = path.replaceAll("^[\"|']|[\"|']$", ""); - - System.out.println(); - System.out.println("Installing"); - - Path tempDownload = Path.of("tempDownload").resolve("temp.toolbox"); - FileHelper.download("file:" + path, tempDownload); - String importedName = Installer.installPackage(tempDownload); - FileHelper.deleteDirectory(Path.of("tempDownload")); - - System.out.println(); - System.out.println("Imported server as: " + importedName); - pressEnterToCont(input); - setState(State.MENU); - } else if (selectedAction == 3) { - System.out.println("Please enter the URL of the .toolbox file"); - System.out.print("URL: "); - String path = readLine(input).trim(); - path = path.replaceAll("^[\"|']|[\"|']$", ""); - - System.out.println(); - System.out.println("Installing"); - - Path tempDownload = Path.of("tempDownload").resolve("temp.toolbox"); - FileHelper.download(path, tempDownload); - String importedName = Installer.installPackage(tempDownload); - FileHelper.deleteDirectory(Path.of("tempDownload")); - - System.out.println(); - System.out.println("Imported server as: " + importedName); - pressEnterToCont(input); - setState(State.MENU); - } - } - - public static String askServerName(BufferedReader input, String defaultName) { - System.out.println("Please enter a name for this server, or leave blank for default (" + defaultName + "): "); - System.out.println(); - System.out.print("Server Name: "); - String enteredServerName = readLine(input); - if (enteredServerName.isBlank()) enteredServerName = defaultName; - enteredServerName = FileNameCleaner.cleanFileName(enteredServerName); - - if (getServerFromName(enteredServerName) != null) { - System.out.println("A server with that name already exists."); - System.out.println(); - return askServerName(input, defaultName); - } - return enteredServerName; - } - - public static void checkEula(BufferedReader input, InstalledServerInfo serverInfo) { - Path eulaFile = serverInfo.getPath().resolve("eula.txt"); - boolean agreed = EulaChecker.checkEulaAgreement(eulaFile); - - if (agreed) return; - - System.out.println(""" - Do you accept the Minecraft's EULA? - - For your server to run you must accept Minecraft's EULA. - The Minecraft's EULA contains information and rules about what you can do and can't do while using the game. - Agreement of the Minecraft's EULA is strictly needed, otherwise your server would be illegal to operate and thus, won't open. - You can read the EULA here: https://aka.ms/MinecraftEULA - WARNING: LEM WON'T WORK IF MINECRAFT'S EULA IS NOT AGREED! - """); - System.out.print("Do you want to accept the Minecraft's EULA? (Y/N): "); - String eulaAgree = readLine(input); - - if (!eulaAgree.isEmpty() && eulaAgree.substring(0, 1).equalsIgnoreCase("Y")) { - EulaChecker.agreeToEula(eulaFile); - System.out.println("EULA accepted."); - } - System.out.println(); - } - - public static void checkForUpdate(BufferedReader input) { - System.out.println("Checking for Toolbox Updates..."); - String installedVersion = UpdateBootstrapper.getInstalledVersion(); - if (installedVersion.equals("0.0")) { - System.out.println("Toolbox is missing files require to run. The required files will be downloaded automatically."); - System.out.println(); - System.out.println("1. View Latest Release"); - System.out.println("2. Download"); - System.out.println("0. Exit"); - System.out.println(); - System.out.print("Select Option: "); - - if(CMDArgsParser.autoUpdateToolbox()){ - System.out.println("Auto Accepting update..."); - UpdateBootstrapper.installUpdate(); - UpdateBootstrapper.runToolbox(); - return ; - } - - int selection = readInt(input); - if (selection == 1) { - if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { - try { - Desktop.getDesktop().browse(new URI(UpdateBootstrapper.URL)); - } catch (Exception e) { - e.printStackTrace(); - } - } - clearConsole(); - checkForUpdate(input); - } else if (selection == 2) { - System.out.println("Installing latest version"); - UpdateBootstrapper.installUpdate(); - UpdateBootstrapper.runToolbox(); - return; - } else if (selection == 0) { - System.out.println("Exiting..."); - System.exit(0); - } - } - - String update = UpdateBootstrapper.isUpdateAvailable(); - if (update != null) { - Path versionFile = Paths.get(".toolbox/VERSION"); - if (FileHelper.exists(versionFile)) { - System.out.println("Current version: Toolbox 2.0 v" + installedVersion); - System.out.println(); - System.out.println("An update for Toolbox is available: v" + update); - System.out.println(); - System.out.println("1. View Release"); - System.out.println("2. Download Update"); - System.out.println("0. Ignore"); - System.out.println(); - System.out.print("Select Option: "); - - if(CMDArgsParser.autoUpdateToolbox()){ - System.out.println("Auto Accepting update..."); - UpdateBootstrapper.installUpdate(); - UpdateBootstrapper.runToolbox(); - return ; - } - - int selection = readInt(input); - if (selection == 1) { - if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { - try { - Desktop.getDesktop().browse(new URI(UpdateBootstrapper.URL)); - } catch (Exception e) { - e.printStackTrace(); - } - } - clearConsole(); - checkForUpdate(input); - - } else if (selection == 2) { - System.out.println("Installing update"); - UpdateBootstrapper.installUpdate(); - UpdateBootstrapper.runToolbox(); - return; - } - } - } - System.out.println("Already up to date"); - UpdateBootstrapper.runToolbox(); - } - - public static void clearConsole() { - try { - if (System.getProperty("os.name").contains("Windows")) - new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); - else - System.out.print("\033\143"); - } catch (Exception ignored) { - } - } - - public static void pressEnterToCont(BufferedReader input) { - System.out.print("Press ENTER to continue..."); - readLine(input); - } - - public static String readLine(BufferedReader input) { - try { - return input.readLine().trim(); - } catch (Exception ignored) { - } - return ""; - } - - public static int readInt(BufferedReader input) { - try { - return Integer.parseInt(input.readLine()); - } catch (NumberFormatException numberFormatException) { - System.out.print("Please enter a number: "); - return readInt(input); - } catch (Exception ignored) { - } - return -1; - } - - public static InstalledServerInfo getServerFromName(String name) { - List serverInfos = Installer.detectInstalls(); - - for (InstalledServerInfo serverInfo : serverInfos) { - if (serverInfo.getName().equals(name)) return serverInfo; - } - - return null; - } - - public static void setState(State state) { - setState(state, null); - } - - public static void setState(State state, Object stateData) { - Menu.state = state; - Menu.stateData = stateData; - } - - public enum State { - SPLASH, - MENU, - EXISTING_INSTALL, - RUNNING_INSTALL, - INSTALLER, - IMPORT, - EXIT - } -} \ No newline at end of file diff --git a/src/main/java/net/kyrptonaught/ToolBox/ServerRunner.java b/src/main/java/net/kyrptonaught/ToolBox/ServerRunner.java deleted file mode 100644 index 821dcfb..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/ServerRunner.java +++ /dev/null @@ -1,44 +0,0 @@ -package net.kyrptonaught.ToolBox; - -import net.kyrptonaught.ToolBox.holders.InstalledServerInfo; -import net.kyrptonaught.ToolBox.holders.RunningServer; - -import java.util.ArrayList; -import java.util.List; - -public class ServerRunner { - - private static final List runningServers = new ArrayList<>(); - - public static void exit() { - for (RunningServer server : runningServers) { - server.stop(); - } - } - - public static RunningServer runServer(InstalledServerInfo serverInfo) { - RunningServer server = new RunningServer(); - runningServers.add(server); - server.startServer(serverInfo); - server.setActive(); - return server; - } - - public static void resumeServer(RunningServer server) { - server.setActive(); - } - - public static void killServer(RunningServer server) { - server.kill(); - runningServers.remove(server); - } - - public static void stopServer(RunningServer server) { - server.stop(); - runningServers.remove(server); - } - - public static List getRunningServers() { - return runningServers; - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/configs/BranchConfig.java b/src/main/java/net/kyrptonaught/ToolBox/configs/BranchConfig.java deleted file mode 100644 index 2c3a1e3..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/configs/BranchConfig.java +++ /dev/null @@ -1,24 +0,0 @@ -package net.kyrptonaught.ToolBox.configs; - -public class BranchConfig { - public String name; - - public String launchCMD; - - public Dependency[] dependencies; - - public static class Dependency { - public String name; - public String displayName; - public String url; - public String location; - public String autoGeneratedHash; - public boolean gitRepo = false; - public boolean unzip = false; - - public String getDisplayName() { - if (displayName == null) return name; - return displayName; - } - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/configs/BranchesConfig.java b/src/main/java/net/kyrptonaught/ToolBox/configs/BranchesConfig.java deleted file mode 100644 index 475e92f..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/configs/BranchesConfig.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.kyrptonaught.ToolBox.configs; - -import java.util.List; - -public class BranchesConfig { - public List branches; - - public static class BranchInfo { - public String name; - public String url; - public String desc; - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/holders/InstalledDependencyInfo.java b/src/main/java/net/kyrptonaught/ToolBox/holders/InstalledDependencyInfo.java deleted file mode 100644 index 3189315..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/holders/InstalledDependencyInfo.java +++ /dev/null @@ -1,19 +0,0 @@ -package net.kyrptonaught.ToolBox.holders; - -import net.kyrptonaught.ToolBox.configs.BranchConfig; - -import java.util.List; - -public class InstalledDependencyInfo extends BranchConfig.Dependency { - public String hash; - public List installedFiles; - - public InstalledDependencyInfo(BranchConfig.Dependency dependency) { - this.name = dependency.name; - this.displayName = dependency.displayName; - this.url = dependency.url; - this.location = dependency.location; - this.gitRepo = dependency.gitRepo; - this.unzip = dependency.unzip; - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/holders/InstalledServerInfo.java b/src/main/java/net/kyrptonaught/ToolBox/holders/InstalledServerInfo.java deleted file mode 100644 index 7d4a02c..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/holders/InstalledServerInfo.java +++ /dev/null @@ -1,113 +0,0 @@ -package net.kyrptonaught.ToolBox.holders; - -import net.kyrptonaught.ToolBox.IO.FileNameCleaner; -import net.kyrptonaught.ToolBox.configs.BranchConfig; -import net.kyrptonaught.ToolBox.configs.BranchesConfig; - -import java.nio.file.Path; -import java.util.HashMap; -import java.util.regex.Pattern; - -public class InstalledServerInfo { - - private BranchConfig branchConfig; - - private final BranchesConfig.BranchInfo branchInfo; - - private transient Path installPath; - - private String installName; - - private final HashMap launchARGS = new HashMap<>(); - - public InstalledServerInfo(BranchConfig branchConfig, BranchesConfig.BranchInfo branchInfo) { - this.branchConfig = branchConfig; - this.branchInfo = branchInfo; - this.installName = branchConfig.name; - } - - public void updateBranchConfig(BranchConfig config) { - this.branchConfig = config; - } - - public String getName() { - if (installName != null) - return installName; - return branchConfig.name; - } - - public void setName(String name) { - installName = name; - } - - public void setRAMArgs(int ram) { - setCustomLaunchArgs("", "-Xmx" + ram + "G -Xms" + ram + "G"); - } - - public void setCustomLaunchArgs(String key, String args) { - launchARGS.put(key, args); - } - - public String getLaunchCMD() { - return Pattern.compile("<[a-zA-Z0-9]+>") - .matcher(branchConfig.launchCMD) - .replaceAll(matchResult -> launchARGS.getOrDefault(matchResult.group(), "")) - .replaceAll("\\s{2}", " "); //remove double spaces from blank substitutions - } - - public BranchConfig.Dependency[] getDependencies() { - return branchConfig.dependencies; - } - - public BranchesConfig.BranchInfo getBranchInfo() { - return branchInfo; - } - - public void setPath(Path path) { - this.installPath = path; - } - - public void setPath() { - setPath(Path.of("installs").resolve(FileNameCleaner.cleanFileName(getName()))); - } - - public Path getPath() { - return installPath; - } - - public Path getDependencyInstallPath(BranchConfig.Dependency dependency) { - return getPath().resolve(FileNameCleaner.removeFirstSlashAndClean(dependency.location)); - } - - public Path getToolBoxPath() { - return getPath().resolve(".toolbox"); - } - - public Path getDownloadPath() { - return getToolBoxPath().resolve("downloads"); - } - - public Path getDownloadPath(BranchConfig.Dependency dependency) { - return getDownloadPath().resolve(FileNameCleaner.cleanFileName(dependency.name)); - } - - public Path getInstalledDependencyPath() { - return getToolBoxPath().resolve("dependencies"); - } - - public Path getInstalledDependencyPath(BranchConfig.Dependency dependency) { - return getInstalledDependencyPath().resolve(FileNameCleaner.cleanFileName(dependency.name) + ".json"); - } - - public Path getMetaPath() { - return getToolBoxPath().resolve("meta"); - } - - public Path getTempPath() { - return getToolBoxPath().resolve("temp"); - } - - public Path getTempPath(BranchConfig.Dependency dependency) { - return getTempPath().resolve(FileNameCleaner.cleanFileName(dependency.name)); - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/holders/RunningServer.java b/src/main/java/net/kyrptonaught/ToolBox/holders/RunningServer.java deleted file mode 100644 index 784cd2a..0000000 --- a/src/main/java/net/kyrptonaught/ToolBox/holders/RunningServer.java +++ /dev/null @@ -1,123 +0,0 @@ -package net.kyrptonaught.ToolBox.holders; - -import net.kyrptonaught.ToolBox.ServerRunner; - -import java.io.*; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.atomic.AtomicReference; - -public class RunningServer { - private final AtomicReference process = new AtomicReference<>(); - private PipedOutputStream stagingPipe; - private BufferedWriter processInput; - - private final List outputLog = new ArrayList<>(); - - private boolean active = false; - public String stopCMD = "stop"; - public InstalledServerInfo serverInfo; - - public void startServer(InstalledServerInfo serverInfo) { - this.serverInfo = serverInfo; - new Thread(() -> { - try { - process.set(new ProcessBuilder(serverInfo.getLaunchCMD().split(" ")) - .directory(new File(System.getProperty("user.dir") + "/" + serverInfo.getPath() + "/")) - .redirectErrorStream(true) - .start()); - - BufferedReader reader = new BufferedReader(new InputStreamReader(process.get().getInputStream())); - String line; - while ((line = reader.readLine()) != null) { - outputLog.add(line); - if (active) - System.out.println(line); - } - - reader.close(); - ServerRunner.killServer(this); - } catch (Exception ignored) { - } - }, "Server Instance").start(); - - while (process.get() == null) { - //we have to wait for the process to start - } - - processInput = new BufferedWriter(new OutputStreamWriter(process.get().getOutputStream())); - } - - public void setActive() { - active = true; - stagingPipe = new PipedOutputStream(); - Thread stagingThread = new Thread(() -> { - try { - while (true) { - stagingPipe.write(System.in.read()); - } - } catch (Exception ignored) { - } - }, "Server Input Pipe"); - stagingThread.setDaemon(true); - stagingThread.start(); - - for (String line : outputLog) { - System.out.println(line); - } - - try (BufferedReader reader = new BufferedReader(new InputStreamReader(new PipedInputStream(stagingPipe)))) { - String line; - while ((line = reader.readLine()) != null) { - if (line.equals("$b")) { - setDisabled(); - return; - } - processInput.write(line); - processInput.newLine(); - processInput.flush(); - } - } catch (Exception ignored) { - } - } - - public boolean isRunning() { - return process.get() != null; - } - - public void setDisabled() { - active = false; - try { - stagingPipe.close(); - } catch (Exception ignored) { - } - } - - public void stop() { - //issue shutdown cmd - try { - processInput.write(stopCMD); - processInput.newLine(); - processInput.flush(); - } catch (Exception ignored) { - } - - //wait 5 seconds - try { - Thread.sleep(5000); - } catch (Exception ignored) { - } - - //kill - try { - kill(); - } catch (Exception ignored) { - } - } - - public void kill() { - setDisabled(); - process.get().destroy(); - process.set(null); - } -} diff --git a/src/main/java/net/kyrptonaught/ToolBox/IO/FileHelper.java b/src/main/java/net/kyrptonaught/ToolBoxBootstrap/IO/FileHelper.java similarity index 98% rename from src/main/java/net/kyrptonaught/ToolBox/IO/FileHelper.java rename to src/main/java/net/kyrptonaught/ToolBoxBootstrap/IO/FileHelper.java index 9c2608f..65b1900 100644 --- a/src/main/java/net/kyrptonaught/ToolBox/IO/FileHelper.java +++ b/src/main/java/net/kyrptonaught/ToolBoxBootstrap/IO/FileHelper.java @@ -1,4 +1,6 @@ -package net.kyrptonaught.ToolBox.IO; +package net.kyrptonaught.ToolBoxBootstrap.IO; + +import net.kyrptonaught.ToolBoxBootstrap.Main; import java.io.*; import java.math.BigInteger; @@ -44,7 +46,7 @@ public static String download(String fileURL) { } public static T download(String fileURL, Class clazz) { - return ConfigLoader.gson.fromJson(download(fileURL), clazz); + return Main.gson.fromJson(download(fileURL), clazz); } public static List unzipFile(Path zipFile, Path unzipPath, boolean skipTB) { diff --git a/src/main/java/net/kyrptonaught/ToolBox/IO/FileNameCleaner.java b/src/main/java/net/kyrptonaught/ToolBoxBootstrap/IO/FileNameCleaner.java similarity index 97% rename from src/main/java/net/kyrptonaught/ToolBox/IO/FileNameCleaner.java rename to src/main/java/net/kyrptonaught/ToolBoxBootstrap/IO/FileNameCleaner.java index 9b728dc..04e05ad 100644 --- a/src/main/java/net/kyrptonaught/ToolBox/IO/FileNameCleaner.java +++ b/src/main/java/net/kyrptonaught/ToolBoxBootstrap/IO/FileNameCleaner.java @@ -1,4 +1,4 @@ -package net.kyrptonaught.ToolBox.IO; +package net.kyrptonaught.ToolBoxBootstrap.IO; import java.nio.file.Path; import java.util.Arrays; diff --git a/src/main/java/net/kyrptonaught/ToolBoxBootstrap/Main.java b/src/main/java/net/kyrptonaught/ToolBoxBootstrap/Main.java new file mode 100644 index 0000000..b9fe7c6 --- /dev/null +++ b/src/main/java/net/kyrptonaught/ToolBoxBootstrap/Main.java @@ -0,0 +1,151 @@ +package net.kyrptonaught.ToolBoxBootstrap; + +import com.google.gson.Gson; +import net.kyrptonaught.ToolBoxBootstrap.IO.FileHelper; + +import java.awt.*; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.net.URI; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class Main { + public static Gson gson = new Gson().newBuilder() + .setLenient() + .disableHtmlEscaping() + .setPrettyPrinting() + .create(); + + public static void main(String[] args) { + BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); + checkForUpdate(input, args); + } + + public static void checkForUpdate(BufferedReader input, String[] args) { + System.out.println("Checking for Toolbox Updates..."); + String installedVersion = UpdateBootstrapper.getInstalledVersion(); + if (installedVersion.equals("0.0")) { + System.out.println("Toolbox is missing files require to run. The required files will be downloaded automatically."); + System.out.println(); + System.out.println("1. View Latest Release"); + System.out.println("2. Download"); + System.out.println("0. Exit"); + System.out.println(); + System.out.print("Select Option: "); + + if (containsArgs("--autoUpdateToolbox", args)) { + System.out.println("Auto Accepting update..."); + UpdateBootstrapper.installUpdate(); + UpdateBootstrapper.runToolbox(args); + return; + } + + int selection = readInt(input); + if (selection == 1) { + if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { + try { + Desktop.getDesktop().browse(new URI(UpdateBootstrapper.URL)); + } catch (Exception e) { + e.printStackTrace(); + } + } + clearConsole(); + checkForUpdate(input, args); + } else if (selection == 2) { + System.out.println("Installing latest version"); + UpdateBootstrapper.installUpdate(); + UpdateBootstrapper.runToolbox(args); + return; + } else if (selection == 0) { + System.out.println("Exiting..."); + System.exit(0); + } + } + + String update = UpdateBootstrapper.isUpdateAvailable(); + if (update != null) { + Path versionFile = Paths.get(".toolbox/VERSION"); + if (FileHelper.exists(versionFile)) { + System.out.println("Current version: Toolbox 2.0 v" + installedVersion); + System.out.println(); + System.out.println("An update for Toolbox is available: v" + update); + System.out.println(); + System.out.println("1. View Release"); + System.out.println("2. Download Update"); + System.out.println("0. Ignore"); + System.out.println(); + System.out.print("Select Option: "); + + if (containsArgs("--autoUpdateToolbox", args)) { + System.out.println("Auto Accepting update..."); + UpdateBootstrapper.installUpdate(); + UpdateBootstrapper.runToolbox(args); + return; + } + + int selection = readInt(input); + if (selection == 1) { + if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) { + try { + Desktop.getDesktop().browse(new URI(UpdateBootstrapper.URL)); + } catch (Exception e) { + e.printStackTrace(); + } + } + clearConsole(); + checkForUpdate(input, args); + + } else if (selection == 2) { + System.out.println("Installing update"); + UpdateBootstrapper.installUpdate(); + UpdateBootstrapper.runToolbox(args); + return; + } + } + } + System.out.println("Already up to date"); + UpdateBootstrapper.runToolbox(args); + } + + public static void pressEnterToCont(BufferedReader input) { + System.out.print("Press ENTER to continue..."); + readLine(input); + } + + public static void clearConsole() { + try { + if (System.getProperty("os.name").contains("Windows")) + new ProcessBuilder("cmd", "/c", "cls").inheritIO().start().waitFor(); + else + System.out.print("\033\143"); + } catch (Exception ignored) { + } + } + + public static String readLine(BufferedReader input) { + try { + return input.readLine().trim(); + } catch (Exception ignored) { + } + return ""; + } + + public static int readInt(BufferedReader input) { + try { + return Integer.parseInt(input.readLine()); + } catch (NumberFormatException numberFormatException) { + System.out.print("Please enter a number: "); + return readInt(input); + } catch (Exception ignored) { + } + return -1; + } + + public static boolean containsArgs(String arg, String[] args) { + for (String str : args) { + if (str.equalsIgnoreCase(arg)) return true; + } + return false; + } +} \ No newline at end of file diff --git a/src/main/java/net/kyrptonaught/ToolBox/UpdateBootstrapper.java b/src/main/java/net/kyrptonaught/ToolBoxBootstrap/UpdateBootstrapper.java similarity index 84% rename from src/main/java/net/kyrptonaught/ToolBox/UpdateBootstrapper.java rename to src/main/java/net/kyrptonaught/ToolBoxBootstrap/UpdateBootstrapper.java index 0923e4b..c88c47d 100644 --- a/src/main/java/net/kyrptonaught/ToolBox/UpdateBootstrapper.java +++ b/src/main/java/net/kyrptonaught/ToolBoxBootstrap/UpdateBootstrapper.java @@ -1,8 +1,8 @@ -package net.kyrptonaught.ToolBox; +package net.kyrptonaught.ToolBoxBootstrap; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import net.kyrptonaught.ToolBox.IO.FileHelper; +import net.kyrptonaught.ToolBoxBootstrap.IO.FileHelper; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -33,7 +33,7 @@ public static String isUpdateAvailable() { } public static void installUpdate() { - Menu.clearConsole(); + Main.clearConsole(); System.out.println("Installing Toolbox update..."); try { JsonArray response = FileHelper.download(URL.replace("//github.com/", "//api.github.com/repos/"), JsonArray.class); @@ -62,18 +62,19 @@ public static void installUpdate() { System.out.println("Done. Relaunching toolbox..."); System.out.println(); - Menu.pressEnterToCont(input); + Main.pressEnterToCont(input); } - public static void runToolbox() { - launchJar(getToolboxRunArgs()); + public static void runToolbox(String[] args) { + launchJar(getToolboxRunArgs(args)); } private static void launchJar(List args) { try { - URLClassLoader child = new URLClassLoader(new URL[]{Paths.get(".toolbox/launch.jar").toUri().toURL()}, Main.class.getClassLoader()); - Class classToLoad = Class.forName(Main.class.getName(), true, child); - Method method = classToLoad.getDeclaredMethod("main", String[].class); + URL[] urls = {new URL("jar:file:" + ".toolbox/launch.jar" + "!/")}; + URLClassLoader child = new URLClassLoader(urls, Main.class.getClassLoader()); + Class classToLoad = child.loadClass("net.kyrptonaught.ToolBox.Menu"); + Method method = classToLoad.getDeclaredMethod("startStateMachine", String[].class); Object instance = classToLoad.newInstance(); method.invoke(instance, (Object) args.toArray(String[]::new)); } catch (Exception e) { @@ -109,10 +110,10 @@ public static String getInstalledVersion() { } } - private static List getToolboxRunArgs() { + private static List getToolboxRunArgs(String[] args) { List arguments = new ArrayList<>(); arguments.add("--runToolbox"); - arguments.addAll(List.of(CMDArgsParser.args)); + arguments.addAll(List.of(args)); return arguments; } }