From 5e4b956e196b3f984a6734db36374bd930d64360 Mon Sep 17 00:00:00 2001 From: Kristian Knarvik Date: Thu, 25 Jan 2018 17:34:45 +0100 Subject: [PATCH] Reads server versions from file --- config/servertypes.csv | 8 ++ src/Main.java | 46 +++++++--- .../serverlauncher/server/Server.java | 36 ++++++-- .../serverlauncher/server/ServerType.java | 3 - test/ServerTest.java | 87 +++++++++---------- 5 files changed, 113 insertions(+), 67 deletions(-) create mode 100644 config/servertypes.csv diff --git a/config/servertypes.csv b/config/servertypes.csv new file mode 100644 index 0000000..f5ded52 --- /dev/null +++ b/config/servertypes.csv @@ -0,0 +1,8 @@ +Vanilla;Latest,1.12,1.11.2,1.10.2,1.9.4,1.8.9,1.7.10,1.6.4,1.5.2,1.4.7,1.3.2,1.2.5;https://launchermeta.mojang.com/mc/game/version_manifest.json;"release":";";https://s3.amazonaws.com/Minecraft.Download/versions/;/minecraft_server. +Snapshot;Latest;https://launchermeta.mojang.com/mc/game/version_manifest.json;"snapshot":";";https://s3.amazonaws.com/Minecraft.Download/versions/;/minecraft_server. +SpongeVanilla;1.11.2,1.10.2,1.8.9;https://dl-api.spongepowered.org/v1/org.spongepowered/spongevanilla/downloads?type=stable&minecraft=;"version":";",;https://repo.spongepowered.org/maven/org/spongepowered/spongevanilla/;/spongevanilla- +Bungee;Latest;https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/;Artifacts of BungeeCord #; ;http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar; +Spigot;1.12.2,1.11.2,1.10.2,1.9.4,1.9,1.8.8,1.7.10,1.6.4,1.5.2,1.4.7;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Spigot/ +MCPCplus;1.6.4,1.6.2,1.5.2,1.4.7;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/MCPC+/ +Craftbukkit;1.12,1.11.2,1.10.2,1.9.4,1.8.8,1.7.10,1.6.4,1.5.2,1.4.6,1.3.2,1.2.5,1.1,1.0;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Bukkit/ +Custom;; \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index c5d1189..bd112a8 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,9 +1,10 @@ import net.knarcraft.serverlauncher.server.*; -import java.io.FileNotFoundException; +import javax.naming.ConfigurationException; +import java.io.*; import java.lang.Runtime; import java.util.ArrayList; -import java.io.IOException; +import java.util.Scanner; //Java 9 required. /** @@ -15,7 +16,12 @@ public class Main { private static ArrayList serverTypes = new ArrayList<>(); public static void main(String[] args) { - addServerTypes(); + try { + loadServerTypes(); + } catch (ConfigurationException e) { + e.printStackTrace(); + System.exit(1); + } } // TODO: Add gui @@ -23,16 +29,30 @@ public class Main { /** * Adds all the valid server types and versions. */ - private static void addServerTypes() { - //This could be changed to read from a list which is downloaded to the user's computer. - serverTypes.add(new AdvancedServerType("Vanilla", new String[]{"Latest", "1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.9", "1.7.10", "1.6.4", "1.5.2", "1.4.7", "1.3.2", "1.2.5"}, "https://launchermeta.mojang.com/mc/game/version_manifest.json", "\"release\":\"", "\"", "https://s3.amazonaws.com/Minecraft.Download/versions/", "/minecraft_server.")); - serverTypes.add(new AdvancedServerType("Snapshot", new String[]{"Latest"}, "https://launchermeta.mojang.com/mc/game/version_manifest.json", "\"snapshot\":\"", "\"", "https://s3.amazonaws.com/Minecraft.Download/versions/", "/minecraft_server.")); - serverTypes.add(new AdvancedServerType("SpongeVanilla", new String[]{"1.11.2", "1.10.2", "1.8.9"}, "https://dl-api.spongepowered.org/v1/org.spongepowered/spongevanilla/downloads?type=stable&minecraft=", "\"version\":\"", "\",", "https://repo.spongepowered.org/maven/org/spongepowered/spongevanilla/", "/spongevanilla-")); - serverTypes.add(new AdvancedServerType("Bungee", new String[]{"Latest"}, "https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/", "Artifacts of BungeeCord #", " ", "http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar", "")); - serverTypes.add(new ServerType("Spigot", new String[]{"1.12.2", "1.11.2", "1.10.2", "1.9.4", "1.9", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.7"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Spigot/")); - serverTypes.add(new ServerType("MCPCplus", new String[]{"1.6.4", "1.6.2", "1.5.2", "1.4.7"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/MCPC+/")); - serverTypes.add(new ServerType("Craftbukkit", new String[]{"1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.6", "1.3.2", "1.2.5", "1.1", "1.0"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Bukkit/")); - serverTypes.add(new ServerType("Custom", new String[]{""}, "")); + private static void loadServerTypes() throws ConfigurationException { + try (Scanner in = new Scanner(new File("config/servertypes.csv"))) { + while (in.hasNextLine()) { + String[] str = in.nextLine().split(";", -1); + int len = str.length; + String[] ver; + if (str[1].contains(",")) { + ver = str[1].split(",", -1); + } else { + ver = new String[]{str[1]}; + } + if (len == 7) { + serverTypes.add(new AdvancedServerType(str[0], ver, str[2], str[3], str[4], str[5], str[6])); + } else if (len == 3) { + serverTypes.add(new ServerType(str[0], ver, str[2])); + } else { + throw new ConfigurationException("Error: Configuration file invalid."); + } + } + } catch (FileNotFoundException e) { + throw new ConfigurationException("Error: Configuration file not found."); + } catch (ArrayIndexOutOfBoundsException e) { + throw new ConfigurationException("Error: Configuration file invalid."); + } } /** diff --git a/src/net/knarcraft/serverlauncher/server/Server.java b/src/net/knarcraft/serverlauncher/server/Server.java index 22cd137..396ac8a 100644 --- a/src/net/knarcraft/serverlauncher/server/Server.java +++ b/src/net/knarcraft/serverlauncher/server/Server.java @@ -1,14 +1,11 @@ package net.knarcraft.serverlauncher.server; -import java.io.File; +import java.io.*; import java.net.URL; import java.util.Scanner; -import java.io.InputStream; -import java.io.IOException; import java.nio.file.StandardCopyOption; import java.nio.file.*; import java.util.ArrayList; -import java.io.FileNotFoundException; /* Contains all necessary information to create, run and manage a Minecraft server. */ public class Server { @@ -78,6 +75,11 @@ public class Server { this.type = type; } + /** + * Sets the server's server version to a valid version, or ignores the request. + * + * @param serverVersion New version number. + */ public void setServerVersion(String serverVersion) { String[] versions = this.type.getVersions(); for (String version : versions) { @@ -95,6 +97,29 @@ public class Server { this.pid = pid; } + /** + * Runs the Minecraft server. + */ + public void run() { + if (this.isEnabled()) { + try { + this.downloadJar(); + System.out.println("File downloaded."); + } catch (FileNotFoundException e) { + System.out.println("File was not found."); + return; + } + Runtime rt = Runtime.getRuntime(); + try { + Process pr = rt.exec("\"java\" -Xmx" + this.maxRam + " -Xms512M -jar " + "\"" + this.path + "\\" + this.type + "\" nogui", null, new File(this.path)); + this.pid = pr.pid(); + System.out.println("Success"); + } catch (IOException e) { + System.out.println("Error"); + } + } + } + /** * Downloads necessary .jar file for the server. * This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves. @@ -132,7 +157,6 @@ public class Server { throw new FileNotFoundException("Version file could not be downloaded."); } newestVersion = stringBetween(versionText, type.getSrcStart(), type.getSrcEnd()); - System.out.println(newestVersion); success = downloadFile(type.getDownloadURL() + newestVersion + type.getDownloadURLPart() + newestVersion + ".jar", filePath); //TODO: Register the new version number, and only download if version mismatch or missing file. if (!success) { @@ -147,7 +171,7 @@ public class Server { } } break; - case "Sponge": + case "SpongeVanilla": try { versionText = readFile(this.type.getVersionURL() + this.serverVersion); } catch (IOException e) { diff --git a/src/net/knarcraft/serverlauncher/server/ServerType.java b/src/net/knarcraft/serverlauncher/server/ServerType.java index bb3a876..b16b48b 100644 --- a/src/net/knarcraft/serverlauncher/server/ServerType.java +++ b/src/net/knarcraft/serverlauncher/server/ServerType.java @@ -1,8 +1,5 @@ package net.knarcraft.serverlauncher.server; -import java.net.MalformedURLException; -import java.net.URL; - /** * Has a name and contains a list of valid server versions. */ diff --git a/test/ServerTest.java b/test/ServerTest.java index 4e8a6ad..486811d 100644 --- a/test/ServerTest.java +++ b/test/ServerTest.java @@ -1,37 +1,63 @@ +import javax.crypto.Cipher; +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; +import javax.naming.ConfigurationException; + import net.knarcraft.serverlauncher.server.*; -import java.io.*; +import java.io.File; +import java.io.FileNotFoundException; import java.util.ArrayList; +import java.util.Scanner; public class ServerTest { private static ArrayList serverTypes = new ArrayList<>(); private static ArrayList servers = new ArrayList<>(); public static void main(String[] args) { - addServerTypes(); + /*try { + loadServerTypes(); + } catch (ConfigurationException e) { + e.printStackTrace(); + return; + } Server server1 = new Server("Server1"); server1.toggle(); server1.setPath("C:\\Users\\Kristian\\Desktop\\Test"); - server1.setType((AdvancedServerType) serverTypes.get(0)); - server1.setServerVersion("Latest"); + server1.setType((AdvancedServerType) serverTypes.get(2)); + server1.setServerVersion("1.10.2"); server1.setMaxRam("1G"); servers.add(server1); - startServers(servers); + startServers(servers);*/ } /** * Adds all the valid server types and versions. */ - private static void addServerTypes() { - //This could be changed to read from a list which is downloaded to the user's computer. - serverTypes.add(new AdvancedServerType("Vanilla", new String[]{"Latest", "1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.9", "1.7.10", "1.6.4", "1.5.2", "1.4.7", "1.3.2", "1.2.5"}, "https://launchermeta.mojang.com/mc/game/version_manifest.json", "\"release\":\"", "\"", "https://s3.amazonaws.com/Minecraft.Download/versions/", "/minecraft_server.")); - serverTypes.add(new AdvancedServerType("Snapshot", new String[]{"Latest"}, "https://launchermeta.mojang.com/mc/game/version_manifest.json", "\"snapshot\":\"", "\"", "https://s3.amazonaws.com/Minecraft.Download/versions/", "/minecraft_server.")); - serverTypes.add(new AdvancedServerType("SpongeVanilla", new String[]{"1.11.2", "1.10.2", "1.8.9"}, "https://dl-api.spongepowered.org/v1/org.spongepowered/spongevanilla/downloads?type=stable&minecraft=", "\"version\":\"", "\",", "https://repo.spongepowered.org/maven/org/spongepowered/spongevanilla/", "/spongevanilla-")); - serverTypes.add(new AdvancedServerType("Bungee", new String[]{"Latest"}, "https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/", "Artifacts of BungeeCord #", " ", "http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar", "")); - serverTypes.add(new ServerType("Spigot", new String[]{"1.12.2", "1.11.2", "1.10.2", "1.9.4", "1.9", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.7"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Spigot/")); - serverTypes.add(new ServerType("MCPCplus", new String[]{"1.6.4", "1.6.2", "1.5.2", "1.4.7"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/MCPC+/")); - serverTypes.add(new ServerType("Craftbukkit", new String[]{"1.12", "1.11.2", "1.10.2", "1.9.4", "1.8.8", "1.7.10", "1.6.4", "1.5.2", "1.4.6", "1.3.2", "1.2.5", "1.1", "1.0"}, "https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Bukkit/")); - serverTypes.add(new ServerType("Custom", new String[]{""}, "")); + private static void loadServerTypes() throws ConfigurationException { + try (Scanner in = new Scanner(new File("config/servertypes.csv"))) { + while (in.hasNextLine()) { + String[] str = in.nextLine().split(";", -1); + int len = str.length; + String[] ver; + if (str[1].contains(",")) { + ver = str[1].split(",", -1); + } else { + ver = new String[]{str[1]}; + } + if (len == 7) { + serverTypes.add(new AdvancedServerType(str[0], ver, str[2], str[3], str[4], str[5], str[6])); + } else if (len == 3) { + serverTypes.add(new ServerType(str[0], ver, str[2])); + } else { + throw new ConfigurationException("Error: Config file invalid."); + } + } + } catch (FileNotFoundException e) { + throw new ConfigurationException("Error: Config file not found."); + } catch (ArrayIndexOutOfBoundsException e) { + throw new ConfigurationException("Error: Config file invalid."); + } } /** @@ -40,36 +66,7 @@ public class ServerTest { public static void startServers(ArrayList servers) { System.out.println("Starting servers."); for (Server server : servers) { - if (server.isEnabled()) { - String path = server.getPath(); - String type = server.getType(); - try { - server.downloadJar(); - System.out.println("File downloaded."); - } catch (FileNotFoundException e) { - System.out.println("File was not found."); - return; - } - String ram = server.maxRam(); - Runtime rt = Runtime.getRuntime(); - try { - Process pr = rt.exec("java -Xmx" + ram + " -Xms512M -jar " + "\"" + path + "\\" + type + "\"", null, new File(server.getPath())); - String line; - BufferedReader in = new BufferedReader( - new InputStreamReader(pr.getInputStream()) ); - while ((line = in.readLine()) != null) { - System.out.println(line); - } - in.close(); - long pid = pr.pid(); - server.updatePid(pid); - System.out.println("Success"); - } catch (IOException e) { - System.out.println("Error"); - } - } else { - System.out.println("Server disabled"); - } + server.run(); } } }