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

View File

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