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