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 |
---|
34 | */ |
---|
35 | |
---|
36 | #include "Core.h" |
---|
37 | |
---|
38 | #include <cassert> |
---|
39 | #include <ctime> |
---|
40 | #include <fstream> |
---|
41 | #include <vector> |
---|
42 | |
---|
43 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
44 | # ifndef WIN32_LEAN_AND_MEAN |
---|
45 | # define WIN32_LEAN_AND_MEAN |
---|
46 | # endif |
---|
47 | # include <windows.h> |
---|
48 | # undef min |
---|
49 | # undef max |
---|
50 | #endif |
---|
51 | |
---|
52 | #include "util/Debug.h" |
---|
53 | #include "util/Exception.h" |
---|
54 | #include "PathConfig.h" |
---|
55 | #include "CommandLineParser.h" |
---|
56 | |
---|
57 | namespace orxonox |
---|
58 | { |
---|
59 | //! Static pointer to the singleton |
---|
60 | Core* Core::singletonPtr_s = 0; |
---|
61 | |
---|
62 | SetCommandLineArgument(settingsFile, "orxonox.ini").information("THE configuration file"); |
---|
63 | |
---|
64 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
65 | SetCommandLineArgument(limitToCPU, 1).information("Limits the program to one CPU/core (1, 2, 3, etc.). Default is the first core (faster than off)"); |
---|
66 | #endif |
---|
67 | |
---|
68 | Core::Core(const std::string& cmdLine) |
---|
69 | { |
---|
70 | // Set the hard coded fixed paths |
---|
71 | this->pathConfig_.reset(new PathConfig()); |
---|
72 | |
---|
73 | // Parse command line arguments |
---|
74 | CommandLineParser::parseCommandLine(cmdLine); |
---|
75 | |
---|
76 | // Set configurable paths like log, config and media |
---|
77 | this->pathConfig_->setConfigurablePaths(); |
---|
78 | |
---|
79 | // Set the correct log path. Before this call, /tmp (Unix) or %TEMP% (Windows) was used |
---|
80 | OutputHandler::getInstance().setLogPath(PathConfig::getLogPathString()); |
---|
81 | |
---|
82 | // Parse additional options file now that we know its path |
---|
83 | CommandLineParser::parseFile(); |
---|
84 | |
---|
85 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
86 | // limit the main thread to the first core so that QueryPerformanceCounter doesn't jump |
---|
87 | int limitToCPU = CommandLineParser::getValue("limitToCPU").toInt(); |
---|
88 | if (limitToCPU > 0) |
---|
89 | setThreadAffinity(static_cast<unsigned int>(limitToCPU)); |
---|
90 | #endif |
---|
91 | |
---|
92 | // Generate documentation instead of normal run? |
---|
93 | std::string docFilename = CommandLineParser::getValue("generateDoc").toString().toStdString(); |
---|
94 | if (!docFilename.empty()) |
---|
95 | { |
---|
96 | std::ofstream docFile(docFilename.c_str()); |
---|
97 | if (docFile.is_open()) |
---|
98 | { |
---|
99 | CommandLineParser::generateDoc(docFile); |
---|
100 | docFile.close(); |
---|
101 | } |
---|
102 | else |
---|
103 | COUT(0) << "Error: Could not open file for documentation writing" << endl; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | @brief |
---|
109 | All destruction code is handled by QScopedPointers |
---|
110 | */ |
---|
111 | Core::~Core() |
---|
112 | { |
---|
113 | } |
---|
114 | |
---|
115 | //! Function to collect the SetConfigValue-macro calls. |
---|
116 | void Core::setConfigValues() |
---|
117 | { |
---|
118 | #ifdef ORXONOX_RELEASE |
---|
119 | const unsigned int defaultLevelLogFile = 3; |
---|
120 | #else |
---|
121 | const unsigned int defaultLevelLogFile = 4; |
---|
122 | #endif |
---|
123 | /* |
---|
124 | SetConfigValueExternal(softDebugLevelLogFile_, "OutputHandler", "softDebugLevelLogFile", defaultLevelLogFile) |
---|
125 | .description("The maximum level of debug output shown in the log file"); |
---|
126 | OutputHandler::getInstance().setSoftDebugLevel(OutputHandler::logFileOutputListenerName_s, this->softDebugLevelLogFile_); |
---|
127 | |
---|
128 | SetConfigValue(language_, Language::getInstance().defaultLanguage_) |
---|
129 | .description("The language of the in game text") |
---|
130 | .callback(this, &Core::languageChanged); |
---|
131 | SetConfigValue(bInitRandomNumberGenerator_, true) |
---|
132 | .description("If true, all random actions are different each time you start the game") |
---|
133 | .callback(this, &Core::initRandomNumberGenerator); |
---|
134 | SetConfigValue(bStartIOConsole_, true) |
---|
135 | .description("Set to false if you don't want to use the IOConsole (for Lua debugging for instance)"); |
---|
136 | */ |
---|
137 | } |
---|
138 | |
---|
139 | void Core::initRandomNumberGenerator() |
---|
140 | { |
---|
141 | static bool bInitialized = false; |
---|
142 | if (!bInitialized && this->bInitRandomNumberGenerator_) |
---|
143 | { |
---|
144 | srand(static_cast<unsigned int>(time(0))); |
---|
145 | rand(); |
---|
146 | bInitialized = true; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | @note |
---|
152 | The code of this function has been copied and adjusted from OGRE, an open source graphics engine. |
---|
153 | (Object-oriented Graphics Rendering Engine) |
---|
154 | For the latest info, see http://www.ogre3d.org/ |
---|
155 | |
---|
156 | Copyright (c) 2000-2008 Torus Knot Software Ltd |
---|
157 | |
---|
158 | OGRE is licensed under the LGPL. For more info, see OGRE license. |
---|
159 | */ |
---|
160 | void Core::setThreadAffinity(int limitToCPU) |
---|
161 | { |
---|
162 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
163 | |
---|
164 | if (limitToCPU <= 0) |
---|
165 | return; |
---|
166 | |
---|
167 | unsigned int coreNr = limitToCPU - 1; |
---|
168 | // Get the current process core mask |
---|
169 | DWORD procMask; |
---|
170 | DWORD sysMask; |
---|
171 | # if _MSC_VER >= 1400 && defined (_M_X64) |
---|
172 | GetProcessAffinityMask(GetCurrentProcess(), (PDWORD_PTR)&procMask, (PDWORD_PTR)&sysMask); |
---|
173 | # else |
---|
174 | GetProcessAffinityMask(GetCurrentProcess(), &procMask, &sysMask); |
---|
175 | # endif |
---|
176 | |
---|
177 | // If procMask is 0, consider there is only one core available |
---|
178 | // (using 0 as procMask will cause an infinite loop below) |
---|
179 | if (procMask == 0) |
---|
180 | procMask = 1; |
---|
181 | |
---|
182 | // if the core specified with coreNr is not available, take the lowest one |
---|
183 | if (!(procMask & (1 << coreNr))) |
---|
184 | coreNr = 0; |
---|
185 | |
---|
186 | // Find the lowest core that this process uses and coreNr suggests |
---|
187 | DWORD threadMask = 1; |
---|
188 | while ((threadMask & procMask) == 0 || (threadMask < (1u << coreNr))) |
---|
189 | threadMask <<= 1; |
---|
190 | |
---|
191 | // Set affinity to the first core |
---|
192 | SetThreadAffinityMask(GetCurrentThread(), threadMask); |
---|
193 | #endif |
---|
194 | } |
---|
195 | } |
---|