| 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 |  | 
|---|
| 39 | namespace 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 Static function that holds the singleton. | 
|---|
| 120 |     */ | 
|---|
| 121 |     int CoreSettings::getSoftDebugLevel(OutputHandler::OutputDevice device) | 
|---|
| 122 |     { | 
|---|
| 123 |         if (!CoreSettings::isCreatingCoreSettings()) | 
|---|
| 124 |         { | 
|---|
| 125 |             if (device == OutputHandler::LD_All) | 
|---|
| 126 |                 return CoreSettings::getInstance().softDebugLevel_; | 
|---|
| 127 |             else if (device == OutputHandler::LD_Console) | 
|---|
| 128 |                 return CoreSettings::getInstance().softDebugLevelConsole_; | 
|---|
| 129 |             else if (device == OutputHandler::LD_Logfile) | 
|---|
| 130 |                 return CoreSettings::getInstance().softDebugLevelLogfile_; | 
|---|
| 131 |             else if (device == OutputHandler::LD_Shell) | 
|---|
| 132 |                 return CoreSettings::getInstance().softDebugLevelShell_; | 
|---|
| 133 |         } | 
|---|
| 134 |  | 
|---|
| 135 |         // Return a constant value while we're creating the object | 
|---|
| 136 |         return 2; | 
|---|
| 137 |     } | 
|---|
| 138 |  | 
|---|
| 139 |     /** | 
|---|
| 140 |         @brief Returns the configured language. | 
|---|
| 141 |     */ | 
|---|
| 142 |     const std::string& CoreSettings::getLanguage() | 
|---|
| 143 |     { | 
|---|
| 144 |         if (!CoreSettings::isCreatingCoreSettings()) | 
|---|
| 145 |             return CoreSettings::getInstance().language_; | 
|---|
| 146 |  | 
|---|
| 147 |         return Language::getLanguage().defaultLanguage_; | 
|---|
| 148 |     } | 
|---|
| 149 |  | 
|---|
| 150 |     /** | 
|---|
| 151 |         @brief Sets the language in the config-file back to the default. | 
|---|
| 152 |     */ | 
|---|
| 153 |     void CoreSettings::resetLanguage() | 
|---|
| 154 |     { | 
|---|
| 155 |         CoreSettings::getInstance().resetLanguageIntern(); | 
|---|
| 156 |     } | 
|---|
| 157 |  | 
|---|
| 158 |     /** | 
|---|
| 159 |         @brief Sets the language in the config-file back to the default. | 
|---|
| 160 |     */ | 
|---|
| 161 |     void CoreSettings::resetLanguageIntern() | 
|---|
| 162 |     { | 
|---|
| 163 |         ResetConfigValue(language_); | 
|---|
| 164 |     } | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | /** | 
|---|
| 168 |     @brief Returns the soft debug level, stored in the only existing instance of the DebugLevel class, configured in the config-file. | 
|---|
| 169 |     @return The soft debug level | 
|---|
| 170 | */ | 
|---|
| 171 | int getSoftDebugLevel() | 
|---|
| 172 | { | 
|---|
| 173 |     return orxonox::CoreSettings::getSoftDebugLevel(); | 
|---|
| 174 | } | 
|---|