From 1845e9d8f990c7eb7e92eae57e5401c373d148f0 Mon Sep 17 00:00:00 2001 From: ME1312 Date: Fri, 4 Jan 2019 16:51:58 -0500 Subject: [PATCH] Remove the archaic Executable type Since it was virually no different from one, this has been replaced by a String. Additionally, the interpreter for start scripts can now be changed from cmd.exe to git bash on windows systems by using the `bash` or `sh` commands. --- .../SubServers/Bungee/Host/Executable.java | 79 ++++--------------- .../Bungee/Host/External/ExternalHost.java | 3 +- .../Host/External/ExternalSubCreator.java | 2 +- .../Host/External/ExternalSubServer.java | 8 +- .../ME1312/SubServers/Bungee/Host/Host.java | 8 +- .../Bungee/Host/Internal/InternalHost.java | 3 +- .../Host/Internal/InternalSubCreator.java | 58 ++++++++------ .../Host/Internal/InternalSubServer.java | 10 +-- .../SubServers/Bungee/Host/SubServer.java | 2 +- .../Bungee/Host/SubServerController.java | 4 +- .../Network/Packet/PacketExAddServer.java | 7 +- .../ME1312/SubServers/Bungee/SubPlugin.java | 6 +- .../Host/Executable/Executable.java | 79 ++++--------------- .../Host/Executable/SubCreator.java | 61 +++++++------- .../SubServers/Host/Executable/SubServer.java | 6 +- .../Network/Packet/PacketExAddServer.java | 3 +- 16 files changed, 130 insertions(+), 209 deletions(-) diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Executable.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Executable.java index 72c05c6e..538de737 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Executable.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Executable.java @@ -1,75 +1,30 @@ package net.ME1312.SubServers.Bungee.Host; -import net.ME1312.SubServers.Bungee.Library.Util; - import java.io.File; -import java.io.Serializable; /** - * Executable Variable Class + * Executable String Handler Class */ -@SuppressWarnings("serial") -public class Executable implements Serializable { - private boolean isFile; - private File File; - private String Str; +public class Executable { + private Executable() {} + /** - * New Executable + * Format a command to be executed * - * @param exe Executable String or File Path + * @param gitbash Git Bash location (optional) + * @param exec Executable String + * @return */ - public Executable(String exe) { - if (Util.isNull(exe)) throw new NullPointerException(); - if (new File(exe).exists()) { - isFile = true; - File = new File(exe); - Str = exe; + public static String[] parse(String gitbash, String exec) { + String[] cmd; + if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { + if (gitbash != null && (exec.startsWith("bash ") || exec.startsWith("sh "))) + exec = "\"" + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lic \"" + + exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + "\""; + cmd = new String[]{"cmd.exe", "/q", "/c", '"'+exec+'"'}; } else { - isFile = false; - File = null; - Str = exe; + cmd = new String[]{"sh", "-lic", exec}; } + return cmd; } - - /** - * New Executable - * - * @param path File Path - */ - public Executable(File path) { - if (Util.isNull(path)) throw new NullPointerException(); - isFile = true; - File = path; - Str = path.toString(); - } - - @Override - public String toString() { - String String; - if (isFile) { - String = File.toString(); - } else { - String = Str; - } - return String; - } - - /** - * Check if the Executable String is a file - * - * @return File Status - */ - public boolean isFile() { - return isFile; - } - - /** - * Get Executable File - * - * @return File or Null if Executable isn't a file - */ - public File toFile() { - return File; - } - } diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalHost.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalHost.java index 737c0899..33a72b09 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalHost.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalHost.java @@ -2,7 +2,6 @@ package net.ME1312.SubServers.Bungee.Host.External; import net.ME1312.SubServers.Bungee.Event.SubAddServerEvent; import net.ME1312.SubServers.Bungee.Event.SubRemoveServerEvent; -import net.ME1312.SubServers.Bungee.Host.Executable; import net.ME1312.SubServers.Bungee.Host.Host; import net.ME1312.SubServers.Bungee.Host.SubCreator; import net.ME1312.SubServers.Bungee.Host.SubServer; @@ -144,7 +143,7 @@ public class ExternalHost extends Host implements ClientHandler { } @Override - public SubServer addSubServer(UUID player, String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException { + public SubServer addSubServer(UUID player, String name, boolean enabled, int port, String motd, boolean log, String directory, String executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException { if (plugin.api.getServers().keySet().contains(name.toLowerCase())) throw new InvalidServerException("A Server already exists with this name!"); SubServer server = new ExternalSubServer(this, name, enabled, port, motd, log, directory, executable, stopcmd, hidden, restricted); SubAddServerEvent event = new SubAddServerEvent(player, this, server); diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalSubCreator.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalSubCreator.java index 9fdc2be8..9649bbc3 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalSubCreator.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalSubCreator.java @@ -110,7 +110,7 @@ public class ExternalSubCreator extends SubCreator { server.setAll(config); SubServer subserver = host.addSubServer(player, name, server.getBoolean("Enabled"), fport, server.getColoredString("Motd", '&'), server.getBoolean("Log"), server.getRawString("Directory"), - new Executable(server.getRawString("Executable")), server.getRawString("Stop-Command"), server.getBoolean("Hidden"), server.getBoolean("Restricted")); + server.getRawString("Executable"), server.getRawString("Stop-Command"), server.getBoolean("Hidden"), server.getBoolean("Restricted")); if (server.getString("Display").length() > 0) subserver.setDisplayName(server.getString("Display")); for (String group : server.getStringList("Group")) subserver.addGroup(group); SubServer.StopAction action = Util.getDespiteException(() -> SubServer.StopAction.valueOf(server.getRawString("Stop-Action").toUpperCase().replace('-', '_').replace(' ', '_')), null); diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalSubServer.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalSubServer.java index e5d0ddb8..90e45db1 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalSubServer.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/External/ExternalSubServer.java @@ -28,7 +28,7 @@ public class ExternalSubServer extends SubServerContainer { private boolean enabled; private Container log; private String dir; - protected Executable exec; + protected String exec; private String stopcmd; private StopAction stopaction; private LinkedList history; @@ -52,7 +52,7 @@ public class ExternalSubServer extends SubServerContainer { * @param restricted Restricted Status * @throws InvalidServerException */ - public ExternalSubServer(ExternalHost host, String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException { + public ExternalSubServer(ExternalHost host, String name, boolean enabled, int port, String motd, boolean log, String directory, String executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException { super(host, name, port, motd, hidden, restricted); if (Util.isNull(host, name, enabled, port, motd, log, stopcmd, hidden, restricted)) throw new NullPointerException(); this.host = host; @@ -319,7 +319,7 @@ public class ExternalSubServer extends SubServerContainer { case "exec": if (value.isString() && host.removeSubServer(player, getName())) { waitFor(() -> host.getSubServer(getName()), null); - SubServer server = host.addSubServer(player, getName(), isEnabled(), getAddress().getPort(), getMotd(), isLogging(), getPath(), new Executable(value.asRawString()), getStopCommand(), isHidden(), isRestricted()); + SubServer server = host.addSubServer(player, getName(), isEnabled(), getAddress().getPort(), getMotd(), isLogging(), getPath(), value.asRawString(), getStopCommand(), isHidden(), isRestricted()); if (server != null) { if (this.host.plugin.config.get().getSection("Servers").getKeys().contains(getName())) { this.host.plugin.config.get().getSection("Servers").getSection(getName()).set("Executable", value.asRawString()); @@ -506,7 +506,7 @@ public class ExternalSubServer extends SubServerContainer { } @Override - public Executable getExecutable() { + public String getExecutable() { return exec; } diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Host.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Host.java index 44627615..eb746075 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Host.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Host.java @@ -241,14 +241,14 @@ public abstract class Host implements ExtraDataHandler { * @param motd Motd of the Server * @param log Logging Status * @param directory Directory - * @param executable Executable + * @param executable Executable String * @param stopcmd Command to Stop the Server * @param hidden if the server should be hidden from players * @param restricted Players will need a permission to join if true * @return The SubServer * @throws InvalidServerException */ - public abstract SubServer addSubServer(UUID player, String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException; + public abstract SubServer addSubServer(UUID player, String name, boolean enabled, int port, String motd, boolean log, String directory, String executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException; /** * Adds a SubServer @@ -259,14 +259,14 @@ public abstract class Host implements ExtraDataHandler { * @param motd Motd of the Server * @param log Logging Status * @param directory Directory - * @param executable Executable + * @param executable Executable String * @param stopcmd Command to Stop the Server * @param hidden if the server should be hidden from players * @param restricted Players will need a permission to join if true * @return The SubServer * @throws InvalidServerException */ - public SubServer addSubServer(String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException { + public SubServer addSubServer(String name, boolean enabled, int port, String motd, boolean log, String directory, String executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException { return addSubServer(null, name, enabled, port, motd, log, directory, executable, stopcmd, hidden, restricted); } diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalHost.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalHost.java index c5312d4a..fb4d952d 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalHost.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalHost.java @@ -3,7 +3,6 @@ package net.ME1312.SubServers.Bungee.Host.Internal; import com.dosse.upnp.UPnP; import net.ME1312.SubServers.Bungee.Event.SubAddServerEvent; import net.ME1312.SubServers.Bungee.Event.SubRemoveServerEvent; -import net.ME1312.SubServers.Bungee.Host.Executable; import net.ME1312.SubServers.Bungee.Library.Config.YAMLSection; import net.ME1312.SubServers.Bungee.Library.Exception.InvalidServerException; import net.ME1312.SubServers.Bungee.Host.Host; @@ -98,7 +97,7 @@ public class InternalHost extends Host { } @Override - public SubServer addSubServer(UUID player, String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException { + public SubServer addSubServer(UUID player, String name, boolean enabled, int port, String motd, boolean log, String directory, String executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException { if (plugin.api.getServers().keySet().contains(name.toLowerCase())) throw new InvalidServerException("A Server already exists with this name!"); SubServer server = new InternalSubServer(this, name, enabled, port, motd, log, directory, executable, stopcmd, hidden, restricted); SubAddServerEvent event = new SubAddServerEvent(player, this, server); diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalSubCreator.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalSubCreator.java index d2be6f67..b13a519e 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalSubCreator.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalSubCreator.java @@ -58,6 +58,7 @@ public class InternalSubCreator extends SubCreator { private YAMLSection build(File dir, ServerTemplate template, List history) throws SubCreatorException { YAMLSection server = new YAMLSection(); Version version = this.version; + List args = new LinkedList(); boolean error = false; if (history.contains(template)) throw new IllegalStateException("Template Import loop detected"); history.add(template); @@ -81,28 +82,34 @@ public class InternalSubCreator extends SubCreator { try { System.out.println(name + File.separator + "Creator > Loading Template: " + template.getDisplayName()); Util.copyDirectory(template.getDirectory(), dir); - if (template.getType() == ServerType.FORGE || template.getType() == ServerType.SPONGE) { - System.out.println(name + File.separator + "Creator > Searching Versions..."); - YAMLSection spversionmanifest = new YAMLSection(new Gson().fromJson("{\"versions\":" + Util.readAll(new BufferedReader(new InputStreamReader(new URL("https://dl-api.spongepowered.org/v1/org.spongepowered/sponge" + ((template.getType() == ServerType.FORGE)?"forge":"vanilla") + "/downloads?type=stable&minecraft=" + version).openStream(), Charset.forName("UTF-8")))) + '}', Map.class)); + switch (template.getType()) { + case SPONGE: + case FORGE: + System.out.println(name + File.separator + "Creator > Searching Versions..."); + YAMLSection spversionmanifest = new YAMLSection(new Gson().fromJson("{\"versions\":" + Util.readAll(new BufferedReader(new InputStreamReader(new URL("https://dl-api.spongepowered.org/v1/org.spongepowered/sponge" + ((template.getType() == ServerType.FORGE)?"forge":"vanilla") + "/downloads?type=stable&minecraft=" + version).openStream(), Charset.forName("UTF-8")))) + '}', Map.class)); - YAMLSection spprofile = null; - Version spversion = null; - for (YAMLSection profile : spversionmanifest.getSectionList("versions")) { - if (profile.getSection("dependencies").getRawString("minecraft").equalsIgnoreCase(version.toString()) && (spversion == null || new Version(profile.getRawString("version")).compareTo(spversion) >= 0)) { - spprofile = profile; - spversion = new Version(profile.getRawString("version")); + YAMLSection spprofile = null; + Version spversion = null; + for (YAMLSection profile : spversionmanifest.getSectionList("versions")) { + if (profile.getSection("dependencies").getRawString("minecraft").equalsIgnoreCase(version.toString()) && (spversion == null || new Version(profile.getRawString("version")).compareTo(spversion) >= 0)) { + spprofile = profile; + spversion = new Version(profile.getRawString("version")); + } } - } - if (spversion == null) - throw new InvalidServerException("Cannot find Sponge version for Minecraft " + version.toString()); - System.out.println(name + File.separator + "Creator > Found \"sponge" + ((template.getType() == ServerType.FORGE)?"forge":"vanilla") + "-" + spversion.toString() + '"'); + if (spversion == null) + throw new InvalidServerException("Cannot find Sponge version for Minecraft " + version.toString()); + System.out.println(name + File.separator + "Creator > Found \"sponge" + ((template.getType() == ServerType.FORGE)?"forge":"vanilla") + "-" + spversion.toString() + '"'); - if (template.getType() == ServerType.FORGE) { - Version mcfversion = new Version(spprofile.getSection("dependencies").getRawString("minecraft") + '-' + spprofile.getSection("dependencies").getRawString("forge")); - System.out.println(name + File.separator + "Creator > Found \"forge-" + mcfversion.toString() + '"'); + if (template.getType() == ServerType.FORGE) { + Version mcfversion = new Version(spprofile.getSection("dependencies").getRawString("minecraft") + '-' + spprofile.getSection("dependencies").getRawString("forge")); + System.out.println(name + File.separator + "Creator > Found \"forge-" + mcfversion.toString() + '"'); - version = new Version(mcfversion.toString() + " " + spversion.toString()); - } else version = new Version(spversion.toString()); + args.add(mcfversion.toString()); + } + args.add(spversion.toString()); + break; + default: + args.add(version.toString()); } } catch (Exception e) { e.printStackTrace(); @@ -113,15 +120,15 @@ public class InternalSubCreator extends SubCreator { if (template.getBuildOptions().getBoolean("Use-Cache", true)) { cache = new UniversalFile(host.plugin.dir, "SubServers:Cache:Templates:" + template.getName()); cache.mkdirs(); + args.add('\"' + cache.toString().replace(File.separatorChar, '/') + '\"'); } else { cache = null; } - String command = "bash " + template.getBuildOptions().getRawString("Shell-Location") + ' ' + version.toString() + ((cache == null)?"":" \""+cache.toString().replace(File.separatorChar, '/')+'\"'); - if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { - String bash = InternalSubCreator.this.gitBash + ((InternalSubCreator.this.gitBash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe"; - command = "cmd.exe /c \"\"" + bash + "\" --login -i -c \"" + command.replace("\"", "\\\"") + "\"\""; - } else if (template.getBuildOptions().contains("Permission")) { + String command = "bash \"" + template.getBuildOptions().getRawString("Shell-Location") + '\"'; + for (String arg : args) command += ' ' + arg; + + if (System.getProperty("os.name").toLowerCase().indexOf("win") < 0 && template.getBuildOptions().contains("Permission")) { try { Process process = Runtime.getRuntime().exec("chmod " + template.getBuildOptions().getRawString("Permission") + ' ' + template.getBuildOptions().getRawString("Shell-Location"), null, dir); Thread.sleep(500); @@ -136,7 +143,7 @@ public class InternalSubCreator extends SubCreator { try { System.out.println(name + File.separator + "Creator > Launching " + template.getBuildOptions().getRawString("Shell-Location")); - process = Runtime.getRuntime().exec(command, null, dir); + process = Runtime.getRuntime().exec(Executable.parse(gitBash, command), null, dir); log.log.set(host.plugin.config.get().getSection("Settings").getBoolean("Log-Creator")); log.file = new File(dir, "SubCreator-" + template.getName() + "-" + version.toString().replace(" ", "@") + ".log"); log.process = process; @@ -210,7 +217,7 @@ public class InternalSubCreator extends SubCreator { server.setAll(config); SubServer subserver = host.addSubServer(player, name, server.getBoolean("Enabled"), port, server.getColoredString("Motd", '&'), server.getBoolean("Log"), server.getRawString("Directory"), - new Executable(server.getRawString("Executable")), server.getRawString("Stop-Command"), server.getBoolean("Hidden"), server.getBoolean("Restricted")); + server.getRawString("Executable"), server.getRawString("Stop-Command"), server.getBoolean("Hidden"), server.getBoolean("Restricted")); if (server.getString("Display").length() > 0) subserver.setDisplayName(server.getString("Display")); for (String group : server.getStringList("Group")) subserver.addGroup(group); SubServer.StopAction action = Util.getDespiteException(() -> SubServer.StopAction.valueOf(server.getRawString("Stop-Action").toUpperCase().replace('-', '_').replace(' ', '_')), null); @@ -266,6 +273,7 @@ public class InternalSubCreator extends SubCreator { if (Util.isNull(host, gitBash)) throw new NullPointerException(); this.host = host; this.gitBash = (System.getenv("ProgramFiles(x86)") == null)?Pattern.compile("%(ProgramFiles)\\(x86\\)%", Pattern.CASE_INSENSITIVE).matcher(gitBash).replaceAll("%$1%"):gitBash; + if (this.gitBash.endsWith(File.pathSeparator)) this.gitBash = this.gitBash.substring(0, this.gitBash.length() - 1); this.thread = new TreeMap(); reload(); } diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalSubServer.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalSubServer.java index a0f835ef..1ae7852b 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalSubServer.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/Internal/InternalSubServer.java @@ -31,7 +31,7 @@ public class InternalSubServer extends SubServerContainer { private Container log; private String dir; private File directory; - private Executable executable; + private String executable; private String stopcmd; private StopAction stopaction; private LinkedList history; @@ -58,7 +58,7 @@ public class InternalSubServer extends SubServerContainer { * @param restricted Restricted Status * @throws InvalidServerException */ - public InternalSubServer(InternalHost host, String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException { + public InternalSubServer(InternalHost host, String name, boolean enabled, int port, String motd, boolean log, String directory, String executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException { super(host, name, port, motd, hidden, restricted); if (Util.isNull(host, name, enabled, port, motd, log, directory, executable, stopcmd, hidden, restricted)) throw new NullPointerException(); this.host = host; @@ -118,7 +118,7 @@ public class InternalSubServer extends SubServerContainer { private void run() { allowrestart = true; try { - process = Runtime.getRuntime().exec(executable.toString(), null, directory); + process = Runtime.getRuntime().exec(Executable.parse(host.getCreator().getBashDirectory(), executable), null, directory); System.out.println("SubServers > Now starting " + getName()); logger.process = process; logger.start(); @@ -394,7 +394,7 @@ public class InternalSubServer extends SubServerContainer { stop(player); waitFor(); } - executable = new Executable(value.asRawString()); + executable = value.asRawString(); if (this.host.plugin.config.get().getSection("Servers").getKeys().contains(getName())) { this.host.plugin.config.get().getSection("Servers").getSection(getName()).set("Executable", value.asRawString()); this.host.plugin.config.save(); @@ -570,7 +570,7 @@ public class InternalSubServer extends SubServerContainer { } @Override - public Executable getExecutable() { + public String getExecutable() { return executable; } diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubServer.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubServer.java index 2cd24c83..7d5c43d3 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubServer.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubServer.java @@ -274,7 +274,7 @@ public interface SubServer extends Server { * * @return Executable String */ - Executable getExecutable(); + String getExecutable(); /** * Grab the Command to Stop the Server diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubServerController.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubServerController.java index 559e29d1..f25f94ca 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubServerController.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Host/SubServerController.java @@ -124,7 +124,7 @@ public abstract class SubServerController { } @Override - public Executable getExecutable() { + public String getExecutable() { return SubServerController.this.getExecutable(); } @@ -323,7 +323,7 @@ public abstract class SubServerController { * * @return Executable String */ - public abstract Executable getExecutable(); + public abstract String getExecutable(); /** * Grab the Command to Stop the Server diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/Packet/PacketExAddServer.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/Packet/PacketExAddServer.java index cbe4c8f4..bce62d4c 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/Packet/PacketExAddServer.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Network/Packet/PacketExAddServer.java @@ -1,6 +1,5 @@ package net.ME1312.SubServers.Bungee.Network.Packet; -import net.ME1312.SubServers.Bungee.Host.Executable; import net.ME1312.SubServers.Bungee.Library.Config.YAMLSection; import net.ME1312.SubServers.Bungee.Library.Callback; import net.ME1312.SubServers.Bungee.Library.Util; @@ -22,7 +21,7 @@ public class PacketExAddServer implements PacketIn, PacketOut { private int port; private boolean log; private String directory; - private Executable executable; + private String executable; private String stopcmd; private UUID running; private String id; @@ -42,7 +41,7 @@ public class PacketExAddServer implements PacketIn, PacketOut { * @param executable Executable */ @SafeVarargs - public PacketExAddServer(String name, boolean enabled, int port, boolean log, String directory, Executable executable, String stopcmd, UUID running, Callback... callback) { + public PacketExAddServer(String name, boolean enabled, int port, boolean log, String directory, String executable, String stopcmd, UUID running, Callback... callback) { if (Util.isNull(name, enabled, log, directory, executable, callback)) throw new NullPointerException(); this.name = name; this.enabled = enabled; @@ -66,7 +65,7 @@ public class PacketExAddServer implements PacketIn, PacketOut { server.set("port", port); server.set("log", log); server.set("dir", directory); - server.set("exec", executable.toString()); + server.set("exec", executable); server.set("stopcmd", stopcmd); if (running != null) server.set("running", running.toString()); data.set("server", server); diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubPlugin.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubPlugin.java index e82ab494..be00e37c 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubPlugin.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/SubPlugin.java @@ -419,7 +419,7 @@ public final class SubPlugin extends BungeeCord implements Listener { edits.set("log", config.get().getSection("Servers").getSection(name).getBoolean("Log")); if (!config.get().getSection("Servers").getSection(name).getRawString("Directory").equals(server.getPath())) edits.set("dir", config.get().getSection("Servers").getSection(name).getRawString("Directory")); - if (!new Executable(config.get().getSection("Servers").getSection(name).getRawString("Executable")).toString().equals(server.getExecutable().toString())) + if (!config.get().getSection("Servers").getSection(name).getRawString("Executable").equals(server.getExecutable())) edits.set("exec", config.get().getSection("Servers").getSection(name).getRawString("Executable")); if (!config.get().getSection("Servers").getSection(name).getRawString("Stop-Command").equals(server.getStopCommand())) edits.set("stop-cmd", config.get().getSection("Servers").getSection(name).getRawString("Stop-Command")); @@ -441,12 +441,12 @@ public final class SubPlugin extends BungeeCord implements Listener { !config.get().getSection("Servers").getSection(name).getString("Host").equalsIgnoreCase(server.getHost().getName()) || config.get().getSection("Servers").getSection(name).getInt("Port") != server.getAddress().getPort() || !config.get().getSection("Servers").getSection(name).getRawString("Directory").equals(server.getPath()) || - !new Executable(config.get().getSection("Servers").getSection(name).getRawString("Executable")).toString().equals(server.getExecutable().toString()) + !config.get().getSection("Servers").getSection(name).getRawString("Executable").equals(server.getExecutable()) ) { if (server != null) server.getHost().forceRemoveSubServer(name); server = this.hosts.get(config.get().getSection("Servers").getSection(name).getString("Host").toLowerCase()).addSubServer(name, config.get().getSection("Servers").getSection(name).getBoolean("Enabled"), config.get().getSection("Servers").getSection(name).getInt("Port"), config.get().getSection("Servers").getSection(name).getColoredString("Motd", '&'), config.get().getSection("Servers").getSection(name).getBoolean("Log"), - config.get().getSection("Servers").getSection(name).getRawString("Directory"), new Executable(config.get().getSection("Servers").getSection(name).getRawString("Executable")), config.get().getSection("Servers").getSection(name).getRawString("Stop-Command"), + config.get().getSection("Servers").getSection(name).getRawString("Directory"), config.get().getSection("Servers").getSection(name).getRawString("Executable"), config.get().getSection("Servers").getSection(name).getRawString("Stop-Command"), config.get().getSection("Servers").getSection(name).getBoolean("Hidden"), config.get().getSection("Servers").getSection(name).getBoolean("Restricted")); } else { // Server doesn't need to reset if (config.get().getSection("Servers").getSection(name).getBoolean("Enabled") != server.isEnabled()) diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/Executable.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/Executable.java index 9534711e..a3350ed0 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/Executable.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/Executable.java @@ -1,75 +1,30 @@ package net.ME1312.SubServers.Host.Executable; -import net.ME1312.Galaxi.Library.Util; - import java.io.File; -import java.io.Serializable; /** - * Executable Variable Class + * Executable String Handler Class */ -@SuppressWarnings("serial") -public class Executable implements Serializable { - private boolean isFile; - private File File; - private String Str; +public class Executable { + private Executable() {} + /** - * New Executable + * Format a command to be executed * - * @param exe Executable String or File Path + * @param gitbash Git Bash location (optional) + * @param exec Executable String + * @return */ - public Executable(String exe) { - if (Util.isNull(exe)) throw new NullPointerException(); - if (new File(exe).exists()) { - isFile = true; - File = new File(exe); - Str = exe; + public static String[] parse(String gitbash, String exec) { + String[] cmd; + if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { + if (gitbash != null && (exec.startsWith("bash ") || exec.startsWith("sh "))) + exec = "\"" + gitbash + ((gitbash.endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe\" -lic \"" + + exec.replace("\\", "/\\").replace("\"", "\\\"").replace("^", "^^").replace("%", "^%").replace("&", "^&").replace("<", "^<").replace(">", "^>").replace("|", "^|") + "\""; + cmd = new String[]{"cmd.exe", "/q", "/c", '"'+exec+'"'}; } else { - isFile = false; - File = null; - Str = exe; + cmd = new String[]{"sh", "-lic", exec}; } + return cmd; } - - /** - * New Executable - * - * @param path File Path - */ - public Executable(File path) { - if (Util.isNull(path)) throw new NullPointerException(); - isFile = true; - File = path; - Str = path.toString(); - } - - @Override - public String toString() { - String String; - if (isFile) { - String = File.toString(); - } else { - String = Str; - } - return String; - } - - /** - * Check if the Executable String is a file - * - * @return File Status - */ - public boolean isFile() { - return isFile; - } - - /** - * Get Executable File - * - * @return File or Null if Executable isn't a file - */ - public File toFile() { - return File; - } - } diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubCreator.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubCreator.java index c9c829f0..1f991155 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubCreator.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubCreator.java @@ -202,6 +202,7 @@ public class SubCreator { private YAMLSection build(File dir, ServerTemplate template, List history) throws SubCreatorException { YAMLSection server = new YAMLSection(); Version version = this.version; + List args = new LinkedList(); boolean error = false; if (history.contains(template)) throw new IllegalStateException("Template Import loop detected"); history.add(template); @@ -228,31 +229,37 @@ public class SubCreator { log.logger.info.println("Loading Template: " + template.getDisplayName()); host.subdata.sendPacket(new PacketOutExLogMessage(address, "Loading Template: " + template.getDisplayName())); Util.copyDirectory(template.getDirectory(), dir); - if (template.getType() == ServerType.FORGE || template.getType() == ServerType.SPONGE) { - log.logger.info.println("Searching Versions..."); - host.subdata.sendPacket(new PacketOutExLogMessage(address, "Searching Versions...")); - YAMLSection spversionmanifest = new YAMLSection(new JSONObject("{\"versions\":" + Util.readAll(new BufferedReader(new InputStreamReader(new URL("https://dl-api.spongepowered.org/v1/org.spongepowered/sponge" + ((template.getType() == ServerType.FORGE)?"forge":"vanilla") + "/downloads?type=stable&minecraft=" + version).openStream(), Charset.forName("UTF-8")))) + '}')); + switch (template.getType()) { + case SPONGE: + case FORGE: + log.logger.info.println("Searching Versions..."); + host.subdata.sendPacket(new PacketOutExLogMessage(address, "Searching Versions...")); + YAMLSection spversionmanifest = new YAMLSection(new JSONObject("{\"versions\":" + Util.readAll(new BufferedReader(new InputStreamReader(new URL("https://dl-api.spongepowered.org/v1/org.spongepowered/sponge" + ((template.getType() == ServerType.FORGE)?"forge":"vanilla") + "/downloads?type=stable&minecraft=" + version).openStream(), Charset.forName("UTF-8")))) + '}')); - YAMLSection spprofile = null; - Version spversion = null; - for (YAMLSection profile : spversionmanifest.getSectionList("versions")) { - if (profile.getSection("dependencies").getRawString("minecraft").equalsIgnoreCase(version.toString()) && (spversion == null || new Version(profile.getRawString("version")).compareTo(spversion) >= 0)) { - spprofile = profile; - spversion = new Version(profile.getRawString("version")); + YAMLSection spprofile = null; + Version spversion = null; + for (YAMLSection profile : spversionmanifest.getSectionList("versions")) { + if (profile.getSection("dependencies").getRawString("minecraft").equalsIgnoreCase(version.toString()) && (spversion == null || new Version(profile.getRawString("version")).compareTo(spversion) >= 0)) { + spprofile = profile; + spversion = new Version(profile.getRawString("version")); + } } - } - if (spversion == null) - throw new InvalidServerException("Cannot find Sponge version for Minecraft " + version.toString()); - log.logger.info.println("Found \"sponge" + ((template.getType() == ServerType.FORGE)?"forge":"vanilla") + "-" + spversion.toString() + '"'); - host.subdata.sendPacket(new PacketOutExLogMessage(address, "Found \"sponge" + ((template.getType() == ServerType.FORGE)?"forge":"vanilla") + "-" + spversion.toString() + '"')); + if (spversion == null) + throw new InvalidServerException("Cannot find Sponge version for Minecraft " + version.toString()); + log.logger.info.println("Found \"sponge" + ((template.getType() == ServerType.FORGE)?"forge":"vanilla") + "-" + spversion.toString() + '"'); + host.subdata.sendPacket(new PacketOutExLogMessage(address, "Found \"sponge" + ((template.getType() == ServerType.FORGE)?"forge":"vanilla") + "-" + spversion.toString() + '"')); - if (template.getType() == ServerType.FORGE) { - Version mcfversion = new Version(spprofile.getSection("dependencies").getRawString("minecraft") + '-' + spprofile.getSection("dependencies").getRawString("forge")); - log.logger.info.println("Found \"forge-" + mcfversion.toString() + '"'); - host.subdata.sendPacket(new PacketOutExLogMessage(address, "Found \"forge-" + mcfversion.toString() + '"')); + if (template.getType() == ServerType.FORGE) { + Version mcfversion = new Version(spprofile.getSection("dependencies").getRawString("minecraft") + '-' + spprofile.getSection("dependencies").getRawString("forge")); + log.logger.info.println("Found \"forge-" + mcfversion.toString() + '"'); + host.subdata.sendPacket(new PacketOutExLogMessage(address, "Found \"forge-" + mcfversion.toString() + '"')); - version = new Version(mcfversion.toString() + " " + spversion.toString()); - } else version = new Version(spversion.toString()); + args.add(mcfversion.toString()); + } + args.add(spversion.toString()); + break; + default: + args.add(version.toString()); } } catch (Exception e) { log.logger.error.println(e); @@ -263,15 +270,15 @@ public class SubCreator { if (template.getBuildOptions().getBoolean("Use-Cache", true)) { cache = new UniversalFile(GalaxiEngine.getInstance().getRuntimeDirectory(), "Cache:Templates:" + template.getName()); cache.mkdirs(); + args.add("\"" + cache.toString().replace(File.separatorChar, '/') + '\"'); } else { cache = null; } - String command = "bash " + template.getBuildOptions().getRawString("Shell-Location") + ' ' + version.toString() + ((cache == null)?"":" \""+cache.toString().replace(File.separatorChar, '/')+'\"'); - if (System.getProperty("os.name").toLowerCase().indexOf("win") >= 0) { - String bash = host.host.getRawString("Git-Bash") + ((host.host.getRawString("Git-Bash").endsWith(File.separator))?"":File.separator) + "bin" + File.separatorChar + "sh.exe"; - command = "cmd.exe /c \"\"" + bash + "\" --login -i -c \"" + command.replace("\"", "\\\"") + "\"\""; - } else if (template.getBuildOptions().contains("Permission")) { + String command = "bash \"" + template.getBuildOptions().getRawString("Shell-Location") + '\"'; + for (String arg : args) command += ' ' + arg; + + if (System.getProperty("os.name").toLowerCase().indexOf("win") < 0 && template.getBuildOptions().contains("Permission")) { try { Process process = Runtime.getRuntime().exec("chmod " + template.getBuildOptions().getRawString("Permission") + ' ' + template.getBuildOptions().getRawString("Shell-Location"), null, dir); Thread.sleep(500); @@ -289,7 +296,7 @@ public class SubCreator { try { log.logger.info.println("Launching " + template.getBuildOptions().getRawString("Shell-Location")); host.subdata.sendPacket(new PacketOutExLogMessage(address, "Launching " + template.getBuildOptions().getRawString("Shell-Location"))); - process = Runtime.getRuntime().exec(command, null, dir); + process = Runtime.getRuntime().exec(Executable.parse(host.host.getRawString("Git-Bash"), command), null, dir); log.file = new File(dir, "SubCreator-" + template.getName() + "-" + version.toString().replace(" ", "@") + ".log"); log.process = process; log.start(); diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubServer.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubServer.java index 622ab6fd..4f24ccba 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubServer.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Executable/SubServer.java @@ -26,7 +26,7 @@ public class SubServer { private Container log; private String dir; private File directory; - private Executable executable; + private String executable; private Process process; private SubLogger logger; private Thread thread; @@ -47,7 +47,7 @@ public class SubServer { * @param stopcmd Stop Command * @throws InvalidServerException */ - public SubServer(ExHost host, String name, boolean enabled, int port, boolean log, String directory, Executable executable, String stopcmd) throws InvalidServerException { + public SubServer(ExHost host, String name, boolean enabled, int port, boolean log, String directory, String executable, String stopcmd) throws InvalidServerException { if (Util.isNull(host, name, enabled, log, directory, executable)) throw new NullPointerException(); this.host = host; this.name = name; @@ -107,7 +107,7 @@ public class SubServer { boolean falsestart = true; allowrestart = true; try { - process = Runtime.getRuntime().exec(executable.toString(), null, directory); + process = Runtime.getRuntime().exec(Executable.parse(host.host.getRawString("Git-Bash"), executable), null, directory); falsestart = false; host.log.info.println("Now starting " + name); logger.process = process; diff --git a/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExAddServer.java b/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExAddServer.java index b62c3fff..b5ec8542 100644 --- a/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExAddServer.java +++ b/SubServers.Host/src/net/ME1312/SubServers/Host/Network/Packet/PacketExAddServer.java @@ -5,7 +5,6 @@ import net.ME1312.Galaxi.Library.Config.YAMLSection; import net.ME1312.Galaxi.Library.Log.Logger; import net.ME1312.Galaxi.Library.Util; import net.ME1312.Galaxi.Library.Version.Version; -import net.ME1312.SubServers.Host.Executable.Executable; import net.ME1312.SubServers.Host.Executable.SubServer; import net.ME1312.SubServers.Host.Network.PacketIn; import net.ME1312.SubServers.Host.Network.PacketOut; @@ -71,7 +70,7 @@ public class PacketExAddServer implements PacketIn, PacketOut { host.subdata.sendPacket(new PacketExAddServer(0, "Server Already Added", (data.contains("id"))?data.getRawString("id"):null)); } else { SubServer server = new SubServer(host, data.getSection("server").getRawString("name"), data.getSection("server").getBoolean("enabled"), data.getSection("server").getInt("port"), data.getSection("server").getBoolean("log"), - data.getSection("server").getRawString("dir"), new Executable(data.getSection("server").getRawString("exec")), data.getSection("server").getRawString("stopcmd")); + data.getSection("server").getRawString("dir"), data.getSection("server").getRawString("exec"), data.getSection("server").getRawString("stopcmd")); host.servers.put(data.getSection("server").getRawString("name").toLowerCase(), server); if (UPnP.isUPnPAvailable() && host.config.get().getSection("Settings").getSection("UPnP", new YAMLSection()).getBoolean("Forward-Servers", false)) UPnP.openPortTCP(server.getPort()); log.info.println("Added SubServer: " + data.getSection("server").getRawString("name"));