From 8148da675324f89d01dd28eb500df0f44a55dbae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Tue, 6 May 2014 02:17:11 +0200 Subject: [PATCH] script manager class improved script callback enabling --- application/core/ManiaControl.php | 32 ++---------- application/core/Server/ScriptManager.php | 60 +++++++++++++++++++++++ application/core/Server/Server.php | 2 + 3 files changed, 65 insertions(+), 29 deletions(-) create mode 100644 application/core/Server/ScriptManager.php diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index 4fc1419f..1337339f 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -418,9 +418,7 @@ class ManiaControl implements CommandListener, TimerListener { } } catch (Exception $e) { // TODO remove - if ($this->errorHandler) { - $this->errorHandler->handleException($e, false); - } + $this->errorHandler->handleException($e, false); $this->quit($e->getMessage()); } @@ -430,31 +428,7 @@ class ManiaControl implements CommandListener, TimerListener { // Hide old widgets $this->client->sendHideManialinkPage(); - // Enable script callbacks if needed - if ($this->server->getGameMode() != 0) { - return; - } - - try { - $scriptSettings = $this->client->getModeScriptSettings(); - } catch (NotInScriptModeException $e) { - return; - } - - if (!array_key_exists('S_UseScriptCallbacks', $scriptSettings)) { - return; - } - - $scriptSettings['S_UseScriptCallbacks'] = true; - try { - $this->client->setModeScriptSettings($scriptSettings); - } catch (Exception $e) { - // TODO temp added 19.04.2014 - $this->errorHandler->triggerDebugNotice("Exception line 437 ManiaControl.php " . $e->getMessage()); - - trigger_error("Couldn't set mode script settings to enable script callbacks. " . $e->getMessage()); - return; - } - $this->log('Script Callbacks successfully enabled!'); + // Enable script callbacks + $this->server->scriptManager->enableScriptCallbacks(); } } diff --git a/application/core/Server/ScriptManager.php b/application/core/Server/ScriptManager.php new file mode 100644 index 00000000..3df378e8 --- /dev/null +++ b/application/core/Server/ScriptManager.php @@ -0,0 +1,60 @@ + + * @copyright 2014 ManiaControl Team + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + */ +class ScriptManager { + /* + * Private Properties + */ + public $maniaControl = null; + + /** + * Construct a new Script Manager + * + * @param ManiaControl $maniaControl + */ + public function __construct(ManiaControl $maniaControl) { + $this->maniaControl = $maniaControl; + } + + /** + * Enable Script Callbacks + * + * @param bool $enable + * @return bool + */ + public function enableScriptCallbacks($enable = true) { + try { + $scriptSettings = $this->maniaControl->client->getModeScriptSettings(); + } catch (NotInScriptModeException $e) { + return false; + } + + if (!array_key_exists('S_UseScriptCallbacks', $scriptSettings)) { + return false; + } + + $scriptSettings['S_UseScriptCallbacks'] = (bool)$enable; + + try { + $this->maniaControl->client->setModeScriptSettings($scriptSettings); + } catch (Exception $e) { + // TODO temp added 19.04.2014 + $this->maniaControl->errorHandler->handleException($e, false); + trigger_error("Couldn't set mode script settings to enable script callbacks. " . $e->getMessage()); + return false; + } + $this->maniaControl->log('Script Callbacks successfully enabled!'); + return false; + } +} diff --git a/application/core/Server/Server.php b/application/core/Server/Server.php index 001c4e92..aa0cfa5a 100644 --- a/application/core/Server/Server.php +++ b/application/core/Server/Server.php @@ -39,6 +39,7 @@ class Server implements CallbackListener { public $serverCommands = null; public $usageReporter = null; public $rankingManager = null; + public $scriptManager = null; /* * Private Properties @@ -58,6 +59,7 @@ class Server implements CallbackListener { $this->serverCommands = new ServerCommands($maniaControl); $this->usageReporter = new UsageReporter($maniaControl); $this->rankingManager = new RankingManager($maniaControl); + $this->scriptManager = new ScriptManager($maniaControl); // Register for callbacks $this->maniaControl->callbackManager->registerCallbackListener(CallbackManager::CB_ONINIT, $this, 'onInit');