Added Phar plugins

This commit is contained in:
Shoghi Cervantes 2014-03-31 23:49:43 +02:00
parent e8c4507ac6
commit 760695e4bc
4 changed files with 161 additions and 17 deletions

View File

@ -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)

View File

@ -0,0 +1,146 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
namespace PocketMine\Plugin;
use PocketMine\Event\Plugin\PluginDisableEvent;
use PocketMine\Event\Plugin\PluginEnableEvent;
use PocketMine\Server;
/**
* Handles different types of plugins
*/
class PharPluginLoader implements PluginLoader{
/** @var Server */
private $server;
/**
* @param Server $server
*/
public function __construct(Server $server){
$this->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);
}
}
}

View File

@ -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;
}

View File

@ -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"])){