Changing code parts [Hey! Don't look here!]

This commit is contained in:
Shoghi Cervantes Pueyo 2013-01-10 23:59:03 +01:00
parent eebbc263b0
commit 8d3ad0c5ec
7 changed files with 74 additions and 51 deletions

View File

@ -30,10 +30,7 @@ require_once("classes/PocketMinecraftServer.class.php");
require_once("API/ServerAPI.php"); require_once("API/ServerAPI.php");
$server = new ServerAPI(); $server = new ServerAPI();
if($server->start() !== true){ $server->run();//$server->start();
//Stop //$server->join();
}else{
//Restart
}
kill(getmypid()); //Fix for segfault kill(getmypid()); //Fix for segfault

View File

@ -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; var $restart = false;
private $server, $config, $apiList = array(); 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."logs/", 0777, true);
@mkdir(FILE_PATH."players/", 0777); @mkdir(FILE_PATH."players/", 0777);
@mkdir(FILE_PATH."worlds/", 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..."); console("[INFO] Starting ServerAPI server handler...");
file_put_contents(FILE_PATH."logs/packets.log", ""); 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){ 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] OS: ".PHP_OS.", ".Utils::getOS());
console("[INFO] uname -a: ".php_uname("a")); console("[INFO] uname -a: ".php_uname("a"));
console("[INFO] PHP Version: ".phpversion()); 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::writeLong("-1152921504606846976");
$test .= Utils::writeTriad(16777215); $test .= Utils::writeTriad(16777215);
$test .= Utils::writeTriad(16777216); $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::writeLong($str->hashCode());
$test .= Utils::writeDataArray(array("a", "b", "c", "\xff\xff\xff\xff")); $test .= Utils::writeDataArray(array("a", "b", "c", "\xff\xff\xff\xff"));
$test .= Utils::hexToStr("012334567890"); $test .= Utils::hexToStr("012334567890");
file_put_contents(FILE_PATH."logs/test.bin.log", $test); 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"); console("[ERROR] Test error, please send your console.log + test.bin.log to the Github repo");
die(); 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(); $this->server->init();
unregister_tick_function(array($this->server, "tick")); unregister_tick_function(array($this->server, "tick"));
$this->__destruct(); $this->__destruct();

View File

