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