/
GameManager.cpp
475 lines (382 loc) · 13.4 KB
/
GameManager.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include "GameManager.h"
#include "i18n.h"
#include "iregistry.h"
#include "itextstream.h"
#include "ifiletypes.h"
#include "ifilesystem.h"
#include "settings/PreferenceSystem.h"
#include "ui/prefdialog/PrefDialog.h"
#include "ui/prefdialog/GameSetupDialog.h"
#include "os/file.h"
#include "os/dir.h"
#include "os/path.h"
#include "os/fs.h"
#include "string/trim.h"
#include "string/convert.h"
#include "string/replace.h"
#include "string/predicate.h"
#include "string/split.h"
#include "GameFileLoader.h"
#include "wxutil/dialog/MessageBox.h"
#include "modulesystem/StaticModule.h"
#include "modulesystem/ApplicationContextImpl.h"
#include <sigc++/bind.h>
#include <iostream>
namespace game
{
namespace
{
const char* const GKEY_PREFAB_FOLDER = "/mapFormat/prefabFolder";
const char* const GKEY_MAPS_FOLDER = "/mapFormat/mapFolder";
}
Manager::Manager()
{}
const std::string& Manager::getName() const
{
static std::string _name(MODULE_GAMEMANAGER);
return _name;
}
const StringSet& Manager::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_XMLREGISTRY);
_dependencies.insert(MODULE_PREFERENCESYSTEM);
_dependencies.insert(MODULE_FILETYPES);
_dependencies.insert(MODULE_VIRTUALFILESYSTEM);
_dependencies.insert(MODULE_COMMANDSYSTEM);
}
return _dependencies;
}
void Manager::initialiseModule(const ApplicationContext& ctx)
{
// Read command line parameters, these override any existing preference setting
const ApplicationContext::ArgumentList& args = ctx.getCmdLineArgs();
for (const std::string& arg : args)
{
if (string::istarts_with(arg, "fs_game="))
{
GlobalRegistry().set(RKEY_FS_GAME, arg.substr(8));
}
else if (string::istarts_with(arg, "fs_game_base="))
{
GlobalRegistry().set(RKEY_FS_GAME_BASE, arg.substr(13));
}
}
GlobalCommandSystem().addCommand("ProjectSettings", std::bind(&Manager::showGameSetupDialog, this, std::placeholders::_1));
initialise(ctx.getRuntimeDataPath());
initEnginePath();
}
const std::string& Manager::getFSGame() const
{
return GlobalRegistry().get(RKEY_FS_GAME);
}
const std::string& Manager::getFSGameBase() const
{
return GlobalRegistry().get(RKEY_FS_GAME_BASE);
}
const std::string& Manager::getModPath() const
{
// Return the fs_game path if available
return (!_config.modPath.empty()) ? _config.modPath : _config.modBasePath;
}
const std::string& Manager::getModBasePath() const
{
return _config.modBasePath;
}
IGamePtr Manager::currentGame()
{
if (_config.gameType.empty())
{
// No game type selected, bail out, the program will crash anyway on module load
wxutil::Messagebox::ShowFatalError(_("GameManager: No game type selected, can't continue."), NULL);
}
return _games[_config.gameType];
}
const Manager::GameList& Manager::getSortedGameList()
{
return _sortedGames;
}
void Manager::constructPreferences()
{
#if 0
IPreferencePage& page = GetPreferenceSystem().getPage(_("Game"));
ComboBoxValueList gameList;
for (const IGamePtr& game : _sortedGames)
{
gameList.push_back(game->getKeyValue("name"));
}
page.appendCombo(_("Select a Game:"), RKEY_GAME_TYPE, gameList, true);
page.appendPathEntry(_("Engine Path"), RKEY_ENGINE_PATH, true);
page.appendEntry(_("Mod (fs_game)"), RKEY_FS_GAME);
page.appendEntry(_("Mod Base (fs_game_base, optional)"), RKEY_FS_GAME_BASE);
#endif
}
void Manager::initialise(const std::string& appPath)
{
// Scan the <applicationpath>/games folder for .game files
loadGameFiles(appPath);
if (_games.empty())
{
// No game types available, bail out, the program would crash anyway on
// module load
wxutil::Messagebox::ShowFatalError(
_("GameManager: No valid game files found, can't continue."), nullptr
);
}
// Add the settings widgets to the Preference Dialog, we might need it
constructPreferences();
// Find the user's selected game from the XML registry, and attempt to find
// it in the set of available games.
std::string gameType = GlobalRegistry().get(RKEY_GAME_TYPE);
GameMap::iterator i = _games.find(gameType);
if (gameType.empty() || i == _games.end())
{
// We have at least one game type available, select the first
// Store the name of the only game into the Registry
rMessage() << "No game selected, will choose the highest ranked one." << std::endl;
if (_sortedGames.empty())
{
wxutil::Messagebox::ShowFatalError(
"GameManager: Sorted game list is empty, can't continue.", nullptr
);
}
GlobalRegistry().set(RKEY_GAME_TYPE, _sortedGames.front()->getKeyValue("name"));
}
// Load the value from the registry, there should be one selected at this point
_config.gameType = GlobalRegistry().get(RKEY_GAME_TYPE);
// The game type should be selected now
if (!_config.gameType.empty())
{
rMessage() << "GameManager: Selected game type: " << _config.gameType << std::endl;
}
else
{
// No game type selected, bail out, the program would crash anyway on module load
wxutil::Messagebox::ShowFatalError(_("No game type selected."));
}
}
std::string Manager::getUserEnginePath()
{
#if defined(POSIX)
// First check for a local copy of the game tree, e.g. ~/.doom3
std::string localGamePrefix = currentGame()->getKeyValue("prefix");
if (!localGamePrefix.empty())
{
std::string homeDir = getenv("HOME");
fs::path localPath = fs::path(homeDir) / localGamePrefix;
if (fs::exists(localPath))
{
return os::standardPathWithSlash(localPath);
}
}
#endif
// Otherwise (Windows, or no local mod path found) return the regular engine path
return _config.enginePath;
}
void Manager::initEnginePath()
{
// Try to retrieve a saved value for the game setup
_config.loadFromRegistry();
if (!settingsValid())
{
showGameSetupDialog(cmd::ArgumentList());
}
// Extract the fs_game / fs_game_base settings from the mod path
std::string fsGame = os::getRelativePath(_config.modPath, _config.enginePath);
string::trim_right(fsGame, "/");
std::string fsGameBase = os::getRelativePath(_config.modBasePath, _config.enginePath);
string::trim_right(fsGameBase, "/");
registry::setValue(RKEY_FS_GAME, fsGame);
registry::setValue(RKEY_FS_GAME_BASE, fsGameBase);
// Register as observer, to get notified about future engine path changes
observeKey(RKEY_ENGINE_PATH);
observeKey(RKEY_FS_GAME);
observeKey(RKEY_FS_GAME_BASE);
// Force an update ((re-)initialises the VFS)
updateEnginePath(true);
#if 0
// Add the note to the preference page
IPreferencePage& page = GetPreferenceSystem().getPage(_("Game"));
page.appendLabel(_("<b>Note</b>: You will have to restart DarkRadiant\nfor the changes to take effect."));
#endif
}
void Manager::showGameSetupDialog(const cmd::ArgumentList& args)
{
// Paths not valid, ask the user to select something
ui::GameSetupDialog::Result result = ui::GameSetupDialog::Show(cmd::ArgumentList());
if (!result.enginePath.empty())
{
_config.gameType = result.gameName;
_config.enginePath = result.enginePath;
_config.modBasePath = result.modBasePath;
_config.modPath = result.modPath;
_config.saveToRegistry();
}
}
void Manager::observeKey(const std::string& key)
{
// Hide std::string signal argument, replace with bound false value for
// updateEnginePath().
GlobalRegistry().signalForKey(key).connect(
sigc::bind(sigc::mem_fun(this, &Manager::updateEnginePath), false)
);
}
bool Manager::settingsValid() const
{
if (os::fileOrDirExists(_config.enginePath))
{
// Check the mod base path, if appropriate
if (!_config.modBasePath.empty() && !os::fileOrDirExists(_config.modBasePath))
{
// Mod base name is not empty, but folder doesnt' exist
return false;
}
// Check the mod path, if appropriate
if (!_config.modPath.empty() && !os::fileOrDirExists(_config.modPath))
{
// Mod name is not empty, but mod folder doesnt' exist
return false;
}
return true; // all settings there
}
// Engine path doesn't exist
return false;
}
void Manager::setMapAndPrefabPaths(const std::string& baseGamePath)
{
// Construct the map path and make sure the folder exists
std::string mapPath;
// Get the maps folder (e.g. "maps/")
std::string mapFolder = currentGame()->getLocalXPath(GKEY_MAPS_FOLDER)[0].getAttributeValue("value");
if (mapFolder.empty()) {
mapFolder = "maps/";
}
if (_config.modPath.empty() && _config.modBasePath.empty())
{
mapPath = baseGamePath + mapFolder;
}
else if (!_config.modPath.empty())
{
mapPath = _config.modPath + mapFolder;
}
else // _config.modBasePath is not empty
{
mapPath = _config.modBasePath + mapFolder;
}
rMessage() << "GameManager: Map path set to " << mapPath << std::endl;
os::makeDirectory(mapPath);
// Save the map path to the registry
GlobalRegistry().set(RKEY_MAP_PATH, mapPath);
// Setup the prefab path
std::string prefabPath = mapPath;
std::string pfbFolder = currentGame()->getLocalXPath(GKEY_PREFAB_FOLDER)[0].getAttributeValue("value");
// Replace the "maps/" with "prefabs/"
string::replace_last(prefabPath, mapFolder, pfbFolder);
// Store the path into the registry
rMessage() << "GameManager: Prefab path set to " << prefabPath << std::endl;
GlobalRegistry().set(RKEY_PREFAB_PATH, prefabPath);
}
void Manager::updateEnginePath(bool forced)
{
// Clean the new path
std::string newEnginePath = os::standardPathWithSlash(registry::getValue<std::string>(RKEY_ENGINE_PATH));
std::string newModPath = os::standardPathWithSlash(registry::getValue<std::string>(RKEY_MOD_PATH));
std::string newModBasePath = os::standardPathWithSlash(registry::getValue<std::string>(RKEY_MOD_BASE_PATH));
// Only update if any settings were changed, or if this is a "forced" update
if (forced || newEnginePath != _config.enginePath || newModPath != _config.modPath || newModBasePath != _config.modBasePath)
{
// The list of paths which will be passed to the VFS init method
vfs::SearchPaths vfsSearchPaths;
vfs::VirtualFileSystem::ExtensionSet extensions;
string::split(extensions, currentGame()->getKeyValue("archivetypes"), " ");
// Set the new mod and engine paths
_config.modPath = newModPath;
_config.modBasePath = newModBasePath;
_config.enginePath = newEnginePath;
if (!_config.modPath.empty())
{
// We have a MOD, register this directory first
vfsSearchPaths.insertIfNotExists(_config.modPath);
#ifdef POSIX
std::string fsGame = os::getRelativePath(_config.modPath, _config.enginePath);
// On Linux, the above was in ~/.doom3/, search the engine mod path as well
std::string baseModPath = os::standardPathWithSlash(_config.enginePath + fsGame);
vfsSearchPaths.insertIfNotExists(baseModPath);
#endif
}
if (!_config.modBasePath.empty())
{
// We have a MOD base, register this directory as second
vfsSearchPaths.insertIfNotExists(_config.modBasePath);
#ifdef POSIX
std::string fsGameBase = os::getRelativePath(_config.modBasePath, _config.enginePath);
// On Linux, the above was in ~/.doom3/, search the engine mod base path as well
std::string baseModPath = os::standardPathWithSlash(_config.enginePath + fsGameBase);
vfsSearchPaths.insertIfNotExists(baseModPath);
#endif
}
// On UNIX this is the user-local enginepath, e.g. ~/.doom3/base/
// On Windows this will be the same as global engine path
std::string userBasePath = os::standardPathWithSlash(
getUserEnginePath() + // ~/.doom3
currentGame()->getKeyValue("basegame") // base
);
vfsSearchPaths.insertIfNotExists(userBasePath);
// Register the base game folder (/usr/local/games/doom3/<basegame>) last
// This will always be searched, but *after* the other paths
std::string baseGame = os::standardPathWithSlash(
_config.enginePath + currentGame()->getKeyValue("basegame")
);
vfsSearchPaths.insertIfNotExists(baseGame);
// Update map and prefab paths
setMapAndPrefabPaths(userBasePath);
// Initialise the filesystem, if we were initialised before
GlobalFileSystem().initialise(vfsSearchPaths, extensions);
}
}
const Manager::PathList& Manager::getVFSSearchPaths() const
{
return GlobalFileSystem().getVfsSearchPaths();
}
const std::string& Manager::getEnginePath() const {
return _config.enginePath;
}
void Manager::loadGameFiles(const std::string& appPath)
{
std::string gamePath = appPath + "games/";
rMessage() << "GameManager: Scanning for game description files: " << gamePath << std::endl;
// Invoke a GameFileLoader functor on every file in the games/ dir.
GameFileLoader gameFileLoader(_games, gamePath);
try
{
os::foreachItemInDirectory(gamePath, gameFileLoader);
rMessage() << "GameManager: Found game definitions: " << std::endl;
for (GameMap::value_type& pair : _games)
{
rMessage() << " " << pair.first << std::endl;
}
// Populate the sorted games list
_sortedGames.clear();
// Sort the games by their index
std::multimap<int, GamePtr> sortedGameMap;
for (const GameMap::value_type& pair : _games)
{
int index = string::convert<int>(pair.second->getKeyValue("index"), 9999);
sortedGameMap.insert(std::make_pair(index, pair.second));
}
for (const auto& pair : sortedGameMap)
{
_sortedGames.push_back(pair.second);
}
rMessage() << std::endl;
}
catch (os::DirectoryNotFoundException&)
{
rError() << "Could not find directory with game files in " << gamePath << std::endl;
}
}
} // namespace game
// The static module definition (self-registers)
module::StaticModule<game::Manager> gameManagerModule;