From b3b38605b5b4059cad28c17986ce901aa3ca49a5 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Sun, 6 Jan 2013 13:31:30 +0100 Subject: [PATCH] Multi-update system and version numeration --- src/API/ServerAPI.php | 19 +++- src/classes/PocketMinecraftServer.class.php | 4 +- src/common/functions.php | 2 + src/misc/utils/VersionString.php | 96 +++++++++++++++++++++ 4 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 src/misc/utils/VersionString.php diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 45b979fd4..600d70d30 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -121,11 +121,22 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run if($info === false or !isset($info[0])){ console("[ERROR] GitHub API Error"); }else{ - $info = $info[0]; - if($info["name"] != MAJOR_VERSION){ + + $newest = new VersionString(MAJOR_VERSION); + $newest = array(-1, $newest->getNumber()); + foreach($info as $i => $tag){ + $update = new VersionString($tag["name"]); + $update = $update->getNumber(); + if($update > $newest[1]){ + $newest = array($i, $update); + } + } + + if($newest[0] !== -1){ + $target = $info[$newest[0]]; console("[NOTICE] A new STABLE version of PocketMine-MP has been released"); - console("[NOTICE] Version \"".$info["name"]."\" [".substr($info["commit"]["sha"], 0, 10)."]"); - console("[NOTICE] Download it at ".$info["zipball_url"]); + console("[NOTICE] Version \"".(new VersionString($newest[1]))."\" #".$newest[1]." [".substr($target["commit"]["sha"], 0, 10)."]"); + console("[NOTICE] Download it at ".$target["zipball_url"]); console("[NOTICE] This message will dissapear as soon as you update"); sleep(5); }else{ diff --git a/src/classes/PocketMinecraftServer.class.php b/src/classes/PocketMinecraftServer.class.php index c3c500b4a..e05ed24f4 100644 --- a/src/classes/PocketMinecraftServer.class.php +++ b/src/classes/PocketMinecraftServer.class.php @@ -30,7 +30,9 @@ class PocketMinecraftServer extends stdClass{ private $database, $interface, $evCnt, $handCnt, $events, $handlers, $version, $serverType, $lastTick; function __construct($name, $gamemode = 1, $seed = false, $port = 19132, $serverID = false){ $this->port = (int) $port; //19132 - 19135 - console("[INFO] PocketMine-MP ".MAJOR_VERSION." by @shoghicp, LGPL License", true, true, 0); + $vNumber = new VersionString(); + $vNumber = $vNumber->getNumber(); + console("[INFO] PocketMine-MP ".MAJOR_VERSION." #".$vNumber." by @shoghicp, LGPL License", true, true, 0); console("[DEBUG] Target Minecraft PE: ".CURRENT_MINECRAFT_VERSION, true, true, 2); console("[INFO] Starting Minecraft PE Server at *:".$this->port); if($this->port < 19132 or $this->port > 19135){ diff --git a/src/common/functions.php b/src/common/functions.php index 760e1bb2b..018082e47 100644 --- a/src/common/functions.php +++ b/src/common/functions.php @@ -25,6 +25,8 @@ the Free Software Foundation, either version 3 of the License, or */ + + function kill($pid){ switch(Utils::getOS()){ case "win": diff --git a/src/misc/utils/VersionString.php b/src/misc/utils/VersionString.php new file mode 100644 index 000000000..ed43e9705 --- /dev/null +++ b/src/misc/utils/VersionString.php @@ -0,0 +1,96 @@ + 0, + "beta" => 1, + "final" => 2, + ); + private $stage, $major, $release, $minor; + public function __construct($version = MAJOR_VERSION){ + if(is_int($version)){ + $this->minor = $version & 0x1F; + $this->major = ($version >> 5) & 0x0F; + $this->generation = ($version >> 9) & 0x0F; + $this->stage = array_search(($version >> 13) & 0x0F, VersionString::$stageOrder, true); + }else{ + $version = preg_split("/([A-Za-z]*)[ _\-]([0-9]*)\.([0-9]*)\.{0,1}([0-9]*)/", $version, -1, PREG_SPLIT_DELIM_CAPTURE); + $this->stage = strtolower($version[1]); //0-15 + $this->generation = (int) $version[2]; //0-15 + $this->major = (int) $version[3]; //0-15 + $this->minor = (int) $version[4]; //0-31 + } + } + + public function getNumber(){ + return (int) (VersionString::$stageOrder[$this->stage] << 13) + ($this->generation << 9) + ($this->major << 5) + $this->minor; + } + + public function getStage(){ + return $this->stage; + } + + public function getGeneration(){ + return $this->generation; + } + + public function getMajor(){ + return $this->major; + } + + public function getMinor(){ + return $this->minor; + } + + public function getRelease(){ + return $this->generation . "." . $this->major . "." . $this->minor; + } + + public function __toString(){ + return ucfirst($this->stage) . "_" . $this->generation . "." . $this->major . "." . $this->minor; + } + + public function compare($target, $diff = false){ + if(($target instanceof VersionString) === false){ + $target = new VersionString($target); + } + $number = $this->getNumber(); + $tNumber = $target->getNumber(); + if($diff === true){ + return $tNumber - $number; + } + if($number > $tNumber){ + return -1; //Target is older + }elseif($number < $tNumber){ + return 1; //Target is newer + }else{ + return 0; //Same version + } + } +} \ No newline at end of file