Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/core/Core.cc @ 5791

Last change on this file since 5791 was 5778, checked in by landauf, 15 years ago

Removed the Factory class.
Moved the networkID↔Identifier map from Factory to Identifier.
Replaced the name↔Identifier map from Factory with the already existing Identifier map in Identifier. This map additionally contains Identifiers without Factory. You can separate them with the new function identifier→hasFactory().

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