From 19aa4bd5274445a6c90cea6e4644dacfa8f19d56 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Mon, 7 Jan 2013 01:58:15 +0100 Subject: [PATCH] Allow console parameters --- src/API/ServerAPI.php | 63 ++++++++++++++++----- src/classes/PocketMinecraftServer.class.php | 2 + src/common/functions.php | 4 +- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 0d4a11734..58c0baac3 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -87,10 +87,10 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run console("[DEBUG] Loading server.properties...", true, true, 2); $this->parseProperties(); - define("DEBUG", $this->config["debug"]); + define("DEBUG", $this->getProperty("debug")); $this->server = new PocketMinecraftServer($this->getProperty("server-name"), $this->getProperty("gamemode"), false, $this->getProperty("port"), $this->getProperty("server-id")); $this->server->api = $this; - if($this->getProperty("upnp-forwarding") === true ){ + if($this->getProperty("upnp-forwarding") === true){ console("[INFO] [UPnP] Trying to port forward..."); UPnP_PortForward($this->getProperty("port")); } @@ -211,8 +211,8 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run private function loadProperties(){ - if(isset($this->config["memory-limit"])){ - @ini_set("memory_limit", $this->config["memory-limit"]); + if($this->getProperty("memory-limit") !== false){ + @ini_set("memory_limit", $this->getProperty("memory-limit")); }else{ $this->config["memory-limit"] = "256M"; } @@ -220,15 +220,15 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run $this->config["invisible"] = false; } if(is_object($this->server)){ - $this->server->setType($this->config["server-type"]); - $this->server->timePerSecond = $this->config["time-per-second"]; - $this->server->invisible = $this->config["invisible"]; - $this->server->maxClients = $this->config["max-players"]; - $this->server->description = $this->config["description"]; - $this->server->motd = $this->config["motd"]; - $this->server->gamemode = $this->config["gamemode"]; - $this->server->difficulty = $this->config["difficulty"]; - $this->server->whitelist = $this->config["white-list"]; + $this->server->setType($this->getProperty("server-type")); + $this->server->timePerSecond = $this->getProperty("time-per-second"); + $this->server->invisible = $this->getProperty("invisible"); + $this->server->maxClients = $this->getProperty("max-players"); + $this->server->description = $this->getProperty("description"); + $this->server->motd = $this->getProperty("motd"); + $this->server->gamemode = $this->getProperty("gamemode"); + $this->server->difficulty = $this->getProperty("difficulty"); + $this->server->whitelist = $this->getProperty("white-list"); $this->server->reloadConfig(); } } @@ -389,6 +389,43 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run } public function getProperty($name){ + if(($v = arg($name)) !== false){ //Allow for command-line arguments + switch(strtolower(trim($v))){ + case "on": + case "true": + case "yes": + $v = true; + break; + case "off": + case "false": + case "no": + $v = false; + break; + } + switch($name){ + case "last-update": + if($v === false){ + $v = time(); + }else{ + $v = (int) $v; + } + break; + case "gamemode": + case "max-players": + case "port": + case "debug": + case "difficulty": + case "time-per-second": + $v = (int) $v; + break; + case "server-id": + if($v !== false){ + $v = preg_match("/[^0-9\-]/", $v) > 0 ? Utils::readInt(substr(md5($v, true), 0, 4)):$v; + } + break; + } + return $v; + } if(isset($this->config[$name])){ return $this->config[$name]; } diff --git a/src/classes/PocketMinecraftServer.class.php b/src/classes/PocketMinecraftServer.class.php index 9108eb9d2..da3e484d4 100644 --- a/src/classes/PocketMinecraftServer.class.php +++ b/src/classes/PocketMinecraftServer.class.php @@ -335,6 +335,8 @@ class PocketMinecraftServer extends stdClass{ $dump .= "Zend version: ".zend_version()."\r\n"; $dump .= "OS : " .PHP_OS.", ".Utils::getOS()."\r\n"; $dump .= "Debug Info: ".var_export($this->debugInfo(false), true)."\r\n\r\n\r\n"; + global $arguments; + $dump .= "Parameters: ".var_export($arguments, true)."\r\n\r\n\r\n"; $dump .= "server.properties: ".var_export($this->api->getProperties(), true)."\r\n\r\n\r\n"; $dump .= "Loaded Modules: ".var_export(get_loaded_extensions(), true)."\r\n\r\n"; $name = "error_dump_".time(); diff --git a/src/common/functions.php b/src/common/functions.php index 1c834ee4d..a3bf2d1f9 100644 --- a/src/common/functions.php +++ b/src/common/functions.php @@ -107,7 +107,7 @@ function parseNBTData($data){ } -function arg($name, $default){ +function arg($name, $default = false){ global $arguments, $argv; if(!isset($arguments)){ $arguments = arguments($argv); @@ -127,7 +127,7 @@ function arguments ( $args ){ array_shift( $args ); $args = join( $args, ' ' ); - preg_match_all('/ (--\w+ (?:[= ] [^-]+ [^\s-] )? ) | (-\w+) | (\w+) /x', $args, $match ); + preg_match_all('/ (--[\w\-]+ (?:[= ] [^-]+ [^\s-] )? ) | (-\w+) | (\w+) /x', $args, $match ); $args = array_shift( $match ); $ret = array(