Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/Core.cc @ 3323

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

ORXONOX_USE_WINMAIN should now work as expected.

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