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