Inheritance: extends VanillaCommand
Esempio n. 1
0
 public static function reload()
 {
     if (Server::getInstance()->getPluginManager()->useTimings()) {
         foreach (self::$HANDLERS as $timings) {
             $timings->reset();
         }
         TimingsCommand::$timingStart = microtime(true);
     }
 }
Esempio n. 2
0
 /**
  * @param string $directory
  * @param array  $newLoaders
  *
  * @return Plugin[]
  */
 public function loadPlugins($directory, $newLoaders = null)
 {
     if (is_dir($directory)) {
         $plugins = [];
         $loadedPlugins = [];
         $dependencies = [];
         $softDependencies = [];
         if (is_array($newLoaders)) {
             $loaders = [];
             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;
                 }
                 $file = $directory . $file;
                 try {
                     $description = $loader->getPluginDescription($file);
                     if ($description instanceof PluginDescription) {
                         $name = $description->getName();
                         if (stripos($name, "pocketmine") !== false or stripos($name, "minecraft") !== false or stripos($name, "mojang") !== false) {
                             $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [$name, "%pocketmine.plugin.restrictedName"]));
                             continue;
                         } elseif (strpos($name, " ") !== false) {
                             $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.plugin.spacesDiscouraged", [$name]));
                         }
                         if (isset($plugins[$name]) or $this->getPlugin($name) instanceof Plugin) {
                             $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.duplicateError", [$name]));
                             continue;
                         }
                         //1=outdated plugin, 2=outdated server, 4=minor, 8=normal
                         $compatibility = 0;
                         //Check multiple dependencies
                         foreach ($description->getCompatibleApis() as $versionString) {
                             //Format: majorVersion.minorVersion.patch
                             $version = array_map("intval", explode(".", $versionString));
                             $apiVersion = array_map("intval", explode(".", $this->server->getApiVersion()));
                             //Completely different API version: too old
                             if ($version[0] < $apiVersion[0]) {
                                 $compatibility |= 1;
                                 continue;
                             }
                             //Completely different API version: too new
                             if ($version[0] > $apiVersion[0]) {
                                 $compatibility |= 2;
                                 continue;
                             }
                             //If the plugin requires new API features, being backwards compatible
                             if ($version[1] > $apiVersion[1]) {
                                 $compatibility |= 4;
                                 continue;
                             }
                             $compatibility |= 8;
                             break;
                         }
                         if (($compatibility & 8) === 0) {
                             $accept1 = $this->server->getProperty("settings.incompatible-plugins.plugin-too-old", false);
                             $accept2 = $this->server->getProperty("settings.incompatible-plugins.plugin-too-new.major", false);
                             $accept4 = $this->server->getProperty("settings.incompatible-plugins.plugin-too-new.minor", false);
                             if (!$accept1) {
                                 $compatibility &= ~1;
                             }
                             if (!$accept2) {
                                 $compatibility &= ~2;
                             }
                             if (!$accept4) {
                                 $compatibility &= ~4;
                             }
                             if ($compatibility === 0) {
                                 if (($compatibility & 4) === 0) {
                                     $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [$name, "%pocketmine.plugin.incompatibleAPI.minorTooNew"]));
                                 } elseif (($compatibility & 2) === 0) {
                                     $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [$name, "%pocketmine.plugin.incompatibleAPI.majortooNew"]));
                                 } else {
                                     $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [$name, "%pocketmine.plugin.incompatibleAPI.majorTooOld"]));
                                 }
                                 continue;
                                 #Abort loading of this plugin, move on to the next
                             } elseif ($compatibility & 4) {
                                 $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.plugin.incompatibleAPIWarning.minorTooNew", [$name]));
                             } elseif ($compatibility & 2) {
                                 $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.plugin.incompatibleAPIWarning.majorTooNew", [$name]));
                             } elseif ($compatibility & 1) {
                                 $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.plugin.incompatibleAPIWarning.majorTooOld", [$name]));
                             }
                         }
                         $plugins[$name] = $file;
                         $softDependencies[$name] = (array) $description->getSoftDepend();
                         $dependencies[$name] = (array) $description->getDepend();
                         foreach ($description->getLoadBefore() as $before) {
                             if (isset($softDependencies[$before])) {
                                 $softDependencies[$before][] = $name;
                             } else {
                                 $softDependencies[$before] = [$name];
                             }
                         }
                     }
                 } catch (Throwable $e) {
                     $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.fileError", [$file, $directory, $e->getMessage()]));
                     $this->server->getLogger()->logException($e);
                 }
             }
         }
         while (count($plugins) > 0) {
             $missingDependency = true;
             foreach ($plugins as $name => $file) {
                 if (isset($dependencies[$name])) {
                     foreach ($dependencies[$name] as $key => $dependency) {
                         if (isset($loadedPlugins[$dependency]) or $this->getPlugin($dependency) instanceof Plugin) {
                             unset($dependencies[$name][$key]);
                         } elseif (!isset($plugins[$dependency])) {
                             $this->server->getLogger()->critical($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [$name, "%pocketmine.plugin.unknownDependency"]));
                             break;
                         }
                     }
                     if (count($dependencies[$name]) === 0) {
                         unset($dependencies[$name]);
                     }
                 }
                 if (isset($softDependencies[$name])) {
                     foreach ($softDependencies[$name] as $key => $dependency) {
                         if (isset($loadedPlugins[$dependency]) or $this->getPlugin($dependency) instanceof Plugin) {
                             unset($softDependencies[$name][$key]);
                         }
                     }
                     if (count($softDependencies[$name]) === 0) {
                         unset($softDependencies[$name]);
                     }
                 }
                 if (!isset($dependencies[$name]) and !isset($softDependencies[$name])) {
                     unset($plugins[$name]);
                     $missingDependency = false;
                     if ($plugin = $this->loadPlugin($file, $loaders) and $plugin instanceof Plugin) {
                         $loadedPlugins[$name] = $plugin;
                     } else {
                         $this->server->getLogger()->critical($this->server->getLanguage()->translateString("pocketmine.plugin.genericLoadError", [$name]));
                     }
                 }
             }
             if ($missingDependency === true) {
                 foreach ($plugins as $name => $file) {
                     if (!isset($dependencies[$name])) {
                         unset($softDependencies[$name]);
                         unset($plugins[$name]);
                         $missingDependency = false;
                         if ($plugin = $this->loadPlugin($file, $loaders) and $plugin instanceof Plugin) {
                             $loadedPlugins[$name] = $plugin;
                         } else {
                             $this->server->getLogger()->critical($this->server->getLanguage()->translateString("pocketmine.plugin.genericLoadError", [$name]));
                         }
                     }
                 }
                 //No plugins loaded :(
                 if ($missingDependency === true) {
                     foreach ($plugins as $name => $file) {
                         $this->server->getLogger()->critical($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [$name, "%pocketmine.plugin.circularDependency"]));
                     }
                     $plugins = [];
                 }
             }
         }
         TimingsCommand::$timingStart = microtime(true);
         return $loadedPlugins;
     } else {
         TimingsCommand::$timingStart = microtime(true);
         return [];
     }
 }
