Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp6/src/core/Core.cc @ 3231

Last change on this file since 3231 was 3214, checked in by scheusso, 15 years ago

merged netp5 back to trunk

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