Skip to content

Commit

Permalink
Merge pull request #19 from PocketDock/back-compat
Browse files Browse the repository at this point in the history
Backwards compatibility
  • Loading branch information
Humerus committed Dec 25, 2015
2 parents d8f20f8 + 770957d commit cddaa48
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 18 deletions.
4 changes: 2 additions & 2 deletions plugin.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions resources/PluginIndex.html
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion src/PocketDockConsole/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/PocketDockConsole/PDCApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/PocketDockConsole/PDCServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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();
Expand Down
87 changes: 80 additions & 7 deletions src/PocketDockConsole/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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

}

}

0 comments on commit cddaa48

Please sign in to comment.