[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
[2896] | 24 | * Reto Grieder |
---|
[1505] | 25 | * Co-authors: |
---|
[2896] | 26 | * ... |
---|
[1505] | 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | /** |
---|
[3196] | 31 | @file |
---|
| 32 | @brief |
---|
| 33 | Implementation of the Core singleton with its global variables (avoids boost include) |
---|
[1505] | 34 | */ |
---|
| 35 | |
---|
[1524] | 36 | #include "Core.h" |
---|
[2710] | 37 | |
---|
[1756] | 38 | #include <cassert> |
---|
[7427] | 39 | #include <cstdlib> |
---|
| 40 | #include <ctime> |
---|
[7401] | 41 | #include <fstream> |
---|
[5929] | 42 | #include <vector> |
---|
[2710] | 43 | |
---|
| 44 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[2896] | 45 | # ifndef WIN32_LEAN_AND_MEAN |
---|
| 46 | # define WIN32_LEAN_AND_MEAN |
---|
| 47 | # endif |
---|
[2710] | 48 | # include <windows.h> |
---|
[3214] | 49 | # undef min |
---|
| 50 | # undef max |
---|
[2710] | 51 | #endif |
---|
| 52 | |
---|
[5929] | 53 | #include "util/Clock.h" |
---|
[2896] | 54 | #include "util/Debug.h" |
---|
[2710] | 55 | #include "util/Exception.h" |
---|
[6417] | 56 | #include "util/Scope.h" |
---|
[7284] | 57 | #include "util/ScopedSingletonManager.h" |
---|
[2896] | 58 | #include "util/SignalHandler.h" |
---|
[5929] | 59 | #include "PathConfig.h" |
---|
[6021] | 60 | #include "CommandLineParser.h" |
---|
[2896] | 61 | #include "ConfigFileManager.h" |
---|
| 62 | #include "ConfigValueIncludes.h" |
---|
| 63 | #include "CoreIncludes.h" |
---|
[5693] | 64 | #include "DynLibManager.h" |
---|
[5781] | 65 | #include "GameMode.h" |
---|
| 66 | #include "GraphicsManager.h" |
---|
| 67 | #include "GUIManager.h" |
---|
[2896] | 68 | #include "Identifier.h" |
---|
[1505] | 69 | #include "Language.h" |
---|
[5695] | 70 | #include "LuaState.h" |
---|
[7284] | 71 | #include "command/ConsoleCommand.h" |
---|
| 72 | #include "command/IOConsole.h" |
---|
| 73 | #include "command/TclBind.h" |
---|
| 74 | #include "command/TclThreadManager.h" |
---|
[5781] | 75 | #include "input/InputManager.h" |
---|
[1505] | 76 | |
---|
| 77 | namespace orxonox |
---|
| 78 | { |
---|
[3196] | 79 | //! Static pointer to the singleton |
---|
[3370] | 80 | Core* Core::singletonPtr_s = 0; |
---|
[2662] | 81 | |
---|
[3280] | 82 | SetCommandLineArgument(settingsFile, "orxonox.ini").information("THE configuration file"); |
---|
[6746] | 83 | SetCommandLineSwitch(noIOConsole).information("Use this if you don't want to use the IOConsole (for instance for Lua debugging)"); |
---|
[7163] | 84 | |
---|
[3280] | 85 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[6417] | 86 | SetCommandLineArgument(limitToCPU, 1).information("Limits the program to one CPU/core (1, 2, 3, etc.). Default is the first core (faster than off)"); |
---|
[3280] | 87 | #endif |
---|
[2710] | 88 | |
---|
[3323] | 89 | Core::Core(const std::string& cmdLine) |
---|
[3370] | 90 | // Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands) |
---|
| 91 | : identifierDestroyer_(Identifier::destroyAllIdentifiers) |
---|
[5781] | 92 | // Cleanup guard for external console commands that don't belong to an Identifier |
---|
[7284] | 93 | , consoleCommandDestroyer_(ConsoleCommand::destroyAll) |
---|
[5781] | 94 | , bGraphicsLoaded_(false) |
---|
[6746] | 95 | , bStartIOConsole_(true) |
---|
[7870] | 96 | , lastLevelTimestamp_(0) |
---|
| 97 | , ogreConfigTimestamp_(0) |
---|
[3280] | 98 | { |
---|
[5693] | 99 | // Set the hard coded fixed paths |
---|
[5929] | 100 | this->pathConfig_.reset(new PathConfig()); |
---|
[3280] | 101 | |
---|
[5693] | 102 | // Create a new dynamic library manager |
---|
| 103 | this->dynLibManager_.reset(new DynLibManager()); |
---|
[2896] | 104 | |
---|
[5693] | 105 | // Load modules |
---|
[5929] | 106 | const std::vector<std::string>& modulePaths = this->pathConfig_->getModulePaths(); |
---|
| 107 | for (std::vector<std::string>::const_iterator it = modulePaths.begin(); it != modulePaths.end(); ++it) |
---|
[5693] | 108 | { |
---|
[5929] | 109 | try |
---|
[5693] | 110 | { |
---|
[5929] | 111 | this->dynLibManager_->load(*it); |
---|
[5693] | 112 | } |
---|
[5929] | 113 | catch (...) |
---|
| 114 | { |
---|
| 115 | COUT(1) << "Couldn't load module \"" << *it << "\": " << Exception::handleMessage() << std::endl; |
---|
| 116 | } |
---|
[5693] | 117 | } |
---|
| 118 | |
---|
| 119 | // Parse command line arguments AFTER the modules have been loaded (static code!) |
---|
[6021] | 120 | CommandLineParser::parseCommandLine(cmdLine); |
---|
[5693] | 121 | |
---|
| 122 | // Set configurable paths like log, config and media |
---|
[5929] | 123 | this->pathConfig_->setConfigurablePaths(); |
---|
[5693] | 124 | |
---|
[6105] | 125 | // create a signal handler (only active for Linux) |
---|
[2896] | 126 | // This call is placed as soon as possible, but after the directories are set |
---|
[3370] | 127 | this->signalHandler_.reset(new SignalHandler()); |
---|
[5929] | 128 | this->signalHandler_->doCatch(PathConfig::getExecutablePathString(), PathConfig::getLogPathString() + "orxonox_crash.log"); |
---|
[2896] | 129 | |
---|
[6105] | 130 | // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% (Windows) was used |
---|
| 131 | OutputHandler::getInstance().setLogPath(PathConfig::getLogPathString()); |
---|
[2710] | 132 | |
---|
[3280] | 133 | // Parse additional options file now that we know its path |
---|
[6021] | 134 | CommandLineParser::parseFile(); |
---|
[3280] | 135 | |
---|
| 136 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 137 | // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump |
---|
| 138 | // do this after ogre has initialised. Somehow Ogre changes the settings again (not through |
---|
| 139 | // the timer though). |
---|
[6021] | 140 | int limitToCPU = CommandLineParser::getValue("limitToCPU"); |
---|
[3280] | 141 | if (limitToCPU > 0) |
---|
| 142 | setThreadAffinity(static_cast<unsigned int>(limitToCPU)); |
---|
| 143 | #endif |
---|
| 144 | |
---|
[2896] | 145 | // Manage ini files and set the default settings file (usually orxonox.ini) |
---|
[3370] | 146 | this->configFileManager_.reset(new ConfigFileManager()); |
---|
[2896] | 147 | this->configFileManager_->setFilename(ConfigFileType::Settings, |
---|
[6021] | 148 | CommandLineParser::getValue("settingsFile").getString()); |
---|
[2896] | 149 | |
---|
[3280] | 150 | // Required as well for the config values |
---|
[3370] | 151 | this->languageInstance_.reset(new Language()); |
---|
[2896] | 152 | |
---|
[6417] | 153 | // Do this soon after the ConfigFileManager has been created to open up the |
---|
| 154 | // possibility to configure everything below here |
---|
| 155 | ClassIdentifier<Core>::getIdentifier("Core")->initialiseObject(this, "Core", true); |
---|
| 156 | this->setConfigValues(); |
---|
| 157 | |
---|
[6105] | 158 | // create persistent io console |
---|
[6746] | 159 | if (CommandLineParser::getValue("noIOConsole").getBool()) |
---|
| 160 | { |
---|
| 161 | ModifyConfigValue(bStartIOConsole_, tset, false); |
---|
| 162 | } |
---|
| 163 | if (this->bStartIOConsole_) |
---|
| 164 | this->ioConsole_.reset(new IOConsole()); |
---|
[6105] | 165 | |
---|
[5695] | 166 | // creates the class hierarchy for all classes with factories |
---|
[5929] | 167 | Identifier::createClassHierarchy(); |
---|
[5695] | 168 | |
---|
[5781] | 169 | // Load OGRE excluding the renderer and the render window |
---|
| 170 | this->graphicsManager_.reset(new GraphicsManager(false)); |
---|
| 171 | |
---|
| 172 | // initialise Tcl |
---|
[5929] | 173 | this->tclBind_.reset(new TclBind(PathConfig::getDataPathString())); |
---|
[5781] | 174 | this->tclThreadManager_.reset(new TclThreadManager(tclBind_->getTclInterpreter())); |
---|
| 175 | |
---|
[5929] | 176 | // Create singletons that always exist (in other libraries) |
---|
| 177 | this->rootScope_.reset(new Scope<ScopeID::Root>()); |
---|
[7401] | 178 | |
---|
| 179 | // Generate documentation instead of normal run? |
---|
| 180 | std::string docFilename; |
---|
| 181 | CommandLineParser::getValue("generateDoc", &docFilename); |
---|
| 182 | if (!docFilename.empty()) |
---|
| 183 | { |
---|
| 184 | std::ofstream docFile(docFilename.c_str()); |
---|
| 185 | if (docFile.is_open()) |
---|
| 186 | { |
---|
| 187 | CommandLineParser::generateDoc(docFile); |
---|
| 188 | docFile.close(); |
---|
| 189 | } |
---|
| 190 | else |
---|
| 191 | COUT(0) << "Error: Could not open file for documentation writing" << endl; |
---|
| 192 | } |
---|
[1505] | 193 | } |
---|
| 194 | |
---|
| 195 | /** |
---|
[3370] | 196 | @brief |
---|
[5695] | 197 | All destruction code is handled by scoped_ptrs and ScopeGuards. |
---|
[1505] | 198 | */ |
---|
[1524] | 199 | Core::~Core() |
---|
[1505] | 200 | { |
---|
[6417] | 201 | // Remove us from the object lists again to avoid problems when destroying them |
---|
| 202 | this->unregisterObject(); |
---|
[3370] | 203 | } |
---|
[2896] | 204 | |
---|
[6417] | 205 | //! Function to collect the SetConfigValue-macro calls. |
---|
| 206 | void Core::setConfigValues() |
---|
| 207 | { |
---|
| 208 | #ifdef ORXONOX_RELEASE |
---|
| 209 | const unsigned int defaultLevelLogFile = 3; |
---|
| 210 | #else |
---|
| 211 | const unsigned int defaultLevelLogFile = 4; |
---|
| 212 | #endif |
---|
[7167] | 213 | SetConfigValueExternal(softDebugLevelLogFile_, "OutputHandler", "softDebugLevelLogFile", defaultLevelLogFile) |
---|
[6417] | 214 | .description("The maximum level of debug output shown in the log file"); |
---|
| 215 | OutputHandler::getInstance().setSoftDebugLevel(OutputHandler::logFileOutputListenerName_s, this->softDebugLevelLogFile_); |
---|
| 216 | |
---|
| 217 | SetConfigValue(language_, Language::getInstance().defaultLanguage_) |
---|
| 218 | .description("The language of the in game text") |
---|
| 219 | .callback(this, &Core::languageChanged); |
---|
| 220 | SetConfigValue(bInitRandomNumberGenerator_, true) |
---|
| 221 | .description("If true, all random actions are different each time you start the game") |
---|
| 222 | .callback(this, &Core::initRandomNumberGenerator); |
---|
[6746] | 223 | SetConfigValue(bStartIOConsole_, true) |
---|
| 224 | .description("Set to false if you don't want to use the IOConsole (for Lua debugging for instance)"); |
---|
[7870] | 225 | SetConfigValue(lastLevelTimestamp_, 0) |
---|
| 226 | .description("Timestamp when the last level was started."); |
---|
| 227 | SetConfigValue(ogreConfigTimestamp_, 0) |
---|
| 228 | .description("Timestamp when the ogre config file was changed."); |
---|
[6417] | 229 | } |
---|
| 230 | |
---|
| 231 | //! Callback function if the language has changed. |
---|
| 232 | void Core::languageChanged() |
---|
| 233 | { |
---|
| 234 | // Read the translation file after the language was configured |
---|
| 235 | Language::getInstance().readTranslatedLanguageFile(); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | void Core::initRandomNumberGenerator() |
---|
| 239 | { |
---|
| 240 | static bool bInitialized = false; |
---|
| 241 | if (!bInitialized && this->bInitRandomNumberGenerator_) |
---|
| 242 | { |
---|
| 243 | srand(static_cast<unsigned int>(time(0))); |
---|
| 244 | rand(); |
---|
| 245 | bInitialized = true; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
[5781] | 249 | void Core::loadGraphics() |
---|
| 250 | { |
---|
| 251 | // Any exception should trigger this, even in upgradeToGraphics (see its remarks) |
---|
| 252 | Loki::ScopeGuard unloader = Loki::MakeObjGuard(*this, &Core::unloadGraphics); |
---|
| 253 | |
---|
| 254 | // Upgrade OGRE to receive a render window |
---|
[7175] | 255 | try |
---|
| 256 | { |
---|
| 257 | graphicsManager_->upgradeToGraphics(); |
---|
| 258 | } |
---|
[7872] | 259 | catch (const InitialisationFailedException&) |
---|
[7868] | 260 | { |
---|
| 261 | // Exit the application if the Ogre config dialog was canceled |
---|
| 262 | COUT(1) << Exception::handleMessage() << std::endl; |
---|
| 263 | exit(EXIT_FAILURE); |
---|
| 264 | } |
---|
[7175] | 265 | catch (...) |
---|
| 266 | { |
---|
| 267 | // Recovery from this is very difficult. It requires to completely |
---|
| 268 | // destroy Ogre related objects and load again (without graphics). |
---|
| 269 | // However since Ogre 1.7 there seems to be a problem when Ogre |
---|
| 270 | // throws an exception and the graphics engine then gets destroyed |
---|
| 271 | // and reloaded between throw and catch (access violation in MSVC). |
---|
| 272 | // That's why we abort completely and only display the exception. |
---|
[7868] | 273 | COUT(1) << "An exception occurred during upgrade to graphics. " |
---|
[7175] | 274 | << "That is unrecoverable. The message was:" << endl |
---|
| 275 | << Exception::handleMessage() << endl; |
---|
| 276 | abort(); |
---|
| 277 | } |
---|
[5781] | 278 | |
---|
| 279 | // Calls the InputManager which sets up the input devices. |
---|
| 280 | inputManager_.reset(new InputManager()); |
---|
| 281 | |
---|
[5929] | 282 | // Load the CEGUI interface |
---|
[6746] | 283 | guiManager_.reset(new GUIManager(inputManager_->getMousePosition())); |
---|
[5781] | 284 | |
---|
[5929] | 285 | bGraphicsLoaded_ = true; |
---|
| 286 | GameMode::bShowsGraphics_s = true; |
---|
| 287 | |
---|
| 288 | // Load some sort of a debug overlay (only denoted by its name, "debug.oxo") |
---|
| 289 | graphicsManager_->loadDebugOverlay(); |
---|
| 290 | |
---|
| 291 | // Create singletons associated with graphics (in other libraries) |
---|
| 292 | graphicsScope_.reset(new Scope<ScopeID::Graphics>()); |
---|
| 293 | |
---|
[5781] | 294 | unloader.Dismiss(); |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | void Core::unloadGraphics() |
---|
| 298 | { |
---|
[5929] | 299 | this->graphicsScope_.reset(); |
---|
| 300 | this->guiManager_.reset(); |
---|
| 301 | this->inputManager_.reset(); |
---|
[5781] | 302 | this->graphicsManager_.reset(); |
---|
| 303 | |
---|
| 304 | // Load Ogre::Root again, but without the render system |
---|
| 305 | try |
---|
| 306 | { this->graphicsManager_.reset(new GraphicsManager(false)); } |
---|
| 307 | catch (...) |
---|
| 308 | { |
---|
| 309 | COUT(0) << "An exception occurred during 'unloadGraphics':" << Exception::handleMessage() << std::endl |
---|
| 310 | << "Another exception might be being handled which may lead to undefined behaviour!" << std::endl |
---|
| 311 | << "Terminating the program." << std::endl; |
---|
| 312 | abort(); |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | bGraphicsLoaded_ = false; |
---|
[5929] | 316 | GameMode::bShowsGraphics_s = false; |
---|
[5781] | 317 | } |
---|
| 318 | |
---|
[6417] | 319 | //! Sets the language in the config-file back to the default. |
---|
| 320 | void Core::resetLanguage() |
---|
[1505] | 321 | { |
---|
[6417] | 322 | ResetConfigValue(language_); |
---|
[1505] | 323 | } |
---|
| 324 | |
---|
| 325 | /** |
---|
[2896] | 326 | @note |
---|
| 327 | The code of this function has been copied and adjusted from OGRE, an open source graphics engine. |
---|
| 328 | (Object-oriented Graphics Rendering Engine) |
---|
| 329 | For the latest info, see http://www.ogre3d.org/ |
---|
| 330 | |
---|
| 331 | Copyright (c) 2000-2008 Torus Knot Software Ltd |
---|
| 332 | |
---|
| 333 | OGRE is licensed under the LGPL. For more info, see OGRE license. |
---|
[2710] | 334 | */ |
---|
[2896] | 335 | void Core::setThreadAffinity(int limitToCPU) |
---|
[2710] | 336 | { |
---|
[3280] | 337 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 338 | |
---|
[2896] | 339 | if (limitToCPU <= 0) |
---|
| 340 | return; |
---|
[2710] | 341 | |
---|
[2896] | 342 | unsigned int coreNr = limitToCPU - 1; |
---|
| 343 | // Get the current process core mask |
---|
| 344 | DWORD procMask; |
---|
| 345 | DWORD sysMask; |
---|
| 346 | # if _MSC_VER >= 1400 && defined (_M_X64) |
---|
| 347 | GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); |
---|
| 348 | # else |
---|
| 349 | GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); |
---|
| 350 | # endif |
---|
[2710] | 351 | |
---|
[2896] | 352 | // If procMask is 0, consider there is only one core available |
---|
| 353 | // (using 0 as procMask will cause an infinite loop below) |
---|
| 354 | if (procMask == 0) |
---|
| 355 | procMask = 1; |
---|
| 356 | |
---|
| 357 | // if the core specified with coreNr is not available, take the lowest one |
---|
| 358 | if (!(procMask & (1 << coreNr))) |
---|
| 359 | coreNr = 0; |
---|
| 360 | |
---|
| 361 | // Find the lowest core that this process uses and coreNr suggests |
---|
| 362 | DWORD threadMask = 1; |
---|
| 363 | while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr))) |
---|
| 364 | threadMask <<= 1; |
---|
| 365 | |
---|
| 366 | // Set affinity to the first core |
---|
| 367 | SetThreadAffinityMask(GetCurrentThread(), threadMask); |
---|
| 368 | #endif |
---|
[2710] | 369 | } |
---|
| 370 | |
---|
[5695] | 371 | void Core::preUpdate(const Clock& time) |
---|
[2896] | 372 | { |
---|
[6417] | 373 | // Update singletons before general ticking |
---|
| 374 | ScopedSingletonManager::preUpdate<ScopeID::Root>(time); |
---|
[5781] | 375 | if (this->bGraphicsLoaded_) |
---|
| 376 | { |
---|
[6417] | 377 | // Process input events |
---|
| 378 | this->inputManager_->preUpdate(time); |
---|
| 379 | // Update GUI |
---|
| 380 | this->guiManager_->preUpdate(time); |
---|
| 381 | // Update singletons before general ticking |
---|
| 382 | ScopedSingletonManager::preUpdate<ScopeID::Graphics>(time); |
---|
[5781] | 383 | } |
---|
[6417] | 384 | // Process console events and status line |
---|
[6746] | 385 | if (this->ioConsole_ != NULL) |
---|
| 386 | this->ioConsole_->preUpdate(time); |
---|
[6417] | 387 | // Process thread commands |
---|
| 388 | this->tclThreadManager_->preUpdate(time); |
---|
[2896] | 389 | } |
---|
[3370] | 390 | |
---|
[5695] | 391 | void Core::postUpdate(const Clock& time) |
---|
[3370] | 392 | { |
---|
[6417] | 393 | // Update singletons just before rendering |
---|
| 394 | ScopedSingletonManager::postUpdate<ScopeID::Root>(time); |
---|
[5781] | 395 | if (this->bGraphicsLoaded_) |
---|
| 396 | { |
---|
[6417] | 397 | // Update singletons just before rendering |
---|
| 398 | ScopedSingletonManager::postUpdate<ScopeID::Graphics>(time); |
---|
[5781] | 399 | // Render (doesn't throw) |
---|
[6417] | 400 | this->graphicsManager_->postUpdate(time); |
---|
[5781] | 401 | } |
---|
[3370] | 402 | } |
---|
[7870] | 403 | |
---|
| 404 | void Core::updateLastLevelTimestamp() |
---|
| 405 | { |
---|
| 406 | ModifyConfigValue(lastLevelTimestamp_, set, static_cast<long long>(time(NULL))); |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | void Core::updateOgreConfigTimestamp() |
---|
| 410 | { |
---|
| 411 | ModifyConfigValue(ogreConfigTimestamp_, set, static_cast<long long>(time(NULL))); |
---|
| 412 | } |
---|
[1505] | 413 | } |
---|