From 6a996611f898dcdd7f98b1be4a6f0ac4d2fc7d42 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 19 Feb 2018 11:56:22 +0000 Subject: [PATCH] Move functions from PocketMine.php to Utils This cleans a lot of mess out of the bootstrap file, and also has the added bonus that threads which do not inherit functions can now get access to them by autoloading Utils. --- src/pocketmine/CrashDump.php | 6 +- src/pocketmine/PocketMine.php | 81 +------------------ src/pocketmine/Server.php | 8 +- .../network/mcpe/protocol/LoginPacket.php | 2 +- src/pocketmine/utils/MainLogger.php | 4 +- src/pocketmine/utils/ServerKiller.php | 2 +- src/pocketmine/utils/Utils.php | 79 ++++++++++++++++++ 7 files changed, 91 insertions(+), 91 deletions(-) diff --git a/src/pocketmine/CrashDump.php b/src/pocketmine/CrashDump.php index d34a533e5..04840952a 100644 --- a/src/pocketmine/CrashDump.php +++ b/src/pocketmine/CrashDump.php @@ -148,7 +148,7 @@ class CrashDump{ $error = $lastExceptionError; }else{ $error = (array) error_get_last(); - $error["trace"] = getTrace(4); //Skipping CrashDump->baseCrash, CrashDump->construct, Server->crashDump + $error["trace"] = Utils::getTrace(4); //Skipping CrashDump->baseCrash, CrashDump->construct, Server->crashDump $errorConversion = [ E_ERROR => "E_ERROR", E_WARNING => "E_WARNING", @@ -167,7 +167,7 @@ class CrashDump{ E_USER_DEPRECATED => "E_USER_DEPRECATED" ]; $error["fullFile"] = $error["file"]; - $error["file"] = cleanPath($error["file"]); + $error["file"] = Utils::cleanPath($error["file"]); $error["type"] = $errorConversion[$error["type"]] ?? $error["type"]; if(($pos = strpos($error["message"], "\n")) !== false){ $error["message"] = substr($error["message"], 0, $pos); @@ -195,7 +195,7 @@ class CrashDump{ $file = $reflection->getProperty("file"); $file->setAccessible(true); foreach($this->server->getPluginManager()->getPlugins() as $plugin){ - $filePath = \pocketmine\cleanPath($file->getValue($plugin)); + $filePath = Utils::cleanPath($file->getValue($plugin)); if(strpos($error["file"], $filePath) === 0){ $this->data["plugin"] = $plugin->getName(); $this->addLine("BAD PLUGIN: " . $plugin->getDescription()->getFullName()); diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index 0c17a258b..021e743a8 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -261,85 +261,6 @@ namespace pocketmine { } } - function kill($pid){ - global $logger; - if($logger instanceof MainLogger){ - $logger->syncFlushBuffer(); - } - switch(Utils::getOS()){ - case "win": - exec("taskkill.exe /F /PID " . ((int) $pid) . " > NUL"); - break; - case "mac": - case "linux": - default: - if(function_exists("posix_kill")){ - posix_kill($pid, 9); //SIGKILL - }else{ - exec("kill -9 " . ((int) $pid) . " > /dev/null 2>&1"); - } - } - } - - /** - * @param object $value - * @param bool $includeCurrent - * - * @return int - */ - function getReferenceCount($value, $includeCurrent = true){ - ob_start(); - debug_zval_dump($value); - $ret = explode("\n", ob_get_contents()); - ob_end_clean(); - - if(count($ret) >= 1 and preg_match('/^.* refcount\\(([0-9]+)\\)\\{$/', trim($ret[0]), $m) > 0){ - return ((int) $m[1]) - ($includeCurrent ? 3 : 4); //$value + zval call + extra call - } - return -1; - } - - /** - * @param int $start - * @param array|null $trace - * - * @return array - */ - function getTrace($start = 0, $trace = null){ - if($trace === null){ - if(function_exists("xdebug_get_function_stack")){ - $trace = array_reverse(xdebug_get_function_stack()); - }else{ - $e = new \Exception(); - $trace = $e->getTrace(); - } - } - - $messages = []; - $j = 0; - for($i = (int) $start; isset($trace[$i]); ++$i, ++$j){ - $params = ""; - if(isset($trace[$i]["args"]) or isset($trace[$i]["params"])){ - if(isset($trace[$i]["args"])){ - $args = $trace[$i]["args"]; - }else{ - $args = $trace[$i]["params"]; - } - - $params = implode(", ", array_map(function($value){ - return (is_object($value) ? get_class($value) . " object" : gettype($value) . " " . (is_array($value) ? "Array()" : Utils::printable(@strval($value)))); - }, $args)); - } - $messages[] = "#$j " . (isset($trace[$i]["file"]) ? cleanPath($trace[$i]["file"]) : "") . "(" . (isset($trace[$i]["line"]) ? $trace[$i]["line"] : "") . "): " . (isset($trace[$i]["class"]) ? $trace[$i]["class"] . (($trace[$i]["type"] === "dynamic" or $trace[$i]["type"] === "->") ? "->" : "::") : "") . $trace[$i]["function"] . "(" . Utils::printable($params) . ")"; - } - - return $messages; - } - - function cleanPath($path){ - return str_replace(["\\", ".php", "phar://", str_replace(["\\", "phar://"], ["/", ""], \pocketmine\PATH), str_replace(["\\", "phar://"], ["/", ""], \pocketmine\PLUGIN_PATH)], ["/", "", "", "", ""], $path); - } - if(extension_loaded("xdebug")){ $logger->warning(PHP_EOL . PHP_EOL . PHP_EOL . "\tYou are running " . \pocketmine\NAME . " with xdebug enabled. This has a major impact on performance." . PHP_EOL . PHP_EOL); } @@ -394,7 +315,7 @@ namespace pocketmine { if(\pocketmine\DEBUG > 1){ echo "Some threads could not be stopped, performing a force-kill" . PHP_EOL . PHP_EOL; } - kill(getmypid()); + Utils::kill(getmypid()); } }while(false); diff --git a/src/pocketmine/Server.php b/src/pocketmine/Server.php index a801196d6..caec8746a 100644 --- a/src/pocketmine/Server.php +++ b/src/pocketmine/Server.php @@ -2113,7 +2113,7 @@ class Server{ }catch(\Throwable $e){ $this->logger->logException($e); $this->logger->emergency("Crashed while crashing, killing process"); - @kill(getmypid()); + @Utils::kill(getmypid()); } } @@ -2195,7 +2195,7 @@ class Server{ $errstr = preg_replace('/\s+/', ' ', trim($errstr)); - $errfile = cleanPath($errfile); + $errfile = Utils::cleanPath($errfile); $this->logger->logException($e, $trace); @@ -2205,7 +2205,7 @@ class Server{ "fullFile" => $e->getFile(), "file" => $errfile, "line" => $errline, - "trace" => getTrace(0, $trace) + "trace" => Utils::getTrace(0, $trace) ]; global $lastExceptionError, $lastError; @@ -2277,7 +2277,7 @@ class Server{ $this->forceShutdown(); $this->isRunning = false; - @kill(getmypid()); + @Utils::kill(getmypid()); exit(1); } diff --git a/src/pocketmine/network/mcpe/protocol/LoginPacket.php b/src/pocketmine/network/mcpe/protocol/LoginPacket.php index a96385c3b..24f9c520d 100644 --- a/src/pocketmine/network/mcpe/protocol/LoginPacket.php +++ b/src/pocketmine/network/mcpe/protocol/LoginPacket.php @@ -95,7 +95,7 @@ class LoginPacket extends DataPacket{ $logger = MainLogger::getLogger(); $logger->debug(get_class($e) . " was thrown while decoding connection request in login (protocol version " . ($this->protocol ?? "unknown") . "): " . $e->getMessage()); - foreach(\pocketmine\getTrace(0, $e->getTrace()) as $line){ + foreach(Utils::getTrace(0, $e->getTrace()) as $line){ $logger->debug($line); } } diff --git a/src/pocketmine/utils/MainLogger.php b/src/pocketmine/utils/MainLogger.php index 36427ece0..00a5dcf71 100644 --- a/src/pocketmine/utils/MainLogger.php +++ b/src/pocketmine/utils/MainLogger.php @@ -159,9 +159,9 @@ class MainLogger extends \AttachableThreadedLogger{ } $errno = $errorConversion[$errno] ?? $errno; $errstr = preg_replace('/\s+/', ' ', trim($errstr)); - $errfile = \pocketmine\cleanPath($errfile); + $errfile = Utils::cleanPath($errfile); $this->log($type, get_class($e) . ": \"$errstr\" ($errno) in \"$errfile\" at line $errline"); - foreach(\pocketmine\getTrace(0, $trace) as $i => $line){ + foreach(Utils::getTrace(0, $trace) as $i => $line){ $this->debug($line, true); } diff --git a/src/pocketmine/utils/ServerKiller.php b/src/pocketmine/utils/ServerKiller.php index 40298911b..6c9a3a9bc 100644 --- a/src/pocketmine/utils/ServerKiller.php +++ b/src/pocketmine/utils/ServerKiller.php @@ -40,7 +40,7 @@ class ServerKiller extends Thread{ }); if(time() - $start >= $this->time){ echo "\nTook too long to stop, server was killed forcefully!\n"; - @\pocketmine\kill(getmypid()); + @Utils::kill(getmypid()); } } diff --git a/src/pocketmine/utils/Utils.php b/src/pocketmine/utils/Utils.php index bb4d5f872..adc2ba9e4 100644 --- a/src/pocketmine/utils/Utils.php +++ b/src/pocketmine/utils/Utils.php @@ -543,4 +543,83 @@ class Utils{ return json_decode(base64_decode(strtr($payloadB64, '-_', '+/'), true), true); } + + public static function kill($pid) : void{ + global $logger; + if($logger instanceof MainLogger){ + $logger->syncFlushBuffer(); + } + switch(Utils::getOS()){ + case "win": + exec("taskkill.exe /F /PID " . ((int) $pid) . " > NUL"); + break; + case "mac": + case "linux": + default: + if(function_exists("posix_kill")){ + posix_kill($pid, 9); //SIGKILL + }else{ + exec("kill -9 " . ((int) $pid) . " > /dev/null 2>&1"); + } + } + } + + /** + * @param object $value + * @param bool $includeCurrent + * + * @return int + */ + public static function getReferenceCount($value, $includeCurrent = true){ + ob_start(); + debug_zval_dump($value); + $ret = explode("\n", ob_get_contents()); + ob_end_clean(); + + if(count($ret) >= 1 and preg_match('/^.* refcount\\(([0-9]+)\\)\\{$/', trim($ret[0]), $m) > 0){ + return ((int) $m[1]) - ($includeCurrent ? 3 : 4); //$value + zval call + extra call + } + return -1; + } + + /** + * @param int $start + * @param array|null $trace + * + * @return array + */ + public static function getTrace($start = 0, $trace = null){ + if($trace === null){ + if(function_exists("xdebug_get_function_stack")){ + $trace = array_reverse(xdebug_get_function_stack()); + }else{ + $e = new \Exception(); + $trace = $e->getTrace(); + } + } + + $messages = []; + $j = 0; + for($i = (int) $start; isset($trace[$i]); ++$i, ++$j){ + $params = ""; + if(isset($trace[$i]["args"]) or isset($trace[$i]["params"])){ + if(isset($trace[$i]["args"])){ + $args = $trace[$i]["args"]; + }else{ + $args = $trace[$i]["params"]; + } + + $params = implode(", ", array_map(function($value){ + return (is_object($value) ? get_class($value) . " object" : gettype($value) . " " . (is_array($value) ? "Array()" : Utils::printable(@strval($value)))); + }, $args)); + } + $messages[] = "#$j " . (isset($trace[$i]["file"]) ? self::cleanPath($trace[$i]["file"]) : "") . "(" . (isset($trace[$i]["line"]) ? $trace[$i]["line"] : "") . "): " . (isset($trace[$i]["class"]) ? $trace[$i]["class"] . (($trace[$i]["type"] === "dynamic" or $trace[$i]["type"] === "->") ? "->" : "::") : "") . $trace[$i]["function"] . "(" . Utils::printable($params) . ")"; + } + + return $messages; + } + + public static function cleanPath($path){ + return str_replace(["\\", ".php", "phar://", str_replace(["\\", "phar://"], ["/", ""], \pocketmine\PATH), str_replace(["\\", "phar://"], ["/", ""], \pocketmine\PLUGIN_PATH)], ["/", "", "", "", ""], $path); + } }