Skip to content

Commit

Permalink
Remove the archaic Executable type
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ME1312 committed Jan 4, 2019
1 parent eafacf7 commit 1845e9d
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 211 deletions.
@@ -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;
/**
* New Executable
*
* @param exe Executable String or File Path
*/
public Executable(String exe) {
if (Util.isNull(exe)) throw new NullPointerException();
if (new File(exe).exists()) {
isFile = true;
File = new File(exe);
Str = exe;
} else {
isFile = false;
File = null;
Str = exe;
}
}
public class Executable {
private Executable() {}

/**
* New Executable
* Format a command to be executed
*
* @param path File Path
* @param gitbash Git Bash location (optional)
* @param exec Executable String
* @return
*/
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();
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 {
String = Str;
cmd = new String[]{"sh", "-lic", exec};
}
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;
return cmd;
}

}
Expand Up @@ -2,7 +2,6 @@

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;
Expand Down Expand Up @@ -144,7 +143,7 @@ public SubServer getSubServer(String name) {
}

@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);
Expand Down
Expand Up @@ -110,7 +110,7 @@ public boolean create(UUID player, String name, ServerTemplate template, Version
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);
Expand Down
Expand Up @@ -28,7 +28,7 @@ public class ExternalSubServer extends SubServerContainer {
private boolean enabled;
private Container<Boolean> log;
private String dir;
protected Executable exec;
protected String exec;
private String stopcmd;
private StopAction stopaction;
private LinkedList<LoggedCommand> history;
Expand All @@ -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;
Expand Down Expand Up @@ -319,7 +319,7 @@ public int edit(UUID player, YAMLSection edit) {
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());
Expand Down Expand Up @@ -506,7 +506,7 @@ public String getPath() {
}

@Override
public Executable getExecutable() {
public String getExecutable() {
return exec;
}

Expand Down
Expand Up @@ -241,14 +241,14 @@ public int command(UUID player, String command, String... servers) {
* @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
Expand All @@ -259,14 +259,14 @@ public int command(UUID player, String command, String... servers) {
* @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);
}

Expand Down
Expand Up @@ -3,7 +3,6 @@
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;
Expand Down Expand Up @@ -98,7 +97,7 @@ public SubServer getSubServer(String name) {
}

@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);
Expand Down

0 comments on commit 1845e9d

Please sign in to comment.