xoreos  0.0.5
console.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 <algorithm>
26 
27 #include <boost/bind.hpp>
28 
29 #include "src/common/ustring.h"
30 #include "src/common/util.h"
31 #include "src/common/filepath.h"
32 #include "src/common/filelist.h"
33 #include "src/common/configman.h"
34 
35 #include "src/aurora/resman.h"
36 
38 
40 
47 
48 namespace Engines {
49 
50 namespace Witcher {
51 
53  ::Engines::Console(engine, Graphics::Aurora::kSystemFontMono, 13),
54  _engine(&engine), _maxSizeMusic(0) {
55 
56  registerCommand("listmusic" , boost::bind(&Console::cmdListMusic , this, _1),
57  "Usage: listmusic\nList all available music resources");
58  registerCommand("stopmusic" , boost::bind(&Console::cmdStopMusic , this, _1),
59  "Usage: stopmusic\nStop the currently playing music resource");
60  registerCommand("playmusic" , boost::bind(&Console::cmdPlayMusic , this, _1),
61  "Usage: playmusic [<music>]\nPlay the specified music resource. "
62  "If none was specified, play the default area music.");
63  registerCommand("move" , boost::bind(&Console::cmdMove , this, _1),
64  "Usage: move <x> <y> <z>\nMove to this position in the current area");
65  registerCommand("listareas" , boost::bind(&Console::cmdListAreas , this, _1),
66  "Usage: listareas\nList all areas in the current module");
67  registerCommand("gotoarea" , boost::bind(&Console::cmdGotoArea , this, _1),
68  "Usage: gotoarea <area>\nMove to a specific area");
69  registerCommand("listcampaigns", boost::bind(&Console::cmdListCampaigns, this, _1),
70  "Usage: listcampaigns\nList all campaigns");
71  registerCommand("loadcampaign" , boost::bind(&Console::cmdLoadCampaign , this, _1),
72  "Usage: loadcampaign <campaign>\nLoads a campaign, "
73  "replacing the currently running one");
74  registerCommand("listmodules" , boost::bind(&Console::cmdListModules , this, _1),
75  "Usage: listmodules\nList all modules");
76  registerCommand("loadmodule" , boost::bind(&Console::cmdLoadModule , this, _1),
77  "Usage: loadmodule <module>\nLoads a module, "
78  "replacing the currently running one");
79 }
80 
82 }
83 
86 
87  updateMusic();
89  updateModules();
90  updateAreas();
91 }
92 
94  _music.clear();
95  _maxSizeMusic = 0;
96 
97  setArguments("playmusic", _music);
98 
99  Common::UString musicDir = Common::FilePath::findSubDirectory(ResMan.getDataBase(), "data/music", true);
100  if (musicDir.empty())
101  return;
102 
103  Common::FileList musicFiles;
104  if (!musicFiles.addDirectory(musicDir, -1))
105  return;
106 
107  Common::FileList oggFiles;
108  if (!musicFiles.getSubList(".ogg", true, oggFiles))
109  return;
110 
111  for (Common::FileList::const_iterator o = oggFiles.begin(); o != oggFiles.end(); ++o) {
112  _music.push_back(Common::FilePath::getStem(*o));
113 
114  _maxSizeMusic = MAX(_maxSizeMusic, _music.back().size());
115  }
116 
117  std::sort(_music.begin(), _music.end(), Common::UString::iless());
118  setArguments("playmusic", _music);
119 }
120 
123 
124  setArguments("loadcampaign", _campaigns);
125 }
126 
129 
130  setArguments("loadmodule", _modules);
131 }
132 
134  setArguments("gotoarea", _engine->getGame().getModule().getIFO().getAreas());
135 }
136 
138  updateMusic();
140 }
141 
144 }
145 
147  _engine->getGame().playMusic(cl.args);
148 }
149 
150 void Console::cmdMove(const CommandLine &cl) {
151  std::vector<Common::UString> args;
152  splitArguments(cl.args, args);
153 
154  float x, z, y;
155  if ((args.size() < 3) ||
156  (std::sscanf(args[0].c_str(), "%f", &x) != 1) ||
157  (std::sscanf(args[1].c_str(), "%f", &y) != 1) ||
158  (std::sscanf(args[2].c_str(), "%f", &z) != 1)) {
159 
160  printCommandHelp(cl.cmd);
161  return;
162  }
163 
164  _engine->getGame().getModule().movePC(x, y, z);
165 }
166 
168  updateAreas();
169 
170  const std::vector<Common::UString> &areas = _engine->getGame().getModule().getIFO().getAreas();
171  for (std::vector<Common::UString>::const_iterator a = areas.begin(); a != areas.end(); ++a)
172  printf("%s (\"%s\")", a->c_str(), Area::getName(*a).getString().c_str());
173 }
174 
176  if (cl.args.empty()) {
177  printCommandHelp(cl.cmd);
178  return;
179  }
180 
181  const std::vector<Common::UString> &areas = _engine->getGame().getModule().getIFO().getAreas();
182  for (std::vector<Common::UString>::const_iterator a = areas.begin(); a != areas.end(); ++a)
183  if (a->equalsIgnoreCase(cl.args)) {
184  hide();
185  _engine->getGame().getModule().movePC(*a);
186  return;
187  }
188 
189  printf("Area \"%s\" does not exist", cl.args.c_str());
190 }
191 
193  updateCampaigns();
194 
195  for (std::vector<Common::UString>::const_iterator c = _campaigns.begin(); c != _campaigns.end(); ++c)
196  printf("%s (\"%s\")", c->c_str(), Campaign::getName(*c).c_str());
197 }
198 
200  if (cl.args.empty()) {
201  printCommandHelp(cl.cmd);
202  return;
203  }
204 
205  updateCampaigns();
206  for (std::vector<Common::UString>::const_iterator c = _campaigns.begin(); c != _campaigns.end(); ++c) {
207  if (c->equalsIgnoreCase(cl.args)) {
208  hide();
209  _engine->getGame().getCampaign().load(*c);
210  return;
211  }
212  }
213 
214  printf("No such campaign \"%s\"", cl.args.c_str());
215 }
216 
218  updateModules();
219 
220  for (std::vector<Common::UString>::const_iterator m = _modules.begin(); m != _modules.end(); ++m) {
221  const Common::UString name = Module::getName(*m);
222 
223  if (!name.empty())
224  printf("%s (\"%s\")", m->c_str(), name.c_str());
225  else
226  printf("%s", m->c_str());
227  }
228 }
229 
231  if (cl.args.empty()) {
232  printCommandHelp(cl.cmd);
233  return;
234  }
235 
236  updateModules();
237  for (std::vector<Common::UString>::const_iterator m = _modules.begin(); m != _modules.end(); ++m) {
238  if (m->equalsIgnoreCase(cl.args)) {
239  hide();
241  return;
242  }
243  }
244 
245  printf("No such module \"%s\"", cl.args.c_str());
246 }
247 
248 } // End of namespace Witcher
249 
250 } // End of namespace Engines
void load(const Common::UString &campaign)
Load a campaign.
Definition: campaign.cpp:115
The context holding a The Witcher campaign.
#define ResMan
Shortcut for accessing the sound manager.
Definition: resman.h:557
Common::UString args
Definition: console.h:218
A class holding an UTF-8 string.
Definition: ustring.h:48
const Aurora::LocString & getName() const
Return the name of the current campaign.
Definition: campaign.cpp:67
The global config manager.
static void splitArguments(Common::UString argLine, std::vector< Common::UString > &args)
Definition: console.cpp:1539
void cmdStopMusic(const CommandLine &cl)
Definition: console.cpp:142
Game & getGame()
Return the context running the actual game.
Definition: witcher.cpp:133
void cmdListMusic(const CommandLine &cl)
Definition: console.cpp:137
std::list< UString >::const_iterator const_iterator
Definition: filelist.h:37
void cmdLoadModule(const CommandLine &cl)
Definition: console.cpp:230
void cmdPlayMusic(const CommandLine &cl)
Definition: console.cpp:146
static void getCampaigns(std::vector< Common::UString > &campaigns)
Return a list of all campaigns.
Definition: game.cpp:153
std::vector< Common::UString > _music
All known music resources.
Definition: console.h:51
const Common::UString & getString(Language language, LanguageGender gender=kLanguageGenderCurrent) const
Get the string of that language.
Definition: locstring.cpp:82
void cmdListAreas(const CommandLine &cl)
Definition: console.cpp:167
Module & getModule()
Return the module context.
Definition: game.cpp:67
The Witcher (debug) console.
static void getModules(std::vector< Common::UString > &modules)
Return a list of all modules.
Definition: game.cpp:172
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
#define UNUSED(x)
Definition: system.h:170
Basic Aurora graphics types.
Utility templates and functions.
void cmdMove(const CommandLine &cl)
Definition: console.cpp:150
const Aurora::IFOFile & getIFO() const
Return the IFO of the currently loaded module.
Definition: module.cpp:488
const char * kSystemFontMono
Identifier used for the monospaced system font.
Definition: fontman.cpp:41
const std::vector< Common::UString > & getAreas() const
Return the list of areas in the module.
Definition: ifofile.cpp:311
void cmdListModules(const CommandLine &cl)
Definition: console.cpp:217
void printList(const std::vector< Common::UString > &list, size_t maxSize=0)
Definition: console.cpp:1479
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
std::vector< Common::UString > _campaigns
All known campaigns.
Definition: console.h:52
void cmdLoadCampaign(const CommandLine &cl)
Definition: console.cpp:199
void stopMusic()
Force all currently playing music stopped.
Definition: game.cpp:135
void cmdGotoArea(const CommandLine &cl)
Definition: console.cpp:175
Unicode string handling.
static UString getStem(const UString &p)
Return a file name&#39;s stem.
Definition: filepath.cpp:87
bool getSubList(const UString &str, bool caseInsensitive, FileList &subList) const
Add files ending with the given string into another FileList.
Definition: filelist.cpp:149
Console(WitcherEngine &engine)
Definition: console.cpp:52
const Aurora::LocString & getName() const
Return the module&#39;s name.
Definition: module.cpp:492
A list of files.
Definition: filelist.h:35
The context needed to run a The Witcher module.
The context holding a The Witcher area.
void printf(const char *s,...) GCC_PRINTF(2
Definition: console.cpp:1093
const_iterator begin() const
Return a const_iterator pointing to the beginning of the list.
Definition: filelist.cpp:94
bool addDirectory(const UString &directory, int recurseDepth=0)
Add a directory to the list.
Definition: filelist.cpp:102
WitcherEngine * _engine
Definition: console.h:48
Generic Aurora engines utility functions.
Campaign & getCampaign()
Return the campaign context.
Definition: game.cpp:61
A list of files.
Engine class handling The Witcher.
virtual void updateCaches()
Definition: console.cpp:1122
void printCommandHelp(const Common::UString &cmd)
Definition: console.cpp:1391
The context handling the gameplay in The Witcher.
const Aurora::LocString & getName() const
Return the area&#39;s name.
Definition: area.cpp:115
T MAX(T a, T b)
Definition: util.h:71
void setArguments(const Common::UString &cmd, const std::vector< Common::UString > &args)
Definition: console.cpp:1565
bool registerCommand(const Common::UString &cmd, const CommandCallback &callback, const Common::UString &help)
Definition: console.cpp:1576
void playMusic(const Common::UString &music="")
Overwrite all currently playing music.
Definition: game.cpp:124
The global resource manager for Aurora resources.
Utility class for manipulating file paths.
const_iterator end() const
Return a const_iterator pointing past the end of the list.
Definition: filelist.cpp:98
std::vector< Common::UString > _modules
All known modules.
Definition: console.h:53
static UString findSubDirectory(const UString &directory, const UString &subDirectory, bool caseInsensitive=false)
Find a directory&#39;s subdirectory.
Definition: filepath.cpp:318
void loadModule(const Common::UString &module)
Load a stand-alone module as a campaign.
Definition: campaign.cpp:127
void movePC(const Common::UString &area)
Move the player character to this area.
Definition: module.cpp:418
void cmdListCampaigns(const CommandLine &cl)
Definition: console.cpp:192