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