Esempio n. 3
0
 /**
  * @param string $directory
  * @param array  $newLoaders
  *
  * @return Plugin[]
  */
 public function loadPlugins($directory, $newLoaders = null)
 {
     if (is_dir($directory)) {
         $plugins = [];
         $loadedPlugins = [];
         $dependencies = [];
         $softDependencies = [];
         if (is_array($newLoaders)) {
             $loaders = [];
             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;
                 }
                 $file = $directory . $file;
                 try {
                     $description = $loader->getPluginDescription($file);
                     if ($description instanceof PluginDescription) {
                         $name = $description->getName();
                         if (stripos($name, "pocketmine") !== false or stripos($name, "minecraft") !== false or stripos($name, "mojang") !== false) {
                             $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [$name, "%pocketmine.plugin.restrictedName"]));
                             continue;
                         } elseif (strpos($name, " ") !== false) {
                             $this->server->getLogger()->warning($this->server->getLanguage()->translateString("pocketmine.plugin.spacesDiscouraged", [$name]));
                         }
                         if (isset($plugins[$name]) or $this->getPlugin($name) instanceof Plugin) {
                             $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.duplicateError", [$name]));
                             continue;
                         }
                         $compatible = false;
                         //Check multiple dependencies
                         foreach ($description->getCompatibleApis() as $version) {
                             //Format: majorVersion.minorVersion.patch
                             $version = array_map("intval", explode(".", $version));
                             $apiVersion = array_map("intval", explode(".", $this->server->getApiVersion()));
                             //Completely different API version
                             if ($version[0] !== $apiVersion[0]) {
                                 continue;
                             }
                             //If the plugin requires new API features, being backwards compatible
                             if ($version[1] > $apiVersion[1]) {
                                 continue;
                             }
                             $compatible = true;
                             break;
                         }
                         if ($compatible === false) {
                             $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [$name, "%pocketmine.plugin.incompatibleAPI"]));
                             continue;
                         }
                         $plugins[$name] = $file;
                         $softDependencies[$name] = (array) $description->getSoftDepend();
                         $dependencies[$name] = (array) $description->getDepend();
                         foreach ($description->getLoadBefore() as $before) {
                             if (isset($softDependencies[$before])) {
                                 $softDependencies[$before][] = $name;
                             } else {
                                 $softDependencies[$before] = [$name];
                             }
                         }
                     }
                 } catch (\Exception $e) {
                     $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.plugin.fileError", [$file, $directory, $e->getMessage()]));
                     $logger = $this->server->getLogger();
                     if ($logger instanceof MainLogger) {
                         $logger->logException($e);
                     }
                 }
             }
         }
         while (count($plugins) > 0) {
             $missingDependency = true;
             foreach ($plugins as $name => $file) {
                 if (isset($dependencies[$name])) {
                     foreach ($dependencies[$name] as $key => $dependency) {
                         if (isset($loadedPlugins[$dependency]) or $this->getPlugin($dependency) instanceof Plugin) {
                             unset($dependencies[$name][$key]);
                         } elseif (!isset($plugins[$dependency])) {
                             $this->server->getLogger()->critical($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [$name, "%pocketmine.plugin.unknownDependency"]));
                             break;
                         }
                     }
                     if (count($dependencies[$name]) === 0) {
                         unset($dependencies[$name]);
                     }
                 }
                 if (isset($softDependencies[$name])) {
                     foreach ($softDependencies[$name] as $key => $dependency) {
                         if (isset($loadedPlugins[$dependency]) or $this->getPlugin($dependency) instanceof Plugin) {
                             unset($softDependencies[$name][$key]);
                         }
                     }
                     if (count($softDependencies[$name]) === 0) {
                         unset($softDependencies[$name]);
                     }
                 }
                 if (!isset($dependencies[$name]) and !isset($softDependencies[$name])) {
                     unset($plugins[$name]);
                     $missingDependency = false;
                     if ($plugin = $this->loadPlugin($file, $loaders) and $plugin instanceof Plugin) {
                         $loadedPlugins[$name] = $plugin;
                     } else {
                         $this->server->getLogger()->critical($this->server->getLanguage()->translateString("pocketmine.plugin.genericLoadError", [$name]));
                     }
                 }
             }
             if ($missingDependency === true) {
                 foreach ($plugins as $name => $file) {
                     if (!isset($dependencies[$name])) {
                         unset($softDependencies[$name]);
                         unset($plugins[$name]);
                         $missingDependency = false;
                         if ($plugin = $this->loadPlugin($file, $loaders) and $plugin instanceof Plugin) {
                             $loadedPlugins[$name] = $plugin;
                         } else {
                             $this->server->getLogger()->critical($this->server->getLanguage()->translateString("pocketmine.plugin.genericLoadError", [$name]));
                         }
                     }
                 }
                 //No plugins loaded :(
                 if ($missingDependency === true) {
                     foreach ($plugins as $name => $file) {
                         $this->server->getLogger()->critical($this->server->getLanguage()->translateString("pocketmine.plugin.loadError", [$name, "%pocketmine.plugin.circularDependency"]));
                     }
                     $plugins = [];
                 }
             }
         }
         TimingsCommand::$timingStart = microtime(true);
         return $loadedPlugins;
     } else {
         TimingsCommand::$timingStart = microtime(true);
         return [];
     }
 }
