TrackManiaControl/core/Plugins/PluginMenu.php

429 lines
15 KiB
PHP
Raw Normal View History

<?php
namespace ManiaControl\Plugins;
2014-06-15 03:15:06 +02:00
use FML\Components\CheckBox;
2014-06-15 03:23:44 +02:00
use FML\Components\ValuePicker;
2014-01-18 20:05:50 +01:00
use FML\Controls\Entry;
use FML\Controls\Frame;
2013-12-09 20:31:02 +01:00
use FML\Controls\Label;
2014-01-05 20:42:16 +01:00
use FML\Controls\Labels\Label_Button;
2013-12-09 20:31:02 +01:00
use FML\Controls\Labels\Label_Text;
2014-06-15 03:15:06 +02:00
use FML\Controls\Quad;
use FML\Controls\Quads\Quad_Icons128x128_1;
2014-05-02 17:40:47 +02:00
use FML\Controls\Quads\Quad_Icons128x32_1;
use FML\Controls\Quads\Quad_Icons64x64_1;
2014-04-27 16:22:12 +02:00
use FML\Script\Features\Paging;
2014-01-05 20:42:16 +01:00
use FML\Script\Script;
2014-01-31 16:55:01 +01:00
use ManiaControl\Admin\AuthenticationManager;
use ManiaControl\Callbacks\CallbackListener;
2014-01-05 20:42:16 +01:00
use ManiaControl\Callbacks\CallbackManager;
use ManiaControl\Configurator\ConfiguratorMenu;
use ManiaControl\Logger;
2014-01-05 20:42:16 +01:00
use ManiaControl\ManiaControl;
2014-01-18 21:05:45 +01:00
use ManiaControl\Manialinks\ManialinkPageAnswerListener;
2014-01-05 20:42:16 +01:00
use ManiaControl\Players\Player;
use ManiaControl\Settings\Setting;
/**
* Configurator for enabling and disabling Plugins
*
2014-05-02 17:40:47 +02:00
* @author ManiaControl Team <mail@maniacontrol.com>
2017-02-04 11:49:23 +01:00
* @copyright 2014-2017 ManiaControl Team
2014-05-02 17:40:47 +02:00
* @license http://www.gnu.org/licenses/ GNU General Public License, Version 3
*/
2014-01-18 21:05:45 +01:00
class PluginMenu implements CallbackListener, ConfiguratorMenu, ManialinkPageAnswerListener {
/*
* Constants
*/
2014-02-13 00:46:41 +01:00
const ACTION_PREFIX_ENABLEPLUGIN = 'PluginMenu.Enable.';
const ACTION_PREFIX_DISABLEPLUGIN = 'PluginMenu.Disable.';
const ACTION_PREFIX_SETTINGS = 'PluginMenu.Settings.';
2014-05-27 23:00:24 +02:00
const ACTION_PREFIX_SETTING = 'PluginMenuSetting.';
2014-02-13 00:46:41 +01:00
const ACTION_BACK_TO_PLUGINS = 'PluginMenu.BackToPlugins';
const ACTION_PREFIX_UPDATEPLUGIN = 'PluginMenu.Update.';
2014-04-16 01:38:28 +02:00
const ACTION_UPDATEPLUGINS = 'PluginMenu.Update.All';
2014-01-31 16:55:01 +01:00
const SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS = 'Change Plugin Settings';
const CACHE_SETTING_CLASS = 'PluginMenuCache.SettingClass';
2014-01-18 21:05:45 +01:00
/*
* 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;
2014-01-05 20:42:16 +01:00
// Callbacks
2014-08-13 11:05:52 +02:00
$this->maniaControl->getCallbackManager()->registerCallbackListener(CallbackManager::CB_MP_PLAYERMANIALINKPAGEANSWER, $this, 'handleManialinkPageAnswer');
$this->maniaControl->getManialinkManager()->registerManialinkPageAnswerListener(self::ACTION_BACK_TO_PLUGINS, $this, 'backToPlugins');
// Permissions
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->definePermissionLevel(self::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS, AuthenticationManager::AUTH_LEVEL_SUPERADMIN);
}
2014-01-18 21:05:45 +01:00
/**
2017-03-25 18:40:15 +01:00
* @see \ManiaControl\Configurator\ConfiguratorMenu::getTitle()
2014-01-18 21:05:45 +01:00
*/
public static function getTitle() {
return 'Plugins';
2014-01-18 21:05:45 +01:00
}
2014-05-02 17:40:47 +02:00
/**
* Return back to the plugins overview page
*
* @param array $callback
* @param Player $player
2014-05-02 17:40:47 +02:00
*/
public function backToPlugins($callback, Player $player) {
$player->destroyCache($this, self::CACHE_SETTING_CLASS);
2014-08-13 11:05:52 +02:00
$this->maniaControl->getConfigurator()->showMenu($player, $this);
2014-05-02 17:40:47 +02:00
}
/**
2017-03-25 18:40:15 +01:00
* @see \ManiaControl\Configurator\ConfiguratorMenu::getMenu()
*/
public function getMenu($width, $height, Script $script, Player $player) {
2014-05-02 17:40:47 +02:00
$paging = new Paging();
$script->addFeature($paging);
$frame = new Frame();
2014-01-05 20:42:16 +01:00
2014-08-13 11:05:52 +02:00
$pluginClasses = $this->maniaControl->getPluginManager()->getPluginClasses();
2014-01-05 20:42:16 +01:00
// Config
$pagerSize = 9.;
$entryHeight = 5.;
$pageMaxCount = 10;
2014-01-05 20:42:16 +01:00
// 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);
2014-01-05 20:42:16 +01:00
$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);
2014-01-05 20:42:16 +01:00
2017-03-25 18:40:15 +01:00
$paging->addButtonControl($pagerNext);
$paging->addButtonControl($pagerPrev);
2014-01-05 20:42:16 +01:00
$pageCountLabel = new Label_Text();
$frame->add($pageCountLabel);
2014-06-22 19:02:18 +02:00
$pageCountLabel->setHAlign($pageCountLabel::RIGHT);
$pageCountLabel->setPosition($width * 0.35, $height * -0.44, 1);
$pageCountLabel->setStyle($pageCountLabel::STYLE_TextTitle1);
$pageCountLabel->setTextSize(2);
2014-01-05 20:42:16 +01:00
2014-05-02 17:40:47 +02:00
$paging->setLabel($pageCountLabel);
2014-01-18 20:05:50 +01:00
2014-05-10 10:12:48 +02:00
$settingClass = $player->getCache($this, self::CACHE_SETTING_CLASS);
if ($settingClass) {
// Show Settings Menu
return $this->getPluginSettingsMenu($frame, $width, $height, $paging, $player, $settingClass);
2014-01-18 20:05:50 +01:00
}
// Display normal Plugin List
// Plugin pages
$posY = 0.;
2014-08-13 11:05:52 +02:00
$pluginUpdates = $this->maniaControl->getUpdateManager()->getPluginUpdateManager()->getPluginsUpdates();
usort($pluginClasses, function ($pluginClassA, $pluginClassB) {
/** @var Plugin $pluginClassA */
/** @var Plugin $pluginClassB */
return strcmp($pluginClassA::getName(), $pluginClassB::getName());
});
2014-05-11 15:28:53 +02:00
$pageFrame = null;
2014-05-02 17:40:47 +02:00
foreach ($pluginClasses as $index => $pluginClass) {
2014-02-13 00:46:41 +01:00
/** @var Plugin $pluginClass */
if ($index % $pageMaxCount === 0) {
2013-12-09 20:31:02 +01:00
$pageFrame = new Frame();
$frame->add($pageFrame);
2017-03-25 18:40:15 +01:00
$paging->addPageControl($pageFrame);
$posY = $height * 0.41;
2013-12-09 20:31:02 +01:00
}
2014-01-05 20:42:16 +01:00
2014-08-13 11:05:52 +02:00
$active = $this->maniaControl->getPluginManager()->isPluginActive($pluginClass);
2014-01-05 20:42:16 +01:00
$pluginFrame = new Frame();
$pageFrame->add($pluginFrame);
$pluginFrame->setY($posY);
2014-01-05 20:42:16 +01:00
$activeQuad = new Quad_Icons64x64_1();
$pluginFrame->add($activeQuad);
$activeQuad->setPosition($width * -0.45, -0.1, 1);
$activeQuad->setSize($entryHeight * 0.9, $entryHeight * 0.9);
2014-02-13 00:46:41 +01:00
if ($active) {
$activeQuad->setSubStyle($activeQuad::SUBSTYLE_LvlGreen);
2014-01-05 20:42:16 +01:00
} else {
$activeQuad->setSubStyle($activeQuad::SUBSTYLE_LvlRed);
}
2014-01-05 20:42:16 +01:00
$nameLabel = new Label_Text();
$pluginFrame->add($nameLabel);
2014-06-22 19:02:18 +02:00
$nameLabel->setHAlign($nameLabel::LEFT);
2013-12-09 20:31:02 +01:00
$nameLabel->setX($width * -0.4);
$nameLabel->setSize($width * 0.5, $entryHeight);
$nameLabel->setStyle($nameLabel::STYLE_TextCardSmall);
$nameLabel->setTextSize(2);
$nameLabel->setText($pluginClass::getName());
2014-01-05 20:42:16 +01:00
2013-12-09 20:31:02 +01:00
$descriptionLabel = new Label();
$pageFrame->add($descriptionLabel);
2014-06-22 19:02:18 +02:00
$descriptionLabel->setAlign($descriptionLabel::LEFT, $descriptionLabel::TOP);
$descriptionLabel->setPosition($width * -0.45, $height * -0.22);
$descriptionLabel->setSize($width * 0.7, $entryHeight);
$descriptionLabel->setTextSize(2);
2013-12-09 20:31:02 +01:00
$descriptionLabel->setTranslate(true);
$descriptionLabel->setVisible(false);
$descriptionLabel->setMaxLines(5);
2017-03-25 18:40:15 +01:00
$descriptionLabel->setLineSpacing(1);
$description = "Author: {$pluginClass::getAuthor()}\nVersion: {$pluginClass::getVersion()}\nDesc: {$pluginClass::getDescription()}";
2017-03-25 18:40:15 +01:00
$nameLabel->addTooltipLabelFeature($descriptionLabel,$description);
2014-01-05 20:42:16 +01:00
2014-01-18 20:05:50 +01:00
$quad = new Quad_Icons128x32_1();
$pluginFrame->add($quad);
$quad->setSubStyle($quad::SUBSTYLE_Settings);
$quad->setX(15);
2014-01-18 21:05:45 +01:00
$quad->setZ(1);
2014-01-18 20:05:50 +01:00
$quad->setSize(5, 5);
$quad->setAction(self::ACTION_PREFIX_SETTINGS . $pluginClass);
$statusChangeButton = new Label_Button();
$pluginFrame->add($statusChangeButton);
2014-06-22 19:02:18 +02:00
$statusChangeButton->setHAlign($statusChangeButton::RIGHT);
$statusChangeButton->setX($width * 0.45);
$statusChangeButton->setStyle($statusChangeButton::STYLE_CardButtonSmall);
2014-02-13 00:46:41 +01:00
if ($active) {
$statusChangeButton->setTextPrefix('$f00');
$statusChangeButton->setText('Deactivate');
$statusChangeButton->setAction(self::ACTION_PREFIX_DISABLEPLUGIN . $pluginClass);
2014-01-05 20:42:16 +01:00
} else {
$statusChangeButton->setTextPrefix('a');
$statusChangeButton->setText('Activate');
$statusChangeButton->setAction(self::ACTION_PREFIX_ENABLEPLUGIN . $pluginClass);
}
2014-01-05 20:42:16 +01:00
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;
2013-12-09 20:31:02 +01:00
}
2014-01-05 20:42:16 +01:00
if ($pluginUpdates) {
$updatePluginsButton = new Label_Button();
$frame->add($updatePluginsButton);
2014-06-22 19:02:18 +02:00
$updatePluginsButton->setHAlign($updatePluginsButton::RIGHT);
$updatePluginsButton->setPosition($width * 0.5, -29, 2);
$updatePluginsButton->setWidth(10);
$updatePluginsButton->setStyle($updatePluginsButton::STYLE_CardButtonSmallS);
2014-05-02 17:40:47 +02:00
$updatePluginsButton->setText(count($pluginUpdates) . ' update(s)');
$updatePluginsButton->setAction(self::ACTION_UPDATEPLUGINS);
}
2014-04-16 01:38:28 +02:00
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) {
2014-06-15 03:15:06 +02:00
// TODO: centralize menu code to use by mc settings and plugin settings
2014-08-13 11:05:52 +02:00
$settings = $this->maniaControl->getSettingManager()->getSettingsByClass($settingClass);
2014-06-15 03:15:06 +02:00
$pageSettingsMaxCount = 11;
$posY = 0;
$index = 0;
$settingHeight = 5.;
$pageFrame = null;
2014-05-11 15:28:53 +02:00
2014-06-15 03:15:06 +02:00
//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);
2017-03-25 18:40:15 +01:00
$paging->addPageControl($pageFrame);
2014-06-15 03:15:06 +02:00
$posY = $height * 0.41 - $settingHeight * 1.5;
}
$settingFrame = new Frame();
$pageFrame->add($settingFrame);
$settingFrame->setY($posY);
$nameLabel = new Label_Text();
$settingFrame->add($nameLabel);
2014-06-22 19:02:18 +02:00
$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);
2014-06-22 19:02:18 +02:00
$nameLabel->setTextColor('fff');
if ($setting->type === Setting::TYPE_BOOL) {
2014-06-15 03:15:06 +02:00
// Boolean checkbox
$quad = new Quad();
$quad->setPosition($width * 0.33, 0, -0.01);
$quad->setSize(4, 4);
2014-06-15 03:15:06 +02:00
$checkBox = new CheckBox(self::ACTION_PREFIX_SETTING . $setting->index, $setting->value, $quad);
$settingFrame->add($checkBox);
2014-06-15 03:23:44 +02:00
} 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);
2014-06-15 03:15:06 +02:00
} else {
// Value entry
$entry = new Entry();
$settingFrame->add($entry);
2014-06-15 03:23:44 +02:00
$entry->setX($width * 0.33);
2014-06-15 03:15:06 +02:00
$entry->setSize($width * 0.3, $settingHeight * 0.9);
2014-06-15 03:23:44 +02:00
$entry->setTextSize(1);
$entry->setStyle(Label_Text::STYLE_TextValueSmall);
2014-06-15 03:15:06 +02:00
$entry->setName(self::ACTION_PREFIX_SETTING . $setting->index);
$entry->setDefault($setting->value);
}
$posY -= $settingHeight;
$index++;
}
2014-06-22 19:02:18 +02:00
$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) {
2014-05-09 19:40:20 +02:00
$login = $callback[1][1];
2014-08-13 11:05:52 +02:00
$player = $this->maniaControl->getPlayerManager()->getPlayer($login);
2014-05-09 19:40:20 +02:00
if (!$player) {
return;
}
2014-06-15 03:15:06 +02:00
$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;
}
2014-05-09 19:40:20 +02:00
2014-02-13 00:46:41 +01:00
if ($enable) {
$pluginClass = substr($actionId, strlen(self::ACTION_PREFIX_ENABLEPLUGIN));
2014-02-13 00:46:41 +01:00
/** @var Plugin $pluginClass */
2014-08-13 11:05:52 +02:00
$activated = $this->maniaControl->getPluginManager()->activatePlugin($pluginClass, $player->login);
2014-02-13 00:46:41 +01:00
if ($activated) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess($pluginClass::getName() . ' activated!', $player);
Logger::logInfo("{$player->login} activated '{$pluginClass}'!", true);
2014-01-05 20:42:16 +01:00
} else {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError('Error activating ' . $pluginClass::getName() . '!', $player);
}
2014-02-13 00:46:41 +01:00
} else if ($disable) {
$pluginClass = substr($actionId, strlen(self::ACTION_PREFIX_DISABLEPLUGIN));
2014-02-13 00:46:41 +01:00
/** @var Plugin $pluginClass */
2014-08-13 11:05:52 +02:00
$deactivated = $this->maniaControl->getPluginManager()->deactivatePlugin($pluginClass);
2014-02-13 00:46:41 +01:00
if ($deactivated) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess($pluginClass::getName() . ' deactivated!', $player);
Logger::logInfo("{$player->login} deactivated '{$pluginClass}'!", true);
2014-01-05 20:42:16 +01:00
} else {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendError('Error deactivating ' . $pluginClass::getName() . '!', $player);
}
2014-05-09 19:40:20 +02:00
} else if ($settings) {
// Open Settings Menu
$pluginClass = substr($actionId, strlen(self::ACTION_PREFIX_SETTINGS));
2014-05-10 10:12:48 +02:00
$player->setCache($this, self::CACHE_SETTING_CLASS, $pluginClass);
}
2014-01-05 20:42:16 +01:00
2014-05-09 19:40:20 +02:00
// Reopen the Menu
2014-08-13 11:05:52 +02:00
$this->maniaControl->getConfigurator()->showMenu($player, $this);
}
2014-01-18 20:05:50 +01:00
2014-05-02 17:40:47 +02:00
/**
2017-03-25 18:40:15 +01:00
* @see \ManiaControl\Configurator\ConfiguratorMenu::saveConfigData()
2014-05-02 17:40:47 +02:00
*/
public function saveConfigData(array $configData, Player $player) {
2014-08-13 11:05:52 +02:00
if (!$this->maniaControl->getAuthenticationManager()->checkPermission($player, self::SETTING_PERMISSION_CHANGE_PLUGIN_SETTINGS)
2014-08-05 02:17:41 +02:00
) {
2014-08-13 11:05:52 +02:00
$this->maniaControl->getAuthenticationManager()->sendNotAllowed($player);
2014-05-02 17:40:47 +02:00
return;
}
if (!$configData[3] || strpos($configData[3][0]['Name'], self::ACTION_PREFIX_SETTING) !== 0) {
return;
}
$prefixLength = strlen(self::ACTION_PREFIX_SETTING);
2014-06-15 03:23:44 +02:00
foreach ($configData[3] as $settingData) {
$settingIndex = (int)substr($settingData['Name'], $prefixLength);
2014-08-13 11:05:52 +02:00
$settingObject = $this->maniaControl->getSettingManager()->getSettingObjectByIndex($settingIndex);
2014-05-27 23:00:24 +02:00
if (!$settingObject) {
2014-05-02 17:40:47 +02:00
continue;
}
2014-06-15 03:23:44 +02:00
if (!$settingData || $settingData['Value'] == $settingObject->value) {
2014-05-02 17:40:47 +02:00
continue;
}
2014-06-15 03:23:44 +02:00
$settingObject->value = $settingData['Value'];
2014-08-13 11:05:52 +02:00
$this->maniaControl->getSettingManager()->saveSetting($settingObject);
2014-05-02 17:40:47 +02:00
}
2014-08-13 11:05:52 +02:00
$this->maniaControl->getChat()->sendSuccess('Plugin Settings saved!', $player);
2014-05-16 20:42:37 +02:00
2014-06-15 03:23:44 +02:00
// Reopen the Menu
2014-08-13 11:05:52 +02:00
$this->maniaControl->getConfigurator()->showMenu($player, $this);
2014-05-02 17:40:47 +02:00
}
}