Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/core/CoreSettings.cc @ 1427

Last change on this file since 1427 was 1326, checked in by landauf, 17 years ago

fixed crash when displaying debug-level 4 and 5 in the InGameConsole. It works perfectly now, although it's not recommended, because you won't see much of your own input in the console any more ;)
however, performance is quite good. maybe i can improve it even more.

File size: 6.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 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file CoreSettings.cc
31    @brief Implementation of the CoreSettings class.
32*/
33
34#include "CoreSettings.h"
35#include "Language.h"
36#include "CoreIncludes.h"
37#include "ConfigValueIncludes.h"
38
39namespace orxonox
40{
41    /**
42        @brief Constructor: Registers the object and sets the config-values.
43        @param A reference to a global variable, used to avoid an infinite recursion in getSoftDebugLevel()
44    */
45    CoreSettings::CoreSettings()
46    {
47        RegisterRootObject(CoreSettings);
48        this->setConfigValues();
49    }
50
51    /**
52        @brief Sets the bool to true to avoid static functions accessing a deleted object.
53    */
54    CoreSettings::~CoreSettings()
55    {
56        isCreatingCoreSettings() = true;
57    }
58
59    /**
60        @brief Returns true if the CoreSettings instance is not yet ready and the static functions have to return a default value.
61    */
62    bool& CoreSettings::isCreatingCoreSettings()
63    {
64        static bool bCreatingCoreSettings = true;
65        static bool bFirstTime = true;
66        if (bFirstTime)
67        {
68            bFirstTime = false;
69            CoreSettings::getInstance();
70        }
71        return bCreatingCoreSettings;
72    }
73
74    /**
75        @brief Returns a unique instance of CoreSettings.
76        @return The instance
77    */
78    CoreSettings& CoreSettings::getInstance()
79    {
80        static CoreSettings instance = CoreSettings();
81
82        // If bCreatingSoftDebugLevelObject is true, we're just about to create an instance of the DebugLevel class
83        if (CoreSettings::isCreatingCoreSettings())
84        {
85            isCreatingCoreSettings() = false;
86            instance.setConfigValues();
87        }
88        return instance;
89    }
90
91    /**
92        @brief Function to collect the SetConfigValue-macro calls.
93    */
94    void CoreSettings::setConfigValues()
95    {
96        SetConfigValue(softDebugLevelConsole_, 3).description("The maximal level of debug output shown in the console");
97        SetConfigValue(softDebugLevelLogfile_, 3).description("The maximal level of debug output shown in the logfile");
98        SetConfigValue(softDebugLevelShell_, 1).description("The maximal level of debug output shown in the ingame shell");
99
100        // softDebugLevel_ is the maximum of the 3 variables
101        this->softDebugLevel_ = this->softDebugLevelConsole_;
102        if (this->softDebugLevelLogfile_ > this->softDebugLevel_)
103            this->softDebugLevel_ = this->softDebugLevelLogfile_;
104        if (this->softDebugLevelShell_ > this->softDebugLevel_)
105            this->softDebugLevel_ = this->softDebugLevelShell_;
106
107
108        std::string temp = this->language_;
109        SetConfigValue(language_, Language::getLanguage().defaultLanguage_).description("The language of the ingame text");
110
111        if (this->language_ != temp)
112        {
113            // Read the translation file after the language was configured
114            Language::getLanguage().readTranslatedLanguageFile();
115        }
116    }
117
118    /**
119        @brief Returns the softDebugLevel for the given device (returns a default-value if the class ist right about to be created).
120        @param device The device
121        @return The softDebugLevel
122    */
123    int CoreSettings::getSoftDebugLevel(OutputHandler::OutputDevice device)
124    {
125        if (!CoreSettings::isCreatingCoreSettings())
126        {
127            if (device == OutputHandler::LD_All)
128                return CoreSettings::getInstance().softDebugLevel_;
129            else if (device == OutputHandler::LD_Console)
130                return CoreSettings::getInstance().softDebugLevelConsole_;
131            else if (device == OutputHandler::LD_Logfile)
132                return CoreSettings::getInstance().softDebugLevelLogfile_;
133            else if (device == OutputHandler::LD_Shell)
134                return CoreSettings::getInstance().softDebugLevelShell_;
135        }
136
137        // Return a constant value while we're creating the object
138        return 2;
139    }
140
141     /**
142        @brief Sets the softDebugLevel for the given device. Please use this only temporary and restore the value afterwards, as it overrides the configured value.
143        @param device The device
144        @param level The level
145    */
146     void CoreSettings::setSoftDebugLevel(OutputHandler::OutputDevice device, int level)
147     {
148        if (!CoreSettings::isCreatingCoreSettings())
149        {
150            if (device == OutputHandler::LD_All)
151                CoreSettings::getInstance().softDebugLevel_ = level;
152            else if (device == OutputHandler::LD_Console)
153                CoreSettings::getInstance().softDebugLevelConsole_ = level;
154            else if (device == OutputHandler::LD_Logfile)
155                CoreSettings::getInstance().softDebugLevelLogfile_ = level;
156            else if (device == OutputHandler::LD_Shell)
157                CoreSettings::getInstance().softDebugLevelShell_ = level;
158        }
159     }
160
161    /**
162        @brief Returns the configured language.
163    */
164    const std::string& CoreSettings::getLanguage()
165    {
166        if (!CoreSettings::isCreatingCoreSettings())
167            return CoreSettings::getInstance().language_;
168
169        return Language::getLanguage().defaultLanguage_;
170    }
171
172    /**
173        @brief Sets the language in the config-file back to the default.
174    */
175    void CoreSettings::resetLanguage()
176    {
177        CoreSettings::getInstance().resetLanguageIntern();
178    }
179
180    /**
181        @brief Sets the language in the config-file back to the default.
182    */
183    void CoreSettings::resetLanguageIntern()
184    {
185        ResetConfigValue(language_);
186    }
187}
188
189/**
190    @brief Returns the soft debug level, stored in the only existing instance of the DebugLevel class, configured in the config-file.
191    @return The soft debug level
192*/
193int getSoftDebugLevel()
194{
195    return orxonox::CoreSettings::getSoftDebugLevel();
196}
Note: See TracBrowser for help on using the repository browser.