Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: sandbox_light/src/libraries/core/Core.cc @ 6684

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

Also synchronised sandbox_light with current trunk.

  • Property svn:eol-style set to native
File size: 9.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 <ctime>
40#include <vector>
41
42#ifdef ORXONOX_PLATFORM_WINDOWS
43#  ifndef WIN32_LEAN_AND_MEAN
44#    define WIN32_LEAN_AND_MEAN
45#  endif
46#  include <windows.h>
47#  undef min
48#  undef max
49#endif
50
51#include "util/Clock.h"
52#include "util/Debug.h"
53#include "util/Exception.h"
54#include "util/SignalHandler.h"
55#include "PathConfig.h"
56#include "CommandLineParser.h"
57#include "LuaState.h"
58
59namespace orxonox
60{
61    //! Static pointer to the singleton
62    Core* Core::singletonPtr_s  = 0;
63
64    SetCommandLineArgument(settingsFile, "orxonox.ini").information("THE configuration file");
65#ifdef ORXONOX_PLATFORM_WINDOWS
66    SetCommandLineArgument(limitToCPU, 0).information("Limits the program to one cpu/core (1, 2, 3, etc.). 0 turns it off (default)");
67#endif
68
69    /**
70    @brief
71        Helper class for the Core singleton: we cannot derive
72        Core from OrxonoxClass because we need to handle the Identifier
73        destruction in the Core destructor.
74    */
75    class CoreConfiguration
76    {
77    public:
78        CoreConfiguration()
79        {
80        }
81
82        void initialise()
83        {
84#ifdef NDEBUG
85            const unsigned int defaultLevelConsole = 1;
86            const unsigned int defaultLevelLogfile = 3;
87            const unsigned int defaultLevelShell   = 1;
88#else
89            const unsigned int defaultLevelConsole = 3;
90            const unsigned int defaultLevelLogfile = 4;
91            const unsigned int defaultLevelShell   = 3;
92#endif
93            softDebugLevelConsole_ = defaultLevelConsole;
94            softDebugLevelLogfile_ = defaultLevelLogfile;
95            softDebugLevelShell_   = defaultLevelShell;
96            this->debugLevelChanged();
97
98            bInitializeRandomNumberGenerator_ = true;
99            this->initializeRandomNumberGenerator();
100        }
101
102        /**
103            @brief Callback function if the debug level has changed.
104        */
105        void debugLevelChanged()
106        {
107            // softDebugLevel_ is the maximum of the 3 variables
108            this->softDebugLevel_ = this->softDebugLevelConsole_;
109            if (this->softDebugLevelLogfile_ > this->softDebugLevel_)
110                this->softDebugLevel_ = this->softDebugLevelLogfile_;
111            if (this->softDebugLevelShell_ > this->softDebugLevel_)
112                this->softDebugLevel_ = this->softDebugLevelShell_;
113
114            OutputHandler::setSoftDebugLevel(OutputHandler::LD_All,     this->softDebugLevel_);
115            OutputHandler::setSoftDebugLevel(OutputHandler::LD_Console, this->softDebugLevelConsole_);
116            OutputHandler::setSoftDebugLevel(OutputHandler::LD_Logfile, this->softDebugLevelLogfile_);
117            OutputHandler::setSoftDebugLevel(OutputHandler::LD_Shell,   this->softDebugLevelShell_);
118        }
119
120        void initializeRandomNumberGenerator()
121        {
122            static bool bInitialized = false;
123            if (!bInitialized && this->bInitializeRandomNumberGenerator_)
124            {
125                srand(static_cast<unsigned int>(time(0)));
126                rand();
127                bInitialized = true;
128            }
129        }
130
131        int softDebugLevel_;                            //!< The debug level
132        int softDebugLevelConsole_;                     //!< The debug level for the console
133        int softDebugLevelLogfile_;                     //!< The debug level for the logfile
134        int softDebugLevelShell_;                       //!< The debug level for the ingame shell
135        bool bInitializeRandomNumberGenerator_;         //!< If true, srand(time(0)) is called
136    };
137
138
139    Core::Core(const std::string& cmdLine)
140        // Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands)
141        : configuration_(new CoreConfiguration()) // Don't yet create config values!
142    {
143        // Set the hard coded fixed paths
144        this->pathConfig_.reset(new PathConfig());
145
146        // Parse command line arguments AFTER the modules have been loaded (static code!)
147        CommandLineParser::parseCommandLine(cmdLine);
148
149        // Set configurable paths like log, config and media
150        this->pathConfig_->setConfigurablePaths();
151
152        // create a signal handler (only active for linux)
153        // This call is placed as soon as possible, but after the directories are set
154        this->signalHandler_.reset(new SignalHandler());
155        this->signalHandler_->doCatch(PathConfig::getExecutablePathString(), PathConfig::getLogPathString() + "orxonox_crash.log");
156
157        // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used
158        OutputHandler::getOutStream().setLogPath(PathConfig::getLogPathString());
159
160        // Parse additional options file now that we know its path
161        CommandLineParser::parseFile();
162
163#ifdef ORXONOX_PLATFORM_WINDOWS
164        // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump
165        // do this after ogre has initialised. Somehow Ogre changes the settings again (not through
166        // the timer though).
167        int limitToCPU = CommandLineParser::getValue("limitToCPU");
168        if (limitToCPU > 0)
169            setThreadAffinity(static_cast<unsigned int>(limitToCPU));
170#endif
171
172        // Do this soon after the ConfigFileManager has been created to open up the
173        // possibility to configure everything below here
174        this->configuration_->initialise();
175    }
176
177    /**
178    @brief
179        All destruction code is handled by scoped_ptrs and ScopeGuards.
180    */
181    Core::~Core()
182    {
183    }
184
185    /**
186        @brief Returns the softDebugLevel for the given device (returns a default-value if the class is right about to be created).
187        @param device The device
188        @return The softDebugLevel
189    */
190    /*static*/ int Core::getSoftDebugLevel(OutputHandler::OutputDevice device)
191    {
192        switch (device)
193        {
194        case OutputHandler::LD_All:
195            return Core::getInstance().configuration_->softDebugLevel_;
196        case OutputHandler::LD_Console:
197            return Core::getInstance().configuration_->softDebugLevelConsole_;
198        case OutputHandler::LD_Logfile:
199            return Core::getInstance().configuration_->softDebugLevelLogfile_;
200        case OutputHandler::LD_Shell:
201            return Core::getInstance().configuration_->softDebugLevelShell_;
202        default:
203            assert(0);
204            return 2;
205        }
206    }
207
208     /**
209        @brief Sets the softDebugLevel for the given device. Please use this only temporary and restore the value afterwards, as it overrides the configured value.
210        @param device The device
211        @param level The level
212    */
213    /*static*/ void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level)
214    {
215        if (device == OutputHandler::LD_All)
216            Core::getInstance().configuration_->softDebugLevel_ = level;
217        else if (device == OutputHandler::LD_Console)
218            Core::getInstance().configuration_->softDebugLevelConsole_ = level;
219        else if (device == OutputHandler::LD_Logfile)
220            Core::getInstance().configuration_->softDebugLevelLogfile_ = level;
221        else if (device == OutputHandler::LD_Shell)
222            Core::getInstance().configuration_->softDebugLevelShell_ = level;
223
224        OutputHandler::setSoftDebugLevel(device, level);
225    }
226
227    /**
228    @note
229        The code of this function has been copied and adjusted from OGRE, an open source graphics engine.
230            (Object-oriented Graphics Rendering Engine)
231        For the latest info, see http://www.ogre3d.org/
232
233        Copyright (c) 2000-2008 Torus Knot Software Ltd
234
235        OGRE is licensed under the LGPL. For more info, see OGRE license.
236    */
237    void Core::setThreadAffinity(int limitToCPU)
238    {
239#ifdef ORXONOX_PLATFORM_WINDOWS
240
241        if (limitToCPU <= 0)
242            return;
243
244        unsigned int coreNr = limitToCPU - 1;
245        // Get the current process core mask
246        DWORD procMask;
247        DWORD sysMask;
248#  if _MSC_VER >= 1400 && defined (_M_X64)
249        GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask);
250#  else
251        GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask);
252#  endif
253
254        // If procMask is 0, consider there is only one core available
255        // (using 0 as procMask will cause an infinite loop below)
256        if (procMask == 0)
257            procMask = 1;
258
259        // if the core specified with coreNr is not available, take the lowest one
260        if (!(procMask & (1 << coreNr)))
261            coreNr = 0;
262
263        // Find the lowest core that this process uses and coreNr suggests
264        DWORD threadMask = 1;
265        while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr)))
266            threadMask <<= 1;
267
268        // Set affinity to the first core
269        SetThreadAffinityMask(GetCurrentThread(), threadMask);
270#endif
271    }
272}
Note: See TracBrowser for help on using the repository browser.