[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> |
---|
[2710] | 39 | #include <fstream> |
---|
| 40 | #include <cstdlib> |
---|
| 41 | #include <cstdio> |
---|
| 42 | #include <boost/filesystem.hpp> |
---|
[3370] | 43 | #include <OgreRenderWindow.h> |
---|
[2710] | 44 | |
---|
| 45 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[2896] | 46 | # ifndef WIN32_LEAN_AND_MEAN |
---|
| 47 | # define WIN32_LEAN_AND_MEAN |
---|
| 48 | # endif |
---|
[2710] | 49 | # include <windows.h> |
---|
[3214] | 50 | # undef min |
---|
| 51 | # undef max |
---|
[2710] | 52 | #elif defined(ORXONOX_PLATFORM_APPLE) |
---|
| 53 | # include <sys/param.h> |
---|
| 54 | # include <mach-o/dyld.h> |
---|
| 55 | #else /* Linux */ |
---|
| 56 | # include <sys/types.h> |
---|
| 57 | # include <unistd.h> |
---|
| 58 | #endif |
---|
| 59 | |
---|
| 60 | #include "SpecialConfig.h" |
---|
[2896] | 61 | #include "util/Debug.h" |
---|
[2710] | 62 | #include "util/Exception.h" |
---|
[2896] | 63 | #include "util/SignalHandler.h" |
---|
| 64 | #include "Clock.h" |
---|
| 65 | #include "CommandExecutor.h" |
---|
| 66 | #include "CommandLine.h" |
---|
| 67 | #include "ConfigFileManager.h" |
---|
| 68 | #include "ConfigValueIncludes.h" |
---|
| 69 | #include "CoreIncludes.h" |
---|
| 70 | #include "Factory.h" |
---|
[3370] | 71 | #include "GameMode.h" |
---|
| 72 | #include "GraphicsManager.h" |
---|
| 73 | #include "GUIManager.h" |
---|
[2896] | 74 | #include "Identifier.h" |
---|
[1505] | 75 | #include "Language.h" |
---|
[2710] | 76 | #include "LuaBind.h" |
---|
[2896] | 77 | #include "Shell.h" |
---|
| 78 | #include "TclBind.h" |
---|
| 79 | #include "TclThreadManager.h" |
---|
[3370] | 80 | #include "input/InputManager.h" |
---|
[1505] | 81 | |
---|
| 82 | namespace orxonox |
---|
| 83 | { |
---|
[3196] | 84 | //! Static pointer to the singleton |
---|
[3370] | 85 | Core* Core::singletonPtr_s = 0; |
---|
[2662] | 86 | |
---|
[5679] | 87 | SetCommandLineArgument(externalMediaPath, "").information("Path to the external media files"); |
---|
[3280] | 88 | SetCommandLineOnlyArgument(writingPathSuffix, "").information("Additional subfolder for config and log files"); |
---|
| 89 | SetCommandLineArgument(settingsFile, "orxonox.ini").information("THE configuration file"); |
---|
| 90 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 91 | SetCommandLineArgument(limitToCPU, 0).information("Limits the program to one cpu/core (1, 2, 3, etc.). 0 turns it off (default)"); |
---|
| 92 | #endif |
---|
[2710] | 93 | |
---|
[3280] | 94 | /** |
---|
| 95 | @brief |
---|
| 96 | Helper class for the Core singleton: we cannot derive |
---|
| 97 | Core from OrxonoxClass because we need to handle the Identifier |
---|
| 98 | destruction in the Core destructor. |
---|
| 99 | */ |
---|
| 100 | class CoreConfiguration : public OrxonoxClass |
---|
[1505] | 101 | { |
---|
[3280] | 102 | public: |
---|
| 103 | CoreConfiguration() |
---|
| 104 | { |
---|
| 105 | } |
---|
[2662] | 106 | |
---|
[3280] | 107 | void initialise() |
---|
| 108 | { |
---|
| 109 | RegisterRootObject(CoreConfiguration); |
---|
| 110 | this->setConfigValues(); |
---|
[2710] | 111 | |
---|
[5679] | 112 | // External media directory only exists for dev runs |
---|
| 113 | if (Core::isDevelopmentRun()) |
---|
| 114 | { |
---|
| 115 | // Possible media path override by the command line |
---|
| 116 | if (!CommandLine::getArgument("externalMediaPath")->hasDefaultValue()) |
---|
| 117 | tsetExternalMediaPath(CommandLine::getValue("externalMediaPath")); |
---|
| 118 | } |
---|
[3280] | 119 | } |
---|
| 120 | |
---|
| 121 | /** |
---|
| 122 | @brief Function to collect the SetConfigValue-macro calls. |
---|
| 123 | */ |
---|
| 124 | void setConfigValues() |
---|
[2896] | 125 | { |
---|
[3280] | 126 | #ifdef NDEBUG |
---|
| 127 | const unsigned int defaultLevelConsole = 1; |
---|
| 128 | const unsigned int defaultLevelLogfile = 3; |
---|
| 129 | const unsigned int defaultLevelShell = 1; |
---|
| 130 | #else |
---|
| 131 | const unsigned int defaultLevelConsole = 3; |
---|
| 132 | const unsigned int defaultLevelLogfile = 4; |
---|
| 133 | const unsigned int defaultLevelShell = 3; |
---|
| 134 | #endif |
---|
| 135 | SetConfigValue(softDebugLevelConsole_, defaultLevelConsole) |
---|
| 136 | .description("The maximal level of debug output shown in the console") |
---|
| 137 | .callback(this, &CoreConfiguration::debugLevelChanged); |
---|
| 138 | SetConfigValue(softDebugLevelLogfile_, defaultLevelLogfile) |
---|
| 139 | .description("The maximal level of debug output shown in the logfile") |
---|
| 140 | .callback(this, &CoreConfiguration::debugLevelChanged); |
---|
| 141 | SetConfigValue(softDebugLevelShell_, defaultLevelShell) |
---|
| 142 | .description("The maximal level of debug output shown in the ingame shell") |
---|
| 143 | .callback(this, &CoreConfiguration::debugLevelChanged); |
---|
| 144 | |
---|
[3370] | 145 | SetConfigValue(language_, Language::getInstance().defaultLanguage_) |
---|
[3280] | 146 | .description("The language of the ingame text") |
---|
| 147 | .callback(this, &CoreConfiguration::languageChanged); |
---|
| 148 | SetConfigValue(bInitializeRandomNumberGenerator_, true) |
---|
| 149 | .description("If true, all random actions are different each time you start the game") |
---|
| 150 | .callback(this, &CoreConfiguration::initializeRandomNumberGenerator); |
---|
[2896] | 151 | } |
---|
[3280] | 152 | |
---|
| 153 | /** |
---|
| 154 | @brief Callback function if the debug level has changed. |
---|
| 155 | */ |
---|
| 156 | void debugLevelChanged() |
---|
[2896] | 157 | { |
---|
[3280] | 158 | // softDebugLevel_ is the maximum of the 3 variables |
---|
| 159 | this->softDebugLevel_ = this->softDebugLevelConsole_; |
---|
| 160 | if (this->softDebugLevelLogfile_ > this->softDebugLevel_) |
---|
| 161 | this->softDebugLevel_ = this->softDebugLevelLogfile_; |
---|
| 162 | if (this->softDebugLevelShell_ > this->softDebugLevel_) |
---|
| 163 | this->softDebugLevel_ = this->softDebugLevelShell_; |
---|
| 164 | |
---|
| 165 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_All, this->softDebugLevel_); |
---|
| 166 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Console, this->softDebugLevelConsole_); |
---|
| 167 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Logfile, this->softDebugLevelLogfile_); |
---|
| 168 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Shell, this->softDebugLevelShell_); |
---|
[2896] | 169 | } |
---|
[2662] | 170 | |
---|
[3280] | 171 | /** |
---|
| 172 | @brief Callback function if the language has changed. |
---|
| 173 | */ |
---|
| 174 | void languageChanged() |
---|
| 175 | { |
---|
| 176 | // Read the translation file after the language was configured |
---|
[3370] | 177 | Language::getInstance().readTranslatedLanguageFile(); |
---|
[3280] | 178 | } |
---|
[2896] | 179 | |
---|
[3280] | 180 | /** |
---|
| 181 | @brief Sets the language in the config-file back to the default. |
---|
| 182 | */ |
---|
| 183 | void resetLanguage() |
---|
| 184 | { |
---|
| 185 | ResetConfigValue(language_); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | /** |
---|
| 189 | @brief |
---|
| 190 | Temporary sets the media path |
---|
| 191 | @param path |
---|
| 192 | The new media path |
---|
| 193 | */ |
---|
[5679] | 194 | void tsetExternalMediaPath(const std::string& path) |
---|
[3280] | 195 | { |
---|
[5679] | 196 | mediaPath_ = boost::filesystem::path(path); |
---|
[3280] | 197 | } |
---|
| 198 | |
---|
| 199 | void initializeRandomNumberGenerator() |
---|
| 200 | { |
---|
| 201 | static bool bInitialized = false; |
---|
| 202 | if (!bInitialized && this->bInitializeRandomNumberGenerator_) |
---|
| 203 | { |
---|
| 204 | srand(static_cast<unsigned int>(time(0))); |
---|
| 205 | rand(); |
---|
| 206 | bInitialized = true; |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | int softDebugLevel_; //!< The debug level |
---|
| 211 | int softDebugLevelConsole_; //!< The debug level for the console |
---|
| 212 | int softDebugLevelLogfile_; //!< The debug level for the logfile |
---|
| 213 | int softDebugLevelShell_; //!< The debug level for the ingame shell |
---|
| 214 | std::string language_; //!< The language |
---|
| 215 | bool bInitializeRandomNumberGenerator_; //!< If true, srand(time(0)) is called |
---|
| 216 | |
---|
| 217 | //! Path to the parent directory of the ones above if program was installed with relativ pahts |
---|
| 218 | boost::filesystem::path rootPath_; |
---|
| 219 | boost::filesystem::path executablePath_; //!< Path to the executable |
---|
| 220 | boost::filesystem::path mediaPath_; //!< Path to the media file folder |
---|
[5679] | 221 | boost::filesystem::path externalMediaPath_; //!< Path to the media file folder |
---|
[3280] | 222 | boost::filesystem::path configPath_; //!< Path to the config file folder |
---|
| 223 | boost::filesystem::path logPath_; //!< Path to the log file folder |
---|
| 224 | }; |
---|
| 225 | |
---|
| 226 | |
---|
[3323] | 227 | Core::Core(const std::string& cmdLine) |
---|
[3370] | 228 | // Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands) |
---|
| 229 | : identifierDestroyer_(Identifier::destroyAllIdentifiers) |
---|
| 230 | // Cleanup guard for external console commands that don't belong to an Identifier |
---|
| 231 | , consoleCommandDestroyer_(CommandExecutor::destroyExternalCommands) |
---|
| 232 | , configuration_(new CoreConfiguration()) // Don't yet create config values! |
---|
| 233 | , bDevRun_(false) |
---|
| 234 | , bGraphicsLoaded_(false) |
---|
[3280] | 235 | { |
---|
| 236 | // Parse command line arguments first |
---|
[3323] | 237 | CommandLine::parseCommandLine(cmdLine); |
---|
[3280] | 238 | |
---|
[2896] | 239 | // Determine and set the location of the executable |
---|
| 240 | setExecutablePath(); |
---|
| 241 | |
---|
| 242 | // Determine whether we have an installed or a binary dir run |
---|
| 243 | // The latter occurs when simply running from the build directory |
---|
| 244 | checkDevBuild(); |
---|
| 245 | |
---|
| 246 | // Make sure the directories we write in exist or else make them |
---|
| 247 | createDirectories(); |
---|
| 248 | |
---|
| 249 | // create a signal handler (only active for linux) |
---|
| 250 | // This call is placed as soon as possible, but after the directories are set |
---|
[3370] | 251 | this->signalHandler_.reset(new SignalHandler()); |
---|
[3280] | 252 | this->signalHandler_->doCatch(configuration_->executablePath_.string(), Core::getLogPathString() + "orxonox_crash.log"); |
---|
[2896] | 253 | |
---|
[2710] | 254 | // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used |
---|
| 255 | OutputHandler::getOutStream().setLogPath(Core::getLogPathString()); |
---|
| 256 | |
---|
[3280] | 257 | // Parse additional options file now that we know its path |
---|
| 258 | CommandLine::parseFile(); |
---|
| 259 | |
---|
| 260 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 261 | // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump |
---|
| 262 | // do this after ogre has initialised. Somehow Ogre changes the settings again (not through |
---|
| 263 | // the timer though). |
---|
| 264 | int limitToCPU = CommandLine::getValue("limitToCPU"); |
---|
| 265 | if (limitToCPU > 0) |
---|
| 266 | setThreadAffinity(static_cast<unsigned int>(limitToCPU)); |
---|
| 267 | #endif |
---|
| 268 | |
---|
[2896] | 269 | // Manage ini files and set the default settings file (usually orxonox.ini) |
---|
[3370] | 270 | this->configFileManager_.reset(new ConfigFileManager()); |
---|
[2896] | 271 | this->configFileManager_->setFilename(ConfigFileType::Settings, |
---|
| 272 | CommandLine::getValue("settingsFile").getString()); |
---|
| 273 | |
---|
[3280] | 274 | // Required as well for the config values |
---|
[3370] | 275 | this->languageInstance_.reset(new Language()); |
---|
[2896] | 276 | |
---|
[5678] | 277 | // creates the class hierarchy for all classes with factories |
---|
| 278 | Factory::createClassHierarchy(); |
---|
| 279 | |
---|
[2896] | 280 | // Do this soon after the ConfigFileManager has been created to open up the |
---|
| 281 | // possibility to configure everything below here |
---|
[3280] | 282 | this->configuration_->initialise(); |
---|
[2896] | 283 | |
---|
| 284 | // Create the lua interface |
---|
[3370] | 285 | this->luaBind_.reset(new LuaBind()); |
---|
[2896] | 286 | |
---|
[5678] | 287 | // Load OGRE excluding the renderer and the render window |
---|
[5680] | 288 | //this->graphicsManager_.reset(new GraphicsManager(false)); |
---|
[5678] | 289 | |
---|
[2896] | 290 | // initialise Tcl |
---|
[5679] | 291 | this->tclBind_.reset(new TclBind(Core::getExternalMediaPathString())); |
---|
[3370] | 292 | this->tclThreadManager_.reset(new TclThreadManager(tclBind_->getTclInterpreter())); |
---|
[2896] | 293 | |
---|
| 294 | // create a shell |
---|
[3370] | 295 | this->shell_.reset(new Shell()); |
---|
[1505] | 296 | } |
---|
| 297 | |
---|
| 298 | /** |
---|
[3370] | 299 | @brief |
---|
| 300 | All destruction code is handled by scoped_ptrs and SimpleScopeGuards. |
---|
[1505] | 301 | */ |
---|
[1524] | 302 | Core::~Core() |
---|
[1505] | 303 | { |
---|
[3370] | 304 | } |
---|
[2896] | 305 | |
---|
[3370] | 306 | void Core::loadGraphics() |
---|
| 307 | { |
---|
[5677] | 308 | // Any exception should trigger this, even in upgradeToGraphics (see its remarks) |
---|
| 309 | Loki::ScopeGuard unloader = Loki::MakeObjGuard(*this, &Core::unloadGraphics); |
---|
[2896] | 310 | |
---|
[5677] | 311 | // Upgrade OGRE to receive a render window |
---|
[5680] | 312 | //graphicsManager_->upgradeToGraphics(); |
---|
| 313 | graphicsManager_.reset(new GraphicsManager(true)); |
---|
[1505] | 314 | |
---|
[3370] | 315 | // The render window width and height are used to set up the mouse movement. |
---|
| 316 | size_t windowHnd = 0; |
---|
[5677] | 317 | graphicsManager_->getRenderWindow()->getCustomAttribute("WINDOW", &windowHnd); |
---|
[3370] | 318 | |
---|
| 319 | // Calls the InputManager which sets up the input devices. |
---|
[5677] | 320 | inputManager_.reset(new InputManager(windowHnd)); |
---|
[3370] | 321 | |
---|
| 322 | // load the CEGUI interface |
---|
[5677] | 323 | guiManager_.reset(new GUIManager(graphicsManager_->getRenderWindow())); |
---|
[3370] | 324 | |
---|
[5677] | 325 | unloader.Dismiss(); |
---|
[3370] | 326 | |
---|
| 327 | bGraphicsLoaded_ = true; |
---|
[1747] | 328 | } |
---|
[1505] | 329 | |
---|
[3370] | 330 | void Core::unloadGraphics() |
---|
| 331 | { |
---|
| 332 | this->guiManager_.reset();; |
---|
| 333 | this->inputManager_.reset();; |
---|
| 334 | this->graphicsManager_.reset(); |
---|
| 335 | |
---|
[5677] | 336 | // Load Ogre::Root again, but without the render system |
---|
[5680] | 337 | //try |
---|
| 338 | // { this->graphicsManager_.reset(new GraphicsManager(false)); } |
---|
| 339 | //catch (...) |
---|
| 340 | //{ |
---|
| 341 | // COUT(0) << "An exception occurred during 'new GraphicsManager' while " |
---|
| 342 | // << "another exception was being handled. This will lead to undefined behaviour!" << std::endl |
---|
| 343 | // << "Terminating the program." << std::endl; |
---|
| 344 | // abort(); |
---|
| 345 | //} |
---|
| 346 | this->graphicsManager_.reset(); |
---|
[5677] | 347 | |
---|
[3370] | 348 | bGraphicsLoaded_ = false; |
---|
| 349 | } |
---|
| 350 | |
---|
[1747] | 351 | /** |
---|
[3280] | 352 | @brief Returns the softDebugLevel for the given device (returns a default-value if the class is right about to be created). |
---|
[1505] | 353 | @param device The device |
---|
| 354 | @return The softDebugLevel |
---|
| 355 | */ |
---|
[3280] | 356 | /*static*/ int Core::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
[1505] | 357 | { |
---|
[2662] | 358 | switch (device) |
---|
[1505] | 359 | { |
---|
[2662] | 360 | case OutputHandler::LD_All: |
---|
[3280] | 361 | return Core::getInstance().configuration_->softDebugLevel_; |
---|
[2662] | 362 | case OutputHandler::LD_Console: |
---|
[3280] | 363 | return Core::getInstance().configuration_->softDebugLevelConsole_; |
---|
[2662] | 364 | case OutputHandler::LD_Logfile: |
---|
[3280] | 365 | return Core::getInstance().configuration_->softDebugLevelLogfile_; |
---|
[2662] | 366 | case OutputHandler::LD_Shell: |
---|
[3280] | 367 | return Core::getInstance().configuration_->softDebugLevelShell_; |
---|
[2662] | 368 | default: |
---|
| 369 | assert(0); |
---|
| 370 | return 2; |
---|
[1505] | 371 | } |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | /** |
---|
| 375 | @brief Sets the softDebugLevel for the given device. Please use this only temporary and restore the value afterwards, as it overrides the configured value. |
---|
| 376 | @param device The device |
---|
| 377 | @param level The level |
---|
| 378 | */ |
---|
[3280] | 379 | /*static*/ void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level) |
---|
| 380 | { |
---|
[2662] | 381 | if (device == OutputHandler::LD_All) |
---|
[3280] | 382 | Core::getInstance().configuration_->softDebugLevel_ = level; |
---|
[2662] | 383 | else if (device == OutputHandler::LD_Console) |
---|
[3280] | 384 | Core::getInstance().configuration_->softDebugLevelConsole_ = level; |
---|
[2662] | 385 | else if (device == OutputHandler::LD_Logfile) |
---|
[3280] | 386 | Core::getInstance().configuration_->softDebugLevelLogfile_ = level; |
---|
[2662] | 387 | else if (device == OutputHandler::LD_Shell) |
---|
[3280] | 388 | Core::getInstance().configuration_->softDebugLevelShell_ = level; |
---|
[1747] | 389 | |
---|
[2662] | 390 | OutputHandler::setSoftDebugLevel(device, level); |
---|
[3280] | 391 | } |
---|
[1505] | 392 | |
---|
| 393 | /** |
---|
| 394 | @brief Returns the configured language. |
---|
| 395 | */ |
---|
[3280] | 396 | /*static*/ const std::string& Core::getLanguage() |
---|
[1505] | 397 | { |
---|
[3280] | 398 | return Core::getInstance().configuration_->language_; |
---|
[1505] | 399 | } |
---|
| 400 | |
---|
| 401 | /** |
---|
| 402 | @brief Sets the language in the config-file back to the default. |
---|
| 403 | */ |
---|
[3280] | 404 | /*static*/ void Core::resetLanguage() |
---|
[1505] | 405 | { |
---|
[3280] | 406 | Core::getInstance().configuration_->resetLanguage(); |
---|
[1505] | 407 | } |
---|
| 408 | |
---|
[5679] | 409 | /*static*/ void Core::tsetExternalMediaPath(const std::string& path) |
---|
[1505] | 410 | { |
---|
[5679] | 411 | getInstance().configuration_->tsetExternalMediaPath(path); |
---|
[1505] | 412 | } |
---|
[2662] | 413 | |
---|
[2710] | 414 | /*static*/ const boost::filesystem::path& Core::getMediaPath() |
---|
| 415 | { |
---|
[3280] | 416 | return getInstance().configuration_->mediaPath_; |
---|
[2710] | 417 | } |
---|
| 418 | /*static*/ std::string Core::getMediaPathString() |
---|
| 419 | { |
---|
[3280] | 420 | return getInstance().configuration_->mediaPath_.string() + '/'; |
---|
[2710] | 421 | } |
---|
| 422 | |
---|
[5679] | 423 | /*static*/ const boost::filesystem::path& Core::getExternalMediaPath() |
---|
| 424 | { |
---|
| 425 | return getInstance().configuration_->externalMediaPath_; |
---|
| 426 | } |
---|
| 427 | /*static*/ std::string Core::getExternalMediaPathString() |
---|
| 428 | { |
---|
| 429 | return getInstance().configuration_->externalMediaPath_.string() + '/'; |
---|
| 430 | } |
---|
| 431 | |
---|
[2710] | 432 | /*static*/ const boost::filesystem::path& Core::getConfigPath() |
---|
| 433 | { |
---|
[3280] | 434 | return getInstance().configuration_->configPath_; |
---|
[2710] | 435 | } |
---|
| 436 | /*static*/ std::string Core::getConfigPathString() |
---|
| 437 | { |
---|
[3280] | 438 | return getInstance().configuration_->configPath_.string() + '/'; |
---|
[2710] | 439 | } |
---|
| 440 | |
---|
| 441 | /*static*/ const boost::filesystem::path& Core::getLogPath() |
---|
| 442 | { |
---|
[3280] | 443 | return getInstance().configuration_->logPath_; |
---|
[2710] | 444 | } |
---|
| 445 | /*static*/ std::string Core::getLogPathString() |
---|
| 446 | { |
---|
[3280] | 447 | return getInstance().configuration_->logPath_.string() + '/'; |
---|
[2710] | 448 | } |
---|
| 449 | |
---|
[3370] | 450 | /*static*/ const boost::filesystem::path& Core::getRootPath() |
---|
| 451 | { |
---|
| 452 | return getInstance().configuration_->rootPath_; |
---|
| 453 | } |
---|
| 454 | /*static*/ std::string Core::getRootPathString() |
---|
| 455 | { |
---|
| 456 | return getInstance().configuration_->rootPath_.string() + '/'; |
---|
| 457 | } |
---|
| 458 | |
---|
[2710] | 459 | /** |
---|
[2896] | 460 | @note |
---|
| 461 | The code of this function has been copied and adjusted from OGRE, an open source graphics engine. |
---|
| 462 | (Object-oriented Graphics Rendering Engine) |
---|
| 463 | For the latest info, see http://www.ogre3d.org/ |
---|
| 464 | |
---|
| 465 | Copyright (c) 2000-2008 Torus Knot Software Ltd |
---|
| 466 | |
---|
| 467 | OGRE is licensed under the LGPL. For more info, see OGRE license. |
---|
[2710] | 468 | */ |
---|
[2896] | 469 | void Core::setThreadAffinity(int limitToCPU) |
---|
[2710] | 470 | { |
---|
[3280] | 471 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 472 | |
---|
[2896] | 473 | if (limitToCPU <= 0) |
---|
| 474 | return; |
---|
[2710] | 475 | |
---|
[2896] | 476 | unsigned int coreNr = limitToCPU - 1; |
---|
| 477 | // Get the current process core mask |
---|
| 478 | DWORD procMask; |
---|
| 479 | DWORD sysMask; |
---|
| 480 | # if _MSC_VER >= 1400 && defined (_M_X64) |
---|
| 481 | GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); |
---|
| 482 | # else |
---|
| 483 | GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); |
---|
| 484 | # endif |
---|
[2710] | 485 | |
---|
[2896] | 486 | // If procMask is 0, consider there is only one core available |
---|
| 487 | // (using 0 as procMask will cause an infinite loop below) |
---|
| 488 | if (procMask == 0) |
---|
| 489 | procMask = 1; |
---|
| 490 | |
---|
| 491 | // if the core specified with coreNr is not available, take the lowest one |
---|
| 492 | if (!(procMask & (1 << coreNr))) |
---|
| 493 | coreNr = 0; |
---|
| 494 | |
---|
| 495 | // Find the lowest core that this process uses and coreNr suggests |
---|
| 496 | DWORD threadMask = 1; |
---|
| 497 | while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr))) |
---|
| 498 | threadMask <<= 1; |
---|
| 499 | |
---|
| 500 | // Set affinity to the first core |
---|
| 501 | SetThreadAffinityMask(GetCurrentThread(), threadMask); |
---|
| 502 | #endif |
---|
[2710] | 503 | } |
---|
| 504 | |
---|
| 505 | /** |
---|
| 506 | @brief |
---|
| 507 | Compares the executable path with the working directory |
---|
| 508 | */ |
---|
[2896] | 509 | void Core::setExecutablePath() |
---|
[2710] | 510 | { |
---|
| 511 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
| 512 | // get executable module |
---|
| 513 | TCHAR buffer[1024]; |
---|
| 514 | if (GetModuleFileName(NULL, buffer, 1024) == 0) |
---|
| 515 | ThrowException(General, "Could not retrieve executable path."); |
---|
| 516 | |
---|
| 517 | #elif defined(ORXONOX_PLATFORM_APPLE) |
---|
| 518 | char buffer[1024]; |
---|
| 519 | unsigned long path_len = 1023; |
---|
| 520 | if (_NSGetExecutablePath(buffer, &path_len)) |
---|
| 521 | ThrowException(General, "Could not retrieve executable path."); |
---|
| 522 | |
---|
| 523 | #else /* Linux */ |
---|
| 524 | /* written by Nicolai Haehnle <prefect_@gmx.net> */ |
---|
| 525 | |
---|
| 526 | /* Get our PID and build the name of the link in /proc */ |
---|
| 527 | char linkname[64]; /* /proc/<pid>/exe */ |
---|
| 528 | if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", getpid()) < 0) |
---|
| 529 | { |
---|
| 530 | /* This should only happen on large word systems. I'm not sure |
---|
| 531 | what the proper response is here. |
---|
| 532 | Since it really is an assert-like condition, aborting the |
---|
| 533 | program seems to be in order. */ |
---|
| 534 | assert(false); |
---|
| 535 | } |
---|
| 536 | |
---|
| 537 | /* Now read the symbolic link */ |
---|
| 538 | char buffer[1024]; |
---|
| 539 | int ret; |
---|
| 540 | ret = readlink(linkname, buffer, 1024); |
---|
| 541 | /* In case of an error, leave the handling up to the caller */ |
---|
| 542 | if (ret == -1) |
---|
| 543 | ThrowException(General, "Could not retrieve executable path."); |
---|
| 544 | |
---|
| 545 | /* Ensure proper NUL termination */ |
---|
| 546 | buffer[ret] = 0; |
---|
| 547 | #endif |
---|
| 548 | |
---|
[3280] | 549 | configuration_->executablePath_ = boost::filesystem::path(buffer); |
---|
[2710] | 550 | #ifndef ORXONOX_PLATFORM_APPLE |
---|
[3280] | 551 | configuration_->executablePath_ = configuration_->executablePath_.branch_path(); // remove executable name |
---|
[2710] | 552 | #endif |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | /** |
---|
| 556 | @brief |
---|
| 557 | Checks for "orxonox_dev_build.keep_me" in the executable diretory. |
---|
| 558 | If found it means that this is not an installed run, hence we |
---|
| 559 | don't write the logs and config files to ~/.orxonox |
---|
[3196] | 560 | @throws |
---|
| 561 | GeneralException |
---|
[2710] | 562 | */ |
---|
[2896] | 563 | void Core::checkDevBuild() |
---|
[2710] | 564 | { |
---|
[3280] | 565 | if (boost::filesystem::exists(configuration_->executablePath_ / "orxonox_dev_build.keep_me")) |
---|
[2710] | 566 | { |
---|
| 567 | COUT(1) << "Running from the build tree." << std::endl; |
---|
[3370] | 568 | Core::bDevRun_ = true; |
---|
[5679] | 569 | configuration_->mediaPath_ = specialConfig::mediaDevDirectory; |
---|
| 570 | configuration_->externalMediaPath_ = specialConfig::externalMediaDevDirectory; |
---|
| 571 | configuration_->configPath_ = specialConfig::configDevDirectory; |
---|
| 572 | configuration_->logPath_ = specialConfig::logDevDirectory; |
---|
[2710] | 573 | } |
---|
| 574 | else |
---|
| 575 | { |
---|
| 576 | #ifdef INSTALL_COPYABLE // --> relative paths |
---|
| 577 | // Also set the root path |
---|
[5679] | 578 | boost::filesystem::path relativeExecutablePath(specialConfig::defaultRuntimePath); |
---|
[3280] | 579 | configuration_->rootPath_ = configuration_->executablePath_; |
---|
| 580 | while (!boost::filesystem::equivalent(configuration_->rootPath_ / relativeExecutablePath, configuration_->executablePath_) |
---|
| 581 | && !configuration_->rootPath_.empty()) |
---|
| 582 | configuration_->rootPath_ = configuration_->rootPath_.branch_path(); |
---|
| 583 | if (configuration_->rootPath_.empty()) |
---|
[2710] | 584 | ThrowException(General, "Could not derive a root directory. Might the binary installation directory contain '..' when taken relative to the installation prefix path?"); |
---|
| 585 | |
---|
| 586 | // Using paths relative to the install prefix, complete them |
---|
[5679] | 587 | configuration_->mediaPath_ = configuration_->rootPath_ / specialConfig::defaultMediaPath; |
---|
| 588 | configuration_->configPath_ = configuration_->rootPath_ / specialConfig::defaultConfigPath; |
---|
| 589 | configuration_->logPath_ = configuration_->rootPath_ / specialConfig::defaultLogPath; |
---|
[2710] | 590 | #else |
---|
| 591 | // There is no root path, so don't set it at all |
---|
| 592 | |
---|
[5679] | 593 | configuration_->mediaPath_ = specialConfig::mediaInstallDirectory; |
---|
[2710] | 594 | |
---|
| 595 | // Get user directory |
---|
| 596 | # ifdef ORXONOX_PLATFORM_UNIX /* Apple? */ |
---|
| 597 | char* userDataPathPtr(getenv("HOME")); |
---|
| 598 | # else |
---|
| 599 | char* userDataPathPtr(getenv("APPDATA")); |
---|
| 600 | # endif |
---|
| 601 | if (userDataPathPtr == NULL) |
---|
| 602 | ThrowException(General, "Could not retrieve user data path."); |
---|
| 603 | boost::filesystem::path userDataPath(userDataPathPtr); |
---|
| 604 | userDataPath /= ".orxonox"; |
---|
| 605 | |
---|
[5679] | 606 | configuration_->configPath_ = userDataPath / specialConfig::defaultConfigPath; |
---|
| 607 | configuration_->logPath_ = userDataPath / specialConfig::defaultLogPath; |
---|
[2710] | 608 | #endif |
---|
| 609 | } |
---|
| 610 | |
---|
| 611 | // Option to put all the config and log files in a separate folder |
---|
[2896] | 612 | if (!CommandLine::getArgument("writingPathSuffix")->hasDefaultValue()) |
---|
[2710] | 613 | { |
---|
[2896] | 614 | std::string directory(CommandLine::getValue("writingPathSuffix").getString()); |
---|
[3280] | 615 | configuration_->configPath_ = configuration_->configPath_ / directory; |
---|
| 616 | configuration_->logPath_ = configuration_->logPath_ / directory; |
---|
[2710] | 617 | } |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | /* |
---|
| 621 | @brief |
---|
| 622 | Checks for the log and the config directory and creates them |
---|
| 623 | if necessary. Otherwise me might have problems opening those files. |
---|
[3196] | 624 | @throws |
---|
| 625 | orxonox::GeneralException if the directory to be created is a file. |
---|
[2710] | 626 | */ |
---|
[2896] | 627 | void Core::createDirectories() |
---|
[2710] | 628 | { |
---|
| 629 | std::vector<std::pair<boost::filesystem::path, std::string> > directories; |
---|
[3280] | 630 | directories.push_back(std::make_pair(boost::filesystem::path(configuration_->configPath_), "config")); |
---|
| 631 | directories.push_back(std::make_pair(boost::filesystem::path(configuration_->logPath_), "log")); |
---|
[2710] | 632 | |
---|
| 633 | for (std::vector<std::pair<boost::filesystem::path, std::string> >::iterator it = directories.begin(); |
---|
| 634 | it != directories.end(); ++it) |
---|
| 635 | { |
---|
| 636 | if (boost::filesystem::exists(it->first) && !boost::filesystem::is_directory(it->first)) |
---|
| 637 | { |
---|
| 638 | ThrowException(General, std::string("The ") + it->second + " directory has been preoccupied by a file! \ |
---|
[2759] | 639 | Please remove " + it->first.string()); |
---|
[2710] | 640 | } |
---|
| 641 | if (boost::filesystem::create_directories(it->first)) // function may not return true at all (bug?) |
---|
| 642 | { |
---|
| 643 | COUT(4) << "Created " << it->second << " directory" << std::endl; |
---|
| 644 | } |
---|
| 645 | } |
---|
| 646 | } |
---|
[2896] | 647 | |
---|
[3370] | 648 | bool Core::preUpdate(const Clock& time) throw() |
---|
[2896] | 649 | { |
---|
[3370] | 650 | std::string exceptionMessage; |
---|
| 651 | try |
---|
| 652 | { |
---|
| 653 | if (this->bGraphicsLoaded_) |
---|
| 654 | { |
---|
| 655 | // process input events |
---|
| 656 | this->inputManager_->update(time); |
---|
| 657 | // process gui events |
---|
| 658 | this->guiManager_->update(time); |
---|
| 659 | } |
---|
| 660 | // process thread commands |
---|
| 661 | this->tclThreadManager_->update(time); |
---|
| 662 | } |
---|
| 663 | catch (const std::exception& ex) |
---|
| 664 | { exceptionMessage = ex.what(); } |
---|
| 665 | catch (...) |
---|
| 666 | { exceptionMessage = "Unknown exception"; } |
---|
| 667 | if (!exceptionMessage.empty()) |
---|
| 668 | { |
---|
| 669 | COUT(0) << "An exception occurred in the Core preUpdate: " << exceptionMessage << std::endl; |
---|
| 670 | COUT(0) << "This should really never happen! Closing the program." << std::endl; |
---|
| 671 | return false; |
---|
| 672 | } |
---|
| 673 | return true; |
---|
[2896] | 674 | } |
---|
[3370] | 675 | |
---|
| 676 | bool Core::postUpdate(const Clock& time) throw() |
---|
| 677 | { |
---|
| 678 | std::string exceptionMessage; |
---|
| 679 | try |
---|
| 680 | { |
---|
| 681 | if (this->bGraphicsLoaded_) |
---|
| 682 | { |
---|
| 683 | // Render (doesn't throw) |
---|
| 684 | this->graphicsManager_->update(time); |
---|
| 685 | } |
---|
| 686 | } |
---|
| 687 | catch (const std::exception& ex) |
---|
| 688 | { exceptionMessage = ex.what(); } |
---|
| 689 | catch (...) |
---|
| 690 | { exceptionMessage = "Unknown exception"; } |
---|
| 691 | if (!exceptionMessage.empty()) |
---|
| 692 | { |
---|
| 693 | COUT(0) << "An exception occurred in the Core postUpdate: " << exceptionMessage << std::endl; |
---|
| 694 | COUT(0) << "This should really never happen! Closing the program." << std::endl; |
---|
| 695 | return false; |
---|
| 696 | } |
---|
| 697 | return true; |
---|
| 698 | } |
---|
[1505] | 699 | } |
---|