xoreos  0.0.5
enginemanager.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/scopedptr.h"
28 #include "src/common/util.h"
29 #include "src/common/error.h"
30 #include "src/common/ustring.h"
31 #include "src/common/readfile.h"
32 #include "src/common/filelist.h"
33 #include "src/common/filepath.h"
34 #include "src/common/debugman.h"
35 #include "src/common/configman.h"
36 
37 #include "src/aurora/types.h"
38 #include "src/aurora/util.h"
39 #include "src/aurora/language.h"
40 #include "src/aurora/resman.h"
41 #include "src/aurora/talkman.h"
42 #include "src/aurora/2dareg.h"
43 
44 #include "src/graphics/graphics.h"
45 
49 
52 
53 #include "src/events/events.h"
54 #include "src/events/requests.h"
55 
58 
61 
62 #include "src/engines/engine.h"
63 
65 
66 namespace Engines {
67 
69 }
70 
72 }
73 
74 
76 public:
77  GameInstanceEngine(const Common::UString &target);
79 
80  Common::UString getGameName(bool platform) const;
81 
83  bool probe(const std::list<const EngineProbe *> &probes);
85  void reset();
86 
88  void run();
89 
91  void listLanguages();
92 
93 private:
95 
98 
99  bool probe(const Common::FileList &rootFiles, const std::list<const EngineProbe *> &probes);
100  bool probe(Common::SeekableReadStream &stream, const std::list<const EngineProbe *> &probes);
101 
102  void createEngine();
103  void destroyEngine();
104 };
105 
106 GameInstanceEngine::GameInstanceEngine(const Common::UString &target) : _target(target), _probe(0) {
107 }
108 
110 }
111 
113  destroyEngine();
114  _probe = 0;
115 }
116 
117 bool GameInstanceEngine::probe(const std::list<const EngineProbe *> &probes) {
119  // Try to probe from that directory
120 
121  Common::FileList rootFiles;
122 
123  if (!rootFiles.addDirectory(_target))
124  // Fatal: can't read the directory
125  return false;
126 
127  return probe(rootFiles, probes);
128  }
129 
131  // Try to probe from that file
132 
133  Common::ReadFile file;
134  if (file.open(_target))
135  return probe(file, probes);
136  }
137 
138  return false;
139 }
140 
142  const std::list<const EngineProbe *> &probes) {
143 
144  // Try to find the first engine able to handle the directory's data
145  for (std::list<const EngineProbe *>::const_iterator p = probes.begin(); p != probes.end(); ++p) {
146  if ((*p)->probe(_target, rootFiles)) {
147  _probe = *p;
148  return true;
149  }
150  }
151 
152  return false;
153 }
154 
156  const std::list<const EngineProbe *> &probes) {
157 
158  // Try to find the first engine able to handle the directory's data
159  for (std::list<const EngineProbe *>::const_iterator p = probes.begin(); p != probes.end(); ++p) {
160  if ((*p)->probe(stream)) {
161  _probe = *p;
162  return true;
163  }
164  }
165 
166  return false;
167 }
168 
170  if (!_probe)
171  return "";
172 
173  Common::UString gameName = _probe->getGameName();
174  if (platform)
175  gameName += " (" + Aurora::getPlatformDescription(_probe->getPlatform()) + ")";
176 
177  return gameName;
178 }
179 
181  if (!_probe)
182  throw Common::Exception("GameInstanceEngine::createEngine(): No game probed");
183 
184  destroyEngine();
185  _engine.reset(_probe->createEngine());
186 }
187 
189  _engine.reset();
190 }
191 
193  createEngine();
194 
195  std::vector<Aurora::Language> langs;
196  if (_engine->detectLanguages(_probe->getGameID(), _target, _probe->getPlatform(), langs)) {
197  if (!langs.empty()) {
198  info("Available languages:");
199  for (std::vector<Aurora::Language>::iterator l = langs.begin(); l != langs.end(); ++l)
200  info("- %s", LangMan.getLanguageName(*l).c_str());
201  }
202  }
203 
204  std::vector<Aurora::Language> langsT, langsV;
205  if (_engine->detectLanguages(_probe->getGameID(), _target, _probe->getPlatform(), langsT, langsV)) {
206  if (!langsT.empty()) {
207  info("Available text languages:");
208  for (std::vector<Aurora::Language>::iterator l = langsT.begin(); l != langsT.end(); ++l)
209  info("- %s", LangMan.getLanguageName(*l).c_str());
210  }
211 
212  if (!langsV.empty()) {
213  info("Available voice languages:");
214  for (std::vector<Aurora::Language>::iterator l = langsV.begin(); l != langsV.end(); ++l)
215  info("- %s", LangMan.getLanguageName(*l).c_str());
216  }
217  }
218 
219  destroyEngine();
220 }
221 
223  createEngine();
224 
226 
227  destroyEngine();
228 }
229 
230 
232  const std::list<const EngineProbe *> &probes) const {
233 
235  if (game->probe(probes))
236  return game.release();
237 
238  return 0;
239 }
240 
242  GameInstanceEngine *gameEngine = dynamic_cast<GameInstanceEngine *>(&game);
243  assert(gameEngine);
244 
245  gameEngine->listLanguages();
246 }
247 
248 void EngineManager::run(GameInstance &game) const {
249  GameInstanceEngine *gameEngine = dynamic_cast<GameInstanceEngine *>(&game);
250  assert(gameEngine);
251 
252  gameEngine->run();
253 
254  GfxMan.lockFrame();
255  EventMan.requestQuit();
256  GfxMan.unlockFrame();
257 
258  cleanup();
259 }
260 
262  try {
264 
265  RequestMan.sync();
266 
267  FontMan.clear();
268  CursorMan.clear();
269 
270  MaterialMan.deinit();
271  SurfaceMan.deinit();
272 
273  TextureMan.clear();
274 
275  TokenMan.clear();
276 
277  LangMan.clear();
278  TalkMan.clear();
279  TwoDAReg.clear();
280  ResMan.clear();
281 
282  ConfigMan.setGame();
283 
284  } catch (...) {
285  }
286 }
287 
288 } // End of namespace Engines
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
Inter-thread request events.
const EngineProbe * _probe
The global graphics manager.
#define TalkMan
Shortcut for accessing the talk manager.
Definition: talkman.h:111
Generic engine interface.
virtual const Common::UString & getGameName() const =0
Return a string of the full game name.
A class holding an UTF-8 string.
Definition: ustring.h:48
#define TextureMan
Shortcut for accessing the texture manager.
Definition: textureman.h:127
The global config manager.
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Definition: scopedptr.h:103
#define SurfaceMan
Shortcut for accessing the shader manager.
Definition: surfaceman.h:74
The Aurora texture manager.
virtual Aurora::GameID getGameID() const =0
Get the GameID that the probe is able to detect.
static bool isDirectory(const UString &p)
Does specified path exist and is it a directory?
Definition: filepath.cpp:56
void reset()
Reset the GameInstance to a pre-probe state.
A simple streaming file reading class.
Definition: readfile.h:40
Utility functions to handle files used in BioWare&#39;s Aurora engine.
virtual Aurora::Platform getPlatform() const =0
Get the Platform that the probe is able to detect.
GameInstanceEngine(const Common::UString &target)
A probe that checks if an engine can handle game data found in a specific directory and creates an in...
The Aurora font manager.
A simple scoped smart pointer template.
The global engine manager.
Definition: enginemanager.h:55
Basic exceptions to throw.
The debug manager, managing debug channels.
bool open(const UString &fileName)
Try to open the file with the given fileName.
Definition: readfile.cpp:61
#define ConfigMan
Shortcut for accessing the config manager.
Definition: configman.h:176
static bool isRegularFile(const UString &p)
Does specified path exist and is it a regular file?
Definition: filepath.cpp:52
Utility templates and functions.
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
void unregisterModelLoader()
Definition: model.cpp:41
bool probe(const std::list< const EngineProbe *> &probes)
Find an engine capable of running the game found in the GameInstance&#39;s target.
The global events manager.
void info(const char *s,...)
Definition: util.cpp:69
void run(GameInstance &game) const
Run this game instance.
Types and functions related to language.
The global shader material manager.
The global shader surface manager.
#define TwoDAReg
Shortcut for accessing the 2da registry.
Definition: 2dareg.h:101
#define MaterialMan
Shortcut for accessing the shader material manager.
Definition: materialman.h:74
StackException Exception
Definition: error.h:59
The global 2DA registry.
A scoped plain pointer, allowing pointer-y access and normal deletion.
Definition: scopedptr.h:120
void run()
Run the probed game in the GameInstance&#39;s target.
void listLanguages()
List all available languages supported by this GameInstance&#39;s target.
#define CursorMan
Shortcut for accessing the cursor manager.
Definition: cursorman.h:129
#define EventMan
Shortcut for accessing the events manager.
Definition: events.h:210
The Aurora cursor manager.
Implementing the stream reading interfaces for files.
Unicode string handling.
Basic type definitions to handle files used in BioWare&#39;s Aurora engine.
A probe able to detect one specific game.
Definition: engineprobe.h:44
Common::UString getGameName(bool platform) const
#define LangMan
Shortcut for accessing the language manager.
Definition: language.h:275
Common::ScopedPtr< Engine > _engine
The global talk manager for Aurora strings.
A list of files.
Definition: filelist.h:35
GameInstance * probeGame(const Common::UString &target, const std::list< const EngineProbe *> &probes) const
Return a game instance capable of running the game in this directory.
#define RequestMan
Shortcut for accessing the request manager.
Definition: requests.h:127
bool addDirectory(const UString &directory, int recurseDepth=0)
Add a directory to the list.
Definition: filelist.cpp:102
A list of files.
Common::UString getPlatformDescription(Platform platform)
Return the human readable string of a Platform.
Definition: util.cpp:430
The global engine manager, omniscient about all engines.
Manager for tokens in Aurora engines text strings.
Interface for a seekable & readable data stream.
Definition: readstream.h:265
#define TokenMan
Shortcut for accessing the token manager.
Definition: tokenman.h:63
#define GfxMan
Shortcut for accessing the graphics manager.
Definition: graphics.h:299
The global resource manager for Aurora resources.
Utility class for manipulating file paths.
void listLanguages(GameInstance &game) const
List all available languages supported by this GameInstance&#39;s target.
#define FontMan
Shortcut for accessing the font manager.
Definition: fontman.h:105
virtual Engine * createEngine() const =0
Create the respective engine for the GameID.
Generic Aurora engines model functions.