xoreos  0.0.5
engine.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "src/common/util.h"
28 #include "src/common/configman.h"
29 
32 
33 #include "src/engines/engine.h"
34 
36 
37 namespace Engines {
38 
40 }
41 
43 }
44 
46  Aurora::Platform UNUSED(platform),
47  std::vector<Aurora::Language> &UNUSED(languages)) const {
48  return false;
49 }
50 
52  Aurora::Platform UNUSED(platform),
53  std::vector<Aurora::Language> &UNUSED(languagesText),
54  std::vector<Aurora::Language> &UNUSED(languagesVoice)) const {
55  return false;
56 }
57 
58 bool Engine::detectLanguages(std::vector<Aurora::Language> &languages) const {
59  return detectLanguages(_game, _target, _platform, languages);
60 }
61 
62 bool Engine::detectLanguages(std::vector<Aurora::Language> &languagesText,
63  std::vector<Aurora::Language> &languagesVoice) const {
64 
65  return detectLanguages(_game, _target, _platform, languagesText, languagesVoice);
66 }
67 
69  return false;
70 }
71 
73  Aurora::Language &UNUSED(languageVoice)) const {
74  return false;
75 }
76 
78  return false;
79 }
80 
81 void Engine::start(Aurora::GameID game, const Common::UString &target, Aurora::Platform platform) {
82  showFPS();
83 
84  _game = game;
85  _platform = platform;
86  _target = target;
87 
88  run();
89 }
90 
92  bool show = ConfigMan.getBool("showfps", false);
93 
94  if ( show && !_fps) {
95 
97  _fps->show();
98 
99  } else if (!show && _fps) {
100 
101  _fps.reset();
102 
103  }
104 }
105 
106 static bool hasLanguage(const std::vector<Aurora::Language> &langs, Aurora::Language lang) {
107  return std::find(langs.begin(), langs.end(), lang) != langs.end();
108 }
109 
110 static void fiddleLangChinese(const std::vector<Aurora::Language> &langs, Aurora::Language &lang) {
111  // If we're given a generic Chinese language, look if we have traditional or simplified Chinese
112  if (lang == Aurora::kLanguageChinese) {
117 
118  if (lang != Aurora::kLanguageChinese)
119  status("Substituting %s for %s",
120  LangMan.getLanguageName(lang).c_str(),
121  LangMan.getLanguageName(Aurora::kLanguageChinese).c_str());
122  }
123 
124  // Substitute simplified for traditional Chinese if necessary
129 
131  status("Substituting %s for %s",
132  LangMan.getLanguageName(Aurora::kLanguageChineseSimplified).c_str(),
133  LangMan.getLanguageName(Aurora::kLanguageChineseTraditional).c_str());
134  }
135 
136  // Substitute traditional for simplified Chinese if necessary
141 
143  status("Substituting %s for %s",
144  LangMan.getLanguageName(Aurora::kLanguageChineseTraditional).c_str(),
145  LangMan.getLanguageName(Aurora::kLanguageChineseSimplified).c_str());
146  }
147 }
148 
149 static bool resolveLangInvalid(const std::vector<Aurora::Language> &langs, Aurora::Language &lang,
150  const Common::UString &conf, const Common::UString &specifier,
151  bool find) {
152 
153  if (lang != Aurora::kLanguageInvalid)
154  return true;
155 
156  if (!find || langs.empty())
157  return false;
158 
159  if (!conf.empty())
160  warning("Failed to parse \"%s\" into a language", conf.c_str());
161 
162  status("Available languages:");
163  for (std::vector<Aurora::Language>::const_iterator l = langs.begin(); l != langs.end(); ++l)
164  status("- %s", LangMan.getLanguageName(*l).c_str());
165 
166  lang = langs.front();
167  status("Using the first available language (%s)%s",
168  LangMan.getLanguageName(lang).c_str(), specifier.c_str());
169 
170  return true;
171 }
172 
173 static bool resolveLangUnavailable(const std::vector<Aurora::Language> &langs, Aurora::Language &lang,
174  const Common::UString &specifier, bool find) {
175 
176  if (hasLanguage(langs, lang))
177  return true;
178 
179  if (!find || langs.empty())
180  return false;
181 
182  Aurora::Language oldLang = lang;
183 
184  warning("This game version does not come with %s language files%s",
185  LangMan.getLanguageName(oldLang).c_str(), specifier.c_str());
186 
187  status("Available languages:");
188  for (std::vector<Aurora::Language>::const_iterator l = langs.begin(); l != langs.end(); ++l)
189  status("- %s", LangMan.getLanguageName(*l).c_str());
190 
191  lang = langs.front();
192  status("Using the first available language (%s)%s",
193  LangMan.getLanguageName(lang).c_str(), specifier.c_str());
194 
195  return true;
196 }
197 
198 bool Engine::evaluateLanguage(bool find, Aurora::Language &language) const {
199  language = Aurora::kLanguageInvalid;
200 
201  std::vector<Aurora::Language> langs;
202  bool detected = detectLanguages(langs);
203  assert(detected);
204 
205  if (langs.empty())
206  return true;
207 
208  Common::UString confLang = ConfigMan.getString("lang", "");
209  Common::UString confLangText = ConfigMan.getString("langtext", "");
210  Common::UString confLangVoice = ConfigMan.getString("langvoice", "");
211 
212  if (confLangText.empty())
213  confLangText = confLang;
214  if (confLangVoice.empty())
215  confLangVoice = confLang;
216 
218  Aurora::Language langText = LangMan.parseLanguage(confLangText);
219  Aurora::Language langVoice = LangMan.parseLanguage(confLangVoice);
220 
221  if (langText != langVoice) {
222  if (confLangText.empty())
223  langText = langVoice;
224  if (confLangVoice.empty())
225  langVoice = langText;
226 
227  if (langText != langVoice) {
228  warning("Game does not support different languages for voice and text");
229 
230  langText = langs.front();
231  status("Using the first available language (%s)", LangMan.getLanguageName(langText).c_str());
232  }
233  }
234 
235  confLang = confLangText;
236  lang = langText;
237 
238  fiddleLangChinese(langs, lang);
239 
240  if (!resolveLangInvalid(langs, lang, confLang, "", find))
241  return false;
242  if (!resolveLangUnavailable(langs, lang, "", find))
243  return false;
244 
245  language = lang;
246 
247  return true;
248 }
249 
250 bool Engine::evaluateLanguage(bool find, Aurora::Language &languageText,
251  Aurora::Language &languageVoice) const {
252 
253  languageText = languageVoice = Aurora::kLanguageInvalid;
254 
255  std::vector<Aurora::Language> langsText, langsVoice;
256  bool detected = detectLanguages(langsText, langsVoice);
257  assert(detected);
258 
259  if (langsText.empty() || langsVoice.empty())
260  return true;
261 
262  Common::UString confLang = ConfigMan.getString("lang", "");
263  Common::UString confLangText = ConfigMan.getString("langtext", "");
264  Common::UString confLangVoice = ConfigMan.getString("langvoice", "");
265 
266  if (confLangText.empty())
267  confLangText = confLang;
268  if (confLangVoice.empty())
269  confLangVoice = confLang;
270 
271  Aurora::Language langText = LangMan.parseLanguage(confLangText);
272  Aurora::Language langVoice = LangMan.parseLanguage(confLangVoice);
273 
274  fiddleLangChinese(langsText , langText);
275  fiddleLangChinese(langsVoice, langVoice);
276 
277  if (!resolveLangInvalid(langsText , langText , confLangText , " for text" , find))
278  return false;
279  if (!resolveLangInvalid(langsVoice, langVoice, confLangVoice, " for voices", find))
280  return false;
281 
282  if (!resolveLangUnavailable(langsText , langText , " for text" , find))
283  return false;
284  if (!resolveLangUnavailable(langsVoice, langVoice, " for voices", find))
285  return false;
286 
287  languageText = langText;
288  languageVoice = langVoice;
289 
290  return true;
291 }
292 
293 } // End of namespace Engines
virtual bool getLanguage(Aurora::Language &language) const
Return the game&#39;s current language.
Definition: engine.cpp:68
Aurora::Platform _platform
Definition: engine.h:84
GameID
Definition: types.h:393
Generic engine interface.
A class holding an UTF-8 string.
Definition: ustring.h:48
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
The global config manager.
bool evaluateLanguage(bool find, Aurora::Language &language) const
Definition: engine.cpp:198
Language
Definition: language.h:46
Unknown game.
Definition: types.h:394
The Aurora font manager.
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
static bool resolveLangUnavailable(const std::vector< Aurora::Language > &langs, Aurora::Language &lang, const Common::UString &specifier, bool find)
Definition: engine.cpp:173
#define ConfigMan
Shortcut for accessing the config manager.
Definition: configman.h:176
#define UNUSED(x)
Definition: system.h:170
Utility templates and functions.
const char * kSystemFontMono
Identifier used for the monospaced system font.
Definition: fontman.cpp:41
An autonomous FPS display.
Definition: fps.h:37
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
virtual void run()=0
Run the game.
Generic Aurora engines (debug) console.
virtual ~Engine()
Definition: engine.cpp:42
void warning(const char *s,...)
Definition: util.cpp:33
static bool resolveLangInvalid(const std::vector< Aurora::Language > &langs, Aurora::Language &lang, const Common::UString &conf, const Common::UString &specifier, bool find)
Definition: engine.cpp:149
void start(Aurora::GameID game, const Common::UString &target, Aurora::Platform platform)
Definition: engine.cpp:81
virtual bool changeLanguage()
Change the game&#39;s current language.
Definition: engine.cpp:77
Common::UString _target
Definition: engine.h:85
static void fiddleLangChinese(const std::vector< Aurora::Language > &langs, Aurora::Language &lang)
Definition: engine.cpp:110
#define LangMan
Shortcut for accessing the language manager.
Definition: language.h:275
void showFPS()
Evaluate the FPS display setting and show/hide the FPS display.
Definition: engine.cpp:91
Pseudo value that means either traditional or simplified Chinese.
Definition: language.h:63
Aurora::GameID _game
Definition: engine.h:83
void status(const char *s,...)
Definition: util.cpp:52
A text object displaying the current FPS.
Common::ScopedPtr< Graphics::Aurora::FPS > _fps
Definition: engine.h:89
Unknown (must be last).
Definition: types.h:439
virtual bool detectLanguages(Aurora::GameID game, const Common::UString &target, Aurora::Platform platform, std::vector< Aurora::Language > &languages) const
Detect which languages this game instance supports.
Definition: engine.cpp:45
Platform
Definition: types.h:429
static bool hasLanguage(const std::vector< Aurora::Language > &langs, Aurora::Language lang)
Definition: engine.cpp:106
#define FontMan
Shortcut for accessing the font manager.
Definition: fontman.h:105