/
games.cpp
301 lines (251 loc) · 7.64 KB
/
games.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/** @file games.cpp Specialized collection for a set of logical Games.
*
* @authors Copyright © 2012-2013 Daniel Swanson <danij@dengine.net>
* @authors Copyright © 2012-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA</small>
*/
#include "de_base.h"
#include "games.h"
#include "dd_main.h"
#include "con_main.h"
#include "con_bar.h"
#include "filesys/fs_main.h"
#include "filesys/manifest.h"
#include "resource/zip.h"
#include <de/App>
#include <de/Log>
#include <QtAlgorithms>
namespace de {
DENG2_PIMPL(Games)
{
/// The actual collection.
Games::All games;
/// Special "null-game" object for this collection.
NullGame *nullGame;
Instance(Public *i) : Base(i), games(), nullGame(0)
{
/*
* One-time creation and initialization of the special "null-game"
* object (activated once created).
*/
nullGame = new NullGame;
}
~Instance()
{
clear();
delete nullGame;
}
void clear()
{
DENG2_ASSERT(nullGame != 0);
qDeleteAll(games);
games.clear();
}
};
Games::Games() : d(new Instance(this))
{}
Game &Games::nullGame() const
{
return *d->nullGame;
}
int Games::numPlayable() const
{
int count = 0;
foreach(Game *game, d->games)
{
if(game->allStartupFilesFound())
{
count += 1;
}
}
return count;
}
Game *Games::firstPlayable() const
{
foreach(Game *game, d->games)
{
if(game->allStartupFilesFound()) return game;
}
return NULL;
}
gameid_t Games::id(Game const &game) const
{
if(&game == d->nullGame) return 0; // Invalid id.
int idx = d->games.indexOf(const_cast<Game *>(&game));
if(idx < 0)
{
/// @throw NotFoundError The specified @a game is not a member of the collection.
throw NotFoundError("Games::id", QString("Game %p is not a member of the collection").arg(de::dintptr(&game)));
}
return gameid_t(idx+1);
}
Game &Games::byId(gameid_t gameId) const
{
if(gameId <= 0 || gameId > d->games.count())
{
/// @throw NotFoundError The specified @a gameId is out of range.
throw NotFoundError("Games::byId", QString("There is no Game with id %i").arg(gameId));
}
return *d->games[gameId-1];
}
Game &Games::byIdentityKey(String identityKey) const
{
if(!identityKey.isEmpty())
{
foreach(Game *game, d->games)
{
if(!game->identityKey().compareWithoutCase(identityKey))
return *game;
}
}
/// @throw NotFoundError The specified @a identityKey string is not associated with a game in the collection.
throw NotFoundError("Games::byIdentityKey", "No game exists with identity key '" + identityKey + "'");
}
Game &Games::byIndex(int idx) const
{
if(idx < 0 || idx > d->games.count())
{
/// @throw NotFoundError No game is associated with index @a idx.
throw NotFoundError("Games::byIndex", QString("There is no Game at index %i").arg(idx));
}
return *d->games[idx];
}
void Games::clear()
{
d->clear();
}
Games::All const &Games::all() const
{
return d->games;
}
int Games::collectAll(GameList &collected)
{
int numFoundSoFar = collected.count();
foreach(Game *game, d->games)
{
collected.push_back(GameListItem(game));
}
return collected.count() - numFoundSoFar;
}
void Games::add(Game &game)
{
// Already a member of the collection?
if(d->games.indexOf(&game) >= 0) return;
d->games.push_back(&game);
DENG2_FOR_AUDIENCE(Addition, i)
{
i->gameAdded(game);
}
}
void Games::locateStartupResources(Game &game)
{
Game *oldCurrentGame = &App_CurrentGame();
if(oldCurrentGame != &game)
{
/// @attention Kludge: Temporarily switch Game.
App::app().setGame(game);
DD_ExchangeGamePluginEntryPoints(game.pluginId());
// Re-init the filesystem subspace schemes using the search paths of this Game.
App_FileSystem().resetAllSchemes();
}
foreach(ResourceManifest *manifest, game.manifests())
{
// We are only interested in startup resources at this time.
if(manifest->fileFlags() & FF_STARTUP)
{
manifest->locateFile();
}
}
if(oldCurrentGame != &game)
{
// Kludge end - Restore the old Game.
App::app().setGame(*oldCurrentGame);
DD_ExchangeGamePluginEntryPoints(oldCurrentGame->pluginId());
// Re-init the filesystem subspace schemes using the search paths of this Game.
App_FileSystem().resetAllSchemes();
}
}
static int locateAllResourcesWorker(void *context)
{
Games *games = (Games *) context;
int n = 0;
foreach(Game *game, games->all())
{
LOG_MSG("Locating \"%s\"...") << game->title();
games->locateStartupResources(*game);
Con_SetProgress((n + 1) * 200 / games->count() - 1);
VERBOSE( Game::print(*game, PGF_LIST_STARTUP_RESOURCES|PGF_STATUS) )
++n;
}
BusyMode_WorkerEnd();
return 0;
}
void Games::locateAllResources()
{
BusyMode_RunNewTaskWithName(BUSYF_STARTUP | BUSYF_PROGRESS_BAR | (verbose? BUSYF_CONSOLE_OUTPUT : 0),
locateAllResourcesWorker, (void *)this, "Locating game resources...");
}
D_CMD(ListGames)
{
DENG2_UNUSED3(src, argc, argv);
Games &games = App_Games();
if(!games.count())
{
LOG_MSG("No game is currently registered.");
return true;
}
LOG_MSG(_E(1) "Registered Games:");
LOG_VERBOSE("Key: %s'!'=Incomplete/Not playable %s'*'=Loaded")
<< _E(>) _E(D) << _E(B);
LOG_MSG(_E(R) "\n");
Games::GameList found;
games.collectAll(found);
// Sort so we get a nice alphabetical list.
qSort(found.begin(), found.end());
String list;
int numCompleteGames = 0;
DENG2_FOR_EACH_CONST(Games::GameList, i, found)
{
Game *game = i->game;
bool isCurrent = (&App_CurrentGame() == game);
if(!list.isEmpty()) list += "\n";
list += String(_E(0)
_E(Ta) "%1%2 "
_E(Tb) "%3 "
_E(Tc) _E(2) "%4 " _E(i) "(%5)")
.arg(isCurrent? _E(B) _E(b) :
!game->allStartupFilesFound()? _E(D) : "")
.arg(isCurrent? "*" : !game->allStartupFilesFound()? "!" : " ")
.arg(game->identityKey())
.arg(game->title())
.arg(game->author());
if(game->allStartupFilesFound())
{
numCompleteGames++;
}
}
LOG_MSG("%s") << list;
LOG_MSG(_E(R) "\n");
LOG_MSG("%i of %i games playable.") << numCompleteGames << games.count();
LOG_MSG("Use the " _E(b) "load" _E(.) "command to load a game. For example: \"load gamename\".");
return true;
}
void Games::consoleRegister() //static
{
C_CMD("listgames", "", ListGames);
Game::consoleRegister();
}
} // namespace de