[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> |
---|
[5929] | 39 | #include <vector> |
---|
[2710] | 40 | |
---|
| 41 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[2896] | 42 | # ifndef WIN32_LEAN_AND_MEAN |
---|
| 43 | # define WIN32_LEAN_AND_MEAN |
---|
| 44 | # endif |
---|
[2710] | 45 | # include <windows.h> |
---|
[3214] | 46 | # undef min |
---|
| 47 | # undef max |
---|
[2710] | 48 | #endif |
---|
| 49 | |
---|
[5929] | 50 | #include "util/Clock.h" |
---|
[2896] | 51 | #include "util/Debug.h" |
---|
[2710] | 52 | #include "util/Exception.h" |
---|
[6417] | 53 | #include "util/Scope.h" |
---|
[2896] | 54 | #include "util/SignalHandler.h" |
---|
[5929] | 55 | #include "PathConfig.h" |
---|
[5781] | 56 | #include "CommandExecutor.h" |
---|
[6021] | 57 | #include "CommandLineParser.h" |
---|
[2896] | 58 | #include "ConfigFileManager.h" |
---|
| 59 | #include "ConfigValueIncludes.h" |
---|
| 60 | #include "CoreIncludes.h" |
---|
[5693] | 61 | #include "DynLibManager.h" |
---|
[5781] | 62 | #include "GameMode.h" |
---|
| 63 | #include "GraphicsManager.h" |
---|
| 64 | #include "GUIManager.h" |
---|
[2896] | 65 | #include "Identifier.h" |
---|
[1505] | 66 | #include "Language.h" |
---|
[6105] | 67 | #include "IOConsole.h" |
---|
[5695] | 68 | #include "LuaState.h" |
---|
[5929] | 69 | #include "ScopedSingletonManager.h" |
---|
[5781] | 70 | #include "TclBind.h" |
---|
| 71 | #include "TclThreadManager.h" |
---|
| 72 | #include "input/InputManager.h" |
---|
[1505] | 73 | |
---|
| 74 | namespace orxonox |
---|
| 75 | { |
---|
[3196] | 76 | //! Static pointer to the singleton |
---|
[3370] | 77 | Core* Core::singletonPtr_s = 0; |
---|
[2662] | 78 | |
---|
[3280] | 79 | SetCommandLineArgument(settingsFile, "orxonox.ini").information("THE configuration file"); |
---|
[6746] | 80 | SetCommandLineSwitch(noIOConsole).information("Use this if you don't want to use the IOConsole (for instance for Lua debugging)"); |
---|
[3280] | 81 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[6417] | 82 | SetCommandLineArgument(limitToCPU, 1).information("Limits the program to one CPU/core (1, 2, 3, etc.). Default is the first core (faster than off)"); |
---|
[3280] | 83 | #endif |
---|
[2710] | 84 | |
---|
[3323] | 85 | Core::Core(const std::string& cmdLine) |
---|
[3370] | 86 | // Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands) |
---|
| 87 | : identifierDestroyer_(Identifier::destroyAllIdentifiers) |
---|
[5781] | 88 | // Cleanup guard for external console commands that don't belong to an Identifier |
---|
| 89 | , consoleCommandDestroyer_(CommandExecutor::destroyExternalCommands) |
---|
| 90 | , bGraphicsLoaded_(false) |
---|
[6746] | 91 | , bStartIOConsole_(true) |
---|
[3280] | 92 | { |
---|
[5693] | 93 | // Set the hard coded fixed paths |
---|
[5929] | 94 | this->pathConfig_.reset(new PathConfig()); |
---|
[3280] | 95 | |
---|
[5693] | 96 | // Create a new dynamic library manager |
---|
| 97 | this->dynLibManager_.reset(new DynLibManager()); |
---|
[2896] | 98 | |
---|
[5693] | 99 | // Load modules |
---|
[5929] | 100 | const std::vector<std::string>& modulePaths = this->pathConfig_->getModulePaths(); |
---|
| 101 | for (std::vector<std::string>::const_iterator it = modulePaths.begin(); it != modulePaths.end(); ++it) |
---|
[5693] | 102 | { |
---|
[5929] | 103 | try |
---|
[5693] | 104 | { |
---|
[5929] | 105 | this->dynLibManager_->load(*it); |
---|
[5693] | 106 | } |
---|
[5929] | 107 | catch (...) |
---|
| 108 | { |
---|
| 109 | COUT(1) << "Couldn't load module \"" << *it << "\": " << Exception::handleMessage() << std::endl; |
---|
| 110 | } |
---|
[5693] | 111 | } |
---|
| 112 | |
---|
| 113 | // Parse command line arguments AFTER the modules have been loaded (static code!) |
---|
[6021] | 114 | CommandLineParser::parseCommandLine(cmdLine); |
---|
[5693] | 115 | |
---|
| 116 | // Set configurable paths like log, config and media |
---|
[5929] | 117 | this->pathConfig_->setConfigurablePaths(); |
---|
[5693] | 118 | |
---|
[6105] | 119 | // create a signal handler (only active for Linux) |
---|
[2896] | 120 | // This call is placed as soon as possible, but after the directories are set |
---|
[3370] | 121 | this->signalHandler_.reset(new SignalHandler()); |
---|
[5929] | 122 | this->signalHandler_->doCatch(PathConfig::getExecutablePathString(), PathConfig::getLogPathString() + "orxonox_crash.log"); |
---|
[2896] | 123 | |
---|
[6105] | 124 | // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% (Windows) was used |
---|
| 125 | OutputHandler::getInstance().setLogPath(PathConfig::getLogPathString()); |
---|
[2710] | 126 | |
---|
[3280] | 127 | // Parse additional options file now that we know its path |
---|
[6021] | 128 | CommandLineParser::parseFile(); |
---|
[3280] | 129 | |
---|
| 130 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 131 | // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump |
---|
| 132 | // do this after ogre has initialised. Somehow Ogre changes the settings again (not through |
---|
| 133 | // the timer though). |
---|
[6021] | 134 | int limitToCPU = CommandLineParser::getValue("limitToCPU"); |
---|
[3280] | 135 | if (limitToCPU > 0) |
---|
| 136 | setThreadAffinity(static_cast<unsigned int>(limitToCPU)); |
---|
| 137 | #endif |
---|
| 138 | |
---|
[2896] | 139 | // Manage ini files and set the default settings file (usually orxonox.ini) |
---|
[3370] | 140 | this->configFileManager_.reset(new ConfigFileManager()); |
---|
[2896] | 141 | this->configFileManager_->setFilename(ConfigFileType::Settings, |
---|
[6021] | 142 | CommandLineParser::getValue("settingsFile").getString()); |
---|
[2896] | 143 | |
---|
[3280] | 144 | // Required as well for the config values |
---|
[3370] | 145 | this->languageInstance_.reset(new Language()); |
---|
[2896] | 146 | |
---|
[6417] | 147 | // Do this soon after the ConfigFileManager has been created to open up the |
---|
| 148 | // possibility to configure everything below here |
---|
| 149 | ClassIdentifier<Core>::getIdentifier("Core")->initialiseObject(this, "Core", true); |
---|
| 150 | this->setConfigValues(); |
---|
| 151 | |
---|
[6105] | 152 | // create persistent io console |
---|
[6746] | 153 | if (CommandLineParser::getValue("noIOConsole").getBool()) |
---|
| 154 | { |
---|
| 155 | ModifyConfigValue(bStartIOConsole_, tset, false); |
---|
| 156 | } |
---|
| 157 | if (this->bStartIOConsole_) |
---|
| 158 | this->ioConsole_.reset(new IOConsole()); |
---|
[6105] | 159 | |
---|
[5695] | 160 | // creates the class hierarchy for all classes with factories |
---|
[5929] | 161 | Identifier::createClassHierarchy(); |
---|
[5695] | 162 | |
---|
[5781] | 163 | // Load OGRE excluding the renderer and the render window |
---|
| 164 | this->graphicsManager_.reset(new GraphicsManager(false)); |
---|
| 165 | |
---|
| 166 | // initialise Tcl |
---|
[5929] | 167 | this->tclBind_.reset(new TclBind(PathConfig::getDataPathString())); |
---|
[5781] | 168 | this->tclThreadManager_.reset(new TclThreadManager(tclBind_->getTclInterpreter())); |
---|
| 169 | |
---|
[5929] | 170 | // Create singletons that always exist (in other libraries) |
---|
| 171 | this->rootScope_.reset(new Scope<ScopeID::Root>()); |
---|
[1505] | 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
[3370] | 175 | @brief |
---|
[5695] | 176 | All destruction code is handled by scoped_ptrs and ScopeGuards. |
---|
[1505] | 177 | */ |
---|
[1524] | 178 | Core::~Core() |
---|
[1505] | 179 | { |
---|
[6417] | 180 | // Remove us from the object lists again to avoid problems when destroying them |
---|
| 181 | this->unregisterObject(); |
---|
[3370] | 182 | } |
---|
[2896] | 183 | |
---|
[6417] | 184 | //! Function to collect the SetConfigValue-macro calls. |
---|
| 185 | void Core::setConfigValues() |
---|
| 186 | { |
---|
| 187 | #ifdef ORXONOX_RELEASE |
---|
| 188 | const unsigned int defaultLevelLogFile = 3; |
---|
| 189 | #else |
---|
| 190 | const unsigned int defaultLevelLogFile = 4; |
---|
| 191 | #endif |
---|
| 192 | setConfigValueGeneric(this, &this->softDebugLevelLogFile_, ConfigFileType::Settings, "OutputHandler", "softDebugLevelLogFile", defaultLevelLogFile) |
---|
| 193 | .description("The maximum level of debug output shown in the log file"); |
---|
| 194 | OutputHandler::getInstance().setSoftDebugLevel(OutputHandler::logFileOutputListenerName_s, this->softDebugLevelLogFile_); |
---|
| 195 | |
---|
| 196 | SetConfigValue(language_, Language::getInstance().defaultLanguage_) |
---|
| 197 | .description("The language of the in game text") |
---|
| 198 | .callback(this, &Core::languageChanged); |
---|
| 199 | SetConfigValue(bInitRandomNumberGenerator_, true) |
---|
| 200 | .description("If true, all random actions are different each time you start the game") |
---|
| 201 | .callback(this, &Core::initRandomNumberGenerator); |
---|
[6746] | 202 | SetConfigValue(bStartIOConsole_, true) |
---|
| 203 | .description("Set to false if you don't want to use the IOConsole (for Lua debugging for instance)"); |
---|
[6417] | 204 | } |
---|
| 205 | |
---|
| 206 | //! Callback function if the language has changed. |
---|
| 207 | void Core::languageChanged() |
---|
| 208 | { |
---|
| 209 | // Read the translation file after the language was configured |
---|
| 210 | Language::getInstance().readTranslatedLanguageFile(); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | void Core::initRandomNumberGenerator() |
---|
| 214 | { |
---|
| 215 | static bool bInitialized = false; |
---|
| 216 | if (!bInitialized && this->bInitRandomNumberGenerator_) |
---|
| 217 | { |
---|
| 218 | srand(static_cast<unsigned int>(time(0))); |
---|
| 219 | rand(); |
---|
| 220 | bInitialized = true; |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | |
---|
[5781] | 224 | void Core::loadGraphics() |
---|
| 225 | { |
---|
| 226 | // Any exception should trigger this, even in upgradeToGraphics (see its remarks) |
---|
| 227 | Loki::ScopeGuard unloader = Loki::MakeObjGuard(*this, &Core::unloadGraphics); |
---|
| 228 | |
---|
| 229 | // Upgrade OGRE to receive a render window |
---|
| 230 | graphicsManager_->upgradeToGraphics(); |
---|
| 231 | |
---|
| 232 | // Calls the InputManager which sets up the input devices. |
---|
| 233 | inputManager_.reset(new InputManager()); |
---|
| 234 | |
---|
[5929] | 235 | // Load the CEGUI interface |
---|
[6746] | 236 | guiManager_.reset(new GUIManager(inputManager_->getMousePosition())); |
---|
[5781] | 237 | |
---|
[5929] | 238 | bGraphicsLoaded_ = true; |
---|
| 239 | GameMode::bShowsGraphics_s = true; |
---|
| 240 | |
---|
| 241 | // Load some sort of a debug overlay (only denoted by its name, "debug.oxo") |
---|
| 242 | graphicsManager_->loadDebugOverlay(); |
---|
| 243 | |
---|
| 244 | // Create singletons associated with graphics (in other libraries) |
---|
| 245 | graphicsScope_.reset(new Scope<ScopeID::Graphics>()); |
---|
| 246 | |
---|
[5781] | 247 | unloader.Dismiss(); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | void Core::unloadGraphics() |
---|
| 251 | { |
---|
[5929] | 252 | this->graphicsScope_.reset(); |
---|
| 253 | this->guiManager_.reset(); |
---|
| 254 | this->inputManager_.reset(); |
---|
[5781] | 255 | this->graphicsManager_.reset(); |
---|
| 256 | |
---|
| 257 | // Load Ogre::Root again, but without the render system |
---|
| 258 | try |
---|
| 259 | { this->graphicsManager_.reset(new GraphicsManager(false)); } |
---|
| 260 | catch (...) |
---|
| 261 | { |
---|
| 262 | COUT(0) << "An exception occurred during 'unloadGraphics':" << Exception::handleMessage() << std::endl |
---|
| 263 | << "Another exception might be being handled which may lead to undefined behaviour!" << std::endl |
---|
| 264 | << "Terminating the program." << std::endl; |
---|
| 265 | abort(); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | bGraphicsLoaded_ = false; |
---|
[5929] | 269 | GameMode::bShowsGraphics_s = false; |
---|
[5781] | 270 | } |
---|
| 271 | |
---|
[6417] | 272 | //! Sets the language in the config-file back to the default. |
---|
| 273 | void Core::resetLanguage() |
---|
[1505] | 274 | { |
---|
[6417] | 275 | ResetConfigValue(language_); |
---|
[1505] | 276 | } |
---|
| 277 | |
---|
| 278 | /** |
---|
[2896] | 279 | @note |
---|
| 280 | The code of this function has been copied and adjusted from OGRE, an open source graphics engine. |
---|
| 281 | (Object-oriented Graphics Rendering Engine) |
---|
| 282 | For the latest info, see http://www.ogre3d.org/ |
---|
| 283 | |
---|
| 284 | Copyright (c) 2000-2008 Torus Knot Software Ltd |
---|
| 285 | |
---|
| 286 | OGRE is licensed under the LGPL. For more info, see OGRE license. |
---|
[2710] | 287 | */ |
---|
[2896] | 288 | void Core::setThreadAffinity(int limitToCPU) |
---|
[2710] | 289 | { |
---|
[3280] | 290 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 291 | |
---|
[2896] | 292 | if (limitToCPU <= 0) |
---|
| 293 | return; |
---|
[2710] | 294 | |
---|
[2896] | 295 | unsigned int coreNr = limitToCPU - 1; |
---|
| 296 | // Get the current process core mask |
---|
| 297 | DWORD procMask; |
---|
| 298 | DWORD sysMask; |
---|
| 299 | # if _MSC_VER >= 1400 && defined (_M_X64) |
---|
| 300 | GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); |
---|
| 301 | # else |
---|
| 302 | GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); |
---|
| 303 | # endif |
---|
[2710] | 304 | |
---|
[2896] | 305 | // If procMask is 0, consider there is only one core available |
---|
| 306 | // (using 0 as procMask will cause an infinite loop below) |
---|
| 307 | if (procMask == 0) |
---|
| 308 | procMask = 1; |
---|
| 309 | |
---|
| 310 | // if the core specified with coreNr is not available, take the lowest one |
---|
| 311 | if (!(procMask & (1 << coreNr))) |
---|
| 312 | coreNr = 0; |
---|
| 313 | |
---|
| 314 | // Find the lowest core that this process uses and coreNr suggests |
---|
| 315 | DWORD threadMask = 1; |
---|
| 316 | while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr))) |
---|
| 317 | threadMask <<= 1; |
---|
| 318 | |
---|
| 319 | // Set affinity to the first core |
---|
| 320 | SetThreadAffinityMask(GetCurrentThread(), threadMask); |
---|
| 321 | #endif |
---|
[2710] | 322 | } |
---|
| 323 | |
---|
[5695] | 324 | void Core::preUpdate(const Clock& time) |
---|
[2896] | 325 | { |
---|
[6417] | 326 | // Update singletons before general ticking |
---|
| 327 | ScopedSingletonManager::preUpdate<ScopeID::Root>(time); |
---|
[5781] | 328 | if (this->bGraphicsLoaded_) |
---|
| 329 | { |
---|
[6417] | 330 | // Process input events |
---|
| 331 | this->inputManager_->preUpdate(time); |
---|
| 332 | // Update GUI |
---|
| 333 | this->guiManager_->preUpdate(time); |
---|
| 334 | // Update singletons before general ticking |
---|
| 335 | ScopedSingletonManager::preUpdate<ScopeID::Graphics>(time); |
---|
[5781] | 336 | } |
---|
[6417] | 337 | // Process console events and status line |
---|
[6746] | 338 | if (this->ioConsole_ != NULL) |
---|
| 339 | this->ioConsole_->preUpdate(time); |
---|
[6417] | 340 | // Process thread commands |
---|
| 341 | this->tclThreadManager_->preUpdate(time); |
---|
[2896] | 342 | } |
---|
[3370] | 343 | |
---|
[5695] | 344 | void Core::postUpdate(const Clock& time) |
---|
[3370] | 345 | { |
---|
[6417] | 346 | // Update singletons just before rendering |
---|
| 347 | ScopedSingletonManager::postUpdate<ScopeID::Root>(time); |
---|
[5781] | 348 | if (this->bGraphicsLoaded_) |
---|
| 349 | { |
---|
[6417] | 350 | // Update singletons just before rendering |
---|
| 351 | ScopedSingletonManager::postUpdate<ScopeID::Graphics>(time); |
---|
[5781] | 352 | // Render (doesn't throw) |
---|
[6417] | 353 | this->graphicsManager_->postUpdate(time); |
---|
[5781] | 354 | } |
---|
[3370] | 355 | } |
---|
[1505] | 356 | } |
---|