[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 | /** |
---|
[2171] | 30 | @file |
---|
[1524] | 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" |
---|
[2685] | 39 | #include "LuaBind.h" |
---|
| 40 | #include "CommandLine.h" |
---|
[1505] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[2087] | 44 | bool Core::bShowsGraphics_s = false; |
---|
| 45 | bool Core::bHasServer_s = false; |
---|
| 46 | bool Core::bIsClient_s = false; |
---|
| 47 | bool Core::bIsStandalone_s = false; |
---|
| 48 | bool Core::bIsMaster_s = false; |
---|
| 49 | |
---|
[2685] | 50 | bool Core::isDevBuild_s = false; |
---|
| 51 | std::string Core::configPath_s(ORXONOX_CONFIG_INSTALL_PATH); // from OrxonoxConfig.h |
---|
| 52 | std::string Core::logPath_s (ORXONOX_LOG_INSTALL_PATH); // from OrxonoxConfig.h |
---|
| 53 | |
---|
[2662] | 54 | Core* Core::singletonRef_s = 0; |
---|
| 55 | |
---|
[2685] | 56 | SetCommandLineArgument(mediaPath, "").information("PATH"); |
---|
| 57 | |
---|
[1505] | 58 | /** |
---|
| 59 | @brief Constructor: Registers the object and sets the config-values. |
---|
| 60 | @param A reference to a global variable, used to avoid an infinite recursion in getSoftDebugLevel() |
---|
| 61 | */ |
---|
[1524] | 62 | Core::Core() |
---|
[1505] | 63 | { |
---|
[1524] | 64 | RegisterRootObject(Core); |
---|
[2662] | 65 | |
---|
| 66 | assert(Core::singletonRef_s == 0); |
---|
| 67 | Core::singletonRef_s = this; |
---|
[2685] | 68 | |
---|
[2662] | 69 | this->bInitializeRandomNumberGenerator_ = false; |
---|
[2685] | 70 | this->setConfigValues(); |
---|
[2662] | 71 | |
---|
[2685] | 72 | // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used |
---|
| 73 | OutputHandler::getOutStream().setLogPath(Core::logPath_s); |
---|
| 74 | |
---|
| 75 | // Possible media path override by the command line |
---|
| 76 | if (!CommandLine::getArgument("mediaPath")->hasDefaultValue()) |
---|
| 77 | { |
---|
| 78 | std::string mediaPath = CommandLine::getValue("mediaPath"); |
---|
| 79 | Core::tsetMediaPath(mediaPath); |
---|
| 80 | } |
---|
[1505] | 81 | } |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | @brief Sets the bool to true to avoid static functions accessing a deleted object. |
---|
| 85 | */ |
---|
[1524] | 86 | Core::~Core() |
---|
[1505] | 87 | { |
---|
[2662] | 88 | assert(Core::singletonRef_s); |
---|
| 89 | Core::singletonRef_s = 0; |
---|
[1505] | 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | @brief Function to collect the SetConfigValue-macro calls. |
---|
| 94 | */ |
---|
[1524] | 95 | void Core::setConfigValues() |
---|
[1505] | 96 | { |
---|
[2685] | 97 | #ifdef NDEBUG |
---|
| 98 | const unsigned int defaultLevelConsole = 1; |
---|
| 99 | const unsigned int defaultLevelLogfile = 3; |
---|
| 100 | const unsigned int defaultLevelShell = 1; |
---|
| 101 | #else |
---|
| 102 | const unsigned int defaultLevelConsole = 3; |
---|
| 103 | const unsigned int defaultLevelLogfile = 4; |
---|
| 104 | const unsigned int defaultLevelShell = 3; |
---|
| 105 | #endif |
---|
| 106 | SetConfigValue(softDebugLevelConsole_, defaultLevelConsole) |
---|
| 107 | .description("The maximal level of debug output shown in the console").callback(this, &Core::debugLevelChanged); |
---|
| 108 | SetConfigValue(softDebugLevelLogfile_, defaultLevelLogfile) |
---|
| 109 | .description("The maximal level of debug output shown in the logfile").callback(this, &Core::debugLevelChanged); |
---|
| 110 | SetConfigValue(softDebugLevelShell_, defaultLevelShell) |
---|
| 111 | .description("The maximal level of debug output shown in the ingame shell").callback(this, &Core::debugLevelChanged); |
---|
| 112 | |
---|
[1747] | 113 | SetConfigValue(language_, Language::getLanguage().defaultLanguage_).description("The language of the ingame text").callback(this, &Core::languageChanged); |
---|
[2662] | 114 | SetConfigValue(bInitializeRandomNumberGenerator_, true).description("If true, all random actions are different each time you start the game").callback(this, &Core::initializeRandomNumberGenerator); |
---|
[2685] | 115 | |
---|
| 116 | // Media path (towards config and log path) is ini-configurable |
---|
| 117 | const char* defaultMediaPath = ORXONOX_MEDIA_INSTALL_PATH; |
---|
| 118 | if (Core::isDevBuild()) |
---|
| 119 | defaultMediaPath = ORXONOX_MEDIA_DEV_PATH; |
---|
| 120 | |
---|
| 121 | SetConfigValue(mediaPath_, defaultMediaPath) |
---|
| 122 | .description("Relative path to the game data.").callback(this, &Core::mediaPathChanged); |
---|
| 123 | |
---|
[1747] | 124 | } |
---|
[1505] | 125 | |
---|
[1747] | 126 | /** |
---|
| 127 | @brief Callback function if the debug level has changed. |
---|
| 128 | */ |
---|
| 129 | void Core::debugLevelChanged() |
---|
| 130 | { |
---|
[1505] | 131 | // softDebugLevel_ is the maximum of the 3 variables |
---|
| 132 | this->softDebugLevel_ = this->softDebugLevelConsole_; |
---|
| 133 | if (this->softDebugLevelLogfile_ > this->softDebugLevel_) |
---|
| 134 | this->softDebugLevel_ = this->softDebugLevelLogfile_; |
---|
| 135 | if (this->softDebugLevelShell_ > this->softDebugLevel_) |
---|
| 136 | this->softDebugLevel_ = this->softDebugLevelShell_; |
---|
| 137 | |
---|
[1747] | 138 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_All, this->softDebugLevel_); |
---|
| 139 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Console, this->softDebugLevelConsole_); |
---|
| 140 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Logfile, this->softDebugLevelLogfile_); |
---|
| 141 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Shell, this->softDebugLevelShell_); |
---|
| 142 | } |
---|
[1505] | 143 | |
---|
[1747] | 144 | /** |
---|
| 145 | @brief Callback function if the language has changed. |
---|
| 146 | */ |
---|
| 147 | void Core::languageChanged() |
---|
| 148 | { |
---|
| 149 | // Read the translation file after the language was configured |
---|
| 150 | Language::getLanguage().readTranslatedLanguageFile(); |
---|
[1505] | 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
[2685] | 154 | @brief |
---|
| 155 | Callback function if the media path has changed. |
---|
| 156 | */ |
---|
| 157 | void Core::mediaPathChanged() |
---|
| 158 | { |
---|
| 159 | if (mediaPath_ != "" && mediaPath_[mediaPath_.size() - 1] != '/') |
---|
| 160 | { |
---|
| 161 | ModifyConfigValue(mediaPath_, set, mediaPath_ + "/"); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | if (mediaPath_ == "") |
---|
| 165 | { |
---|
| 166 | ModifyConfigValue(mediaPath_, set, "/"); |
---|
| 167 | COUT(2) << "Warning: Data path set to \"/\", is that really correct?" << std::endl; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
[1505] | 172 | @brief Returns the softDebugLevel for the given device (returns a default-value if the class ist right about to be created). |
---|
| 173 | @param device The device |
---|
| 174 | @return The softDebugLevel |
---|
| 175 | */ |
---|
[1524] | 176 | int Core::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
[1505] | 177 | { |
---|
[2662] | 178 | switch (device) |
---|
[1505] | 179 | { |
---|
[2662] | 180 | case OutputHandler::LD_All: |
---|
| 181 | return Core::getInstance().softDebugLevel_; |
---|
| 182 | case OutputHandler::LD_Console: |
---|
| 183 | return Core::getInstance().softDebugLevelConsole_; |
---|
| 184 | case OutputHandler::LD_Logfile: |
---|
| 185 | return Core::getInstance().softDebugLevelLogfile_; |
---|
| 186 | case OutputHandler::LD_Shell: |
---|
| 187 | return Core::getInstance().softDebugLevelShell_; |
---|
| 188 | default: |
---|
| 189 | assert(0); |
---|
| 190 | return 2; |
---|
[1505] | 191 | } |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /** |
---|
| 195 | @brief Sets the softDebugLevel for the given device. Please use this only temporary and restore the value afterwards, as it overrides the configured value. |
---|
| 196 | @param device The device |
---|
| 197 | @param level The level |
---|
| 198 | */ |
---|
[1524] | 199 | void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level) |
---|
[1505] | 200 | { |
---|
[2662] | 201 | if (device == OutputHandler::LD_All) |
---|
| 202 | Core::getInstance().softDebugLevel_ = level; |
---|
| 203 | else if (device == OutputHandler::LD_Console) |
---|
| 204 | Core::getInstance().softDebugLevelConsole_ = level; |
---|
| 205 | else if (device == OutputHandler::LD_Logfile) |
---|
| 206 | Core::getInstance().softDebugLevelLogfile_ = level; |
---|
| 207 | else if (device == OutputHandler::LD_Shell) |
---|
| 208 | Core::getInstance().softDebugLevelShell_ = level; |
---|
[1747] | 209 | |
---|
[2662] | 210 | OutputHandler::setSoftDebugLevel(device, level); |
---|
[1505] | 211 | } |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | @brief Returns the configured language. |
---|
| 215 | */ |
---|
[1524] | 216 | const std::string& Core::getLanguage() |
---|
[1505] | 217 | { |
---|
[2662] | 218 | return Core::getInstance().language_; |
---|
[1505] | 219 | } |
---|
| 220 | |
---|
| 221 | /** |
---|
| 222 | @brief Sets the language in the config-file back to the default. |
---|
| 223 | */ |
---|
[1524] | 224 | void Core::resetLanguage() |
---|
[1505] | 225 | { |
---|
[1524] | 226 | Core::getInstance().resetLanguageIntern(); |
---|
[1505] | 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | @brief Sets the language in the config-file back to the default. |
---|
| 231 | */ |
---|
[1524] | 232 | void Core::resetLanguageIntern() |
---|
[1505] | 233 | { |
---|
| 234 | ResetConfigValue(language_); |
---|
| 235 | } |
---|
[2662] | 236 | |
---|
[2685] | 237 | /** |
---|
| 238 | @brief |
---|
| 239 | Temporary sets the media path |
---|
| 240 | @param path |
---|
| 241 | The new media path |
---|
| 242 | */ |
---|
| 243 | void Core::_tsetMediaPath(const std::string& path) |
---|
| 244 | { |
---|
| 245 | if (*path.end() != '/' && *path.end() != '\\') |
---|
| 246 | { |
---|
| 247 | ModifyConfigValue(mediaPath_, tset, path + "/"); |
---|
| 248 | } |
---|
| 249 | else |
---|
| 250 | { |
---|
| 251 | ModifyConfigValue(mediaPath_, tset, path); |
---|
| 252 | } |
---|
| 253 | } |
---|
| 254 | |
---|
[2662] | 255 | void Core::initializeRandomNumberGenerator() |
---|
| 256 | { |
---|
| 257 | static bool bInitialized = false; |
---|
| 258 | if (!bInitialized && this->bInitializeRandomNumberGenerator_) |
---|
| 259 | { |
---|
| 260 | srand(time(0)); |
---|
| 261 | rand(); |
---|
| 262 | bInitialized = true; |
---|
| 263 | } |
---|
| 264 | } |
---|
[2685] | 265 | |
---|
| 266 | /*static*/ void Core::setDevBuild() |
---|
| 267 | { |
---|
| 268 | // Be careful never to call this function before main()! |
---|
| 269 | |
---|
| 270 | Core::isDevBuild_s = true; |
---|
| 271 | // Constants taken from OrxonoxConfig.h |
---|
| 272 | Core::configPath_s = ORXONOX_CONFIG_DEV_PATH; |
---|
| 273 | Core::logPath_s = ORXONOX_LOG_DEV_PATH; |
---|
| 274 | } |
---|
[1505] | 275 | } |
---|