[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> |
---|
[6040] | 39 | #include <ctime> |
---|
| 40 | #include <vector> |
---|
[2710] | 41 | |
---|
| 42 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[2896] | 43 | # ifndef WIN32_LEAN_AND_MEAN |
---|
| 44 | # define WIN32_LEAN_AND_MEAN |
---|
| 45 | # endif |
---|
[2710] | 46 | # include <windows.h> |
---|
[3214] | 47 | # undef min |
---|
| 48 | # undef max |
---|
[2710] | 49 | #endif |
---|
| 50 | |
---|
[6040] | 51 | #include "util/Clock.h" |
---|
[2896] | 52 | #include "util/Debug.h" |
---|
[2710] | 53 | #include "util/Exception.h" |
---|
[2896] | 54 | #include "util/SignalHandler.h" |
---|
[6040] | 55 | #include "PathConfig.h" |
---|
| 56 | #include "CommandLineParser.h" |
---|
[5695] | 57 | #include "LuaState.h" |
---|
[1505] | 58 | |
---|
| 59 | namespace orxonox |
---|
| 60 | { |
---|
[3196] | 61 | //! Static pointer to the singleton |
---|
[3370] | 62 | Core* Core::singletonPtr_s = 0; |
---|
[2662] | 63 | |
---|
[3280] | 64 | SetCommandLineArgument(settingsFile, "orxonox.ini").information("THE configuration file"); |
---|
| 65 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 66 | SetCommandLineArgument(limitToCPU, 0).information("Limits the program to one cpu/core (1, 2, 3, etc.). 0 turns it off (default)"); |
---|
| 67 | #endif |
---|
[2710] | 68 | |
---|
[3280] | 69 | /** |
---|
| 70 | @brief |
---|
| 71 | Helper class for the Core singleton: we cannot derive |
---|
| 72 | Core from OrxonoxClass because we need to handle the Identifier |
---|
| 73 | destruction in the Core destructor. |
---|
| 74 | */ |
---|
[5789] | 75 | class CoreConfiguration |
---|
[1505] | 76 | { |
---|
[3280] | 77 | public: |
---|
| 78 | CoreConfiguration() |
---|
| 79 | { |
---|
| 80 | } |
---|
[2662] | 81 | |
---|
[3280] | 82 | void initialise() |
---|
| 83 | { |
---|
| 84 | #ifdef NDEBUG |
---|
| 85 | const unsigned int defaultLevelConsole = 1; |
---|
| 86 | const unsigned int defaultLevelLogfile = 3; |
---|
| 87 | const unsigned int defaultLevelShell = 1; |
---|
| 88 | #else |
---|
| 89 | const unsigned int defaultLevelConsole = 3; |
---|
| 90 | const unsigned int defaultLevelLogfile = 4; |
---|
| 91 | const unsigned int defaultLevelShell = 3; |
---|
| 92 | #endif |
---|
[5789] | 93 | softDebugLevelConsole_ = defaultLevelConsole; |
---|
| 94 | softDebugLevelLogfile_ = defaultLevelLogfile; |
---|
| 95 | softDebugLevelShell_ = defaultLevelShell; |
---|
| 96 | this->debugLevelChanged(); |
---|
[3280] | 97 | |
---|
[5789] | 98 | bInitializeRandomNumberGenerator_ = true; |
---|
| 99 | this->initializeRandomNumberGenerator(); |
---|
[2896] | 100 | } |
---|
[3280] | 101 | |
---|
| 102 | /** |
---|
| 103 | @brief Callback function if the debug level has changed. |
---|
| 104 | */ |
---|
| 105 | void debugLevelChanged() |
---|
[2896] | 106 | { |
---|
[3280] | 107 | // softDebugLevel_ is the maximum of the 3 variables |
---|
| 108 | this->softDebugLevel_ = this->softDebugLevelConsole_; |
---|
| 109 | if (this->softDebugLevelLogfile_ > this->softDebugLevel_) |
---|
| 110 | this->softDebugLevel_ = this->softDebugLevelLogfile_; |
---|
| 111 | if (this->softDebugLevelShell_ > this->softDebugLevel_) |
---|
| 112 | this->softDebugLevel_ = this->softDebugLevelShell_; |
---|
| 113 | |
---|
| 114 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_All, this->softDebugLevel_); |
---|
| 115 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Console, this->softDebugLevelConsole_); |
---|
| 116 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Logfile, this->softDebugLevelLogfile_); |
---|
| 117 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Shell, this->softDebugLevelShell_); |
---|
[2896] | 118 | } |
---|
[2662] | 119 | |
---|
[3280] | 120 | void initializeRandomNumberGenerator() |
---|
| 121 | { |
---|
| 122 | static bool bInitialized = false; |
---|
| 123 | if (!bInitialized && this->bInitializeRandomNumberGenerator_) |
---|
| 124 | { |
---|
| 125 | srand(static_cast<unsigned int>(time(0))); |
---|
| 126 | rand(); |
---|
| 127 | bInitialized = true; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | int softDebugLevel_; //!< The debug level |
---|
| 132 | int softDebugLevelConsole_; //!< The debug level for the console |
---|
| 133 | int softDebugLevelLogfile_; //!< The debug level for the logfile |
---|
| 134 | int softDebugLevelShell_; //!< The debug level for the ingame shell |
---|
| 135 | bool bInitializeRandomNumberGenerator_; //!< If true, srand(time(0)) is called |
---|
| 136 | }; |
---|
| 137 | |
---|
| 138 | |
---|
[3323] | 139 | Core::Core(const std::string& cmdLine) |
---|
[3370] | 140 | // Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands) |
---|
[5789] | 141 | : configuration_(new CoreConfiguration()) // Don't yet create config values! |
---|
[3280] | 142 | { |
---|
[5693] | 143 | // Set the hard coded fixed paths |
---|
[6040] | 144 | this->pathConfig_.reset(new PathConfig()); |
---|
[3280] | 145 | |
---|
[5693] | 146 | // Parse command line arguments AFTER the modules have been loaded (static code!) |
---|
[6040] | 147 | CommandLineParser::parseCommandLine(cmdLine); |
---|
[5693] | 148 | |
---|
| 149 | // Set configurable paths like log, config and media |
---|
[6040] | 150 | this->pathConfig_->setConfigurablePaths(); |
---|
[5693] | 151 | |
---|
[2896] | 152 | // create a signal handler (only active for linux) |
---|
| 153 | // This call is placed as soon as possible, but after the directories are set |
---|
[3370] | 154 | this->signalHandler_.reset(new SignalHandler()); |
---|
[6040] | 155 | this->signalHandler_->doCatch(PathConfig::getExecutablePathString(), PathConfig::getLogPathString() + "orxonox_crash.log"); |
---|
[2896] | 156 | |
---|
[2710] | 157 | // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used |
---|
[6040] | 158 | OutputHandler::getOutStream().setLogPath(PathConfig::getLogPathString()); |
---|
[2710] | 159 | |
---|
[3280] | 160 | // Parse additional options file now that we know its path |
---|
[6040] | 161 | CommandLineParser::parseFile(); |
---|
[3280] | 162 | |
---|
| 163 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 164 | // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump |
---|
| 165 | // do this after ogre has initialised. Somehow Ogre changes the settings again (not through |
---|
| 166 | // the timer though). |
---|
[6040] | 167 | int limitToCPU = CommandLineParser::getValue("limitToCPU"); |
---|
[3280] | 168 | if (limitToCPU > 0) |
---|
| 169 | setThreadAffinity(static_cast<unsigned int>(limitToCPU)); |
---|
| 170 | #endif |
---|
| 171 | |
---|
[2896] | 172 | // Do this soon after the ConfigFileManager has been created to open up the |
---|
| 173 | // possibility to configure everything below here |
---|
[3280] | 174 | this->configuration_->initialise(); |
---|
[1505] | 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
[3370] | 178 | @brief |
---|
[5695] | 179 | All destruction code is handled by scoped_ptrs and ScopeGuards. |
---|
[1505] | 180 | */ |
---|
[1524] | 181 | Core::~Core() |
---|
[1505] | 182 | { |
---|
[3370] | 183 | } |
---|
[2896] | 184 | |
---|
[1747] | 185 | /** |
---|
[3280] | 186 | @brief Returns the softDebugLevel for the given device (returns a default-value if the class is right about to be created). |
---|
[1505] | 187 | @param device The device |
---|
| 188 | @return The softDebugLevel |
---|
| 189 | */ |
---|
[3280] | 190 | /*static*/ int Core::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
[1505] | 191 | { |
---|
[2662] | 192 | switch (device) |
---|
[1505] | 193 | { |
---|
[2662] | 194 | case OutputHandler::LD_All: |
---|
[3280] | 195 | return Core::getInstance().configuration_->softDebugLevel_; |
---|
[2662] | 196 | case OutputHandler::LD_Console: |
---|
[3280] | 197 | return Core::getInstance().configuration_->softDebugLevelConsole_; |
---|
[2662] | 198 | case OutputHandler::LD_Logfile: |
---|
[3280] | 199 | return Core::getInstance().configuration_->softDebugLevelLogfile_; |
---|
[2662] | 200 | case OutputHandler::LD_Shell: |
---|
[3280] | 201 | return Core::getInstance().configuration_->softDebugLevelShell_; |
---|
[2662] | 202 | default: |
---|
| 203 | assert(0); |
---|
| 204 | return 2; |
---|
[1505] | 205 | } |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | /** |
---|
| 209 | @brief Sets the softDebugLevel for the given device. Please use this only temporary and restore the value afterwards, as it overrides the configured value. |
---|
| 210 | @param device The device |
---|
| 211 | @param level The level |
---|
| 212 | */ |
---|
[3280] | 213 | /*static*/ void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level) |
---|
| 214 | { |
---|
[2662] | 215 | if (device == OutputHandler::LD_All) |
---|
[3280] | 216 | Core::getInstance().configuration_->softDebugLevel_ = level; |
---|
[2662] | 217 | else if (device == OutputHandler::LD_Console) |
---|
[3280] | 218 | Core::getInstance().configuration_->softDebugLevelConsole_ = level; |
---|
[2662] | 219 | else if (device == OutputHandler::LD_Logfile) |
---|
[3280] | 220 | Core::getInstance().configuration_->softDebugLevelLogfile_ = level; |
---|
[2662] | 221 | else if (device == OutputHandler::LD_Shell) |
---|
[3280] | 222 | Core::getInstance().configuration_->softDebugLevelShell_ = level; |
---|
[1747] | 223 | |
---|
[2662] | 224 | OutputHandler::setSoftDebugLevel(device, level); |
---|
[3280] | 225 | } |
---|
[1505] | 226 | |
---|
[2710] | 227 | /** |
---|
[2896] | 228 | @note |
---|
| 229 | The code of this function has been copied and adjusted from OGRE, an open source graphics engine. |
---|
| 230 | (Object-oriented Graphics Rendering Engine) |
---|
| 231 | For the latest info, see http://www.ogre3d.org/ |
---|
| 232 | |
---|
| 233 | Copyright (c) 2000-2008 Torus Knot Software Ltd |
---|
| 234 | |
---|
| 235 | OGRE is licensed under the LGPL. For more info, see OGRE license. |
---|
[2710] | 236 | */ |
---|
[2896] | 237 | void Core::setThreadAffinity(int limitToCPU) |
---|
[2710] | 238 | { |
---|
[3280] | 239 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 240 | |
---|
[2896] | 241 | if (limitToCPU <= 0) |
---|
| 242 | return; |
---|
[2710] | 243 | |
---|
[2896] | 244 | unsigned int coreNr = limitToCPU - 1; |
---|
| 245 | // Get the current process core mask |
---|
| 246 | DWORD procMask; |
---|
| 247 | DWORD sysMask; |
---|
| 248 | # if _MSC_VER >= 1400 && defined (_M_X64) |
---|
| 249 | GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); |
---|
| 250 | # else |
---|
| 251 | GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); |
---|
| 252 | # endif |
---|
[2710] | 253 | |
---|
[2896] | 254 | // If procMask is 0, consider there is only one core available |
---|
| 255 | // (using 0 as procMask will cause an infinite loop below) |
---|
| 256 | if (procMask == 0) |
---|
| 257 | procMask = 1; |
---|
| 258 | |
---|
| 259 | // if the core specified with coreNr is not available, take the lowest one |
---|
| 260 | if (!(procMask & (1 << coreNr))) |
---|
| 261 | coreNr = 0; |
---|
| 262 | |
---|
| 263 | // Find the lowest core that this process uses and coreNr suggests |
---|
| 264 | DWORD threadMask = 1; |
---|
| 265 | while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr))) |
---|
| 266 | threadMask <<= 1; |
---|
| 267 | |
---|
| 268 | // Set affinity to the first core |
---|
| 269 | SetThreadAffinityMask(GetCurrentThread(), threadMask); |
---|
| 270 | #endif |
---|
[2710] | 271 | } |
---|
[1505] | 272 | } |
---|