mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-21 08:17:34 +00:00
Removed FolderPluginLoader, allow loading of plugins by other loaders
This commit is contained in:
parent
f71f10da0b
commit
61192b172b
@ -1225,7 +1225,6 @@ class Server{
|
||||
$this->pluginManager = new PluginManager($this, $this->commandMap);
|
||||
$this->pluginManager->subscribeToPermission(Server::BROADCAST_CHANNEL_ADMINISTRATIVE, $this->consoleSender);
|
||||
$this->pluginManager->registerInterface("pocketmine\\plugin\\PharPluginLoader");
|
||||
$this->pluginManager->registerInterface("pocketmine\\plugin\\FolderPluginLoader");
|
||||
$this->pluginManager->loadPlugins($this->pluginPath);
|
||||
|
||||
//TODO: update checking (async)
|
||||
|
@ -1,147 +0,0 @@
|
||||
<?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 FolderPluginLoader 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(is_dir($file) and file_exists($file . "/plugin.yml") and file_exists($file . "/src/")){
|
||||
if(($description = $this->getPluginDescription($file)) instanceof PluginDescription){
|
||||
console("[INFO] Loading " . $description->getFullName());
|
||||
console("[WARNING] Non-packaged plugin " . $description->getName() . " detected, do not use on production.");
|
||||
$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;
|
||||
}
|
||||
|
||||
|
||||
$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(is_dir($file) and file_exists($file . "/plugin.yml")){
|
||||
$yaml = @file_get_contents($file . "/plugin.yml");
|
||||
if($yaml != ""){
|
||||
return new PluginDescription($yaml);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the filename patterns that this loader accepts
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getPluginFilters(){
|
||||
return "/[^\\.]/";
|
||||
}
|
||||
|
||||
/**
|
||||
* @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()->getFullName());
|
||||
|
||||
$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()->getFullName());
|
||||
|
||||
Server::getInstance()->getPluginManager()->callEvent(new PluginDisableEvent($plugin));
|
||||
|
||||
$plugin->setEnabled(false);
|
||||
}
|
||||
}
|
||||
}
|
@ -115,18 +115,18 @@ class PluginManager{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $loader A PluginLoader class name
|
||||
* @param string $loaderName A PluginLoader class name
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function registerInterface($loader){
|
||||
if(is_subclass_of($loader, "pocketmine\\plugin\\PluginLoader")){
|
||||
$loader = new $loader($this->server);
|
||||
public function registerInterface($loaderName){
|
||||
if(is_subclass_of($loaderName, "pocketmine\\plugin\\PluginLoader")){
|
||||
$loader = new $loaderName($this->server);
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->fileAssociations[spl_object_hash($loader)] = $loader;
|
||||
$this->fileAssociations[$loaderName] = $loader;
|
||||
|
||||
return true;
|
||||
}
|
||||
@ -161,17 +161,28 @@ class PluginManager{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $directory
|
||||
* @param string $directory
|
||||
* @param array $newLoaders
|
||||
*
|
||||
* @return Plugin[]
|
||||
*/
|
||||
public function loadPlugins($directory){
|
||||
public function loadPlugins($directory, $newLoaders = null){
|
||||
if(is_dir($directory)){
|
||||
$plugins = array();
|
||||
$loadedPlugins = array();
|
||||
$dependencies = array();
|
||||
$softDependencies = array();
|
||||
foreach($this->fileAssociations as $loader){
|
||||
if(is_array($newLoaders)){
|
||||
$loaders = array();
|
||||
foreach($newLoaders as $key){
|
||||
if(isset($this->fileAssociations[$key])){
|
||||
$loaders[$key] = $this->fileAssociations[$key];
|
||||
}
|
||||
}
|
||||
}else{
|
||||
$loaders = $this->fileAssociations;
|
||||
}
|
||||
foreach($loaders as $loader){
|
||||
foreach(new \RegexIterator(new \DirectoryIterator($directory), $loader->getPluginFilters()) as $file){
|
||||
if($file === "." or $file === ".."){
|
||||
continue;
|
||||
@ -186,7 +197,7 @@ class PluginManager{
|
||||
}elseif(strpos($name, " ") !== false){
|
||||
console("[WARNING] Plugin '" . $name . "' uses spaces in its name, this is discouraged");
|
||||
}
|
||||
if(isset($plugins[$name])){
|
||||
if(isset($plugins[$name]) or $this->getPlugin($name) instanceof Plugin){
|
||||
console("[ERROR] Could not load duplicate plugin '" . $name . "': plugin exists");
|
||||
continue;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user