@ -26,30 +26,30 @@ the Free Software Foundation, either version 3 of the License, or
*/ */
class PocketMinecraftServer{ 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; 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, $version, $serverType, $lastTick, $ticker; private $database, $interface, $evCnt, $handCnt, $events, $handlers, $serverType, $lastTick, $ticker;
function __construct($name, $gamemode = 1, $seed = false, $port = 19132, $serverID = false){
$this->port = (int) $port; //19132 - 19135 private function load(){
$vNumber = new VersionString(); $this->version = new VersionString();
$vNumber = $vNumber->getNumber(); console("[INFO] PocketMine-MP ".MAJOR_VERSION." #".$this->version->getNumber()." by @shoghicp, LGPL License", true, true, 0);
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("[DEBUG] Target Minecraft PE: ".CURRENT_MINECRAFT_VERSION, true, true, 2);
console("[INFO] Starting Minecraft PE Server at *:".$this->port); console("[INFO] Starting Minecraft PE Server at *:".$this->port);
if($this->port < 19132 or $this->port > 19135){ 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"); 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..."); console("[INFO] Loading database...");
$this->api = false;
$this->startDatabase(); $this->startDatabase();
$this->gamemode = (int) $gamemode; $this->doTick = false;
$this->name = $name; $this->api = false;
$this->mapDir = false; $this->mapDir = false;
$this->mapName = false; $this->mapName = false;
$this->events = array(); $this->events = array();
$this->handlers = array(); $this->handlers = array();
$this->map = false; $this->map = false;
$this->invisible = false; $this->invisible = false;
$this->level = false; $this->levelData = false;
$this->difficulty = 1; $this->difficulty = 1;
$this->tileEntities = array(); $this->tileEntities = array();
$this->entities = array(); $this->entities = array();
@ -63,9 +63,6 @@ class PocketMinecraftServer{
$this->description = ""; $this->description = "";
$this->whitelist = false; $this->whitelist = false;
$this->bannedIPs = array(); $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->clients = array();
$this->spawn = array("x" => 128.5,"y" => 100,"z" => 128.5); $this->spawn = array("x" => 128.5,"y" => 100,"z" => 128.5);
$this->time = 0; $this->time = 0;
@ -80,8 +77,14 @@ class PocketMinecraftServer{
$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(){ public function getTPS(){
@ -238,19 +241,19 @@ class PocketMinecraftServer{
public function loadMap(){ public function loadMap(){
if($this->mapName !== false and trim($this->mapName) !== ""){ if($this->mapName !== false and trim($this->mapName) !== ""){
$this->level = unserialize(file_get_contents($this->mapDir."level.dat")); $this->levelData = unserialize(file_get_contents($this->mapDir."level.dat"));
console("[INFO] Map: ".$this->level["LevelName"]); console("[INFO] Map: ".$this->levelData["LevelName"]);
$this->time = (int) $this->level["Time"]; $this->time = (int) $this->levelData["Time"];
$this->seed = (int) $this->level["RandomSeed"]; $this->seed = (int) $this->levelData["RandomSeed"];
if(isset($this->level["SpawnX"])){ if(isset($this->levelData["SpawnX"])){
$this->spawn = array("x" => $this->level["SpawnX"], "y" => $this->level["SpawnY"], "z" => $this->level["SpawnZ"]); $this->spawn = array("x" => $this->levelData["SpawnX"], "y" => $this->levelData["SpawnY"], "z" => $this->levelData["SpawnZ"]);
}else{ }else{
$this->level["SpawnX"] = $this->spawn["x"]; $this->levelData["SpawnX"] = $this->spawn["x"];
$this->level["SpawnY"] = $this->spawn["y"]; $this->levelData["SpawnY"] = $this->spawn["y"];
$this->level["SpawnZ"] = $this->spawn["z"]; $this->levelData["SpawnZ"] = $this->spawn["z"];
} }
$this->level["Time"] = &$this->time; $this->levelData["Time"] = $this->time;
console("[INFO] Spawn: X ".$this->level["SpawnX"]." Y ".$this->level["SpawnY"]." Z ".$this->level["SpawnZ"]); console("[INFO] Spawn: X ".$this->levelData["SpawnX"]." Y ".$this->levelData["SpawnY"]." Z ".$this->levelData["SpawnZ"]);
console("[INFO] Time: ".$this->time); console("[INFO] Time: ".$this->time);
console("[INFO] Seed: ".$this->seed); console("[INFO] Seed: ".$this->seed);
console("[INFO] Gamemode: ".($this->gamemode === 0 ? "survival":"creative")); console("[INFO] Gamemode: ".($this->gamemode === 0 ? "survival":"creative"));
@ -259,7 +262,7 @@ class PocketMinecraftServer{
console("[INFO] Loading map..."); console("[INFO] Loading map...");
$this->map = new ChunkParser(); $this->map = new ChunkParser();
if(!$this->map->loadFile($this->mapDir."chunks.dat")){ 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; $this->map = false;
}else{ }else{
$this->map->loadMap(); $this->map->loadMap();
@ -305,7 +308,8 @@ class PocketMinecraftServer{
public function save($final = false){ public function save($final = false){
if($this->mapName !== 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); $this->map->saveMap($final);
console("[INFO] Saving entities..."); console("[INFO] Saving entities...");
foreach($this->entities as $entity){ foreach($this->entities as $entity){
@ -322,7 +326,7 @@ class PocketMinecraftServer{
} }
console("[INFO] Loading events..."); console("[INFO] Loading events...");
$this->loadEvents(); $this->loadEvents();
$this->ticker = new TickLoop; $this->ticker = new TickLoop($this);
$this->ticker->start(); $this->ticker->start();
declare(ticks=15); declare(ticks=15);
register_tick_function(array($this, "tick")); register_tick_function(array($this, "tick"));
@ -354,13 +358,21 @@ class PocketMinecraftServer{
} }
public function tick(){ 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); $time = microtime(true);
array_shift($this->tickMeasure); array_shift($this->tickMeasure);
$this->tickMeasure[] = $this->lastTick = $time; $this->tickMeasure[] = $this->lastTick = $time;
$this->tickerFunction($time); $this->tickerFunction($time);
$this->trigger("server.tick", $time); $this->trigger("server.tick", $time);
$this->ticker->notify(); $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);
} }
} }

View File

@ -27,7 +27,7 @@ the Free Software Foundation, either version 3 of the License, or
class UDPSocket extends Thread{ class UDPSocket{
private $encrypt; private $encrypt;
var $buffer, $connected, $errors, $sock, $server; var $buffer, $connected, $errors, $sock, $server;
function __construct($server, $port, $listen = false, $socket = false){ function __construct($server, $port, $listen = false, $socket = false){
@ -57,10 +57,6 @@ class UDPSocket extends Thread{
} }
} }
public function run(){
}
public function listenSocket(){ public function listenSocket(){
$sock = @socket_accept($this->sock); $sock = @socket_accept($this->sock);
if($sock !== false){ if($sock !== false){

View File

@ -35,7 +35,11 @@ define("BIG_ENDIAN", 0x00);
define("LITTLE_ENDIAN", 0x01); define("LITTLE_ENDIAN", 0x01);
define("ENDIANNESS", (pack("d", 1) === "\77\360\0\0\0\0\0\0" ? BIG_ENDIAN:LITTLE_ENDIAN)); 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(){ public static function getOS(){
$uname = strtoupper(php_uname("s")); $uname = strtoupper(php_uname("s"));

View File

@ -165,7 +165,6 @@ function arguments ( $args ){
} }
function console($message, $EOL = true, $log = true, $level = 1){ function console($message, $EOL = true, $log = true, $level = 1){
//global $path;
if(!defined("DEBUG") or DEBUG >= $level){ if(!defined("DEBUG") or DEBUG >= $level){
$message .= $EOL === true ? PHP_EOL:""; $message .= $EOL === true ? PHP_EOL:"";
$message = date("H:i:s"). " ". $message; $message = date("H:i:s"). " ". $message;

View File

@ -26,13 +26,20 @@ the Free Software Foundation, either version 3 of the License, or
*/ */
class TickLoop extends Thread{ 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(){ public function run(){
while($this->stop !== true){ while($this->stop !== true){
usleep(1);
$time = microtime(true); $time = microtime(true);
if($this->lastTick <= ($time - 0.05)){ if($this->lastTick <= ($time - 0.05)){
$this->lastTick = $time; $this->lastTick = $time;
$this->tick = true;
$this->wait(); $this->wait();
$this->tick = false;
} }
} }
exit(0); exit(0);