Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource/src/core/Core.cc @ 3348

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

Fixing a problem for the installed version on windows: Media path is not anymore a config value if you don't start orxonox from the build tree. But you can still temporarily change it or supply a new path as command line argument in any case.

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