removed 'application' folder to have everything in the root directory
This commit is contained in:
238
core/Plugins/InstallMenu.php
Normal file
238
core/Plugins/InstallMenu.php
Normal file
@ -0,0 +1,238 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Plugins;
|
||||
|
||||
use FML\Controls\Frame;
|
||||
use FML\Controls\Label;
|
||||
use FML\Controls\Labels\Label_Button;
|
||||
use FML\Controls\Labels\Label_Text;
|
||||
use FML\Controls\Quads\Quad_Icons64x64_1;
|
||||
use FML\Script\Features\Paging;
|
||||
use FML\Script\Script;
|
||||
use ManiaControl\Admin\AuthenticationManager;
|
||||
use ManiaControl\Configurator\ConfiguratorMenu;
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
|
||||
use ManiaControl\Players\Player;
|
||||
use ManiaControl\Utils\WebReader;
|
||||
|
||||
/**
|
||||
* Configurator for installing Plugins
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class InstallMenu implements ConfiguratorMenu, ManialinkPageAnswerListener {
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
const SETTING_PERMISSION_INSTALL_PLUGINS = 'Install Plugins';
|
||||
const ACTION_PREFIX_INSTALL_PLUGIN = 'PluginInstallMenu.Install.';
|
||||
const ACTION_REFRESH_LIST = 'PluginInstallMenu.RefreshList';
|
||||
|
||||
/*
|
||||
* Private properties
|
||||
*/
|
||||
/** @var ManiaControl $maniaControl */
|
||||
private $maniaControl = null;
|
||||
|
||||
/**
|
||||
* Create a new plugin install menu instance
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
|
||||
// Permissions
|
||||
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_INSTALL_PLUGINS, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
|
||||
|
||||
// Callbacks
|
||||
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_REFRESH_LIST, $this, 'handleRefreshListAction');
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
|
||||
*/
|
||||
public static function getTitle() {
|
||||
return 'Install Plugins';
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \ManiaControl\Configurators\ConfiguratorMenu::getMenu()
|
||||
*/
|
||||
public function getMenu($width, $height, Script $script, Player $player) {
|
||||
$paging = new Paging();
|
||||
$script->addFeature($paging);
|
||||
$frame = new Frame();
|
||||
|
||||
// Config
|
||||
$pagerSize = 9.;
|
||||
$entryHeight = 5.;
|
||||
$posY = 0.;
|
||||
$pageFrame = null;
|
||||
|
||||
$url = ManiaControl::URL_WEBSERVICE . 'plugins';
|
||||
$response = WebReader::getUrl($url);
|
||||
$dataJson = $response->getContent();
|
||||
$pluginList = json_decode($dataJson);
|
||||
$index = 0;
|
||||
|
||||
if (!is_array($pluginList)) {
|
||||
// Error text
|
||||
$errorFrame = $this->getErrorFrame();
|
||||
$frame->add($errorFrame);
|
||||
} else if (empty($pluginList)) {
|
||||
// Empty text
|
||||
$emptyFrame = $this->getEmptyFrame();
|
||||
$frame->add($emptyFrame);
|
||||
} else {
|
||||
// Build plugin list
|
||||
// Pagers
|
||||
$pagerPrev = new Quad_Icons64x64_1();
|
||||
$frame->add($pagerPrev);
|
||||
$pagerPrev->setPosition($width * 0.39, $height * -0.44, 2)->setSize($pagerSize, $pagerSize)->setSubStyle($pagerPrev::SUBSTYLE_ArrowPrev);
|
||||
|
||||
$pagerNext = clone $pagerPrev;
|
||||
$frame->add($pagerNext);
|
||||
$pagerNext->setX($width * 0.45);
|
||||
|
||||
$pageCountLabel = new Label_Text();
|
||||
$frame->add($pageCountLabel);
|
||||
$pageCountLabel->setHAlign($pageCountLabel::RIGHT)->setPosition($width * 0.35, $height * -0.44, 1)->setStyle($pageCountLabel::STYLE_TextTitle1)->setTextSize(2);
|
||||
|
||||
$paging->addButton($pagerNext)->addButton($pagerPrev)->setLabel($pageCountLabel);
|
||||
|
||||
// Info tooltip
|
||||
$infoTooltipLabel = new Label();
|
||||
$frame->add($infoTooltipLabel);
|
||||
$infoTooltipLabel->setAlign($infoTooltipLabel::LEFT, $infoTooltipLabel::TOP)->setPosition($width * -0.45, $height * -0.22)->setSize($width * 0.7, $entryHeight)->setTextSize(1)->setTranslate(true)->setVisible(false)->setAutoNewLine(true)->setMaxLines(5);
|
||||
|
||||
// List plugins
|
||||
foreach ($pluginList as $plugin) {
|
||||
if ($this->maniaControl->getPluginManager()->isPluginIdInstalled($plugin->id)
|
||||
) {
|
||||
// Already installed -> Skip
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($index % 10 === 0) {
|
||||
// New page
|
||||
$pageFrame = new Frame();
|
||||
$frame->add($pageFrame);
|
||||
$paging->addPage($pageFrame);
|
||||
$posY = $height * 0.41;
|
||||
}
|
||||
|
||||
$pluginFrame = new Frame();
|
||||
$pageFrame->add($pluginFrame);
|
||||
$pluginFrame->setY($posY);
|
||||
|
||||
$nameLabel = new Label_Text();
|
||||
$pluginFrame->add($nameLabel);
|
||||
$nameLabel->setHAlign($nameLabel::LEFT)->setX($width * -0.46)->setSize($width * 0.62, $entryHeight)->setStyle($nameLabel::STYLE_TextCardSmall)->setTextSize(2)->setText($plugin->name);
|
||||
|
||||
$description = "Author: {$plugin->author}\nVersion: {$plugin->currentVersion->version}\nDesc: {$plugin->description}";
|
||||
$nameLabel->addTooltipLabelFeature($infoTooltipLabel, $description);
|
||||
|
||||
if (!$this->isPluginCompatible($plugin)) {
|
||||
// Incompatibility label
|
||||
$infoLabel = new Label_Text();
|
||||
$pluginFrame->add($infoLabel);
|
||||
$infoLabel->setHAlign($infoLabel::RIGHT)->setX($width * 0.47)->setSize($width * 0.33, $entryHeight)->setTextSize(1)->setTextColor('f30');
|
||||
if ($plugin->currentVersion->min_mc_version > ManiaControl::VERSION) {
|
||||
$infoLabel->setText("Needs at least MC-Version '{$plugin->currentVersion->min_mc_version}'");
|
||||
} else {
|
||||
$infoLabel->setText("Needs at most MC-Version '{$plugin->currentVersion->max_mc_version}'");
|
||||
}
|
||||
} else {
|
||||
// Install button
|
||||
$installButton = new Label_Button();
|
||||
$pluginFrame->add($installButton);
|
||||
$installButton->setHAlign($installButton::RIGHT)->setX($width * 0.47)->setStyle($installButton::STYLE_CardButtonSmall)->setText('Install')->setTranslate(true)->setAction(self::ACTION_PREFIX_INSTALL_PLUGIN . $plugin->id);
|
||||
}
|
||||
|
||||
if ($plugin->currentVersion->verified > 0) {
|
||||
// Suggested quad
|
||||
$suggestedQuad = new Quad_Icons64x64_1();
|
||||
$pluginFrame->add($suggestedQuad);
|
||||
$suggestedQuad->setPosition($width * 0.45, $entryHeight * 0.12, 2)->setSize(4, 4)->setSubStyle($suggestedQuad::SUBSTYLE_StateSuggested);
|
||||
}
|
||||
|
||||
$posY -= $entryHeight;
|
||||
$index++;
|
||||
}
|
||||
}
|
||||
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the Frame to display when an Error occurred
|
||||
*
|
||||
* @return Frame
|
||||
*/
|
||||
private function getErrorFrame() {
|
||||
$frame = new Frame();
|
||||
|
||||
$infoLabel = new Label_Text();
|
||||
$frame->add($infoLabel);
|
||||
$infoLabel->setVAlign($infoLabel::BOTTOM)->setY(2)->setSize(100, 25)->setTextColor('f30')->setTranslate(true)->setText('An error occurred. Please try again later.');
|
||||
|
||||
$refreshQuad = new Quad_Icons64x64_1();
|
||||
$frame->add($refreshQuad);
|
||||
$refreshQuad->setY(-4)->setSize(8, 8)->setSubStyle($refreshQuad::SUBSTYLE_Refresh)->setAction(self::ACTION_REFRESH_LIST);
|
||||
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the Frame to display when no Plugins are left to install
|
||||
*
|
||||
* @return Frame
|
||||
*/
|
||||
private function getEmptyFrame() {
|
||||
$frame = new Frame();
|
||||
|
||||
$infoLabel = new Label_Text();
|
||||
$frame->add($infoLabel);
|
||||
$infoLabel->setSize(100, 50)->setTextColor('0f3')->setTranslate(true)->setText('No other plugins available.');
|
||||
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given Plugin can be installed without Issues
|
||||
*
|
||||
* @param object $plugin
|
||||
* @return bool
|
||||
*/
|
||||
private function isPluginCompatible($plugin) {
|
||||
if ($plugin->currentVersion->min_mc_version > 0 && $plugin->currentVersion->min_mc_version > ManiaControl::VERSION) {
|
||||
// ManiaControl needs to be updated
|
||||
return false;
|
||||
}
|
||||
if ($plugin->currentVersion->max_mc_version > 0 && $plugin->currentVersion->max_mc_version < ManiaControl::VERSION) {
|
||||
// Plugin is outdated
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \ManiaControl\Configurators\ConfiguratorMenu::saveConfigData()
|
||||
*/
|
||||
public function saveConfigData(array $configData, Player $player) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the Refresh MLAction
|
||||
*
|
||||
* @param array $actionCallback
|
||||
* @param Player $player
|
||||
*/
|
||||
public function handleRefreshListAction(array $actionCallback, Player $player) {
|
||||
$this->maniaControl->getConfigurator()->showMenu($player, $this);
|
||||
}
|
||||
}
|
74
core/Plugins/Plugin.php
Normal file
74
core/Plugins/Plugin.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Plugins;
|
||||
|
||||
use ManiaControl\ManiaControl;
|
||||
|
||||
/**
|
||||
* Interface for ManiaControl Plugins
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
interface Plugin {
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
const PLUGIN_INTERFACE = __CLASS__;
|
||||
|
||||
/**
|
||||
* Prepare the Plugin
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public static function prepare(ManiaControl $maniaControl);
|
||||
|
||||
/**
|
||||
* Get plugin id
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function getId();
|
||||
|
||||
/**
|
||||
* Get Plugin Name
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getName();
|
||||
|
||||
/**
|
||||
* Get Plugin Version
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getVersion();
|
||||
|
||||
/**
|
||||
* Get Plugin Author
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getAuthor();
|
||||
|
||||
/**
|
||||
* Get Plugin Description
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function getDescription();
|
||||
|
||||
/**
|
||||
* Load the plugin
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
* @return bool
|
||||
*/
|
||||
public function load(ManiaControl $maniaControl);
|
||||
|
||||
/**
|
||||
* Unload the plugin and its Resources
|
||||
*/
|
||||
public function unload();
|
||||
}
|
482
core/Plugins/PluginManager.php
Normal file
482
core/Plugins/PluginManager.php
Normal file
@ -0,0 +1,482 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Plugins;
|
||||
|
||||
use ManiaControl\Callbacks\CallbackListener;
|
||||
use ManiaControl\Callbacks\TimerListener;
|
||||
use ManiaControl\Commands\CommandListener;
|
||||
use ManiaControl\Files\FileUtil;
|
||||
use ManiaControl\Logger;
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
|
||||
use ManiaControl\Utils\ClassUtil;
|
||||
|
||||
/**
|
||||
* Class managing Plugins
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class PluginManager {
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
const TABLE_PLUGINS = 'mc_plugins';
|
||||
const CB_PLUGIN_LOADED = 'PluginManager.PluginLoaded';
|
||||
const CB_PLUGIN_UNLOADED = 'PluginManager.PluginUnloaded';
|
||||
|
||||
/*
|
||||
* Private properties
|
||||
*/
|
||||
/** @var ManiaControl $maniaControl */
|
||||
private $maniaControl = null;
|
||||
/** @var PluginMenu $pluginMenu */
|
||||
private $pluginMenu = null;
|
||||
/** @var InstallMenu $pluginInstallMenu */
|
||||
private $pluginInstallMenu = null;
|
||||
/** @var Plugin[] $activePlugins */
|
||||
private $activePlugins = array();
|
||||
/** @var string[] $pluginClasses */
|
||||
private $pluginClasses = array();
|
||||
|
||||
/**
|
||||
* Construct a new plugin manager instance
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
$this->initTables();
|
||||
|
||||
$this->pluginMenu = new PluginMenu($maniaControl);
|
||||
$this->maniaControl->getConfigurator()->addMenu($this->pluginMenu);
|
||||
|
||||
$this->pluginInstallMenu = new InstallMenu($maniaControl);
|
||||
$this->maniaControl->getConfigurator()->addMenu($this->pluginInstallMenu);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize necessary database tables
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function initTables() {
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
$pluginsTableQuery = "CREATE TABLE IF NOT EXISTS `" . self::TABLE_PLUGINS . "` (
|
||||
`index` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`className` varchar(100) NOT NULL,
|
||||
`active` tinyint(1) NOT NULL DEFAULT '0',
|
||||
`changed` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`index`),
|
||||
UNIQUE KEY `className` (`className`)
|
||||
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='ManiaControl plugin status' AUTO_INCREMENT=1;";
|
||||
$tableStatement = $mysqli->prepare($pluginsTableQuery);
|
||||
if ($mysqli->error) {
|
||||
trigger_error($mysqli->error, E_USER_ERROR);
|
||||
return false;
|
||||
}
|
||||
$tableStatement->execute();
|
||||
if ($tableStatement->error) {
|
||||
trigger_error($tableStatement->error, E_USER_ERROR);
|
||||
return false;
|
||||
}
|
||||
$tableStatement->close();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Plugin Id if the given Class is a Plugin
|
||||
*
|
||||
* @param string $pluginClass
|
||||
* @return int
|
||||
*/
|
||||
public static function getPluginId($pluginClass) {
|
||||
if (self::isPluginClass($pluginClass)) {
|
||||
/** @var Plugin $pluginClass */
|
||||
return $pluginClass::getId();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given class implements the plugin interface
|
||||
*
|
||||
* @param string $pluginClass
|
||||
* @return bool
|
||||
*/
|
||||
public static function isPluginClass($pluginClass) {
|
||||
$pluginClass = ClassUtil::getClass($pluginClass);
|
||||
if (!class_exists($pluginClass, false)) {
|
||||
return false;
|
||||
}
|
||||
$interfaces = class_implements($pluginClass, false);
|
||||
if (!$interfaces) {
|
||||
return false;
|
||||
}
|
||||
if (!in_array(Plugin::PLUGIN_INTERFACE, $interfaces)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deactivate the Plugin with the given Class
|
||||
*
|
||||
* @param string $pluginClass
|
||||
* @return bool
|
||||
*/
|
||||
public function deactivatePlugin($pluginClass) {
|
||||
$pluginClass = $this->getPluginClass($pluginClass);
|
||||
if (!$pluginClass) {
|
||||
return false;
|
||||
}
|
||||
if (!$this->isPluginActive($pluginClass)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var Plugin $plugin */
|
||||
$plugin = $this->activePlugins[$pluginClass];
|
||||
unset($this->activePlugins[$pluginClass]);
|
||||
|
||||
$plugin->unload();
|
||||
|
||||
if ($plugin instanceof CallbackListener) {
|
||||
$this->maniaControl->getCallbackManager()->unregisterCallbackListener($plugin);
|
||||
$this->maniaControl->getCallbackManager()->unregisterScriptCallbackListener($plugin);
|
||||
}
|
||||
if ($plugin instanceof CommandListener) {
|
||||
$this->maniaControl->getCommandManager()->unregisterCommandListener($plugin);
|
||||
}
|
||||
if ($plugin instanceof ManialinkPageAnswerListener) {
|
||||
$this->maniaControl->getManialinkManager()->unregisterManialinkPageAnswerListener($plugin);
|
||||
}
|
||||
if ($plugin instanceof TimerListener) {
|
||||
$this->maniaControl->getTimerManager()->unregisterTimerListenings($plugin);
|
||||
}
|
||||
|
||||
$this->savePluginStatus($pluginClass, false);
|
||||
|
||||
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_PLUGIN_UNLOADED, $pluginClass, $plugin);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Class of the Plugin
|
||||
*
|
||||
* @param mixed $pluginClass
|
||||
* @return string
|
||||
*/
|
||||
public static function getPluginClass($pluginClass) {
|
||||
$pluginClass = ClassUtil::getClass($pluginClass);
|
||||
if (!self::isPluginClass($pluginClass)) {
|
||||
return null;
|
||||
}
|
||||
return $pluginClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the Plugin is currently running
|
||||
*
|
||||
* @param string $pluginClass
|
||||
* @return bool
|
||||
*/
|
||||
public function isPluginActive($pluginClass) {
|
||||
$pluginClass = $this->getPluginClass($pluginClass);
|
||||
return isset($this->activePlugins[$pluginClass]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Plugin Status in Database
|
||||
*
|
||||
* @param string $className
|
||||
* @param bool $active
|
||||
* @return bool
|
||||
*/
|
||||
private function savePluginStatus($className, $active) {
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
$pluginStatusQuery = "INSERT INTO `" . self::TABLE_PLUGINS . "` (
|
||||
`className`,
|
||||
`active`
|
||||
) VALUES (
|
||||
?, ?
|
||||
) ON DUPLICATE KEY UPDATE
|
||||
`active` = VALUES(`active`);";
|
||||
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
|
||||
if ($mysqli->error) {
|
||||
trigger_error($mysqli->error);
|
||||
return false;
|
||||
}
|
||||
$activeInt = ($active ? 1 : 0);
|
||||
$pluginStatement->bind_param('si', $className, $activeInt);
|
||||
$pluginStatement->execute();
|
||||
if ($pluginStatement->error) {
|
||||
trigger_error($pluginStatement->error);
|
||||
$pluginStatement->close();
|
||||
return false;
|
||||
}
|
||||
$pluginStatement->close();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load complete Plugins Directory and start all configured Plugins
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function loadPlugins() {
|
||||
$pluginsDirectory = MANIACONTROL_PATH . 'plugins' . DIRECTORY_SEPARATOR;
|
||||
|
||||
$classesBefore = get_declared_classes();
|
||||
$this->loadPluginFiles($pluginsDirectory);
|
||||
$classesAfter = get_declared_classes();
|
||||
|
||||
$newPluginClasses = array();
|
||||
|
||||
$newClasses = array_diff($classesAfter, $classesBefore);
|
||||
foreach ($newClasses as $className) {
|
||||
if (!self::isPluginClass($className)) {
|
||||
continue;
|
||||
}
|
||||
if (!self::validatePluginClass($className)) {
|
||||
$message = "The plugin class '{$className}' isn't correctly implemented: You need to return a proper ID by registering it on maniacontrol.com!";
|
||||
Logger::logWarning($message);
|
||||
if (!DEV_MODE) {
|
||||
$message = 'Fix the plugin or turn on DEV_MODE!';
|
||||
$this->maniaControl->quit($message, true);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->addPluginClass($className)) {
|
||||
continue;
|
||||
}
|
||||
array_push($newPluginClasses, $className);
|
||||
|
||||
/** @var Plugin $className */
|
||||
$className::prepare($this->maniaControl);
|
||||
|
||||
if ($this->getSavedPluginStatus($className)) {
|
||||
$this->activatePlugin($className);
|
||||
}
|
||||
}
|
||||
|
||||
return $newPluginClasses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load all Plugin Files from the Directory
|
||||
*
|
||||
* @param string $directory
|
||||
*/
|
||||
public function loadPluginFiles($directory = '') {
|
||||
if (!is_readable($directory) || !is_dir($directory)) {
|
||||
return;
|
||||
}
|
||||
$pluginFiles = scandir($directory);
|
||||
foreach ($pluginFiles as $pluginFile) {
|
||||
if (substr($pluginFile, 0, 1) === '.') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$filePath = $directory . $pluginFile;
|
||||
if (is_file($filePath)) {
|
||||
if (!FileUtil::isPhpFileName($pluginFile)) {
|
||||
continue;
|
||||
}
|
||||
$success = include_once $filePath;
|
||||
if (!$success) {
|
||||
Logger::logError("Couldn't load file '{$filePath}'!");
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
$dirPath = $directory . $pluginFile;
|
||||
if (is_dir($dirPath)) {
|
||||
$this->loadPluginFiles($dirPath . DIRECTORY_SEPARATOR);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that the given class is a correctly implemented plugin class
|
||||
*
|
||||
* @param string $pluginClass
|
||||
* @return bool
|
||||
*/
|
||||
private static function validatePluginClass($pluginClass) {
|
||||
if (!self::isPluginClass($pluginClass)) {
|
||||
return false;
|
||||
}
|
||||
/** @var Plugin $pluginClass */
|
||||
return ($pluginClass::getId() > 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the class to array of loaded plugin classes
|
||||
*
|
||||
* @param string $pluginClass
|
||||
* @return bool
|
||||
*/
|
||||
public function addPluginClass($pluginClass) {
|
||||
$pluginClass = $this->getPluginClass($pluginClass);
|
||||
if (in_array($pluginClass, $this->pluginClasses)) {
|
||||
return false;
|
||||
}
|
||||
if (!$this->isPluginClass($pluginClass)) {
|
||||
return false;
|
||||
}
|
||||
array_push($this->pluginClasses, $pluginClass);
|
||||
sort($this->pluginClasses);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get plugin status from database
|
||||
*
|
||||
* @param string $className
|
||||
* @return bool
|
||||
*/
|
||||
public function getSavedPluginStatus($className) {
|
||||
$mysqli = $this->maniaControl->getDatabase()->getMysqli();
|
||||
$pluginStatusQuery = "SELECT `active` FROM `" . self::TABLE_PLUGINS . "`
|
||||
WHERE `className` = ?;";
|
||||
$pluginStatement = $mysqli->prepare($pluginStatusQuery);
|
||||
if ($mysqli->error) {
|
||||
trigger_error($mysqli->error);
|
||||
return false;
|
||||
}
|
||||
$pluginStatement->bind_param('s', $className);
|
||||
$pluginStatement->execute();
|
||||
if ($pluginStatement->error) {
|
||||
trigger_error($pluginStatement->error);
|
||||
$pluginStatement->close();
|
||||
return false;
|
||||
}
|
||||
$pluginStatement->store_result();
|
||||
if ($pluginStatement->num_rows <= 0) {
|
||||
$pluginStatement->free_result();
|
||||
$pluginStatement->close();
|
||||
$this->savePluginStatus($className, false);
|
||||
return false;
|
||||
}
|
||||
$pluginStatement->bind_result($activeInt);
|
||||
$pluginStatement->fetch();
|
||||
$active = ($activeInt === 1);
|
||||
$pluginStatement->free_result();
|
||||
$pluginStatement->close();
|
||||
return $active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Activate and start the plugin with the given name
|
||||
*
|
||||
* @param string $pluginClass
|
||||
* @param string $adminLogin
|
||||
* @return bool
|
||||
*/
|
||||
public function activatePlugin($pluginClass, $adminLogin = null) {
|
||||
if (!$this->isPluginClass($pluginClass)) {
|
||||
return false;
|
||||
}
|
||||
if ($this->isPluginActive($pluginClass)) {
|
||||
return false;
|
||||
}
|
||||
/** @var Plugin $plugin */
|
||||
$plugin = new $pluginClass();
|
||||
|
||||
try {
|
||||
$plugin->load($this->maniaControl);
|
||||
} catch (\Exception $e) {
|
||||
$message = "Error during Plugin Activation of '{$pluginClass}': '{$e->getMessage()}'";
|
||||
$this->maniaControl->getChat()->sendError($message, $adminLogin);
|
||||
Logger::logError($message);
|
||||
$this->savePluginStatus($pluginClass, false);
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->activePlugins[$pluginClass] = $plugin;
|
||||
$this->savePluginStatus($pluginClass, true);
|
||||
|
||||
$this->maniaControl->getCallbackManager()->triggerCallback(self::CB_PLUGIN_LOADED, $pluginClass, $plugin);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the Plugin with the given ID is already installed and loaded
|
||||
*
|
||||
* @param int $pluginId
|
||||
* @return bool
|
||||
*/
|
||||
public function isPluginIdInstalled($pluginId) {
|
||||
foreach ($this->pluginClasses as $pluginClass) {
|
||||
/** @var Plugin $pluginClass */
|
||||
if ($pluginClass::getId() == $pluginId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Plugin if it is activated
|
||||
*
|
||||
* @param string $pluginClass
|
||||
* @return Plugin
|
||||
*/
|
||||
public function getPlugin($pluginClass) {
|
||||
if ($this->isPluginActive($pluginClass)) {
|
||||
return $this->activePlugins[$pluginClass];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all declared plugin class names
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getPluginClasses() {
|
||||
return $this->pluginClasses;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Ids of all active Plugins
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getActivePluginsIds() {
|
||||
$pluginsIds = array();
|
||||
foreach ($this->getActivePlugins() as $plugin) {
|
||||
$pluginId = $plugin::getId();
|
||||
if (is_numeric($pluginId)) {
|
||||
array_push($pluginsIds, $pluginId);
|
||||
}
|
||||
}
|
||||
return $pluginsIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all active Plugins
|
||||
*
|
||||
* @return Plugin[]
|
||||
*/
|
||||
public function getActivePlugins() {
|
||||
return $this->activePlugins;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch the Plugins List from the ManiaControl Website
|
||||
*
|
||||
* @param callable $function
|
||||
*/
|
||||
public function fetchPluginList(callable $function) {
|
||||
$url = ManiaControl::URL_WEBSERVICE . 'plugins';
|
||||
$this->maniaControl->getFileReader()->loadFile($url, function ($dataJson, $error) use (&$function) {
|
||||
$data = json_decode($dataJson);
|
||||
call_user_func($function, $data, $error);
|
||||
});
|
||||
}
|
||||
}
|
429
core/Plugins/PluginMenu.php
Normal file
429
core/Plugins/PluginMenu.php
Normal file
@ -0,0 +1,429 @@
|
||||
<?php
|
||||
|
||||
namespace ManiaControl\Plugins;
|
||||
|
||||
use FML\Components\CheckBox;
|
||||
use FML\Components\ValuePicker;
|
||||
use FML\Controls\Entry;
|
||||
use FML\Controls\Frame;
|
||||
use FML\Controls\Label;
|
||||
use FML\Controls\Labels\Label_Button;
|
||||
use FML\Controls\Labels\Label_Text;
|
||||
use FML\Controls\Quad;
|
||||
use FML\Controls\Quads\Quad_Icons128x128_1;
|
||||
use FML\Controls\Quads\Quad_Icons128x32_1;
|
||||
use FML\Controls\Quads\Quad_Icons64x64_1;
|
||||
use FML\Script\Features\Paging;
|
||||
use FML\Script\Script;
|
||||
use ManiaControl\Admin\AuthenticationManager;
|
||||
use ManiaControl\Callbacks\CallbackListener;
|
||||
use ManiaControl\Callbacks\CallbackManager;
|
||||
use ManiaControl\Configurator\ConfiguratorMenu;
|
||||
use ManiaControl\Logger;
|
||||
use ManiaControl\ManiaControl;
|
||||
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
|
||||
use ManiaControl\Players\Player;
|
||||
use ManiaControl\Settings\Setting;
|
||||
|
||||
/**
|
||||
* Configurator for enabling and disabling Plugins
|
||||
*
|
||||
* @author ManiaControl Team <mail@maniacontrol.com>
|
||||
* @copyright 2014 ManiaControl Team
|
||||
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
|
||||
*/
|
||||
class PluginMenu implements CallbackListener, ConfiguratorMenu, ManialinkPageAnswerListener {
|
||||
/*
|
||||
* Constants
|
||||
*/
|
||||
const ACTION_PREFIX_ENABLEPLUGIN = 'PluginMenu.Enable.';
|
||||
const ACTION_PREFIX_DISABLEPLUGIN = 'PluginMenu.Disable.';
|
||||
const ACTION_PREFIX_SETTINGS = 'PluginMenu.Settings.';
|
||||
const ACTION_PREFIX_SETTING = 'PluginMenuSetting.';
|
||||
const ACTION_BACK_TO_PLUGINS = 'PluginMenu.BackToPlugins';
|
||||
const ACTION_PREFIX_UPDATEPLUGIN = 'PluginMenu.Update.';
|
||||
const ACTION_UPDATEPLUGINS = 'PluginMenu.Update.All';
|
||||
const SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS = 'Change Plugin Settings';
|
||||
const CACHE_SETTING_CLASS = 'PluginMenuCache.SettingClass';
|
||||
|
||||
/*
|
||||
* Private properties
|
||||
*/
|
||||
/** @var ManiaControl $maniaControl */
|
||||
private $maniaControl = null;
|
||||
|
||||
/**
|
||||
* Construct a new plugin menu instance
|
||||
*
|
||||
* @param ManiaControl $maniaControl
|
||||
*/
|
||||
public function __construct(ManiaControl $maniaControl) {
|
||||
$this->maniaControl = $maniaControl;
|
||||
|
||||
// Callbacks
|
||||
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
|
||||
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_BACK_TO_PLUGINS, $this, 'backToPlugins');
|
||||
|
||||
// Permissions
|
||||
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \ManiaControl\Configurators\ConfiguratorMenu::getTitle()
|
||||
*/
|
||||
public static function getTitle() {
|
||||
return 'Plugins';
|
||||
}
|
||||
|
||||
/**
|
||||
* Return back to the plugins overview page
|
||||
*
|
||||
* @param array $callback
|
||||
* @param Player $player
|
||||
*/
|
||||
public function backToPlugins($callback, Player $player) {
|
||||
$player->destroyCache($this, self::CACHE_SETTING_CLASS);
|
||||
$this->maniaControl->getConfigurator()->showMenu($player, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \ManiaControl\Configurators\ConfiguratorMenu::getMenu()
|
||||
*/
|
||||
public function getMenu($width, $height, Script $script, Player $player) {
|
||||
$paging = new Paging();
|
||||
$script->addFeature($paging);
|
||||
$frame = new Frame();
|
||||
|
||||
$pluginClasses = $this->maniaControl->getPluginManager()->getPluginClasses();
|
||||
|
||||
// Config
|
||||
$pagerSize = 9.;
|
||||
$entryHeight = 5.;
|
||||
$pageMaxCount = 10;
|
||||
|
||||
// Pagers
|
||||
$pagerPrev = new Quad_Icons64x64_1();
|
||||
$frame->add($pagerPrev);
|
||||
$pagerPrev->setPosition($width * 0.39, $height * -0.44, 2);
|
||||
$pagerPrev->setSize($pagerSize, $pagerSize);
|
||||
$pagerPrev->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowPrev);
|
||||
|
||||
$pagerNext = new Quad_Icons64x64_1();
|
||||
$frame->add($pagerNext);
|
||||
$pagerNext->setPosition($width * 0.45, $height * -0.44, 2);
|
||||
$pagerNext->setSize($pagerSize, $pagerSize);
|
||||
$pagerNext->setSubStyle(Quad_Icons64x64_1::SUBSTYLE_ArrowNext);
|
||||
|
||||
$paging->addButton($pagerNext);
|
||||
$paging->addButton($pagerPrev);
|
||||
|
||||
$pageCountLabel = new Label_Text();
|
||||
$frame->add($pageCountLabel);
|
||||
$pageCountLabel->setHAlign($pageCountLabel::RIGHT);
|
||||
$pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1);
|
||||
$pageCountLabel->setStyle($pageCountLabel::STYLE_TextTitle1);
|
||||
$pageCountLabel->setTextSize(2);
|
||||
|
||||
$paging->setLabel($pageCountLabel);
|
||||
|
||||
$settingClass = $player->getCache($this, self::CACHE_SETTING_CLASS);
|
||||
if ($settingClass) {
|
||||
// Show Settings Menu
|
||||
return $this->getPluginSettingsMenu($frame, $width, $height, $paging, $player, $settingClass);
|
||||
}
|
||||
|
||||
// Display normal Plugin List
|
||||
// Plugin pages
|
||||
$posY = 0.;
|
||||
$pluginUpdates = $this->maniaControl->getUpdateManager()->getPluginUpdateManager()->getPluginsUpdates();
|
||||
|
||||
usort($pluginClasses, function ($pluginClassA, $pluginClassB) {
|
||||
/** @var Plugin $pluginClassA */
|
||||
/** @var Plugin $pluginClassB */
|
||||
return strcmp($pluginClassA::getName(), $pluginClassB::getName());
|
||||
});
|
||||
|
||||
$pageFrame = null;
|
||||
foreach ($pluginClasses as $index => $pluginClass) {
|
||||
/** @var Plugin $pluginClass */
|
||||
if ($index % $pageMaxCount === 0) {
|
||||
$pageFrame = new Frame();
|
||||
$frame->add($pageFrame);
|
||||
$paging->addPage($pageFrame);
|
||||
$posY = $height * 0.41;
|
||||
}
|
||||
|
||||
$active = $this->maniaControl->getPluginManager()->isPluginActive($pluginClass);
|
||||
|
||||
$pluginFrame = new Frame();
|
||||
$pageFrame->add($pluginFrame);
|
||||
$pluginFrame->setY($posY);
|
||||
|
||||
$activeQuad = new Quad_Icons64x64_1();
|
||||
$pluginFrame->add($activeQuad);
|
||||
$activeQuad->setPosition($width * -0.45, -0.1, 1);
|
||||
$activeQuad->setSize($entryHeight * 0.9, $entryHeight * 0.9);
|
||||
if ($active) {
|
||||
$activeQuad->setSubStyle($activeQuad::SUBSTYLE_LvlGreen);
|
||||
} else {
|
||||
$activeQuad->setSubStyle($activeQuad::SUBSTYLE_LvlRed);
|
||||
}
|
||||
|
||||
$nameLabel = new Label_Text();
|
||||
$pluginFrame->add($nameLabel);
|
||||
$nameLabel->setHAlign($nameLabel::LEFT);
|
||||
$nameLabel->setX($width * -0.4);
|
||||
$nameLabel->setSize($width * 0.5, $entryHeight);
|
||||
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
|
||||
$nameLabel->setTextSize(2);
|
||||
$nameLabel->setText($pluginClass::getName());
|
||||
|
||||
$descriptionLabel = new Label();
|
||||
$pageFrame->add($descriptionLabel);
|
||||
$descriptionLabel->setAlign($descriptionLabel::LEFT, $descriptionLabel::TOP);
|
||||
$descriptionLabel->setPosition($width * -0.45, $height * -0.22);
|
||||
$descriptionLabel->setSize($width * 0.7, $entryHeight);
|
||||
$descriptionLabel->setTextSize(2);
|
||||
$descriptionLabel->setTranslate(true);
|
||||
$descriptionLabel->setVisible(false);
|
||||
$descriptionLabel->setAutoNewLine(true);
|
||||
$descriptionLabel->setMaxLines(5);
|
||||
$description = "Author: {$pluginClass::getAuthor()}\nVersion: {$pluginClass::getVersion()}\nDesc: {$pluginClass::getDescription()}";
|
||||
$descriptionLabel->setText($description);
|
||||
$nameLabel->addTooltipFeature($descriptionLabel);
|
||||
|
||||
$quad = new Quad_Icons128x32_1();
|
||||
$pluginFrame->add($quad);
|
||||
$quad->setSubStyle($quad::SUBSTYLE_Settings);
|
||||
$quad->setX(15);
|
||||
$quad->setZ(1);
|
||||
$quad->setSize(5, 5);
|
||||
$quad->setAction(self::ACTION_PREFIX_SETTINGS . $pluginClass);
|
||||
|
||||
$statusChangeButton = new Label_Button();
|
||||
$pluginFrame->add($statusChangeButton);
|
||||
$statusChangeButton->setHAlign($statusChangeButton::RIGHT);
|
||||
$statusChangeButton->setX($width * 0.45);
|
||||
$statusChangeButton->setStyle($statusChangeButton::STYLE_CardButtonSmall);
|
||||
if ($active) {
|
||||
$statusChangeButton->setTextPrefix('$f00');
|
||||
$statusChangeButton->setText('Deactivate');
|
||||
$statusChangeButton->setAction(self::ACTION_PREFIX_DISABLEPLUGIN . $pluginClass);
|
||||
} else {
|
||||
$statusChangeButton->setTextPrefix('a');
|
||||
$statusChangeButton->setText('Activate');
|
||||
$statusChangeButton->setAction(self::ACTION_PREFIX_ENABLEPLUGIN . $pluginClass);
|
||||
}
|
||||
|
||||
if ($pluginUpdates && array_key_exists($pluginClass::getId(), $pluginUpdates)) {
|
||||
$quadUpdate = new Quad_Icons128x128_1();
|
||||
$pluginFrame->add($quadUpdate);
|
||||
$quadUpdate->setSubStyle($quadUpdate::SUBSTYLE_ProfileVehicle);
|
||||
$quadUpdate->setX(56);
|
||||
$quadUpdate->setZ(2);
|
||||
$quadUpdate->setSize(5, 5);
|
||||
$quadUpdate->setAction(self::ACTION_PREFIX_UPDATEPLUGIN . $pluginClass);
|
||||
}
|
||||
|
||||
$posY -= $entryHeight;
|
||||
}
|
||||
|
||||
if ($pluginUpdates) {
|
||||
$updatePluginsButton = new Label_Button();
|
||||
$frame->add($updatePluginsButton);
|
||||
$updatePluginsButton->setHAlign($updatePluginsButton::RIGHT);
|
||||
$updatePluginsButton->setPosition($width * 0.5, -29, 2);
|
||||
$updatePluginsButton->setWidth(10);
|
||||
$updatePluginsButton->setStyle($updatePluginsButton::STYLE_CardButtonSmallS);
|
||||
$updatePluginsButton->setText(count($pluginUpdates) . ' update(s)');
|
||||
$updatePluginsButton->setAction(self::ACTION_UPDATEPLUGINS);
|
||||
}
|
||||
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Frame with the Plugin Settings
|
||||
*
|
||||
* @param Frame $frame
|
||||
* @param float $width
|
||||
* @param float $height
|
||||
* @param Paging $paging
|
||||
* @param Player $player
|
||||
* @param string $settingClass
|
||||
* @return Frame
|
||||
*/
|
||||
private function getPluginSettingsMenu(Frame $frame, $width, $height, Paging $paging, Player $player, $settingClass) {
|
||||
// TODO: centralize menu code to use by mc settings and plugin settings
|
||||
$settings = $this->maniaControl->getSettingManager()->getSettingsByClass($settingClass);
|
||||
|
||||
$pageSettingsMaxCount = 11;
|
||||
$posY = 0;
|
||||
$index = 0;
|
||||
$settingHeight = 5.;
|
||||
$pageFrame = null;
|
||||
|
||||
//Headline Label
|
||||
$headLabel = new Label_Text();
|
||||
$frame->add($headLabel);
|
||||
$headLabel->setHAlign($headLabel::LEFT);
|
||||
$headLabel->setPosition($width * -0.46, $height * 0.41);
|
||||
$headLabel->setSize($width * 0.6, $settingHeight);
|
||||
$headLabel->setStyle($headLabel::STYLE_TextCardSmall);
|
||||
$headLabel->setTextSize(3);
|
||||
$headLabel->setText($settingClass);
|
||||
$headLabel->setTextColor('ff0');
|
||||
|
||||
foreach ($settings as $setting) {
|
||||
if ($index % $pageSettingsMaxCount === 0) {
|
||||
$pageFrame = new Frame();
|
||||
$frame->add($pageFrame);
|
||||
$paging->addPage($pageFrame);
|
||||
$posY = $height * 0.41 - $settingHeight * 1.5;
|
||||
}
|
||||
|
||||
$settingFrame = new Frame();
|
||||
$pageFrame->add($settingFrame);
|
||||
$settingFrame->setY($posY);
|
||||
|
||||
$nameLabel = new Label_Text();
|
||||
$settingFrame->add($nameLabel);
|
||||
$nameLabel->setHAlign($nameLabel::LEFT);
|
||||
$nameLabel->setX($width * -0.46);
|
||||
$nameLabel->setSize($width * 0.6, $settingHeight);
|
||||
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
|
||||
$nameLabel->setTextSize(2);
|
||||
$nameLabel->setText($setting->setting);
|
||||
$nameLabel->setTextColor('fff');
|
||||
|
||||
if ($setting->type === Setting::TYPE_BOOL) {
|
||||
// Boolean checkbox
|
||||
$quad = new Quad();
|
||||
$quad->setPosition($width * 0.33, 0, -0.01);
|
||||
$quad->setSize(4, 4);
|
||||
$checkBox = new CheckBox(self::ACTION_PREFIX_SETTING . $setting->index, $setting->value, $quad);
|
||||
$settingFrame->add($checkBox);
|
||||
} else if ($setting->type === Setting::TYPE_SET) {
|
||||
// SET value picker
|
||||
$label = new Label_Text();
|
||||
$label->setX($width * 0.33);
|
||||
$label->setSize($width * 0.3, $settingHeight * 0.9);
|
||||
$label->setStyle($label::STYLE_TextValueSmall);
|
||||
$label->setTextSize(1);
|
||||
$valuePicker = new ValuePicker(self::ACTION_PREFIX_SETTING . $setting->index, $setting->set, $setting->value, $label);
|
||||
$settingFrame->add($valuePicker);
|
||||
} else {
|
||||
// Value entry
|
||||
$entry = new Entry();
|
||||
$settingFrame->add($entry);
|
||||
$entry->setX($width * 0.33);
|
||||
$entry->setSize($width * 0.3, $settingHeight * 0.9);
|
||||
$entry->setTextSize(1);
|
||||
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
|
||||
$entry->setName(self::ACTION_PREFIX_SETTING . $setting->index);
|
||||
$entry->setDefault($setting->value);
|
||||
}
|
||||
|
||||
$posY -= $settingHeight;
|
||||
|
||||
$index++;
|
||||
}
|
||||
|
||||
$backButton = new Label_Button();
|
||||
$frame->add($backButton);
|
||||
$backButton->setStyle($backButton::STYLE_CardMain_Quit);
|
||||
$backButton->setHAlign($backButton::LEFT);
|
||||
$backButton->setScale(0.75);
|
||||
$backButton->setText('Back');
|
||||
$backButton->setPosition(-$width / 2 + 7, -$height / 2 + 7);
|
||||
$backButton->setAction(self::ACTION_BACK_TO_PLUGINS);
|
||||
|
||||
return $frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle PlayerManialinkPageAnswer callback
|
||||
*
|
||||
* @param array $callback
|
||||
*/
|
||||
public function handleManialinkPageAnswer(array $callback) {
|
||||
$login = $callback[1][1];
|
||||
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
|
||||
if (!$player) {
|
||||
return;
|
||||
}
|
||||
|
||||
$actionId = $callback[1][2];
|
||||
$enable = (strpos($actionId, self::ACTION_PREFIX_ENABLEPLUGIN) === 0);
|
||||
$disable = (strpos($actionId, self::ACTION_PREFIX_DISABLEPLUGIN) === 0);
|
||||
$settings = (strpos($actionId, self::ACTION_PREFIX_SETTINGS) === 0);
|
||||
if (!$enable && !$disable && !$settings) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($enable) {
|
||||
$pluginClass = substr($actionId, strlen(self::ACTION_PREFIX_ENABLEPLUGIN));
|
||||
/** @var Plugin $pluginClass */
|
||||
$activated = $this->maniaControl->getPluginManager()->activatePlugin($pluginClass, $player->login);
|
||||
if ($activated) {
|
||||
$this->maniaControl->getChat()->sendSuccess($pluginClass::getName() . ' activated!', $player);
|
||||
Logger::logInfo("{$player->login} activated '{$pluginClass}'!", true);
|
||||
} else {
|
||||
$this->maniaControl->getChat()->sendError('Error activating ' . $pluginClass::getName() . '!', $player);
|
||||
}
|
||||
} else if ($disable) {
|
||||
$pluginClass = substr($actionId, strlen(self::ACTION_PREFIX_DISABLEPLUGIN));
|
||||
/** @var Plugin $pluginClass */
|
||||
$deactivated = $this->maniaControl->getPluginManager()->deactivatePlugin($pluginClass);
|
||||
if ($deactivated) {
|
||||
$this->maniaControl->getChat()->sendSuccess($pluginClass::getName() . ' deactivated!', $player);
|
||||
Logger::logInfo("{$player->login} deactivated '{$pluginClass}'!", true);
|
||||
} else {
|
||||
$this->maniaControl->getChat()->sendError('Error deactivating ' . $pluginClass::getName() . '!', $player);
|
||||
}
|
||||
} else if ($settings) {
|
||||
// Open Settings Menu
|
||||
$pluginClass = substr($actionId, strlen(self::ACTION_PREFIX_SETTINGS));
|
||||
$player->setCache($this, self::CACHE_SETTING_CLASS, $pluginClass);
|
||||
}
|
||||
|
||||
// Reopen the Menu
|
||||
$this->maniaControl->getConfigurator()->showMenu($player, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \ManiaControl\Configurators\ConfiguratorMenu::saveConfigData()
|
||||
*/
|
||||
public function saveConfigData(array $configData, Player $player) {
|
||||
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS)
|
||||
) {
|
||||
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
|
||||
return;
|
||||
}
|
||||
if (!$configData[3] || strpos($configData[3][0]['Name'], self::ACTION_PREFIX_SETTING) !== 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$prefixLength = strlen(self::ACTION_PREFIX_SETTING);
|
||||
|
||||
foreach ($configData[3] as $settingData) {
|
||||
$settingIndex = (int)substr($settingData['Name'], $prefixLength);
|
||||
$settingObject = $this->maniaControl->getSettingManager()->getSettingObjectByIndex($settingIndex);
|
||||
if (!$settingObject) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$settingData || $settingData['Value'] == $settingObject->value) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$settingObject->value = $settingData['Value'];
|
||||
$this->maniaControl->getSettingManager()->saveSetting($settingObject);
|
||||
}
|
||||
|
||||
$this->maniaControl->getChat()->sendSuccess('Plugin Settings saved!', $player);
|
||||
|
||||
// Reopen the Menu
|
||||
$this->maniaControl->getConfigurator()->showMenu($player, $this);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user