Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core4/src/core/Core.cc @ 3248

Last change on this file since 3248 was 3248, checked in by rgrieder, 15 years ago

Build fix for new GCC versions

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