Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/core/Core.cc @ 3166

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

Merged some code work from the Gruppenarbeit.

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