From 8d3ad0c5ec3cf9a45d238a07b573246b3bc67352 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Pueyo Date: Thu, 10 Jan 2013 23:59:03 +0100 Subject: [PATCH] Changing code parts [Hey! Don't look here!] --- PocketMine-MP.php | 7 +- src/API/ServerAPI.php | 20 ++++-- src/classes/PocketMinecraftServer.class.php | 76 ++++++++++++--------- src/classes/UDPSocket.class.php | 6 +- src/classes/Utils.class.php | 6 +- src/common/functions.php | 1 - src/misc/utils/TickLoop.php | 9 ++- 7 files changed, 74 insertions(+), 51 deletions(-) diff --git a/PocketMine-MP.php b/PocketMine-MP.php index 6eb0fa0c0..5b3ef41a8 100644 --- a/PocketMine-MP.php +++ b/PocketMine-MP.php @@ -30,10 +30,7 @@ require_once("classes/PocketMinecraftServer.class.php"); require_once("API/ServerAPI.php"); $server = new ServerAPI(); -if($server->start() !== true){ - //Stop -}else{ - //Restart -} +$server->run();//$server->start(); +//$server->join(); kill(getmypid()); //Fix for segfault \ No newline at end of file diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 58c0baac3..5200bfcee 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -25,10 +25,16 @@ the Free Software Foundation, either version 3 of the License, or */ -class ServerAPI extends stdClass{ //Yay! I can add anything to this class in runtime! +class ServerAPI{ var $restart = false; private $server, $config, $apiList = array(); - function __construct(){ + + public function run(){ + $this->load(); + return $this->init(); + } + + public function load(){ @mkdir(FILE_PATH."logs/", 0777, true); @mkdir(FILE_PATH."players/", 0777); @mkdir(FILE_PATH."worlds/", 0777); @@ -36,7 +42,7 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run console("[INFO] Starting ServerAPI server handler..."); file_put_contents(FILE_PATH."logs/packets.log", ""); if(!file_exists(FILE_PATH."logs/test.bin.log") or md5_file(FILE_PATH."logs/test.bin.log") !== TEST_MD5){ - console("[NOTICE] Executing integrity tests..."); + console("[NOTICE] Executing tests..."); console("[INFO] OS: ".PHP_OS.", ".Utils::getOS()); console("[INFO] uname -a: ".php_uname("a")); console("[INFO] PHP Version: ".phpversion()); @@ -59,12 +65,14 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run $test .= Utils::writeLong("-1152921504606846976"); $test .= Utils::writeTriad(16777215); $test .= Utils::writeTriad(16777216); - $str = new Java_String("THIS_IS_ a TEsT_SEED1_123456789^.,.,\xff\x00\x15"); + $str = new Java_String("TESTING\x00\n\r\t\xff"); $test .= Utils::writeLong($str->hashCode()); $test .= Utils::writeDataArray(array("a", "b", "c", "\xff\xff\xff\xff")); $test .= Utils::hexToStr("012334567890"); file_put_contents(FILE_PATH."logs/test.bin.log", $test); - if(md5($test) !== TEST_MD5){ + $md5 = md5($test); + console("[INFO] MD5 of test: ".$md5); + if($md5 !== TEST_MD5){ console("[ERROR] Test error, please send your console.log + test.bin.log to the Github repo"); die(); } @@ -297,7 +305,7 @@ class ServerAPI extends stdClass{ //Yay! I can add anything to this class in run } } - public function start(){ + public function init(){ $this->server->init(); unregister_tick_function(array($this->server, "tick")); $this->__destruct(); diff --git a/src/classes/PocketMinecraftServer.class.php b/src/classes/PocketMinecraftServer.class.php index 968613560..7b018fd4a 100644 --- a/src/classes/PocketMinecraftServer.class.php +++ b/src/classes/PocketMinecraftServer.class.php @@ -26,30 +26,30 @@ the Free Software Foundation, either version 3 of the License, or */ class PocketMinecraftServer{ - var $invisible, $api, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $timePerSecond, $responses, $spawn, $entities, $mapDir, $mapName, $map, $level, $tileEntities; - private $database, $interface, $evCnt, $handCnt, $events, $handlers, $version, $serverType, $lastTick, $ticker; - function __construct($name, $gamemode = 1, $seed = false, $port = 19132, $serverID = false){ - $this->port = (int) $port; //19132 - 19135 - $vNumber = new VersionString(); - $vNumber = $vNumber->getNumber(); - console("[INFO] PocketMine-MP ".MAJOR_VERSION." #".$vNumber." by @shoghicp, LGPL License", true, true, 0); + var $version, $invisible, $api, $tickMeasure, $preparedSQL, $seed, $gamemode, $name, $maxClients, $clients, $eidCnt, $custom, $description, $motd, $timePerSecond, $responses, $spawn, $entities, $mapDir, $mapName, $map, $levelData, $tileEntities; + private $database, $interface, $evCnt, $handCnt, $events, $handlers, $serverType, $lastTick, $ticker; + + private function load(){ + $this->version = new VersionString(); + console("[INFO] PocketMine-MP ".MAJOR_VERSION." #".$this->version->getNumber()." 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){ console("[WARNING] You've selected a not-standard port. Normal port range is from 19132 to 19135 included"); } + $this->serverID = $this->serverID === false ? Utils::readLong(Utils::getRandomBytes(8)):$this->serverID; + $this->seed = $this->seed === false ? Utils::readInt(Utils::getRandomBytes(4)):$this->seed; console("[INFO] Loading database..."); - $this->api = false; $this->startDatabase(); - $this->gamemode = (int) $gamemode; - $this->name = $name; + $this->doTick = false; + $this->api = false; $this->mapDir = false; $this->mapName = false; $this->events = array(); $this->handlers = array(); $this->map = false; $this->invisible = false; - $this->level = false; + $this->levelData = false; $this->difficulty = 1; $this->tileEntities = array(); $this->entities = array(); @@ -63,9 +63,6 @@ class PocketMinecraftServer{ $this->description = ""; $this->whitelist = false; $this->bannedIPs = array(); - $this->motd = "Welcome to ".$name; - $this->serverID = $serverID === false ? Utils::readLong(Utils::getRandomBytes(8)):$serverID; - $this->seed = $seed === false ? Utils::readInt(Utils::getRandomBytes(4)):$seed; $this->clients = array(); $this->spawn = array("x" => 128.5,"y" => 100,"z" => 128.5); $this->time = 0; @@ -77,11 +74,17 @@ class PocketMinecraftServer{ console("[INFO] Server Name: ".$this->name); console("[INFO] Server GUID: ".$this->serverID); console("[INFO] Protocol Version: ".CURRENT_PROTOCOL); - $this->stop = false; + $this->stop = false; } - public function run(){ - + function __construct($name, $gamemode = 1, $seed = false, $port = 19132, $serverID = false){ + $this->port = (int) $port; //19132 - 19135 + $this->gamemode = (int) $gamemode; + $this->name = $name; + $this->motd = "Welcome to ".$name; + $this->serverID = $serverID; + $this->seed = $seed; + $this->load(); } public function getTPS(){ @@ -238,19 +241,19 @@ class PocketMinecraftServer{ public function loadMap(){ if($this->mapName !== false and trim($this->mapName) !== ""){ - $this->level = unserialize(file_get_contents($this->mapDir."level.dat")); - console("[INFO] Map: ".$this->level["LevelName"]); - $this->time = (int) $this->level["Time"]; - $this->seed = (int) $this->level["RandomSeed"]; - if(isset($this->level["SpawnX"])){ - $this->spawn = array("x" => $this->level["SpawnX"], "y" => $this->level["SpawnY"], "z" => $this->level["SpawnZ"]); + $this->levelData = unserialize(file_get_contents($this->mapDir."level.dat")); + console("[INFO] Map: ".$this->levelData["LevelName"]); + $this->time = (int) $this->levelData["Time"]; + $this->seed = (int) $this->levelData["RandomSeed"]; + if(isset($this->levelData["SpawnX"])){ + $this->spawn = array("x" => $this->levelData["SpawnX"], "y" => $this->levelData["SpawnY"], "z" => $this->levelData["SpawnZ"]); }else{ - $this->level["SpawnX"] = $this->spawn["x"]; - $this->level["SpawnY"] = $this->spawn["y"]; - $this->level["SpawnZ"] = $this->spawn["z"]; + $this->levelData["SpawnX"] = $this->spawn["x"]; + $this->levelData["SpawnY"] = $this->spawn["y"]; + $this->levelData["SpawnZ"] = $this->spawn["z"]; } - $this->level["Time"] = &$this->time; - console("[INFO] Spawn: X ".$this->level["SpawnX"]." Y ".$this->level["SpawnY"]." Z ".$this->level["SpawnZ"]); + $this->levelData["Time"] = $this->time; + console("[INFO] Spawn: X ".$this->levelData["SpawnX"]." Y ".$this->levelData["SpawnY"]." Z ".$this->levelData["SpawnZ"]); console("[INFO] Time: ".$this->time); console("[INFO] Seed: ".$this->seed); console("[INFO] Gamemode: ".($this->gamemode === 0 ? "survival":"creative")); @@ -259,7 +262,7 @@ class PocketMinecraftServer{ console("[INFO] Loading map..."); $this->map = new ChunkParser(); if(!$this->map->loadFile($this->mapDir."chunks.dat")){ - console("[ERROR] Couldn't load the map \"".$this->level["LevelName"]."\"!", true, true, 0); + console("[ERROR] Couldn't load the map \"".$this->levelData["LevelName"]."\"!", true, true, 0); $this->map = false; }else{ $this->map->loadMap(); @@ -305,7 +308,8 @@ class PocketMinecraftServer{ public function save($final = false){ if($this->mapName !== false){ - file_put_contents($this->mapDir."level.dat", serialize($this->level)); + $this->levelData["Time"] = $this->time; + file_put_contents($this->mapDir."level.dat", serialize($this->levelData)); $this->map->saveMap($final); console("[INFO] Saving entities..."); foreach($this->entities as $entity){ @@ -322,7 +326,7 @@ class PocketMinecraftServer{ } console("[INFO] Loading events..."); $this->loadEvents(); - $this->ticker = new TickLoop; + $this->ticker = new TickLoop($this); $this->ticker->start(); declare(ticks=15); register_tick_function(array($this, "tick")); @@ -354,13 +358,21 @@ class PocketMinecraftServer{ } public function tick(){ - if($this->ticker->isWaiting() === true){ + /*if($this->ticker->tick === true and $this->ticker->isWaiting() === true){ + $this->ticker->tick = false; $time = microtime(true); array_shift($this->tickMeasure); $this->tickMeasure[] = $this->lastTick = $time; $this->tickerFunction($time); $this->trigger("server.tick", $time); $this->ticker->notify(); + }*/ + $time = microtime(true); + if($this->lastTick <= ($time - 0.05)){ + array_shift($this->tickMeasure); + $this->tickMeasure[] = $this->lastTick = $time; + $this->tickerFunction($time); + $this->trigger("server.tick", $time); } } diff --git a/src/classes/UDPSocket.class.php b/src/classes/UDPSocket.class.php index bb703798c..124aa91b8 100644 --- a/src/classes/UDPSocket.class.php +++ b/src/classes/UDPSocket.class.php @@ -27,7 +27,7 @@ the Free Software Foundation, either version 3 of the License, or -class UDPSocket extends Thread{ +class UDPSocket{ private $encrypt; var $buffer, $connected, $errors, $sock, $server; function __construct($server, $port, $listen = false, $socket = false){ @@ -56,10 +56,6 @@ class UDPSocket extends Thread{ } } } - - public function run(){ - - } public function listenSocket(){ $sock = @socket_accept($this->sock); diff --git a/src/classes/Utils.class.php b/src/classes/Utils.class.php index f5107c45d..0ffa02461 100644 --- a/src/classes/Utils.class.php +++ b/src/classes/Utils.class.php @@ -35,7 +35,11 @@ define("BIG_ENDIAN", 0x00); define("LITTLE_ENDIAN", 0x01); define("ENDIANNESS", (pack("d", 1) === "\77\360\0\0\0\0\0\0" ? BIG_ENDIAN:LITTLE_ENDIAN)); -class Utils{ +class Utils extends Thread{ + + public function run(){ + + } public static function getOS(){ $uname = strtoupper(php_uname("s")); diff --git a/src/common/functions.php b/src/common/functions.php index a3bf2d1f9..e237a82a1 100644 --- a/src/common/functions.php +++ b/src/common/functions.php @@ -165,7 +165,6 @@ function arguments ( $args ){ } function console($message, $EOL = true, $log = true, $level = 1){ - //global $path; if(!defined("DEBUG") or DEBUG >= $level){ $message .= $EOL === true ? PHP_EOL:""; $message = date("H:i:s"). " ". $message; diff --git a/src/misc/utils/TickLoop.php b/src/misc/utils/TickLoop.php index 65b86b904..c54c39a2b 100644 --- a/src/misc/utils/TickLoop.php +++ b/src/misc/utils/TickLoop.php @@ -26,13 +26,20 @@ the Free Software Foundation, either version 3 of the License, or */ class TickLoop extends Thread{ - var $stop = false, $lastTick = 0; + var $tick = false, $stop = false, $lastTick = 0; + private $server; + public function __construct(PocketMinecraftServer $server){ + $this->server = $server; + } public function run(){ while($this->stop !== true){ + usleep(1); $time = microtime(true); if($this->lastTick <= ($time - 0.05)){ $this->lastTick = $time; + $this->tick = true; $this->wait(); + $this->tick = false; } } exit(0);