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