From e9e1fb94d9738d7f83fb10ee3018cf7abb7c166b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steffen=20Schro=CC=88der?= Date: Mon, 9 Dec 2013 15:43:08 +0100 Subject: [PATCH] - Improved Logging - Error handler - Shutdown function - Added OnShutdown callback --- application/ManiaControl.php | 86 ++++++++++++++++--- .../core/Callbacks/CallbackManager.php | 1 + application/core/ManiaControl.php | 56 ++++++------ application/core/Server/Server.php | 6 +- 4 files changed, 107 insertions(+), 42 deletions(-) diff --git a/application/ManiaControl.php b/application/ManiaControl.php index 926cab9c..d3395e6e 100644 --- a/application/ManiaControl.php +++ b/application/ManiaControl.php @@ -1,29 +1,89 @@ run(); diff --git a/application/core/Callbacks/CallbackManager.php b/application/core/Callbacks/CallbackManager.php index e03e5f0f..53115d33 100644 --- a/application/core/Callbacks/CallbackManager.php +++ b/application/core/Callbacks/CallbackManager.php @@ -18,6 +18,7 @@ class CallbackManager { const CB_MC_5_SECOND = 'ManiaControl.5Second'; const CB_MC_1_MINUTE = 'ManiaControl.1Minute'; const CB_MC_ONINIT = 'ManiaControl.OnInit'; + const CB_MC_ONSHUTDOWN = 'ManiaControl.OnShutdown'; const CB_MC_CLIENTUPDATED = 'ManiaControl.ClientUpdated'; const CB_MC_BEGINMAP = 'ManiaControl.BeginMap'; const CB_MC_ENDMAP = 'ManiaControl.EndMap'; diff --git a/application/core/ManiaControl.php b/application/core/ManiaControl.php index 2a35ceec..07e03262 100644 --- a/application/core/ManiaControl.php +++ b/application/core/ManiaControl.php @@ -86,6 +86,8 @@ class ManiaControl implements CommandListener { * Construct ManiaControl */ public function __construct() { + logMessage('Loading ManiaControl v' . self::VERSION . '...'); + $this->database = new Database($this); $this->callbackManager = new CallbackManager($this); $this->settingManager = new SettingManager($this); @@ -119,8 +121,8 @@ class ManiaControl implements CommandListener { /** * Send ManiaControl version * - * @param array $chat - * @param Player $player + * @param array $chat + * @param Player $player * @return bool */ public function command_Version(array $chat, Player $player) { @@ -134,6 +136,9 @@ class ManiaControl implements CommandListener { * @param string $message */ public function quit($message = '') { + // OnShutdown callback + $this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONSHUTDOWN, array(CallbackManager::CB_MC_ONSHUTDOWN)); + if ($this->client) { // Announce quit $this->chat->sendInformation('ManiaControl shutting down.'); @@ -144,7 +149,7 @@ class ManiaControl implements CommandListener { // Log quit reason if ($message) { - error_log($message); + logMessage($message); } // Shutdown @@ -152,7 +157,7 @@ class ManiaControl implements CommandListener { $this->client->Terminate(); } - error_log("Quitting ManiaControl!"); + logMessage('Quitting ManiaControl!'); exit(); } @@ -160,7 +165,7 @@ class ManiaControl implements CommandListener { * Run ManiaControl */ public function run() { - error_log('Starting ManiaControl v' . self::VERSION . '!'); + logMessage('Starting ManiaControl v' . self::VERSION . '!'); // Load plugins $this->pluginManager->loadPlugins(); @@ -168,13 +173,16 @@ class ManiaControl implements CommandListener { // Connect to server $this->connect(); + // Register shutdown handler + register_shutdown_function(array($this, 'quit')); + // Loading finished - error_log("Loading completed!"); + logMessage('Loading completed!'); // Announce ManiaControl $this->chat->sendInformation('ManiaControl v' . self::VERSION . ' successfully started!'); - // OnInit + // OnInit callback $this->callbackManager->triggerCallback(CallbackManager::CB_MC_ONINIT, array(CallbackManager::CB_MC_ONINIT)); // Main loop @@ -196,7 +204,7 @@ class ManiaControl implements CommandListener { } // Shutdown - $this->client->Terminate(); + $this->quit(); } /** @@ -213,7 +221,7 @@ class ManiaControl implements CommandListener { if (!$host) trigger_error("Invalid server configuration (port).", E_USER_ERROR); $port = (string) $port[0]; - error_log("Connecting to server at {$host}:{$port}..."); + logMessage("Connecting to server at {$host}:{$port}..."); // Connect if (!$this->client->InitWithIp($host, $port, 20)) { @@ -249,26 +257,22 @@ class ManiaControl implements CommandListener { } // Connect finished - error_log("Server connection successfully established!"); + logMessage("Server connection successfully established!"); // Enable script callbacks if needed - if ($this->server->getGameMode() === 0) { - if (!$this->client->query('GetModeScriptSettings')) { - trigger_error("Couldn't get mode script settings. " . $this->getClientErrorText()); - } - else { - $scriptSettings = $this->client->getResponse(); - if (array_key_exists('S_UseScriptCallbacks', $scriptSettings)) { - $scriptSettings['S_UseScriptCallbacks'] = true; - if (!$this->client->query('SetModeScriptSettings', $scriptSettings)) { - trigger_error("Couldn't set mode script settings to enable script callbacks. " . $this->getClientErrorText()); - } - else { - error_log("Script callbacks successfully enabled."); - } - } - } + if ($this->server->getGameMode() != 0) return; + if (!$this->client->query('GetModeScriptSettings')) { + trigger_error("Couldn't get mode script settings. " . $this->getClientErrorText()); + return; } + $scriptSettings = $this->client->getResponse(); + if (!array_key_exists('S_UseScriptCallbacks', $scriptSettings)) return; + $scriptSettings['S_UseScriptCallbacks'] = true; + if (!$this->client->query('SetModeScriptSettings', $scriptSettings)) { + trigger_error("Couldn't set mode script settings to enable script callbacks. " . $this->getClientErrorText()); + return; + } + logMessage('Script callbacks successfully enabled.'); } } diff --git a/application/core/Server/Server.php b/application/core/Server/Server.php index 680b800f..50390f30 100644 --- a/application/core/Server/Server.php +++ b/application/core/Server/Server.php @@ -287,14 +287,14 @@ class Server { $waitBegin = time(); $maxWaitTime = 20; $lastStatus = $response['Name']; - error_log("Waiting for server to reach status {$statusCode}..."); - error_log("Current Status: {$lastStatus}"); + logMessage("Waiting for server to reach status {$statusCode}..."); + logMessage("Current Status: {$lastStatus}"); while ($response['Code'] !== 4) { sleep(1); $this->maniaControl->client->query('GetStatus'); $response = $this->maniaControl->client->getResponse(); if ($lastStatus !== $response['Name']) { - error_log("New Status: " . $response['Name']); + logMessage("New Status: " . $response['Name']); $lastStatus = $response['Name']; } if (time() - $maxWaitTime > $waitBegin) {