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