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 | * Reto Grieder |
---|
25 | * Co-authors: |
---|
26 | * ... |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | /** |
---|
31 | @file |
---|
32 | @brief |
---|
33 | Implementation of the Core singleton with its global variables (avoids boost include) |
---|
34 | */ |
---|
35 | |
---|
36 | #include "Core.h" |
---|
37 | |
---|
38 | #include <cassert> |
---|
39 | #include <fstream> |
---|
40 | #include <cstdlib> |
---|
41 | #include <cstdio> |
---|
42 | #include <boost/version.hpp> |
---|
43 | #include <boost/filesystem.hpp> |
---|
44 | |
---|
45 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
46 | # ifndef WIN32_LEAN_AND_MEAN |
---|
47 | # define WIN32_LEAN_AND_MEAN |
---|
48 | # endif |
---|
49 | # include <windows.h> |
---|
50 | # undef min |
---|
51 | # undef max |
---|
52 | #elif defined(ORXONOX_PLATFORM_APPLE) |
---|
53 | # include <sys/param.h> |
---|
54 | # include <mach-o/dyld.h> |
---|
55 | #else /* Linux */ |
---|
56 | # include <sys/types.h> |
---|
57 | # include <unistd.h> |
---|
58 | #endif |
---|
59 | |
---|
60 | #include "SpecialConfig.h" |
---|
61 | #include "util/Debug.h" |
---|
62 | #include "util/Exception.h" |
---|
63 | #include "util/SignalHandler.h" |
---|
64 | #include "Clock.h" |
---|
65 | #include "CommandLine.h" |
---|
66 | #include "LuaState.h" |
---|
67 | |
---|
68 | // Boost 1.36 has some issues with deprecated functions that have been omitted |
---|
69 | #if (BOOST_VERSION == 103600) |
---|
70 | # define BOOST_LEAF_FUNCTION filename |
---|
71 | #else |
---|
72 | # define BOOST_LEAF_FUNCTION leaf |
---|
73 | #endif |
---|
74 | |
---|
75 | namespace orxonox |
---|
76 | { |
---|
77 | //! Static pointer to the singleton |
---|
78 | Core* Core::singletonPtr_s = 0; |
---|
79 | |
---|
80 | SetCommandLineOnlyArgument(writingPathSuffix, "").information("Additional subfolder for config and log files"); |
---|
81 | SetCommandLineArgument(settingsFile, "orxonox.ini").information("THE configuration file"); |
---|
82 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
83 | SetCommandLineArgument(limitToCPU, 0).information("Limits the program to one cpu/core (1, 2, 3, etc.). 0 turns it off (default)"); |
---|
84 | #endif |
---|
85 | |
---|
86 | /** |
---|
87 | @brief |
---|
88 | Helper class for the Core singleton: we cannot derive |
---|
89 | Core from OrxonoxClass because we need to handle the Identifier |
---|
90 | destruction in the Core destructor. |
---|
91 | */ |
---|
92 | class CoreConfiguration |
---|
93 | { |
---|
94 | public: |
---|
95 | CoreConfiguration() |
---|
96 | { |
---|
97 | } |
---|
98 | |
---|
99 | void initialise() |
---|
100 | { |
---|
101 | #ifdef NDEBUG |
---|
102 | const unsigned int defaultLevelConsole = 1; |
---|
103 | const unsigned int defaultLevelLogfile = 3; |
---|
104 | const unsigned int defaultLevelShell = 1; |
---|
105 | #else |
---|
106 | const unsigned int defaultLevelConsole = 3; |
---|
107 | const unsigned int defaultLevelLogfile = 4; |
---|
108 | const unsigned int defaultLevelShell = 3; |
---|
109 | #endif |
---|
110 | softDebugLevelConsole_ = defaultLevelConsole; |
---|
111 | softDebugLevelLogfile_ = defaultLevelLogfile; |
---|
112 | softDebugLevelShell_ = defaultLevelShell; |
---|
113 | this->debugLevelChanged(); |
---|
114 | |
---|
115 | bInitializeRandomNumberGenerator_ = true; |
---|
116 | this->initializeRandomNumberGenerator(); |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | @brief Callback function if the debug level has changed. |
---|
121 | */ |
---|
122 | void debugLevelChanged() |
---|
123 | { |
---|
124 | // softDebugLevel_ is the maximum of the 3 variables |
---|
125 | this->softDebugLevel_ = this->softDebugLevelConsole_; |
---|
126 | if (this->softDebugLevelLogfile_ > this->softDebugLevel_) |
---|
127 | this->softDebugLevel_ = this->softDebugLevelLogfile_; |
---|
128 | if (this->softDebugLevelShell_ > this->softDebugLevel_) |
---|
129 | this->softDebugLevel_ = this->softDebugLevelShell_; |
---|
130 | |
---|
131 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_All, this->softDebugLevel_); |
---|
132 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Console, this->softDebugLevelConsole_); |
---|
133 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Logfile, this->softDebugLevelLogfile_); |
---|
134 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Shell, this->softDebugLevelShell_); |
---|
135 | } |
---|
136 | |
---|
137 | void initializeRandomNumberGenerator() |
---|
138 | { |
---|
139 | static bool bInitialized = false; |
---|
140 | if (!bInitialized && this->bInitializeRandomNumberGenerator_) |
---|
141 | { |
---|
142 | srand(static_cast<unsigned int>(time(0))); |
---|
143 | rand(); |
---|
144 | bInitialized = true; |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | int softDebugLevel_; //!< The debug level |
---|
149 | int softDebugLevelConsole_; //!< The debug level for the console |
---|
150 | int softDebugLevelLogfile_; //!< The debug level for the logfile |
---|
151 | int softDebugLevelShell_; //!< The debug level for the ingame shell |
---|
152 | bool bInitializeRandomNumberGenerator_; //!< If true, srand(time(0)) is called |
---|
153 | |
---|
154 | //! Path to the parent directory of the ones above if program was installed with relativ pahts |
---|
155 | boost::filesystem::path rootPath_; |
---|
156 | boost::filesystem::path executablePath_; //!< Path to the executable |
---|
157 | boost::filesystem::path dataPath_; //!< Path to the data file folder |
---|
158 | boost::filesystem::path configPath_; //!< Path to the config file folder |
---|
159 | boost::filesystem::path logPath_; //!< Path to the log file folder |
---|
160 | }; |
---|
161 | |
---|
162 | |
---|
163 | Core::Core(const std::string& cmdLine) |
---|
164 | // Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands) |
---|
165 | : configuration_(new CoreConfiguration()) // Don't yet create config values! |
---|
166 | , bDevRun_(false) |
---|
167 | { |
---|
168 | // Set the hard coded fixed paths |
---|
169 | this->setFixedPaths(); |
---|
170 | |
---|
171 | // Parse command line arguments AFTER the modules have been loaded (static code!) |
---|
172 | CommandLine::parseCommandLine(cmdLine); |
---|
173 | |
---|
174 | // Set configurable paths like log, config and media |
---|
175 | this->setConfigurablePaths(); |
---|
176 | |
---|
177 | // create a signal handler (only active for linux) |
---|
178 | // This call is placed as soon as possible, but after the directories are set |
---|
179 | this->signalHandler_.reset(new SignalHandler()); |
---|
180 | this->signalHandler_->doCatch(configuration_->executablePath_.string(), Core::getLogPathString() + "orxonox_crash.log"); |
---|
181 | |
---|
182 | // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used |
---|
183 | OutputHandler::getOutStream().setLogPath(Core::getLogPathString()); |
---|
184 | |
---|
185 | // Parse additional options file now that we know its path |
---|
186 | CommandLine::parseFile(); |
---|
187 | |
---|
188 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
189 | // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump |
---|
190 | // do this after ogre has initialised. Somehow Ogre changes the settings again (not through |
---|
191 | // the timer though). |
---|
192 | int limitToCPU = CommandLine::getValue("limitToCPU"); |
---|
193 | if (limitToCPU > 0) |
---|
194 | setThreadAffinity(static_cast<unsigned int>(limitToCPU)); |
---|
195 | #endif |
---|
196 | |
---|
197 | // Do this soon after the ConfigFileManager has been created to open up the |
---|
198 | // possibility to configure everything below here |
---|
199 | this->configuration_->initialise(); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | @brief |
---|
204 | All destruction code is handled by scoped_ptrs and ScopeGuards. |
---|
205 | */ |
---|
206 | Core::~Core() |
---|
207 | { |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | @brief Returns the softDebugLevel for the given device (returns a default-value if the class is right about to be created). |
---|
212 | @param device The device |
---|
213 | @return The softDebugLevel |
---|
214 | */ |
---|
215 | /*static*/ int Core::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
216 | { |
---|
217 | switch (device) |
---|
218 | { |
---|
219 | case OutputHandler::LD_All: |
---|
220 | return Core::getInstance().configuration_->softDebugLevel_; |
---|
221 | case OutputHandler::LD_Console: |
---|
222 | return Core::getInstance().configuration_->softDebugLevelConsole_; |
---|
223 | case OutputHandler::LD_Logfile: |
---|
224 | return Core::getInstance().configuration_->softDebugLevelLogfile_; |
---|
225 | case OutputHandler::LD_Shell: |
---|
226 | return Core::getInstance().configuration_->softDebugLevelShell_; |
---|
227 | default: |
---|
228 | assert(0); |
---|
229 | return 2; |
---|
230 | } |
---|
231 | } |
---|
232 | |
---|
233 | /** |
---|
234 | @brief Sets the softDebugLevel for the given device. Please use this only temporary and restore the value afterwards, as it overrides the configured value. |
---|
235 | @param device The device |
---|
236 | @param level The level |
---|
237 | */ |
---|
238 | /*static*/ void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level) |
---|
239 | { |
---|
240 | if (device == OutputHandler::LD_All) |
---|
241 | Core::getInstance().configuration_->softDebugLevel_ = level; |
---|
242 | else if (device == OutputHandler::LD_Console) |
---|
243 | Core::getInstance().configuration_->softDebugLevelConsole_ = level; |
---|
244 | else if (device == OutputHandler::LD_Logfile) |
---|
245 | Core::getInstance().configuration_->softDebugLevelLogfile_ = level; |
---|
246 | else if (device == OutputHandler::LD_Shell) |
---|
247 | Core::getInstance().configuration_->softDebugLevelShell_ = level; |
---|
248 | |
---|
249 | OutputHandler::setSoftDebugLevel(device, level); |
---|
250 | } |
---|
251 | |
---|
252 | /*static*/ const boost::filesystem::path& Core::getDataPath() |
---|
253 | { |
---|
254 | return getInstance().configuration_->dataPath_; |
---|
255 | } |
---|
256 | /*static*/ std::string Core::getDataPathString() |
---|
257 | { |
---|
258 | return getInstance().configuration_->dataPath_.string() + '/'; |
---|
259 | } |
---|
260 | |
---|
261 | /*static*/ const boost::filesystem::path& Core::getConfigPath() |
---|
262 | { |
---|
263 | return getInstance().configuration_->configPath_; |
---|
264 | } |
---|
265 | /*static*/ std::string Core::getConfigPathString() |
---|
266 | { |
---|
267 | return getInstance().configuration_->configPath_.string() + '/'; |
---|
268 | } |
---|
269 | |
---|
270 | /*static*/ const boost::filesystem::path& Core::getLogPath() |
---|
271 | { |
---|
272 | return getInstance().configuration_->logPath_; |
---|
273 | } |
---|
274 | /*static*/ std::string Core::getLogPathString() |
---|
275 | { |
---|
276 | return getInstance().configuration_->logPath_.string() + '/'; |
---|
277 | } |
---|
278 | |
---|
279 | /*static*/ const boost::filesystem::path& Core::getRootPath() |
---|
280 | { |
---|
281 | return getInstance().configuration_->rootPath_; |
---|
282 | } |
---|
283 | /*static*/ std::string Core::getRootPathString() |
---|
284 | { |
---|
285 | return getInstance().configuration_->rootPath_.string() + '/'; |
---|
286 | } |
---|
287 | |
---|
288 | /** |
---|
289 | @note |
---|
290 | The code of this function has been copied and adjusted from OGRE, an open source graphics engine. |
---|
291 | (Object-oriented Graphics Rendering Engine) |
---|
292 | For the latest info, see http://www.ogre3d.org/ |
---|
293 | |
---|
294 | Copyright (c) 2000-2008 Torus Knot Software Ltd |
---|
295 | |
---|
296 | OGRE is licensed under the LGPL. For more info, see OGRE license. |
---|
297 | */ |
---|
298 | void Core::setThreadAffinity(int limitToCPU) |
---|
299 | { |
---|
300 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
301 | |
---|
302 | if (limitToCPU <= 0) |
---|
303 | return; |
---|
304 | |
---|
305 | unsigned int coreNr = limitToCPU - 1; |
---|
306 | // Get the current process core mask |
---|
307 | DWORD procMask; |
---|
308 | DWORD sysMask; |
---|
309 | # if _MSC_VER >= 1400 && defined (_M_X64) |
---|
310 | GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); |
---|
311 | # else |
---|
312 | GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); |
---|
313 | # endif |
---|
314 | |
---|
315 | // If procMask is 0, consider there is only one core available |
---|
316 | // (using 0 as procMask will cause an infinite loop below) |
---|
317 | if (procMask == 0) |
---|
318 | procMask = 1; |
---|
319 | |
---|
320 | // if the core specified with coreNr is not available, take the lowest one |
---|
321 | if (!(procMask & (1 << coreNr))) |
---|
322 | coreNr = 0; |
---|
323 | |
---|
324 | // Find the lowest core that this process uses and coreNr suggests |
---|
325 | DWORD threadMask = 1; |
---|
326 | while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr))) |
---|
327 | threadMask <<= 1; |
---|
328 | |
---|
329 | // Set affinity to the first core |
---|
330 | SetThreadAffinityMask(GetCurrentThread(), threadMask); |
---|
331 | #endif |
---|
332 | } |
---|
333 | |
---|
334 | /** |
---|
335 | @brief |
---|
336 | Retrievs the executable path and sets all hard coded fixed path (currently only the module path) |
---|
337 | Also checks for "orxonox_dev_build.keep_me" in the executable diretory. |
---|
338 | If found it means that this is not an installed run, hence we |
---|
339 | don't write the logs and config files to ~/.orxonox |
---|
340 | @throw |
---|
341 | GeneralException |
---|
342 | */ |
---|
343 | void Core::setFixedPaths() |
---|
344 | { |
---|
345 | ////////////////////////// |
---|
346 | // FIND EXECUTABLE PATH // |
---|
347 | ////////////////////////// |
---|
348 | |
---|
349 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
350 | // get executable module |
---|
351 | TCHAR buffer[1024]; |
---|
352 | if (GetModuleFileName(NULL, buffer, 1024) == 0) |
---|
353 | ThrowException(General, "Could not retrieve executable path."); |
---|
354 | |
---|
355 | #elif defined(ORXONOX_PLATFORM_APPLE) |
---|
356 | char buffer[1024]; |
---|
357 | unsigned long path_len = 1023; |
---|
358 | if (_NSGetExecutablePath(buffer, &path_len)) |
---|
359 | ThrowException(General, "Could not retrieve executable path."); |
---|
360 | |
---|
361 | #else /* Linux */ |
---|
362 | /* written by Nicolai Haehnle <prefect_@gmx.net> */ |
---|
363 | |
---|
364 | /* Get our PID and build the name of the link in /proc */ |
---|
365 | char linkname[64]; /* /proc/<pid>/exe */ |
---|
366 | if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", getpid()) < 0) |
---|
367 | { |
---|
368 | /* This should only happen on large word systems. I'm not sure |
---|
369 | what the proper response is here. |
---|
370 | Since it really is an assert-like condition, aborting the |
---|
371 | program seems to be in order. */ |
---|
372 | assert(false); |
---|
373 | } |
---|
374 | |
---|
375 | /* Now read the symbolic link */ |
---|
376 | char buffer[1024]; |
---|
377 | int ret; |
---|
378 | ret = readlink(linkname, buffer, 1024); |
---|
379 | /* In case of an error, leave the handling up to the caller */ |
---|
380 | if (ret == -1) |
---|
381 | ThrowException(General, "Could not retrieve executable path."); |
---|
382 | |
---|
383 | /* Ensure proper NUL termination */ |
---|
384 | buffer[ret] = 0; |
---|
385 | #endif |
---|
386 | |
---|
387 | configuration_->executablePath_ = boost::filesystem::path(buffer); |
---|
388 | #ifndef ORXONOX_PLATFORM_APPLE |
---|
389 | configuration_->executablePath_ = configuration_->executablePath_.branch_path(); // remove executable name |
---|
390 | #endif |
---|
391 | |
---|
392 | ///////////////////// |
---|
393 | // SET MODULE PATH // |
---|
394 | ///////////////////// |
---|
395 | |
---|
396 | if (boost::filesystem::exists(configuration_->executablePath_ / "orxonox_dev_build.keep_me")) |
---|
397 | { |
---|
398 | COUT(1) << "Running from the build tree." << std::endl; |
---|
399 | Core::bDevRun_ = true; |
---|
400 | } |
---|
401 | else |
---|
402 | { |
---|
403 | |
---|
404 | #ifdef INSTALL_COPYABLE // --> relative paths |
---|
405 | |
---|
406 | // Also set the root path |
---|
407 | boost::filesystem::path relativeExecutablePath(specialConfig::defaultRuntimePath); |
---|
408 | configuration_->rootPath_ = configuration_->executablePath_; |
---|
409 | while (!boost::filesystem::equivalent(configuration_->rootPath_ / relativeExecutablePath, configuration_->executablePath_) |
---|
410 | && !configuration_->rootPath_.empty()) |
---|
411 | configuration_->rootPath_ = configuration_->rootPath_.branch_path(); |
---|
412 | if (configuration_->rootPath_.empty()) |
---|
413 | ThrowException(General, "Could not derive a root directory. Might the binary installation directory contain '..' when taken relative to the installation prefix path?"); |
---|
414 | |
---|
415 | #endif |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | /** |
---|
420 | @brief |
---|
421 | Sets config, log and media path and creates folders if necessary. |
---|
422 | @throws |
---|
423 | GeneralException |
---|
424 | */ |
---|
425 | void Core::setConfigurablePaths() |
---|
426 | { |
---|
427 | if (Core::isDevelopmentRun()) |
---|
428 | { |
---|
429 | configuration_->dataPath_ = specialConfig::dataDevDirectory; |
---|
430 | configuration_->configPath_ = specialConfig::configDevDirectory; |
---|
431 | configuration_->logPath_ = specialConfig::logDevDirectory; |
---|
432 | } |
---|
433 | else |
---|
434 | { |
---|
435 | |
---|
436 | #ifdef INSTALL_COPYABLE // --> relative paths |
---|
437 | |
---|
438 | // Using paths relative to the install prefix, complete them |
---|
439 | configuration_->dataPath_ = configuration_->rootPath_ / specialConfig::defaultDataPath; |
---|
440 | configuration_->configPath_ = configuration_->rootPath_ / specialConfig::defaultConfigPath; |
---|
441 | configuration_->logPath_ = configuration_->rootPath_ / specialConfig::defaultLogPath; |
---|
442 | |
---|
443 | #else |
---|
444 | |
---|
445 | configuration_->dataPath_ = specialConfig::dataInstallDirectory; |
---|
446 | |
---|
447 | // Get user directory |
---|
448 | # ifdef ORXONOX_PLATFORM_UNIX /* Apple? */ |
---|
449 | char* userDataPathPtr(getenv("HOME")); |
---|
450 | # else |
---|
451 | char* userDataPathPtr(getenv("APPDATA")); |
---|
452 | # endif |
---|
453 | if (userDataPathPtr == NULL) |
---|
454 | ThrowException(General, "Could not retrieve user data path."); |
---|
455 | boost::filesystem::path userDataPath(userDataPathPtr); |
---|
456 | userDataPath /= ".orxonox"; |
---|
457 | |
---|
458 | configuration_->configPath_ = userDataPath / specialConfig::defaultConfigPath; |
---|
459 | configuration_->logPath_ = userDataPath / specialConfig::defaultLogPath; |
---|
460 | |
---|
461 | #endif |
---|
462 | |
---|
463 | } |
---|
464 | |
---|
465 | // Option to put all the config and log files in a separate folder |
---|
466 | if (!CommandLine::getArgument("writingPathSuffix")->hasDefaultValue()) |
---|
467 | { |
---|
468 | std::string directory(CommandLine::getValue("writingPathSuffix").getString()); |
---|
469 | configuration_->configPath_ = configuration_->configPath_ / directory; |
---|
470 | configuration_->logPath_ = configuration_->logPath_ / directory; |
---|
471 | } |
---|
472 | |
---|
473 | // Create directories to avoid problems when opening files in non existent folders. |
---|
474 | std::vector<std::pair<boost::filesystem::path, std::string> > directories; |
---|
475 | directories.push_back(std::make_pair(boost::filesystem::path(configuration_->configPath_), "config")); |
---|
476 | directories.push_back(std::make_pair(boost::filesystem::path(configuration_->logPath_), "log")); |
---|
477 | |
---|
478 | for (std::vector<std::pair<boost::filesystem::path, std::string> >::iterator it = directories.begin(); |
---|
479 | it != directories.end(); ++it) |
---|
480 | { |
---|
481 | if (boost::filesystem::exists(it->first) && !boost::filesystem::is_directory(it->first)) |
---|
482 | { |
---|
483 | ThrowException(General, std::string("The ") + it->second + " directory has been preoccupied by a file! \ |
---|
484 | Please remove " + it->first.string()); |
---|
485 | } |
---|
486 | if (boost::filesystem::create_directories(it->first)) // function may not return true at all (bug?) |
---|
487 | { |
---|
488 | COUT(4) << "Created " << it->second << " directory" << std::endl; |
---|
489 | } |
---|
490 | } |
---|
491 | } |
---|
492 | } |
---|