diff --git a/plugin.yml b/plugin.yml index 96999e4..f1db96d 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,8 +1,8 @@ name: PocketDockConsole prefix: PDC main: PocketDockConsole\Main -version: 0.0.13 -api: [1.12.0] +version: 0.0.14 +api: [1.0.0] load: POSTWORLD author: humerusj diff --git a/resources/PluginIndex.html b/resources/PluginIndex.html index 0862105..3eb9f1c 100755 --- a/resources/PluginIndex.html +++ b/resources/PluginIndex.html @@ -198,7 +198,7 @@ function getPlugins() { $.ajax({ type: "GET", - url: "http://sleepy-wave-2826.herokuapp.com/plugins", + url: "http://pluginsearch.pocketmine.net/plugins", contentType: "application/json; charset=utf-8", dataType: "json", data: "", @@ -227,7 +227,7 @@ minLength: 0, source: function(request, response) { $.ajax({ - url: "http://sleepy-wave-2826.herokuapp.com/autocomplete?q=" + request.term, + url: "http://pluginsearch.pocketmine.net/autocomplete?q=" + request.term, type: "GET", dataType: "JSON", success: function(data) { diff --git a/src/PocketDockConsole/Main.php b/src/PocketDockConsole/Main.php index f86ab40..75b0df7 100644 --- a/src/PocketDockConsole/Main.php +++ b/src/PocketDockConsole/Main.php @@ -23,7 +23,11 @@ public function onEnable() { $this->reloadConfig(); $this->getLogger()->info(TextFormat::DARK_GREEN . "Enabled"); $this->setPassword(); - $this->thread = new PDCServer("0.0.0.0", $this->getConfig()->get("port"), $this->getServer()->getLogger(), $this->getServer()->getLoader(), $this->getConfig()->get("password"), stream_get_contents($pluginIndex = $this->getResource("PluginIndex.html")), $this->getConfig()->get("backlog")); + $this->legacy = true; + if (method_exists($this->getServer(), "getNetwork")) { + $this->legacy = false; + } + $this->thread = new PDCServer("0.0.0.0", $this->getConfig()->get("port"), $this->getServer()->getLogger(), $this->getServer()->getLoader(), $this->getConfig()->get("password"), stream_get_contents($pluginIndex = $this->getResource("PluginIndex.html")), $this->getConfig()->get("backlog"), $this->legacy); @fclose($pluginIndex); $this->rc = new RunCommand($this); $this->getServer()->getScheduler()->scheduleRepeatingTask($this->rc, 1); diff --git a/src/PocketDockConsole/PDCApp.php b/src/PocketDockConsole/PDCApp.php index 902c347..a0a122b 100644 --- a/src/PocketDockConsole/PDCApp.php +++ b/src/PocketDockConsole/PDCApp.php @@ -13,7 +13,6 @@ class PDCApp extends \Wrench\Application\Application { public function __construct($thread, $password) { $this->thread = $thread; $this->password = $password; - } /** * @see Wrench\Application.Application::onConnect() @@ -43,7 +42,10 @@ public function onUpdate() { } } } - $this->thread->lastLine = count($stuffArray); + //$this->thread->lastLine = count($stuffArray); + $this->thread->lastLine = 1; + $this->thread->stuffToSend = ""; + $this->thread->clearstream = true; $sendto->send($this->thread->stuffTitle); } $jsonArray = explode("\n", $this->thread->jsonStream); @@ -58,7 +60,9 @@ public function onUpdate() { } } } - $this->thread->lastLineJSON = count($jsonArray); + //$this->thread->lastLineJSON = count($jsonArray); + $this->thread->lastLineJSON = 1; + $this->thread->jsonStream = ""; $sendto->send($this->thread->stuffTitle); } } diff --git a/src/PocketDockConsole/PDCServer.php b/src/PocketDockConsole/PDCServer.php index 80a6084..f3a3cba 100644 --- a/src/PocketDockConsole/PDCServer.php +++ b/src/PocketDockConsole/PDCServer.php @@ -16,7 +16,7 @@ class PDCServer extends \pocketmine\Thread { public $stuffTitle = ""; public $stop = false; - public function __construct($host, $port, $logger, $loader, $password, $html, $backlog) { + public function __construct($host, $port, $logger, $loader, $password, $html, $backlog, $legacy = false) { $this->host = $host; $this->port = $port; $this->password = $password; @@ -25,7 +25,8 @@ public function __construct($host, $port, $logger, $loader, $password, $html, $b $this->data = $html; $this->backlog = $backlog; $this->clienttokill = ""; - $this->sendUpate = false; + $this->sendUpate = false;; + $this->legacy = $legacy; $oldloadPaths = array(); $this->addDependency($oldloadPaths, new \ReflectionClass($logger)); $this->addDependency($oldloadPaths, new \ReflectionClass($loader)); @@ -64,9 +65,14 @@ public function run() { } } $this->loader->register(true); - Terminal::init(); + + if (!$this->legacy) { + Terminal::init(); + } + $server = new \Wrench\Server('ws://' . $this->host . ':' . $this->port, array("logger" => function ($msg, $pri) { }), $this); + $server->registerApplication("app", new PDCApp($this, $this->password)); $server->addListener(\Wrench\Server::EVENT_SOCKET_CONNECT, function ($data, $other) { $header = $other->getSocket()->receive(); diff --git a/src/PocketDockConsole/RunCommand.php b/src/PocketDockConsole/RunCommand.php index d1b5003..d80b711 100644 --- a/src/PocketDockConsole/RunCommand.php +++ b/src/PocketDockConsole/RunCommand.php @@ -13,9 +13,6 @@ class RunCommand extends PluginTask { public function __construct($owner) { $this->owner = $owner; - $interfaces = $this->getOwner()->getServer()->getInterfaces(); - $values = array_values($interfaces); - $this->mainInterface = $values[0]; } public function onRun($currentTick) { @@ -36,16 +33,24 @@ public function onRun($currentTick) { $this->getOwner()->thread->buffer = ""; $this->updateInfo(); } + if ($this->getOwner()->thread->sendUpdate) { $this->updateInfo(); $this->getOwner()->sendFiles(); } + $this->getOwner()->thread->sendUpdate = false; + if (substr($currentTick, -2) == 20) { $this->updateInfo(); $this->getOwner()->thread->sendUpdate = false; $this->getOwner()->thread->buffer = ""; } + + if ($this->getOwner()->thread->clearstream) { + $this->getOwner()->attachment->stream = ""; + $this->getOwner()->thread->clearstream = false; + } } public function isJSON($string) { @@ -174,14 +179,25 @@ public function parseJSON($string) { public function updateInfo($user = "") { $data = array("type" => "data", "data" => array("players" => $this->sendPlayers($user), "bans" => $this->sendNameBans(), "ipbans" => $this->sendIPBans(), "ops" => $this->sendOps(), "plugins" => $this->sendPlugins())); $this->getOwner()->thread->jsonStream.= json_encode($data) . "\n"; - $u = Utils::getMemoryUsage(true); - $d = Utils::getRealMemoryUsage(); - $usage = round(($u[0] / 1024) / 1024, 2) . "/" . round(($d[0] / 1024) / 1024, 2) . "/" . round(($u[1] / 1024) / 1024, 2) . "/".round(($u[2] / 1024) / 1024, 2)." MB @ " . Utils::getThreadCount() . " threads"; - $title = "\x1b]0;" . $this->getOwner()->getServer()->getName() . " " . $this->getOwner()->getServer()->getPocketMineVersion() . " | Online " . count($this->getOwner()->getServer()->getOnlinePlayers()) . "/" . $this->getOwner()->getServer()->getMaxPlayers() . " | Memory " . $usage . " | U " . round($this->getOwner()->getServer()->getNetwork()->getUpload() / 1024, 2) . " D " . round($this->getOwner()->getServer()->getNetwork()->getDownload() / 1024, 2) . " kB/s | TPS " . $this->getOwner()->getServer()->getTicksPerSecond() . " | Load " . $this->getOwner()->getServer()->getTickUsage() . "%\x07"; + if (!$this->getOwner()->legacy) { + $u = $this->getMemoryUsage(true); + $d = $this->getRealMemoryUsage(); + $usage = round(($u[0] / 1024) / 1024, 2) . "/" . round(($d[0] / 1024) / 1024, 2) . "/" . round(($u[1] / 1024) / 1024, 2) . "/" . round(($u[2] / 1024) / 1024, 2) . " MB @ " . $this->getThreadCount() . " threads"; + $title = "\x1b]0;" . $this->getOwner()->getServer()->getName() . " " . $this->getOwner()->getServer()->getPocketMineVersion() . " | Online " . count($this->getOwner()->getServer()->getOnlinePlayers()) . "/" . $this->getOwner()->getServer()->getMaxPlayers() . " | Memory " . $usage . " | U " . round($this->getOwner()->getServer()->getNetwork()->getUpload() / 1024, 2) . " D " . round($this->getOwner()->getServer()->getNetwork()->getDownload() / 1024, 2) . " kB/s | TPS " . $this->getOwner()->getServer()->getTicksPerSecond() . " | Load " . $this->getOwner()->getServer()->getTickUsage() . "%\x07"; + } else { + $this->backwardsCompat(); + $title = "\x1b]0;PocketMine-MP " . $this->getOwner()->getServer()->getPocketMineVersion() . " | Online " . count($this->getOwner()->getServer()->getOnlinePlayers()) . "/" . $this->getOwner()->getServer()->getMaxPlayers() . " | RAM " . round((memory_get_usage() / 1024) / 1024, 2) . "/" . round((memory_get_usage(true) / 1024) / 1024, 2) . " MB | U " . round($this->mainInterface->getUploadUsage() / 1024, 2) . " D " . round($this->mainInterface->getDownloadUsage() / 1024, 2) . " kB/s | TPS " . $this->getOwner()->getServer()->getTicksPerSecond() . " | Load " . $this->getOwner()->getServer()->getTickUsage() . "%\x07"; + } $this->getOwner()->thread->stuffTitle = $title; return true; } + public function backwardsCompat() { + $interfaces = $this->getOwner()->getServer()->getInterfaces(); + $values = array_values($interfaces); + $this->mainInterface = $values[0]; + } + public function sendPlugins() { foreach ($this->getOwner()->getServer()->getPluginManager()->getPlugins() as $plugin) { $names[] = str_replace(" ", "-", $plugin->getName()); @@ -276,4 +292,61 @@ public function getUrl($name) { } } + # Taken from PocketMine-MP (new versions) for backwards compatibility + + public function getMemoryUsage($advanced = false) { + $reserved = memory_get_usage(); + $VmSize = null; + $VmRSS = null; + if (Utils::getOS() === "linux" or Utils::getOS() === "android") { + $status = file_get_contents("/proc/self/status"); + if (preg_match("/VmRSS:[ \t]+([0-9]+) kB/", $status, $matches) > 0) { + $VmRSS = $matches[1] * 1024; + } + if (preg_match("/VmSize:[ \t]+([0-9]+) kB/", $status, $matches) > 0) { + $VmSize = $matches[1] * 1024; + } + } + //TODO: more OS + if ($VmRSS === null) { + $VmRSS = memory_get_usage(); + } + if (!$advanced) { + return $VmRSS; + } + if ($VmSize === null) { + $VmSize = memory_get_usage(true); + } + return [$reserved, $VmRSS, $VmSize]; + } + + public function getRealMemoryUsage() { + $stack = 0; + $heap = 0; + if (Utils::getOS() === "linux" or Utils::getOS() === "android") { + $mappings = file("/proc/self/maps"); + foreach ($mappings as $line) { + if (preg_match("#([a-z0-9]+)\\-([a-z0-9]+) [rwxp\\-]{4} [a-z0-9]+ [^\\[]*\\[([a-zA-z0-9]+)\\]#", trim($line), $matches) > 0) { + if (strpos($matches[3], "heap") === 0) { + $heap+= hexdec($matches[2]) - hexdec($matches[1]); + } elseif (strpos($matches[3], "stack") === 0) { + $stack+= hexdec($matches[2]) - hexdec($matches[1]); + } + } + } + } + return [$heap, $stack]; + } + + public function getThreadCount() { + if (Utils::getOS() === "linux" or Utils::getOS() === "android") { + if (preg_match("/Threads:[ \t]+([0-9]+)/", file_get_contents("/proc/self/status"), $matches) > 0) { + return (int)$matches[1]; + } + } + //TODO: more OS + return count(\pocketmine\ThreadManager::getInstance()->getAll()) + 3; //RakLib + MainLogger + Main Thread + + } + }