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 | #include <OgreRenderWindow.h> |
---|
45 | |
---|
46 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
47 | # ifndef WIN32_LEAN_AND_MEAN |
---|
48 | # define WIN32_LEAN_AND_MEAN |
---|
49 | # endif |
---|
50 | # include <windows.h> |
---|
51 | # undef min |
---|
52 | # undef max |
---|
53 | #elif defined(ORXONOX_PLATFORM_APPLE) |
---|
54 | # include <sys/param.h> |
---|
55 | # include <mach-o/dyld.h> |
---|
56 | #else /* Linux */ |
---|
57 | # include <sys/types.h> |
---|
58 | # include <unistd.h> |
---|
59 | #endif |
---|
60 | |
---|
61 | #include "SpecialConfig.h" |
---|
62 | #include "util/Debug.h" |
---|
63 | #include "util/Exception.h" |
---|
64 | #include "util/SignalHandler.h" |
---|
65 | #include "Clock.h" |
---|
66 | #include "CommandExecutor.h" |
---|
67 | #include "CommandLine.h" |
---|
68 | #include "ConfigFileManager.h" |
---|
69 | #include "ConfigValueIncludes.h" |
---|
70 | #include "CoreIncludes.h" |
---|
71 | #include "DynLibManager.h" |
---|
72 | #include "Factory.h" |
---|
73 | #include "GameMode.h" |
---|
74 | #include "GraphicsManager.h" |
---|
75 | #include "GUIManager.h" |
---|
76 | #include "Identifier.h" |
---|
77 | #include "Language.h" |
---|
78 | #include "LuaBind.h" |
---|
79 | #include "Shell.h" |
---|
80 | #include "TclBind.h" |
---|
81 | #include "TclThreadManager.h" |
---|
82 | #include "input/InputManager.h" |
---|
83 | |
---|
84 | // Boost 1.36 has some issues with deprecated functions that have been omitted |
---|
85 | #if (BOOST_VERSION == 103600) |
---|
86 | # define BOOST_LEAF_FUNCTION filename |
---|
87 | #else |
---|
88 | # define BOOST_LEAF_FUNCTION leaf |
---|
89 | #endif |
---|
90 | |
---|
91 | namespace orxonox |
---|
92 | { |
---|
93 | //! Static pointer to the singleton |
---|
94 | Core* Core::singletonPtr_s = 0; |
---|
95 | |
---|
96 | SetCommandLineArgument(mediaPath, "").information("Path to the media/data files"); |
---|
97 | SetCommandLineOnlyArgument(writingPathSuffix, "").information("Additional subfolder for config and log files"); |
---|
98 | SetCommandLineArgument(settingsFile, "orxonox.ini").information("THE configuration file"); |
---|
99 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
100 | SetCommandLineArgument(limitToCPU, 0).information("Limits the program to one cpu/core (1, 2, 3, etc.). 0 turns it off (default)"); |
---|
101 | #endif |
---|
102 | |
---|
103 | /** |
---|
104 | @brief |
---|
105 | Helper class for the Core singleton: we cannot derive |
---|
106 | Core from OrxonoxClass because we need to handle the Identifier |
---|
107 | destruction in the Core destructor. |
---|
108 | */ |
---|
109 | class CoreConfiguration : public OrxonoxClass |
---|
110 | { |
---|
111 | public: |
---|
112 | CoreConfiguration() |
---|
113 | { |
---|
114 | } |
---|
115 | |
---|
116 | void initialise() |
---|
117 | { |
---|
118 | RegisterRootObject(CoreConfiguration); |
---|
119 | this->setConfigValues(); |
---|
120 | |
---|
121 | // Possible media path override by the command line |
---|
122 | if (!CommandLine::getArgument("mediaPath")->hasDefaultValue()) |
---|
123 | tsetMediaPath(CommandLine::getValue("mediaPath")); |
---|
124 | } |
---|
125 | |
---|
126 | /** |
---|
127 | @brief Function to collect the SetConfigValue-macro calls. |
---|
128 | */ |
---|
129 | void setConfigValues() |
---|
130 | { |
---|
131 | #ifdef NDEBUG |
---|
132 | const unsigned int defaultLevelConsole = 1; |
---|
133 | const unsigned int defaultLevelLogfile = 3; |
---|
134 | const unsigned int defaultLevelShell = 1; |
---|
135 | #else |
---|
136 | const unsigned int defaultLevelConsole = 3; |
---|
137 | const unsigned int defaultLevelLogfile = 4; |
---|
138 | const unsigned int defaultLevelShell = 3; |
---|
139 | #endif |
---|
140 | SetConfigValue(softDebugLevelConsole_, defaultLevelConsole) |
---|
141 | .description("The maximal level of debug output shown in the console") |
---|
142 | .callback(this, &CoreConfiguration::debugLevelChanged); |
---|
143 | SetConfigValue(softDebugLevelLogfile_, defaultLevelLogfile) |
---|
144 | .description("The maximal level of debug output shown in the logfile") |
---|
145 | .callback(this, &CoreConfiguration::debugLevelChanged); |
---|
146 | SetConfigValue(softDebugLevelShell_, defaultLevelShell) |
---|
147 | .description("The maximal level of debug output shown in the ingame shell") |
---|
148 | .callback(this, &CoreConfiguration::debugLevelChanged); |
---|
149 | |
---|
150 | SetConfigValue(language_, Language::getInstance().defaultLanguage_) |
---|
151 | .description("The language of the ingame text") |
---|
152 | .callback(this, &CoreConfiguration::languageChanged); |
---|
153 | SetConfigValue(bInitializeRandomNumberGenerator_, true) |
---|
154 | .description("If true, all random actions are different each time you start the game") |
---|
155 | .callback(this, &CoreConfiguration::initializeRandomNumberGenerator); |
---|
156 | |
---|
157 | // Only show this config value for development builds |
---|
158 | if (Core::isDevelopmentRun()) |
---|
159 | { |
---|
160 | SetConfigValue(mediaPathString_, mediaPath_.string()) |
---|
161 | .description("Relative path to the game data.") |
---|
162 | .callback(this, &CoreConfiguration::mediaPathChanged); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | @brief Callback function if the debug level has changed. |
---|
168 | */ |
---|
169 | void debugLevelChanged() |
---|
170 | { |
---|
171 | // softDebugLevel_ is the maximum of the 3 variables |
---|
172 | this->softDebugLevel_ = this->softDebugLevelConsole_; |
---|
173 | if (this->softDebugLevelLogfile_ > this->softDebugLevel_) |
---|
174 | this->softDebugLevel_ = this->softDebugLevelLogfile_; |
---|
175 | if (this->softDebugLevelShell_ > this->softDebugLevel_) |
---|
176 | this->softDebugLevel_ = this->softDebugLevelShell_; |
---|
177 | |
---|
178 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_All, this->softDebugLevel_); |
---|
179 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Console, this->softDebugLevelConsole_); |
---|
180 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Logfile, this->softDebugLevelLogfile_); |
---|
181 | OutputHandler::setSoftDebugLevel(OutputHandler::LD_Shell, this->softDebugLevelShell_); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | @brief Callback function if the language has changed. |
---|
186 | */ |
---|
187 | void languageChanged() |
---|
188 | { |
---|
189 | // Read the translation file after the language was configured |
---|
190 | Language::getInstance().readTranslatedLanguageFile(); |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | @brief |
---|
195 | Callback function if the media path has changed. |
---|
196 | */ |
---|
197 | void mediaPathChanged() |
---|
198 | { |
---|
199 | mediaPath_ = boost::filesystem::path(this->mediaPathString_); |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | @brief Sets the language in the config-file back to the default. |
---|
204 | */ |
---|
205 | void resetLanguage() |
---|
206 | { |
---|
207 | ResetConfigValue(language_); |
---|
208 | } |
---|
209 | |
---|
210 | /** |
---|
211 | @brief |
---|
212 | Temporary sets the media path |
---|
213 | @param path |
---|
214 | The new media path |
---|
215 | */ |
---|
216 | void tsetMediaPath(const std::string& path) |
---|
217 | { |
---|
218 | if (Core::isDevelopmentRun()) |
---|
219 | { |
---|
220 | ModifyConfigValue(mediaPathString_, tset, path); |
---|
221 | } |
---|
222 | else |
---|
223 | { |
---|
224 | // Manual 'config' value without the file entry |
---|
225 | mediaPathString_ = path; |
---|
226 | this->mediaPathChanged(); |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | void initializeRandomNumberGenerator() |
---|
231 | { |
---|
232 | static bool bInitialized = false; |
---|
233 | if (!bInitialized && this->bInitializeRandomNumberGenerator_) |
---|
234 | { |
---|
235 | srand(static_cast<unsigned int>(time(0))); |
---|
236 | rand(); |
---|
237 | bInitialized = true; |
---|
238 | } |
---|
239 | } |
---|
240 | |
---|
241 | int softDebugLevel_; //!< The debug level |
---|
242 | int softDebugLevelConsole_; //!< The debug level for the console |
---|
243 | int softDebugLevelLogfile_; //!< The debug level for the logfile |
---|
244 | int softDebugLevelShell_; //!< The debug level for the ingame shell |
---|
245 | std::string language_; //!< The language |
---|
246 | bool bInitializeRandomNumberGenerator_; //!< If true, srand(time(0)) is called |
---|
247 | std::string mediaPathString_; //!< Path to the data/media file folder as string |
---|
248 | |
---|
249 | //! Path to the parent directory of the ones above if program was installed with relativ pahts |
---|
250 | boost::filesystem::path rootPath_; |
---|
251 | boost::filesystem::path executablePath_; //!< Path to the executable |
---|
252 | boost::filesystem::path pluginPath_; //!< Path to the plugins |
---|
253 | boost::filesystem::path mediaPath_; //!< Path to the media file folder |
---|
254 | boost::filesystem::path configPath_; //!< Path to the config file folder |
---|
255 | boost::filesystem::path logPath_; //!< Path to the log file folder |
---|
256 | }; |
---|
257 | |
---|
258 | |
---|
259 | Core::Core(const std::string& cmdLine) |
---|
260 | // Cleanup guard for identifier destruction (incl. XMLPort, configValues, consoleCommands) |
---|
261 | : identifierDestroyer_(Identifier::destroyAllIdentifiers) |
---|
262 | // Cleanup guard for external console commands that don't belong to an Identifier |
---|
263 | , consoleCommandDestroyer_(CommandExecutor::destroyExternalCommands) |
---|
264 | , configuration_(new CoreConfiguration()) // Don't yet create config values! |
---|
265 | , bDevRun_(false) |
---|
266 | , bGraphicsLoaded_(false) |
---|
267 | { |
---|
268 | // Set the hard coded fixed paths |
---|
269 | this->setFixedPaths(); |
---|
270 | |
---|
271 | // Create a new dynamic library manager |
---|
272 | this->dynLibManager_.reset(new DynLibManager()); |
---|
273 | |
---|
274 | // Load plugins |
---|
275 | try |
---|
276 | { |
---|
277 | // We search for helper files with the following extension |
---|
278 | std::string pluginextension = ".plugin"; |
---|
279 | size_t pluginextensionlength = pluginextension.size(); |
---|
280 | |
---|
281 | // Search in the directory of our executable |
---|
282 | boost::filesystem::path searchpath = this->configuration_->pluginPath_; |
---|
283 | |
---|
284 | boost::filesystem::directory_iterator file(searchpath); |
---|
285 | boost::filesystem::directory_iterator end; |
---|
286 | |
---|
287 | // Iterate through all files |
---|
288 | while (file != end) |
---|
289 | { |
---|
290 | std::string filename = file->BOOST_LEAF_FUNCTION(); |
---|
291 | |
---|
292 | // Check if the file ends with the exension in question |
---|
293 | if (filename.size() > pluginextensionlength) |
---|
294 | { |
---|
295 | if (filename.substr(filename.size() - pluginextensionlength) == pluginextension) |
---|
296 | { |
---|
297 | // We've found a helper file - now load the library with the same name |
---|
298 | std::string library = filename.substr(0, filename.size() - pluginextensionlength); |
---|
299 | boost::filesystem::path librarypath = searchpath / library; |
---|
300 | |
---|
301 | DynLibManager::getInstance().load(librarypath.string()); |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | ++file; |
---|
306 | } |
---|
307 | } |
---|
308 | catch (const std::exception& e) |
---|
309 | { |
---|
310 | COUT(1) << "An error occurred while loading plugins: " << e.what() << std::endl; |
---|
311 | } |
---|
312 | catch (...) |
---|
313 | { |
---|
314 | COUT(1) << "An error occurred while loading plugins." << std::endl; |
---|
315 | } |
---|
316 | |
---|
317 | // Parse command line arguments AFTER the plugins have been loaded (static code!) |
---|
318 | CommandLine::parseCommandLine(cmdLine); |
---|
319 | |
---|
320 | // Set configurable paths like log, config and media |
---|
321 | this->setConfigurablePaths(); |
---|
322 | |
---|
323 | // create a signal handler (only active for linux) |
---|
324 | // This call is placed as soon as possible, but after the directories are set |
---|
325 | this->signalHandler_.reset(new SignalHandler()); |
---|
326 | this->signalHandler_->doCatch(configuration_->executablePath_.string(), Core::getLogPathString() + "orxonox_crash.log"); |
---|
327 | |
---|
328 | // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% was used |
---|
329 | OutputHandler::getOutStream().setLogPath(Core::getLogPathString()); |
---|
330 | |
---|
331 | // Parse additional options file now that we know its path |
---|
332 | CommandLine::parseFile(); |
---|
333 | |
---|
334 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
335 | // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump |
---|
336 | // do this after ogre has initialised. Somehow Ogre changes the settings again (not through |
---|
337 | // the timer though). |
---|
338 | int limitToCPU = CommandLine::getValue("limitToCPU"); |
---|
339 | if (limitToCPU > 0) |
---|
340 | setThreadAffinity(static_cast<unsigned int>(limitToCPU)); |
---|
341 | #endif |
---|
342 | |
---|
343 | // Manage ini files and set the default settings file (usually orxonox.ini) |
---|
344 | this->configFileManager_.reset(new ConfigFileManager()); |
---|
345 | this->configFileManager_->setFilename(ConfigFileType::Settings, |
---|
346 | CommandLine::getValue("settingsFile").getString()); |
---|
347 | |
---|
348 | // Required as well for the config values |
---|
349 | this->languageInstance_.reset(new Language()); |
---|
350 | |
---|
351 | // Do this soon after the ConfigFileManager has been created to open up the |
---|
352 | // possibility to configure everything below here |
---|
353 | this->configuration_->initialise(); |
---|
354 | |
---|
355 | // Create the lua interface |
---|
356 | this->luaBind_.reset(new LuaBind()); |
---|
357 | |
---|
358 | // initialise Tcl |
---|
359 | this->tclBind_.reset(new TclBind(Core::getMediaPathString())); |
---|
360 | this->tclThreadManager_.reset(new TclThreadManager(tclBind_->getTclInterpreter())); |
---|
361 | |
---|
362 | // create a shell |
---|
363 | this->shell_.reset(new Shell()); |
---|
364 | |
---|
365 | // creates the class hierarchy for all classes with factories |
---|
366 | Factory::createClassHierarchy(); |
---|
367 | } |
---|
368 | |
---|
369 | /** |
---|
370 | @brief |
---|
371 | All destruction code is handled by scoped_ptrs and SimpleScopeGuards. |
---|
372 | */ |
---|
373 | Core::~Core() |
---|
374 | { |
---|
375 | } |
---|
376 | |
---|
377 | void Core::loadGraphics() |
---|
378 | { |
---|
379 | if (bGraphicsLoaded_) |
---|
380 | return; |
---|
381 | |
---|
382 | // Load OGRE including the render window |
---|
383 | scoped_ptr<GraphicsManager> graphicsManager(new GraphicsManager()); |
---|
384 | |
---|
385 | // The render window width and height are used to set up the mouse movement. |
---|
386 | size_t windowHnd = 0; |
---|
387 | graphicsManager->getRenderWindow()->getCustomAttribute("WINDOW", &windowHnd); |
---|
388 | |
---|
389 | // Calls the InputManager which sets up the input devices. |
---|
390 | scoped_ptr<InputManager> inputManager(new InputManager(windowHnd)); |
---|
391 | |
---|
392 | // load the CEGUI interface |
---|
393 | guiManager_.reset(new GUIManager(graphicsManager->getRenderWindow())); |
---|
394 | |
---|
395 | // Dismiss scoped pointers |
---|
396 | graphicsManager_.swap(graphicsManager); |
---|
397 | inputManager_.swap(inputManager); |
---|
398 | |
---|
399 | bGraphicsLoaded_ = true; |
---|
400 | } |
---|
401 | |
---|
402 | void Core::unloadGraphics() |
---|
403 | { |
---|
404 | if (!bGraphicsLoaded_) |
---|
405 | return; |
---|
406 | |
---|
407 | this->guiManager_.reset();; |
---|
408 | this->inputManager_.reset();; |
---|
409 | this->graphicsManager_.reset(); |
---|
410 | |
---|
411 | bGraphicsLoaded_ = false; |
---|
412 | } |
---|
413 | |
---|
414 | /** |
---|
415 | @brief Returns the softDebugLevel for the given device (returns a default-value if the class is right about to be created). |
---|
416 | @param device The device |
---|
417 | @return The softDebugLevel |
---|
418 | */ |
---|
419 | /*static*/ int Core::getSoftDebugLevel(OutputHandler::OutputDevice device) |
---|
420 | { |
---|
421 | switch (device) |
---|
422 | { |
---|
423 | case OutputHandler::LD_All: |
---|
424 | return Core::getInstance().configuration_->softDebugLevel_; |
---|
425 | case OutputHandler::LD_Console: |
---|
426 | return Core::getInstance().configuration_->softDebugLevelConsole_; |
---|
427 | case OutputHandler::LD_Logfile: |
---|
428 | return Core::getInstance().configuration_->softDebugLevelLogfile_; |
---|
429 | case OutputHandler::LD_Shell: |
---|
430 | return Core::getInstance().configuration_->softDebugLevelShell_; |
---|
431 | default: |
---|
432 | assert(0); |
---|
433 | return 2; |
---|
434 | } |
---|
435 | } |
---|
436 | |
---|
437 | /** |
---|
438 | @brief Sets the softDebugLevel for the given device. Please use this only temporary and restore the value afterwards, as it overrides the configured value. |
---|
439 | @param device The device |
---|
440 | @param level The level |
---|
441 | */ |
---|
442 | /*static*/ void Core::setSoftDebugLevel(OutputHandler::OutputDevice device, int level) |
---|
443 | { |
---|
444 | if (device == OutputHandler::LD_All) |
---|
445 | Core::getInstance().configuration_->softDebugLevel_ = level; |
---|
446 | else if (device == OutputHandler::LD_Console) |
---|
447 | Core::getInstance().configuration_->softDebugLevelConsole_ = level; |
---|
448 | else if (device == OutputHandler::LD_Logfile) |
---|
449 | Core::getInstance().configuration_->softDebugLevelLogfile_ = level; |
---|
450 | else if (device == OutputHandler::LD_Shell) |
---|
451 | Core::getInstance().configuration_->softDebugLevelShell_ = level; |
---|
452 | |
---|
453 | OutputHandler::setSoftDebugLevel(device, level); |
---|
454 | } |
---|
455 | |
---|
456 | /** |
---|
457 | @brief Returns the configured language. |
---|
458 | */ |
---|
459 | /*static*/ const std::string& Core::getLanguage() |
---|
460 | { |
---|
461 | return Core::getInstance().configuration_->language_; |
---|
462 | } |
---|
463 | |
---|
464 | /** |
---|
465 | @brief Sets the language in the config-file back to the default. |
---|
466 | */ |
---|
467 | /*static*/ void Core::resetLanguage() |
---|
468 | { |
---|
469 | Core::getInstance().configuration_->resetLanguage(); |
---|
470 | } |
---|
471 | |
---|
472 | /*static*/ void Core::tsetMediaPath(const std::string& path) |
---|
473 | { |
---|
474 | getInstance().configuration_->tsetMediaPath(path); |
---|
475 | } |
---|
476 | |
---|
477 | /*static*/ const boost::filesystem::path& Core::getMediaPath() |
---|
478 | { |
---|
479 | return getInstance().configuration_->mediaPath_; |
---|
480 | } |
---|
481 | /*static*/ std::string Core::getMediaPathString() |
---|
482 | { |
---|
483 | return getInstance().configuration_->mediaPath_.string() + '/'; |
---|
484 | } |
---|
485 | |
---|
486 | /*static*/ const boost::filesystem::path& Core::getConfigPath() |
---|
487 | { |
---|
488 | return getInstance().configuration_->configPath_; |
---|
489 | } |
---|
490 | /*static*/ std::string Core::getConfigPathString() |
---|
491 | { |
---|
492 | return getInstance().configuration_->configPath_.string() + '/'; |
---|
493 | } |
---|
494 | |
---|
495 | /*static*/ const boost::filesystem::path& Core::getLogPath() |
---|
496 | { |
---|
497 | return getInstance().configuration_->logPath_; |
---|
498 | } |
---|
499 | /*static*/ std::string Core::getLogPathString() |
---|
500 | { |
---|
501 | return getInstance().configuration_->logPath_.string() + '/'; |
---|
502 | } |
---|
503 | |
---|
504 | /*static*/ const boost::filesystem::path& Core::getRootPath() |
---|
505 | { |
---|
506 | return getInstance().configuration_->rootPath_; |
---|
507 | } |
---|
508 | /*static*/ std::string Core::getRootPathString() |
---|
509 | { |
---|
510 | return getInstance().configuration_->rootPath_.string() + '/'; |
---|
511 | } |
---|
512 | |
---|
513 | /** |
---|
514 | @note |
---|
515 | The code of this function has been copied and adjusted from OGRE, an open source graphics engine. |
---|
516 | (Object-oriented Graphics Rendering Engine) |
---|
517 | For the latest info, see http://www.ogre3d.org/ |
---|
518 | |
---|
519 | Copyright (c) 2000-2008 Torus Knot Software Ltd |
---|
520 | |
---|
521 | OGRE is licensed under the LGPL. For more info, see OGRE license. |
---|
522 | */ |
---|
523 | void Core::setThreadAffinity(int limitToCPU) |
---|
524 | { |
---|
525 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
526 | |
---|
527 | if (limitToCPU <= 0) |
---|
528 | return; |
---|
529 | |
---|
530 | unsigned int coreNr = limitToCPU - 1; |
---|
531 | // Get the current process core mask |
---|
532 | DWORD procMask; |
---|
533 | DWORD sysMask; |
---|
534 | # if _MSC_VER >= 1400 && defined (_M_X64) |
---|
535 | GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); |
---|
536 | # else |
---|
537 | GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); |
---|
538 | # endif |
---|
539 | |
---|
540 | // If procMask is 0, consider there is only one core available |
---|
541 | // (using 0 as procMask will cause an infinite loop below) |
---|
542 | if (procMask == 0) |
---|
543 | procMask = 1; |
---|
544 | |
---|
545 | // if the core specified with coreNr is not available, take the lowest one |
---|
546 | if (!(procMask & (1 << coreNr))) |
---|
547 | coreNr = 0; |
---|
548 | |
---|
549 | // Find the lowest core that this process uses and coreNr suggests |
---|
550 | DWORD threadMask = 1; |
---|
551 | while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr))) |
---|
552 | threadMask <<= 1; |
---|
553 | |
---|
554 | // Set affinity to the first core |
---|
555 | SetThreadAffinityMask(GetCurrentThread(), threadMask); |
---|
556 | #endif |
---|
557 | } |
---|
558 | |
---|
559 | /** |
---|
560 | @brief |
---|
561 | Retrievs the executable path and sets all hard coded fixed path (currently only plugin path) |
---|
562 | Also checks for "orxonox_dev_build.keep_me" in the executable diretory. |
---|
563 | If found it means that this is not an installed run, hence we |
---|
564 | don't write the logs and config files to ~/.orxonox |
---|
565 | @throw |
---|
566 | GeneralException |
---|
567 | */ |
---|
568 | void Core::setFixedPaths() |
---|
569 | { |
---|
570 | ////////////////////////// |
---|
571 | // FIND EXECUTABLE PATH // |
---|
572 | ////////////////////////// |
---|
573 | |
---|
574 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
575 | // get executable module |
---|
576 | TCHAR buffer[1024]; |
---|
577 | if (GetModuleFileName(NULL, buffer, 1024) == 0) |
---|
578 | ThrowException(General, "Could not retrieve executable path."); |
---|
579 | |
---|
580 | #elif defined(ORXONOX_PLATFORM_APPLE) |
---|
581 | char buffer[1024]; |
---|
582 | unsigned long path_len = 1023; |
---|
583 | if (_NSGetExecutablePath(buffer, &path_len)) |
---|
584 | ThrowException(General, "Could not retrieve executable path."); |
---|
585 | |
---|
586 | #else /* Linux */ |
---|
587 | /* written by Nicolai Haehnle <prefect_@gmx.net> */ |
---|
588 | |
---|
589 | /* Get our PID and build the name of the link in /proc */ |
---|
590 | char linkname[64]; /* /proc/<pid>/exe */ |
---|
591 | if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", getpid()) < 0) |
---|
592 | { |
---|
593 | /* This should only happen on large word systems. I'm not sure |
---|
594 | what the proper response is here. |
---|
595 | Since it really is an assert-like condition, aborting the |
---|
596 | program seems to be in order. */ |
---|
597 | assert(false); |
---|
598 | } |
---|
599 | |
---|
600 | /* Now read the symbolic link */ |
---|
601 | char buffer[1024]; |
---|
602 | int ret; |
---|
603 | ret = readlink(linkname, buffer, 1024); |
---|
604 | /* In case of an error, leave the handling up to the caller */ |
---|
605 | if (ret == -1) |
---|
606 | ThrowException(General, "Could not retrieve executable path."); |
---|
607 | |
---|
608 | /* Ensure proper NUL termination */ |
---|
609 | buffer[ret] = 0; |
---|
610 | #endif |
---|
611 | |
---|
612 | configuration_->executablePath_ = boost::filesystem::path(buffer); |
---|
613 | #ifndef ORXONOX_PLATFORM_APPLE |
---|
614 | configuration_->executablePath_ = configuration_->executablePath_.branch_path(); // remove executable name |
---|
615 | #endif |
---|
616 | |
---|
617 | ///////////////////// |
---|
618 | // SET PLUGIN PATH // |
---|
619 | ///////////////////// |
---|
620 | |
---|
621 | if (boost::filesystem::exists(configuration_->executablePath_ / "orxonox_dev_build.keep_me")) |
---|
622 | { |
---|
623 | COUT(1) << "Running from the build tree." << std::endl; |
---|
624 | Core::bDevRun_ = true; |
---|
625 | configuration_->pluginPath_ = ORXONOX_PLUGIN_DEV_PATH; |
---|
626 | } |
---|
627 | else |
---|
628 | { |
---|
629 | |
---|
630 | #ifdef INSTALL_COPYABLE // --> relative paths |
---|
631 | |
---|
632 | // Also set the root path |
---|
633 | boost::filesystem::path relativeExecutablePath(ORXONOX_RUNTIME_INSTALL_PATH); |
---|
634 | configuration_->rootPath_ = configuration_->executablePath_; |
---|
635 | while (!boost::filesystem::equivalent(configuration_->rootPath_ / relativeExecutablePath, configuration_->executablePath_) |
---|
636 | && !configuration_->rootPath_.empty()) |
---|
637 | configuration_->rootPath_ = configuration_->rootPath_.branch_path(); |
---|
638 | if (configuration_->rootPath_.empty()) |
---|
639 | ThrowException(General, "Could not derive a root directory. Might the binary installation directory contain '..' when taken relative to the installation prefix path?"); |
---|
640 | |
---|
641 | // Plugin path is fixed as well |
---|
642 | configuration_->pluginPath_ = configuration_->rootPath_ / ORXONOX_PLUGIN_INSTALL_PATH; |
---|
643 | |
---|
644 | #else |
---|
645 | |
---|
646 | // There is no root path, so don't set it at all |
---|
647 | // Plugin path is fixed as well |
---|
648 | configuration_->pluginPath_ = ORXONOX_PLUGIN_INSTALL_PATH; |
---|
649 | |
---|
650 | #endif |
---|
651 | } |
---|
652 | } |
---|
653 | |
---|
654 | /** |
---|
655 | @brief |
---|
656 | Sets config, log and media path and creates folders if necessary. |
---|
657 | @throws |
---|
658 | GeneralException |
---|
659 | */ |
---|
660 | void Core::setConfigurablePaths() |
---|
661 | { |
---|
662 | if (Core::isDevelopmentRun()) |
---|
663 | { |
---|
664 | configuration_->mediaPath_ = ORXONOX_MEDIA_DEV_PATH; |
---|
665 | configuration_->configPath_ = ORXONOX_CONFIG_DEV_PATH; |
---|
666 | configuration_->logPath_ = ORXONOX_LOG_DEV_PATH; |
---|
667 | } |
---|
668 | else |
---|
669 | { |
---|
670 | |
---|
671 | #ifdef INSTALL_COPYABLE // --> relative paths |
---|
672 | |
---|
673 | // Using paths relative to the install prefix, complete them |
---|
674 | configuration_->mediaPath_ = configuration_->rootPath_ / ORXONOX_MEDIA_INSTALL_PATH; |
---|
675 | configuration_->configPath_ = configuration_->rootPath_ / ORXONOX_CONFIG_INSTALL_PATH; |
---|
676 | configuration_->logPath_ = configuration_->rootPath_ / ORXONOX_LOG_INSTALL_PATH; |
---|
677 | |
---|
678 | #else |
---|
679 | |
---|
680 | configuration_->mediaPath_ = ORXONOX_MEDIA_INSTALL_PATH; |
---|
681 | |
---|
682 | // Get user directory |
---|
683 | # ifdef ORXONOX_PLATFORM_UNIX /* Apple? */ |
---|
684 | char* userDataPathPtr(getenv("HOME")); |
---|
685 | # else |
---|
686 | char* userDataPathPtr(getenv("APPDATA")); |
---|
687 | # endif |
---|
688 | if (userDataPathPtr == NULL) |
---|
689 | ThrowException(General, "Could not retrieve user data path."); |
---|
690 | boost::filesystem::path userDataPath(userDataPathPtr); |
---|
691 | userDataPath /= ".orxonox"; |
---|
692 | |
---|
693 | configuration_->configPath_ = userDataPath / ORXONOX_CONFIG_INSTALL_PATH; |
---|
694 | configuration_->logPath_ = userDataPath / ORXONOX_LOG_INSTALL_PATH; |
---|
695 | |
---|
696 | #endif |
---|
697 | |
---|
698 | } |
---|
699 | |
---|
700 | // Option to put all the config and log files in a separate folder |
---|
701 | if (!CommandLine::getArgument("writingPathSuffix")->hasDefaultValue()) |
---|
702 | { |
---|
703 | std::string directory(CommandLine::getValue("writingPathSuffix").getString()); |
---|
704 | configuration_->configPath_ = configuration_->configPath_ / directory; |
---|
705 | configuration_->logPath_ = configuration_->logPath_ / directory; |
---|
706 | } |
---|
707 | |
---|
708 | // Create directories to avoid problems when opening files in non existent folders. |
---|
709 | std::vector<std::pair<boost::filesystem::path, std::string> > directories; |
---|
710 | directories.push_back(std::make_pair(boost::filesystem::path(configuration_->configPath_), "config")); |
---|
711 | directories.push_back(std::make_pair(boost::filesystem::path(configuration_->logPath_), "log")); |
---|
712 | |
---|
713 | for (std::vector<std::pair<boost::filesystem::path, std::string> >::iterator it = directories.begin(); |
---|
714 | it != directories.end(); ++it) |
---|
715 | { |
---|
716 | if (boost::filesystem::exists(it->first) && !boost::filesystem::is_directory(it->first)) |
---|
717 | { |
---|
718 | ThrowException(General, std::string("The ") + it->second + " directory has been preoccupied by a file! \ |
---|
719 | Please remove " + it->first.string()); |
---|
720 | } |
---|
721 | if (boost::filesystem::create_directories(it->first)) // function may not return true at all (bug?) |
---|
722 | { |
---|
723 | COUT(4) << "Created " << it->second << " directory" << std::endl; |
---|
724 | } |
---|
725 | } |
---|
726 | } |
---|
727 | |
---|
728 | bool Core::preUpdate(const Clock& time) throw() |
---|
729 | { |
---|
730 | std::string exceptionMessage; |
---|
731 | try |
---|
732 | { |
---|
733 | if (this->bGraphicsLoaded_) |
---|
734 | { |
---|
735 | // process input events |
---|
736 | this->inputManager_->update(time); |
---|
737 | // process gui events |
---|
738 | this->guiManager_->update(time); |
---|
739 | } |
---|
740 | // process thread commands |
---|
741 | this->tclThreadManager_->update(time); |
---|
742 | } |
---|
743 | catch (const std::exception& ex) |
---|
744 | { exceptionMessage = ex.what(); } |
---|
745 | catch (...) |
---|
746 | { exceptionMessage = "Unknown exception"; } |
---|
747 | if (!exceptionMessage.empty()) |
---|
748 | { |
---|
749 | COUT(0) << "An exception occurred in the Core preUpdate: " << exceptionMessage << std::endl; |
---|
750 | COUT(0) << "This should really never happen! Closing the program." << std::endl; |
---|
751 | return false; |
---|
752 | } |
---|
753 | return true; |
---|
754 | } |
---|
755 | |
---|
756 | bool Core::postUpdate(const Clock& time) throw() |
---|
757 | { |
---|
758 | std::string exceptionMessage; |
---|
759 | try |
---|
760 | { |
---|
761 | if (this->bGraphicsLoaded_) |
---|
762 | { |
---|
763 | // Render (doesn't throw) |
---|
764 | this->graphicsManager_->update(time); |
---|
765 | } |
---|
766 | } |
---|
767 | catch (const std::exception& ex) |
---|
768 | { exceptionMessage = ex.what(); } |
---|
769 | catch (...) |
---|
770 | { exceptionMessage = "Unknown exception"; } |
---|
771 | if (!exceptionMessage.empty()) |
---|
772 | { |
---|
773 | COUT(0) << "An exception occurred in the Core postUpdate: " << exceptionMessage << std::endl; |
---|
774 | COUT(0) << "This should really never happen! Closing the program." << std::endl; |
---|
775 | return false; |
---|
776 | } |
---|
777 | return true; |
---|
778 | } |
---|
779 | } |
---|