Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/toonshader/src/core/Core.cc @ 6514

Last change on this file since 6514 was 2896, checked in by landauf, 16 years ago

Merged gui branch back to trunk.

I did 2 small changes in IngameManager.cc on line 777 and 888 (yes, really), because const_reverse_iterator strangely doesn't work on MinGW.

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