plugin prepare

This commit is contained in:
kremsy 2014-01-27 20:46:42 +01:00 committed by Steffen Schröder
parent be86717c0b
commit 56f13f9660
2 changed files with 64 additions and 60 deletions

View File

@ -36,6 +36,8 @@ class PluginManager {
$this->pluginMenu = new PluginMenu($maniaControl); $this->pluginMenu = new PluginMenu($maniaControl);
$this->maniaControl->configurator->addMenu($this->pluginMenu); $this->maniaControl->configurator->addMenu($this->pluginMenu);
$this->preparePlugins();
} }
/** /**
@ -54,12 +56,12 @@ class PluginManager {
UNIQUE KEY `className` (`className`) UNIQUE KEY `className` (`className`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='ManiaControl plugin status' AUTO_INCREMENT=1;"; ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='ManiaControl plugin status' AUTO_INCREMENT=1;";
$tableStatement = $mysqli->prepare($pluginsTableQuery); $tableStatement = $mysqli->prepare($pluginsTableQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error, E_USER_ERROR); trigger_error($mysqli->error, E_USER_ERROR);
return false; return false;
} }
$tableStatement->execute(); $tableStatement->execute();
if($tableStatement->error) { if ($tableStatement->error) {
trigger_error($tableStatement->error, E_USER_ERROR); trigger_error($tableStatement->error, E_USER_ERROR);
return false; return false;
} }
@ -74,7 +76,7 @@ class PluginManager {
* @return bool * @return bool
*/ */
public function isPluginActive($pluginClass) { public function isPluginActive($pluginClass) {
if(is_object($pluginClass)) { if (is_object($pluginClass)) {
$pluginClass = get_class($pluginClass); $pluginClass = get_class($pluginClass);
} }
return isset($this->activePlugins[$pluginClass]); return isset($this->activePlugins[$pluginClass]);
@ -87,10 +89,10 @@ class PluginManager {
* @return bool * @return bool
*/ */
public function isPluginClass($pluginClass) { public function isPluginClass($pluginClass) {
if(is_object($pluginClass)) { if (is_object($pluginClass)) {
$pluginClass = get_class($pluginClass); $pluginClass = get_class($pluginClass);
} }
if(!in_array(Plugin::PLUGIN_INTERFACE, class_implements($pluginClass))) { if (!in_array(Plugin::PLUGIN_INTERFACE, class_implements($pluginClass))) {
return false; return false;
} }
return true; return true;
@ -103,13 +105,13 @@ class PluginManager {
* @return bool * @return bool
*/ */
public function addPluginClass($pluginClass) { public function addPluginClass($pluginClass) {
if(is_object($pluginClass)) { if (is_object($pluginClass)) {
$pluginClass = get_class($pluginClass); $pluginClass = get_class($pluginClass);
} }
if(in_array($pluginClass, $this->pluginClasses)) { if (in_array($pluginClass, $this->pluginClasses)) {
return false; return false;
} }
if(!$this->isPluginClass($pluginClass)) { if (!$this->isPluginClass($pluginClass)) {
return false; return false;
} }
array_push($this->pluginClasses, $pluginClass); array_push($this->pluginClasses, $pluginClass);
@ -124,13 +126,13 @@ class PluginManager {
* @return bool * @return bool
*/ */
public function activatePlugin($pluginClass, $adminLogin = null) { public function activatePlugin($pluginClass, $adminLogin = null) {
if(!is_string($pluginClass)) { if (!is_string($pluginClass)) {
return false; return false;
} }
if(!$this->isPluginClass($pluginClass)) { if (!$this->isPluginClass($pluginClass)) {
return false; return false;
} }
if($this->isPluginActive($pluginClass)) { if ($this->isPluginActive($pluginClass)) {
return false; return false;
} }
$plugin = new $pluginClass(); $plugin = new $pluginClass();
@ -157,21 +159,21 @@ class PluginManager {
* @return bool * @return bool
*/ */
public function deactivatePlugin($pluginClass) { public function deactivatePlugin($pluginClass) {
if(is_object($pluginClass)) { if (is_object($pluginClass)) {
$pluginClass = get_class($pluginClass); $pluginClass = get_class($pluginClass);
} }
if(!$this->isPluginActive($pluginClass)) { if (!$this->isPluginActive($pluginClass)) {
return false; return false;
} }
$plugin = $this->activePlugins[$pluginClass]; $plugin = $this->activePlugins[$pluginClass];
unset($this->activePlugins[$pluginClass]); unset($this->activePlugins[$pluginClass]);
$plugin->unload(); $plugin->unload();
$interfaces = class_implements($pluginClass); $interfaces = class_implements($pluginClass);
if(in_array(CallbackListener::CALLBACKLISTENER_INTERFACE, $interfaces)) { if (in_array(CallbackListener::CALLBACKLISTENER_INTERFACE, $interfaces)) {
$this->maniaControl->callbackManager->unregisterCallbackListener($plugin); $this->maniaControl->callbackManager->unregisterCallbackListener($plugin);
$this->maniaControl->callbackManager->unregisterScriptCallbackListener($plugin); $this->maniaControl->callbackManager->unregisterScriptCallbackListener($plugin);
} }
if(in_array(ManialinkPageAnswerListener::MANIALINKPAGEANSWERLISTENER_INTERFACE, $interfaces)) { if (in_array(ManialinkPageAnswerListener::MANIALINKPAGEANSWERLISTENER_INTERFACE, $interfaces)) {
$this->maniaControl->manialinkManager->unregisterManialinkPageAnswerListener($plugin); $this->maniaControl->manialinkManager->unregisterManialinkPageAnswerListener($plugin);
} }
$this->savePluginStatus($pluginClass, false); $this->savePluginStatus($pluginClass, false);
@ -185,25 +187,25 @@ class PluginManager {
$pluginsDirectory = ManiaControlDir . '/plugins/'; $pluginsDirectory = ManiaControlDir . '/plugins/';
$pluginFiles = scandir($pluginsDirectory, 0); $pluginFiles = scandir($pluginsDirectory, 0);
foreach($pluginFiles as $pluginFile) { foreach($pluginFiles as $pluginFile) {
if(stripos($pluginFile, '.') === 0) { if (stripos($pluginFile, '.') === 0) {
continue; continue;
} }
$classesBefore = get_declared_classes(); $classesBefore = get_declared_classes();
$success = include_once $pluginsDirectory . $pluginFile; $success = include_once $pluginsDirectory . $pluginFile;
if(!$success) { if (!$success) {
continue; continue;
} }
$classesAfter = get_declared_classes(); $classesAfter = get_declared_classes();
$newClasses = array_diff($classesAfter, $classesBefore); $newClasses = array_diff($classesAfter, $classesBefore);
foreach($newClasses as $className) { foreach($newClasses as $className) {
if(!$this->isPluginClass($className)) { if (!$this->isPluginClass($className)) {
continue; continue;
} }
$this->addPluginClass($className); $this->addPluginClass($className);
if($this->isPluginActive($className)) { if ($this->isPluginActive($className)) {
continue; continue;
} }
if(!$this->getSavedPluginStatus($className)) { if (!$this->getSavedPluginStatus($className)) {
continue; continue;
} }
$this->activatePlugin($className); $this->activatePlugin($className);
@ -218,12 +220,21 @@ class PluginManager {
* @return Plugin * @return Plugin
*/ */
public function getPlugin($pluginClass) { public function getPlugin($pluginClass) {
if($this->isPluginActive($pluginClass)) { if ($this->isPluginActive($pluginClass)) {
return $this->activePlugins[$pluginClass]; return $this->activePlugins[$pluginClass];
} }
return null; return null;
} }
/**
* Prepare all Plugins
*/
private function preparePlugins() {
foreach($this->pluginClasses as $plugin) {
$plugin::prepare($this->maniaControl);
}
}
/** /**
* Get all declared plugin class names * Get all declared plugin class names
* *
@ -259,14 +270,14 @@ class PluginManager {
) ON DUPLICATE KEY UPDATE ) ON DUPLICATE KEY UPDATE
`active` = VALUES(`active`);"; `active` = VALUES(`active`);";
$pluginStatement = $mysqli->prepare($pluginStatusQuery); $pluginStatement = $mysqli->prepare($pluginStatusQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$activeInt = ($active ? 1 : 0); $activeInt = ($active ? 1 : 0);
$pluginStatement->bind_param('si', $className, $activeInt); $pluginStatement->bind_param('si', $className, $activeInt);
$pluginStatement->execute(); $pluginStatement->execute();
if($pluginStatement->error) { if ($pluginStatement->error) {
trigger_error($pluginStatement->error); trigger_error($pluginStatement->error);
$pluginStatement->close(); $pluginStatement->close();
return false; return false;
@ -286,19 +297,19 @@ class PluginManager {
$pluginStatusQuery = "SELECT `active` FROM `" . self::TABLE_PLUGINS . "` $pluginStatusQuery = "SELECT `active` FROM `" . self::TABLE_PLUGINS . "`
WHERE `className` = ?;"; WHERE `className` = ?;";
$pluginStatement = $mysqli->prepare($pluginStatusQuery); $pluginStatement = $mysqli->prepare($pluginStatusQuery);
if($mysqli->error) { if ($mysqli->error) {
trigger_error($mysqli->error); trigger_error($mysqli->error);
return false; return false;
} }
$pluginStatement->bind_param('s', $className); $pluginStatement->bind_param('s', $className);
$pluginStatement->execute(); $pluginStatement->execute();
if($pluginStatement->error) { if ($pluginStatement->error) {
trigger_error($pluginStatement->error); trigger_error($pluginStatement->error);
$pluginStatement->close(); $pluginStatement->close();
return false; return false;
} }
$pluginStatement->store_result(); $pluginStatement->store_result();
if($pluginStatement->num_rows <= 0) { if ($pluginStatement->num_rows <= 0) {
$pluginStatement->free_result(); $pluginStatement->free_result();
$pluginStatement->close(); $pluginStatement->close();
$this->savePluginStatus($className, false); $this->savePluginStatus($className, false);

View File

@ -43,6 +43,15 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
const SETTING_DONATION_VALUES = 'Donation Values'; const SETTING_DONATION_VALUES = 'Donation Values';
const SETTING_MIN_AMOUNT_SHOWN = 'Minimum Donation amount to get shown'; const SETTING_MIN_AMOUNT_SHOWN = 'Minimum Donation amount to get shown';
/**
* Private properties
*/
/**
* @var maniaControl $maniaControl
*/
private $maniaControl = null;
private $openBills = array();
/** /**
* Prepares the Plugin * Prepares the Plugin
* *
@ -53,18 +62,8 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
// TODO: Implement prepare() method. // TODO: Implement prepare() method.
} }
/**
* Private properties
*/
/**
*
* @var maniaControl $maniaControl
*/
private $maniaControl = null;
private $openBills = array();
/** /**
*
* @see \ManiaControl\Plugins\Plugin::load() * @see \ManiaControl\Plugins\Plugin::load()
*/ */
public function load(ManiaControl $maniaControl) { public function load(ManiaControl $maniaControl) {
@ -96,7 +95,6 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::unload() * @see \ManiaControl\Plugins\Plugin::unload()
*/ */
public function unload() { public function unload() {
@ -110,7 +108,6 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getId() * @see \ManiaControl\Plugins\Plugin::getId()
*/ */
public static function getId() { public static function getId() {
@ -118,7 +115,6 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getName() * @see \ManiaControl\Plugins\Plugin::getName()
*/ */
public static function getName() { public static function getName() {
@ -126,7 +122,6 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getVersion() * @see \ManiaControl\Plugins\Plugin::getVersion()
*/ */
public static function getVersion() { public static function getVersion() {
@ -134,7 +129,6 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getAuthor() * @see \ManiaControl\Plugins\Plugin::getAuthor()
*/ */
public static function getAuthor() { public static function getAuthor() {
@ -142,7 +136,6 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
} }
/** /**
*
* @see \ManiaControl\Plugins\Plugin::getDescription() * @see \ManiaControl\Plugins\Plugin::getDescription()
*/ */
public static function getDescription() { public static function getDescription() {
@ -155,7 +148,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
* @param array $callback * @param array $callback
*/ */
public function displayWidget() { public function displayWidget() {
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATE_WIDGET_ACTIVATED)) { if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATE_WIDGET_ACTIVATED)) {
$this->displayDonateWidget(); $this->displayDonateWidget();
} }
} }
@ -168,7 +161,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
public function handleManialinkPageAnswer(array $callback) { public function handleManialinkPageAnswer(array $callback) {
$actionId = $callback[1][2]; $actionId = $callback[1][2];
$boolSetting = (strpos($actionId, self::ACTION_DONATE_VALUE) === 0); $boolSetting = (strpos($actionId, self::ACTION_DONATE_VALUE) === 0);
if(!$boolSetting) { if (!$boolSetting) {
return; return;
} }
$login = $callback[1][1]; $login = $callback[1][1];
@ -185,7 +178,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
public function handlePlayerConnect(array $callback) { public function handlePlayerConnect(array $callback) {
$player = $callback[1]; $player = $callback[1];
// Display Map Widget // Display Map Widget
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATE_WIDGET_ACTIVATED)) { if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_DONATE_WIDGET_ACTIVATED)) {
$this->displayDonateWidget($player->login); $this->displayDonateWidget($player->login);
} }
} }
@ -212,7 +205,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
$titlePrefix = strtoupper(substr($titleId, 0, 2)); $titlePrefix = strtoupper(substr($titleId, 0, 2));
//If game is shootmania lower the icons position by 20 //If game is shootmania lower the icons position by 20
if($titlePrefix == 'SM') { if ($titlePrefix == 'SM') {
$posY -= $shootManiaOffset; $posY -= $shootManiaOffset;
} }
@ -305,16 +298,16 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
public function command_Donate(array $chatCallback, Player $player) { public function command_Donate(array $chatCallback, Player $player) {
$text = $chatCallback[1][2]; $text = $chatCallback[1][2];
$params = explode(' ', $text); $params = explode(' ', $text);
if(count($params) < 2) { if (count($params) < 2) {
$this->sendDonateUsageExample($player); $this->sendDonateUsageExample($player);
return false; return false;
} }
$amount = (int)$params[1]; $amount = (int)$params[1];
if(!$amount || $amount <= 0) { if (!$amount || $amount <= 0) {
$this->sendDonateUsageExample($player); $this->sendDonateUsageExample($player);
return false; return false;
} }
if(count($params) >= 3) { if (count($params) >= 3) {
$receiver = $params[2]; $receiver = $params[2];
$receiverPlayer = $this->maniaControl->playerManager->getPlayer($receiver); $receiverPlayer = $this->maniaControl->playerManager->getPlayer($receiver);
$receiverName = ($receiverPlayer ? $receiverPlayer['NickName'] : $receiver); $receiverName = ($receiverPlayer ? $receiverPlayer['NickName'] : $receiver);
@ -333,7 +326,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
* @param $value * @param $value
*/ */
private function handleDonation(Player $player, $amount, $receiver = '', $receiverName = false) { private function handleDonation(Player $player, $amount, $receiver = '', $receiverName = false) {
if(!$receiverName) { if (!$receiverName) {
$receiverName = $this->maniaControl->server->getName(); $receiverName = $this->maniaControl->server->getName();
} }
@ -359,22 +352,22 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
* @return bool * @return bool
*/ */
public function command_Pay(array $chatCallback, Player $player) { public function command_Pay(array $chatCallback, Player $player) {
if(!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_SUPERADMIN)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player); $this->maniaControl->authenticationManager->sendNotAllowed($player);
return false; return false;
} }
$text = $chatCallback[1][2]; $text = $chatCallback[1][2];
$params = explode(' ', $text); $params = explode(' ', $text);
if(count($params) < 2) { if (count($params) < 2) {
$this->sendPayUsageExample($player); $this->sendPayUsageExample($player);
return false; return false;
} }
$amount = (int)$params[1]; $amount = (int)$params[1];
if(!$amount || $amount <= 0) { if (!$amount || $amount <= 0) {
$this->sendPayUsageExample($player); $this->sendPayUsageExample($player);
return false; return false;
} }
if(count($params) >= 3) { if (count($params) >= 3) {
$receiver = $params[2]; $receiver = $params[2];
} else { } else {
$receiver = $player->login; $receiver = $player->login;
@ -402,7 +395,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
* @return bool * @return bool
*/ */
public function command_GetPlanets(array $chatCallback, Player $player) { public function command_GetPlanets(array $chatCallback, Player $player) {
if(!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) { if (!$this->maniaControl->authenticationManager->checkRight($player, AuthenticationManager::AUTH_LEVEL_ADMIN)) {
$this->maniaControl->authenticationManager->sendNotAllowed($player); $this->maniaControl->authenticationManager->sendNotAllowed($player);
return false; return false;
} }
@ -425,7 +418,7 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
*/ */
public function handleBillUpdated(array $callback) { public function handleBillUpdated(array $callback) {
$billId = $callback[1][0]; $billId = $callback[1][0];
if(!array_key_exists($billId, $this->openBills)) { if (!array_key_exists($billId, $this->openBills)) {
return false; return false;
} }
$billData = $this->openBills[$billId]; $billData = $this->openBills[$billId];
@ -437,17 +430,17 @@ class DonationPlugin implements CallbackListener, CommandListener, Plugin {
// Payed // Payed
$donation = $billData[0]; $donation = $billData[0];
$amount = $billData[3]; $amount = $billData[3];
if($donation) { if ($donation) {
$player = $this->maniaControl->playerManager->getPlayer($login); $player = $this->maniaControl->playerManager->getPlayer($login);
// Donation // Donation
if(strlen($receiver) > 0) { if (strlen($receiver) > 0) {
// To player // To player
$message = "Successfully donated {$amount} to '{$receiver}'!"; $message = "Successfully donated {$amount} to '{$receiver}'!";
$this->maniaControl->chat->sendSuccess($message, $login); $this->maniaControl->chat->sendSuccess($message, $login);
} else { } else {
// To server // To server
if($this->maniaControl->settingManager->getSetting($this, self::SETTING_ANNOUNCE_SERVERDONATION, true) && $amount >= $this->maniaControl->settingManager->getSetting($this, self::SETTING_MIN_AMOUNT_SHOWN, true) if ($this->maniaControl->settingManager->getSetting($this, self::SETTING_ANNOUNCE_SERVERDONATION, true) && $amount >= $this->maniaControl->settingManager->getSetting($this, self::SETTING_MIN_AMOUNT_SHOWN, true)
) { ) {
$login = null; $login = null;
$message = '$<' . $player->nickname . '$> donated ' . $amount . ' Planets! Thanks.'; $message = '$<' . $player->nickname . '$> donated ' . $amount . ' Planets! Thanks.';