Esempio n. 4
0
 /**
  * @param string $directory
  * @param array  $newLoaders
  *
  * @return Plugin[]
  */
 public function loadPlugins($directory, $newLoaders = \null)
 {
     if (\is_dir($directory)) {
         $plugins = [];
         $loadedPlugins = [];
         $dependencies = [];
         $softDependencies = [];
         if (\is_array($newLoaders)) {
             $loaders = [];
             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;
                 }
                 $file = $directory . $file;
                 try {
                     $description = $loader->getPluginDescription($file);
                     if ($description instanceof PluginDescription) {
                         $name = $description->getName();
                         if (\stripos($name, "pocketmine") !== \false or \stripos($name, "minecraft") !== \false or \stripos($name, "mojang") !== \false) {
                             $this->server->getLogger()->error("Could not load plugin '" . $name . "': restricted name");
                             continue;
                         } elseif (\strpos($name, " ") !== \false) {
                             $this->server->getLogger()->warning("Plugin '" . $name . "' uses spaces in its name, this is discouraged");
                         }
                         if (isset($plugins[$name]) or $this->getPlugin($name) instanceof Plugin) {
                             $this->server->getLogger()->error("Could not load duplicate plugin '" . $name . "': plugin exists");
                             continue;
                         }
                         $compatible = \false;
                         //Check multiple dependencies
                         foreach ($description->getCompatibleApis() as $version) {
                             //Format: majorVersion.minorVersion.patch
                             $version = \array_map("intval", \explode(".", $version));
                             $apiVersion = \array_map("intval", \explode(".", $this->server->getApiVersion()));
                             //Completely different API version
                             if ($version[0] !== $apiVersion[0]) {
                                 continue;
                             }
                             //If the plugin requires new API features, being backwards compatible
                             if ($version[1] > $apiVersion[1]) {
                                 continue;
                             }
                             $compatible = \true;
                             break;
                         }
                         if ($compatible === \false) {
                             $this->server->getLogger()->error("Could not load plugin '" . $name . "': API version not compatible");
                             continue;
                         }
                         $plugins[$name] = $file;
                         $softDependencies[$name] = (array) $description->getSoftDepend();
                         $dependencies[$name] = (array) $description->getDepend();
                         foreach ($description->getLoadBefore() as $before) {
                             if (isset($softDependencies[$before])) {
                                 $softDependencies[$before][] = $name;
                             } else {
                                 $softDependencies[$before] = [$name];
                             }
                         }
                     }
                 } catch (\Exception $e) {
                     $this->server->getLogger()->error("Could not load '" . $file . "' in folder '" . $directory . "': " . $e->getMessage());
                     $logger = $this->server->getLogger();
                     if ($logger instanceof MainLogger) {
                         $logger->logException($e);
                     }
                 }
             }
         }
         while (\count($plugins) > 0) {
             $missingDependency = \true;
             foreach ($plugins as $name => $file) {
                 if (isset($dependencies[$name])) {
                     foreach ($dependencies[$name] as $key => $dependency) {
                         if (isset($loadedPlugins[$dependency]) or $this->getPlugin($dependency) instanceof Plugin) {
                             unset($dependencies[$name][$key]);
                         } elseif (!isset($plugins[$dependency])) {
                             $this->server->getLogger()->critical("Could not load plugin '" . $name . "': Unknown dependency");
                             break;
                         }
                     }
                     if (\count($dependencies[$name]) === 0) {
                         unset($dependencies[$name]);
                     }
                 }
                 if (isset($softDependencies[$name])) {
                     foreach ($softDependencies[$name] as $key => $dependency) {
                         if (isset($loadedPlugins[$dependency]) or $this->getPlugin($dependency) instanceof Plugin) {
                             unset($softDependencies[$name][$key]);
                         }
                     }
                     if (\count($softDependencies[$name]) === 0) {
                         unset($softDependencies[$name]);
                     }
                 }
                 if (!isset($dependencies[$name]) and !isset($softDependencies[$name])) {
                     unset($plugins[$name]);
                     $missingDependency = \false;
                     if ($plugin = $this->loadPlugin($file, $loaders) and $plugin instanceof Plugin) {
                         $loadedPlugins[$name] = $plugin;
                     } else {
                         $this->server->getLogger()->critical("Could not load plugin '" . $name . "'");
                     }
                 }
             }
             if ($missingDependency === \true) {
                 foreach ($plugins as $name => $file) {
                     if (!isset($dependencies[$name])) {
                         unset($softDependencies[$name]);
                         unset($plugins[$name]);
                         $missingDependency = \false;
                         if ($plugin = $this->loadPlugin($file, $loaders) and $plugin instanceof Plugin) {
                             $loadedPlugins[$name] = $plugin;
                         } else {
                             $this->server->getLogger()->critical("Could not load plugin '" . $name . "'");
                         }
                     }
                 }
                 //No plugins loaded :(
                 if ($missingDependency === \true) {
                     foreach ($plugins as $name => $file) {
                         $this->server->getLogger()->critical("Could not load plugin '" . $name . "': circular dependency detected");
                     }
                     $plugins = [];
                 }
             }
         }
         TimingsCommand::$timingStart = \microtime(\true);
         return $loadedPlugins;
     } else {
         TimingsCommand::$timingStart = \microtime(\true);
         return [];
     }
 }