diff --git a/src/PocketMine/Server.php b/src/PocketMine/Server.php index bef0fee6e..b7a5a6609 100644 --- a/src/PocketMine/Server.php +++ b/src/PocketMine/Server.php @@ -694,6 +694,7 @@ class Server{ $this->pluginManager = new PluginManager($this, $this->commandMap); $this->pluginManager->subscribeToPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this->consoleSender); $this->pluginManager->registerInterface("PocketMine\\Plugin\\FolderPluginLoader"); + $this->pluginManager->registerInterface("PocketMine\\Plugin\\PharPluginLoader"); $this->pluginManager->loadPlugins($this->pluginPath); //TODO: update checking (async) diff --git a/src/PocketMine/plugin/PharPluginLoader.php b/src/PocketMine/plugin/PharPluginLoader.php new file mode 100644 index 000000000..4f5154539 --- /dev/null +++ b/src/PocketMine/plugin/PharPluginLoader.php @@ -0,0 +1,146 @@ +server = $server; + } + + /** + * Loads the plugin contained in $file + * + * @param string $file + * + * @return Plugin + */ + public function loadPlugin($file){ + if(\Phar::isValidPharFilename($file) and ($description = $this->getPluginDescription($file)) instanceof PluginDescription){ + $phar = new \Phar($file); + console("[INFO] Loading " . $description->getName()); + $dataFolder = dirname($file) . DIRECTORY_SEPARATOR . $description->getName(); + if(file_exists($dataFolder) and !is_dir($dataFolder)){ + trigger_error("Projected dataFolder '" . $dataFolder . "' for " . $description->getName() . " exists and is not a directory", E_USER_WARNING); + + return null; + } + $file = "phar://$file"; + $className = $description->getMain(); + $this->server->getLoader()->add(substr($className, 0, strpos($className, "\\")), array( + "$file/src" + )); + + if(class_exists($className, true)){ + $plugin = new $className(); + $this->initPlugin($plugin, $description, $dataFolder, $file); + + return $plugin; + }else{ + trigger_error("Couldn't load plugin " . $description->getName() . ": main class not found", E_USER_WARNING); + + return null; + } + } + return null; + } + + /** + * Gets the PluginDescription from the file + * + * @param string $file + * + * @return PluginDescription + */ + public function getPluginDescription($file){ + if(\Phar::isValidPharFilename($file)){ + $phar = new \Phar($file); + if(isset($phar["plugin.yml"])){ + $pluginYml = $phar["plugin.yml"]; + if($pluginYml instanceof \PharFileInfo){ + return new PluginDescription($pluginYml->getContent()); + } + } + } + + return null; + } + + /** + * Returns the filename patterns that this loader accepts + * + * @return array + */ + public function getPluginFilters(){ + return "/\\.phar$/i"; + } + + /** + * @param PluginBase $plugin + * @param PluginDescription $description + * @param string $dataFolder + * @param string $file + */ + private function initPlugin(PluginBase $plugin, PluginDescription $description, $dataFolder, $file){ + $plugin->init($this, $this->server, $description, $dataFolder, $file); + $plugin->onLoad(); + } + + /** + * @param Plugin $plugin + */ + public function enablePlugin(Plugin $plugin){ + if($plugin instanceof PluginBase and !$plugin->isEnabled()){ + console("[INFO] Enabling " . $plugin->getDescription()->getName()); + + $plugin->setEnabled(true); + + Server::getInstance()->getPluginManager()->callEvent(new PluginEnableEvent($plugin)); + } + } + + /** + * @param Plugin $plugin + */ + public function disablePlugin(Plugin $plugin){ + if($plugin instanceof PluginBase and $plugin->isEnabled()){ + console("[INFO] Disabling " . $plugin->getDescription()->getName()); + + Server::getInstance()->getPluginManager()->callEvent(new PluginDisableEvent($plugin)); + + $plugin->setEnabled(false); + } + } +} \ No newline at end of file diff --git a/src/PocketMine/plugin/PluginBase.php b/src/PocketMine/plugin/PluginBase.php index b41998466..3884fa0b7 100644 --- a/src/PocketMine/plugin/PluginBase.php +++ b/src/PocketMine/plugin/PluginBase.php @@ -148,7 +148,7 @@ abstract class PluginBase implements Plugin, CommandExecutor{ * @return bool */ protected function isPhar(){ - return $this->description instanceof \PharFileInfo; + return substr($this->file, 0, 7) === "phar://"; } /** @@ -160,15 +160,11 @@ abstract class PluginBase implements Plugin, CommandExecutor{ */ public function getResource($filename){ $filename = str_replace("\\", "/", $filename); - if($this->isPhar()){ - //TODO - }else{ - if(file_exists($this->dataFolder . "resources/" . $filename)){ - return file_get_contents($this->dataFolder . "resources/" . $filename); - } - - return false; + if(file_exists($this->file . "resources/" . $filename)){ + return file_get_contents($this->file . "resources/" . $filename); } + + return false; } public function saveResource($filename, $replace = false){ @@ -180,7 +176,7 @@ abstract class PluginBase implements Plugin, CommandExecutor{ return false; } - $out = $this->dataFolder . $filename; + $out = $this->file . $filename; if(!file_exists($this->dataFolder)){ @mkdir($this->dataFolder, 0755, true); } @@ -200,7 +196,7 @@ abstract class PluginBase implements Plugin, CommandExecutor{ public function getResources(){ if(!$this->isPhar()){ $resources = array(); - foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->dataFolder . "resources/")) as $resource){ + foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->file . "resources/")) as $resource){ $resources[] = $resource; } diff --git a/src/PocketMine/plugin/PluginDescription.php b/src/PocketMine/plugin/PluginDescription.php index 70086066d..bd97df726 100644 --- a/src/PocketMine/plugin/PluginDescription.php +++ b/src/PocketMine/plugin/PluginDescription.php @@ -96,13 +96,14 @@ class PluginDescription{ $this->order = constant("PocketMine\\Plugin\\PluginLoadOrder::" . $order); } } - + $this->authors = array(); + if(isset($plugin["author"])){ + $this->authors[] = $plugin["author"]; + } if(isset($plugin["authors"])){ - $this->authors = $plugin["authors"]; - }elseif(isset($plugin["author"])){ - $this->authors = array($plugin["author"]); - }else{ - $this->authors = array(); + foreach($plugin["authors"] as $author){ + $this->authors[] = $author; + } } if(isset($plugin["permissions"])){