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 | * Reto Grieder |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | /** |
---|
30 | @file |
---|
31 | @brief Implementation of the Core class. |
---|
32 | */ |
---|
33 | |
---|
34 | #include "Core.h" |
---|
35 | #include <cassert> |
---|
36 | #include <fstream> |
---|
37 | #include <boost/filesystem.hpp> |
---|
38 | |
---|
39 | #include "util/Exception.h" |
---|
40 | #include "Language.h" |
---|
41 | #include "CoreIncludes.h" |
---|
42 | #include "ConfigValueIncludes.h" |
---|
43 | #include "LuaBind.h" |
---|
44 | #include "CommandLine.h" |
---|
45 | |
---|
46 | namespace orxonox |
---|
47 | { |
---|
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 | |
---|
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 |
---|
57 | std::string Core::mediaPath_s (ORXONOX_MEDIA_INSTALL_PATH); // from OrxonoxConfig.h |
---|
58 | |
---|
59 | Core* Core::singletonRef_s = 0; |
---|
60 | |
---|
61 | SetCommandLineArgument(mediaPath, "").information("PATH"); |
---|
62 | |
---|
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 | */ |
---|
67 | Core::Core() |
---|
68 | { |
---|
69 | RegisterRootObject(Core); |
---|
70 | |
---|
71 | assert(Core::singletonRef_s == 0); |
---|
72 | Core::singletonRef_s = this; |
---|
73 | |
---|
74 | this->bInitializeRandomNumberGenerator_ = false; |
---|
75 | this->setConfigValues(); |
---|
76 | |
---|
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 | } |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | @brief Sets the bool to true to avoid static functions accessing a deleted object. |
---|
90 | */ |
---|
91 | Core::~Core() |
---|
92 | { |
---|
93 | assert(Core::singletonRef_s); |
---|
94 | Core::singletonRef_s = 0; |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | @brief Function to collect the SetConfigValue-macro calls. |
---|
99 | */ |
---|
100 | void Core::setConfigValues() |
---|
101 | { |
---|
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 | |
---|
118 | SetConfigValue(language_, Language::getLanguage().defaultLanguage_).description("The language of the ingame text").callback(this, &Core::languageChanged); |
---|
119 | SetConfigValue(bInitializeRandomNumberGenerator_, true).description("If true, all random actions are different each time you start the game").callback(this, &Core::initializeRandomNumberGenerator); |
---|
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 | |
---|
126 | SetConfigValue(mediaPath_s, defaultMediaPath) |
---|
127 | .description("Relative path to the game data.").callback(this, &Core::mediaPathChanged); |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | @brief Callback function if the debug level has changed. |
---|
133 | */ |
---|
134 | void Core::debugLevelChanged() |
---|
135 | { |
---|
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 | |
---|
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 | } |
---|
148 | |
---|
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(); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | @brief |
---|
160 | Callback function if the media path has changed. |
---|
161 | */ |
---|
162 | void Core::mediaPathChanged() |
---|
163 | { |
---|
164 | if (mediaPath_s != "" && mediaPath_s[mediaPath_s.size() - 1] != '/') |
---|
165 | { |
---|
166 | ModifyConfigValue(mediaPath_s, set, mediaPath_s + "/"); |
---|
167 | } |
---|
168 | |
---|
169 | if (mediaPath_s == "") |
---|
170 | { |
---|
171 | ModifyConfigValue(mediaPath_s, set, "/"); |
---|
172 | COUT(2) << "Warning: Data path set to \"/\", is that really correct?" << std::endl; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
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 | */ |
---|
181 | int Core::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
182 | { |
---|
183 | switch (device) |
---|
184 | { |
---|
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; |
---|
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 | */ |
---|
204 | void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level) |
---|
205 | { |
---|
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; |
---|
214 | |
---|
215 | OutputHandler::setSoftDebugLevel(device, level); |
---|
216 | } |
---|
217 | |
---|
218 | /** |
---|
219 | @brief Returns the configured language. |
---|
220 | */ |
---|
221 | const std::string& Core::getLanguage() |
---|
222 | { |
---|
223 | return Core::getInstance().language_; |
---|
224 | } |
---|
225 | |
---|
226 | /** |
---|
227 | @brief Sets the language in the config-file back to the default. |
---|
228 | */ |
---|
229 | void Core::resetLanguage() |
---|
230 | { |
---|
231 | Core::getInstance().resetLanguageIntern(); |
---|
232 | } |
---|
233 | |
---|
234 | /** |
---|
235 | @brief Sets the language in the config-file back to the default. |
---|
236 | */ |
---|
237 | void Core::resetLanguageIntern() |
---|
238 | { |
---|
239 | ResetConfigValue(language_); |
---|
240 | } |
---|
241 | |
---|
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 | { |
---|
252 | ModifyConfigValue(mediaPath_s, tset, path + "/"); |
---|
253 | } |
---|
254 | else |
---|
255 | { |
---|
256 | ModifyConfigValue(mediaPath_s, tset, path); |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
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 | } |
---|
270 | |
---|
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() |
---|
278 | { |
---|
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 | } |
---|
291 | |
---|
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_directories(it->first)) // function may not return true at all (bug?) |
---|
314 | { |
---|
315 | COUT(4) << "Created " << it->second << " directory" << std::endl; |
---|
316 | } |
---|
317 | } |
---|
318 | } |
---|
319 | } |
---|