/
doomsdayapp.cpp
511 lines (432 loc) · 14.2 KB
/
doomsdayapp.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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
/** @file doomsdayapp.cpp Common application-level state and components.
*
* @authors Copyright (c) 2015 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, see:
* http://www.gnu.org/licenses</small>
*/
#include "doomsday/doomsdayapp.h"
#include "doomsday/games.h"
#include "doomsday/filesys/sys_direc.h"
#include "doomsday/filesys/fs_util.h"
#include "doomsday/resource/bundles.h"
#include "doomsday/filesys/datafile.h"
#include "doomsday/filesys/datafolder.h"
#include "doomsday/paths.h"
#include "doomsday/world/world.h"
#include "doomsday/world/entitydef.h"
#include "doomsday/SavedSession"
#include <de/App>
#include <de/Loop>
#include <de/Folder>
#include <de/DirectoryFeed>
#include <de/DictionaryValue>
#include <de/c_wrapper.h>
#include <de/strutil.h>
#include <de/memoryzone.h>
#include <QSettings>
#include <QDir>
#ifdef WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# define ENV_PATH_SEP_CHAR ';'
#else
# define ENV_PATH_SEP_CHAR ':'
#endif
using namespace de;
static String const PATH_LOCAL_WADS("/local/wads");
static DoomsdayApp *theDoomsdayApp = nullptr;
DENG2_PIMPL_NOREF(DoomsdayApp)
{
std::string ddBasePath; // Doomsday root directory is at...?
std::string ddRuntimePath;
bool initialized = false;
Plugins plugins;
Games games;
Game *currentGame = nullptr;
BusyMode busyMode;
Players players;
res::Bundles dataBundles;
/// @c true = We are using a custom user dir specified on the command line.
bool usingUserDir = false;
#ifdef UNIX
# ifndef MACOSX
/// @c true = We are using the user dir defined in the HOME environment.
bool usingHomeDir = false;
# endif
#endif
#ifdef WIN32
HINSTANCE hInstance = NULL;
#endif
/**
* Delegates game change notifications to scripts.
*/
class GameChangeScriptAudience : DENG2_OBSERVES(DoomsdayApp, GameChange)
{
public:
void currentGameChanged(Game const &newGame)
{
ArrayValue args;
args << DictionaryValue() << TextValue(newGame.id());
App::scriptSystem().nativeModule("App")["audienceForGameChange"]
.array().callElements(args);
}
};
GameChangeScriptAudience scriptAudienceForGameChange;
Instance(Players::Constructor playerConstructor)
: players(playerConstructor)
{
Record &appModule = App::scriptSystem().nativeModule("App");
appModule.addArray("audienceForGameChange"); // game change observers
audienceForGameChange += scriptAudienceForGameChange;
#ifdef WIN32
hInstance = GetModuleHandle(NULL);
#endif
}
~Instance()
{
theDoomsdayApp = nullptr;
}
void attachWadFeed(String const &description, NativePath const &path)
{
if(!path.isEmpty())
{
if(path.exists())
{
LOG_RES_NOTE("Using %s WAD folder: %s") << description << path.pretty();
App::rootFolder().locate<Folder>(PATH_LOCAL_WADS).attach(new DirectoryFeed(path));
}
else
{
LOG_RES_NOTE("Ignoring non-existent %s WAD folder: %s")
<< description << path.pretty();
}
}
}
/**
* Doomsday can locate WAD files from various places. This method attaches
* a set of feeds to /local/wads/ so that all the native folders where the
* user keeps WAD files are available in the tree.
*/
void initWadFolders()
{
// "/local" is for various files on the local computer.
Folder &wads = App::fileSystem().makeFolder(PATH_LOCAL_WADS, FS::DontInheritFeeds);
wads.clear();
wads.clearFeeds();
CommandLine &cmdLine = App::commandLine();
NativePath const startupPath = cmdLine.startupPath();
// Feeds are added in ascending priority.
// Check for games installed using Steam.
NativePath const steamBase = steamBasePath();
if(steamBase.exists())
{
NativePath steamPath = steamBase / "SteamApps/common/";
LOG_RES_NOTE("Detected SteamApps path: %s") << steamPath.pretty();
static String const appDirs[] = {
"DOOM 2/base",
"Final DOOM/base",
"Heretic Shadow of the Serpent Riders/base",
"Hexen/base",
"Hexen Deathkings of the Dark Citadel/base",
"Ultimate Doom/base",
"DOOM 3 BFG Edition/base/wads"
};
for(auto const &appDir : appDirs)
{
NativePath const p = steamPath / appDir;
if(p.exists())
{
attachWadFeed("Steam", p);
}
}
}
#ifdef UNIX
NativePath const systemWads("/usr/share/games/doom");
if(systemWads.exists())
{
attachWadFeed("system", systemWads);
}
#endif
// Add all paths from the DOOMWADPATH environment variable.
if(getenv("DOOMWADPATH"))
{
// Interpreted similarly to the PATH variable.
QStringList paths = String(getenv("DOOMWADPATH"))
.split(ENV_PATH_SEP_CHAR, String::SkipEmptyParts);
while(!paths.isEmpty())
{
attachWadFeed(_E(m) "DOOMWADPATH" _E(.), startupPath / paths.takeLast());
}
}
// Add the path from the DOOMWADDIR environment variable.
if(getenv("DOOMWADDIR"))
{
attachWadFeed(_E(m) "DOOMWADDIR" _E(.), startupPath / getenv("DOOMWADDIR"));
}
#ifdef UNIX
// There may be an iwaddir specified in a system-level config file.
filename_t fn;
if(UnixInfo_GetConfigValue("paths", "iwaddir", fn, FILENAME_T_MAXLEN))
{
attachWadFeed("UnixInfo " _E(i) "paths.iwaddir" _E(.), startupPath / fn);
}
#endif
// Command line paths.
if(auto arg = cmdLine.check("-iwad", 1)) // has at least one parameter
{
for(dint p = arg.pos + 1; p < cmdLine.count(); ++p)
{
if(cmdLine.isOption(p)) break;
cmdLine.makeAbsolutePath(p);
attachWadFeed("command-line", cmdLine.at(p));
}
}
// Configured via GUI.
attachWadFeed("user-selected", App::config().gets("resource.iwadFolder", ""));
wads.populate();
}
#ifdef UNIX
void determineGlobalPaths()
{
// By default, make sure the working path is the home folder.
App::setCurrentWorkPath(App::app().nativeHomePath());
# ifndef MACOSX
if(getenv("HOME"))
{
filename_t homePath;
directory_t* temp;
dd_snprintf(homePath, FILENAME_T_MAXLEN, "%s/%s/runtime/", getenv("HOME"),
DENG2_APP->unixHomeFolderName().toLatin1().constData());
temp = Dir_New(homePath);
Dir_mkpath(Dir_Path(temp));
usingHomeDir = Dir_SetCurrent(Dir_Path(temp));
if(usingHomeDir)
{
DD_SetRuntimePath(Dir_Path(temp));
}
Dir_Delete(temp);
}
# endif
// The -userdir option sets the working directory.
if(CommandLine_CheckWith("-userdir", 1))
{
filename_t runtimePath;
directory_t* temp;
strncpy(runtimePath, CommandLine_NextAsPath(), FILENAME_T_MAXLEN);
Dir_CleanPath(runtimePath, FILENAME_T_MAXLEN);
// Ensure the path is closed with a directory separator.
F_AppendMissingSlashCString(runtimePath, FILENAME_T_MAXLEN);
temp = Dir_New(runtimePath);
usingUserDir = Dir_SetCurrent(Dir_Path(temp));
if(usingUserDir)
{
DD_SetRuntimePath(Dir_Path(temp));
# ifndef MACOSX
usingHomeDir = false;
# endif
}
Dir_Delete(temp);
}
# ifndef MACOSX
if(!usingHomeDir && !usingUserDir)
# else
if(!usingUserDir)
# endif
{
// The current working directory is the runtime dir.
directory_t* temp = Dir_NewFromCWD();
DD_SetRuntimePath(Dir_Path(temp));
Dir_Delete(temp);
}
// libcore has determined the native base path, so let FS1 know about it.
DD_SetBasePath(DENG2_APP->nativeBasePath().toUtf8());
}
#endif // UNIX
#ifdef WIN32
void determineGlobalPaths()
{
// Change to a custom working directory?
if(CommandLine_CheckWith("-userdir", 1))
{
if(NativePath::setWorkPath(CommandLine_NextAsPath()))
{
LOG_VERBOSE("Changed current directory to \"%s\"") << NativePath::workPath();
usingUserDir = true;
}
}
// The runtime directory is the current working directory.
DD_SetRuntimePath((NativePath::workPath().withSeparators('/') + '/').toUtf8().constData());
// Use a custom base directory?
if(CommandLine_CheckWith("-basedir", 1))
{
DD_SetBasePath(CommandLine_Next());
}
else
{
// The default base directory is one level up from the bin dir.
String binDir = App::executablePath().fileNamePath().withSeparators('/');
String baseDir = String(QDir::cleanPath(binDir / String(".."))) + '/';
DD_SetBasePath(baseDir.toUtf8().constData());
}
}
#endif // WIN32
DENG2_PIMPL_AUDIENCE(GameUnload)
DENG2_PIMPL_AUDIENCE(GameChange)
};
DENG2_AUDIENCE_METHOD(DoomsdayApp, GameUnload)
DENG2_AUDIENCE_METHOD(DoomsdayApp, GameChange)
DoomsdayApp::DoomsdayApp(Players::Constructor playerConstructor)
: d(new Instance(playerConstructor))
{
DENG2_ASSERT(!theDoomsdayApp);
theDoomsdayApp = this;
App::app().addInitPackage("net.dengine.base");
static SavedSession::Interpreter intrpSavedSession;
static DataBundle::Interpreter intrpDataBundle;
App::fileSystem().addInterpreter(intrpSavedSession);
App::fileSystem().addInterpreter(intrpDataBundle);
}
void DoomsdayApp::initialize()
{
d->initWadFolders();
// "/sys/bundles" has package-like symlinks to files that are not in
// Doomsday 2 format but can be loaded as packages.
App::fileSystem().makeFolder("/sys/bundles", FS::DontInheritFeeds);
d->initialized = true;
d->dataBundles.identify();
}
void DoomsdayApp::determineGlobalPaths()
{
d->determineGlobalPaths();
}
DoomsdayApp &DoomsdayApp::app()
{
DENG2_ASSERT(theDoomsdayApp);
return *theDoomsdayApp;
}
res::Bundles &DoomsdayApp::bundles()
{
return DoomsdayApp::app().d->dataBundles;
}
Plugins &DoomsdayApp::plugins()
{
return DoomsdayApp::app().d->plugins;
}
Games &DoomsdayApp::games()
{
return DoomsdayApp::app().d->games;
}
Players &DoomsdayApp::players()
{
return DoomsdayApp::app().d->players;
}
Game &DoomsdayApp::currentGame()
{
return game();
}
BusyMode &DoomsdayApp::busyMode()
{
return DoomsdayApp::app().d->busyMode;
}
NativePath DoomsdayApp::steamBasePath()
{
#ifdef WIN32
// The path to Steam can be queried from the registry.
{
QSettings st("HKEY_CURRENT_USER\\Software\\Valve\\Steam\\", QSettings::NativeFormat);
String path = st.value("SteamPath").toString();
if(!path.isEmpty()) return path;
}
{
QSettings st("HKEY_LOCAL_MACHINE\\Software\\Valve\\Steam\\", QSettings::NativeFormat);
String path = st.value("InstallPath").toString();
if(!path.isEmpty()) return path;
}
#elif MACOSX
return NativePath(QDir::homePath()) / "Library/Application Support/Steam/";
#endif
/// @todo Where are Steam apps located on Linux?
return "";
}
bool DoomsdayApp::isUsingUserDir() const
{
return d->usingUserDir;
}
std::string const &DoomsdayApp::doomsdayBasePath() const
{
return d->ddBasePath;
}
void DoomsdayApp::setDoomsdayBasePath(NativePath const &path)
{
/// @todo Unfortunately Dir/fs_util assumes fixed-size strings, so we
/// can't take advantage of std::string. -jk
filename_t temp;
strncpy(temp, path.toUtf8(), FILENAME_T_MAXLEN);
Dir_CleanPath(temp, FILENAME_T_MAXLEN);
Dir_MakeAbsolutePath(temp, FILENAME_T_MAXLEN);
// Ensure it ends with a directory separator.
F_AppendMissingSlashCString(temp, FILENAME_T_MAXLEN);
d->ddBasePath = temp;
}
std::string const &DoomsdayApp::doomsdayRuntimePath() const
{
return d->ddRuntimePath;
}
void DoomsdayApp::setDoomsdayRuntimePath(NativePath const &path)
{
d->ddRuntimePath = path.toUtf8().constData();
}
#ifdef WIN32
void *DoomsdayApp::moduleHandle() const
{
return d->hInstance;
}
#endif
Game &DoomsdayApp::game()
{
DENG2_ASSERT(app().d->currentGame != 0);
return *app().d->currentGame;
}
void DoomsdayApp::aboutToChangeGame(Game const &)
{
auto &gx = plugins().gameExports();
if(App_GameLoaded())
{
if(gx.Shutdown)
{
gx.Shutdown();
}
// Tell the plugin it is being unloaded.
void *unloader = plugins().findEntryPoint(game().pluginId(), "DP_Unload");
LOGDEV_MSG("Calling DP_Unload %p") << unloader;
plugins().setActivePluginId(game().pluginId());
if(unloader) ((pluginfunc_t)unloader)();
plugins().setActivePluginId(0);
}
}
void DoomsdayApp::reset()
{
// Reset the world back to it's initial state (unload the map, reset players, etc...).
World::get().reset();
Z_FreeTags(PU_GAMESTATIC, PU_PURGELEVEL - 1);
P_ShutdownMapEntityDefs();
}
void DoomsdayApp::setGame(Game &game)
{
app().d->currentGame = &game;
}
bool App_GameLoaded()
{
return App::appExists() && !DoomsdayApp::currentGame().isNull();
}