/
app.cpp
468 lines (383 loc) · 11.4 KB
/
app.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
/*
* The Doomsday Engine Project -- libdeng2
*
* Copyright (c) 2010-2013 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* 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/>.
*/
#include "de/App"
#include "de/Animation"
#include "de/ArchiveFeed"
#include "de/ArrayValue"
#include "de/Block"
#include "de/DirectoryFeed"
#include "de/Log"
#include "de/LogBuffer"
#include "de/Module"
#include "de/NumberValue"
#include "de/PackageFolder"
#include "de/Record"
#include "de/ScriptSystem"
#include "de/Version"
#include "de/Version"
#include "de/Writer"
#include "de/ZipArchive"
#include "de/math.h"
#include <QDir>
#include <QThread>
namespace de {
static App *singletonApp;
DENG2_PIMPL(App)
{
QThread *mainThread;
CommandLine cmdLine;
LogBuffer logBuffer;
/// Path of the application executable.
NativePath appPath;
NativePath cachedBasePath;
NativePath cachedPluginBinaryPath;
NativePath cachedHomePath;
/// Primary (wall) clock.
Clock clock;
/// Subsystems (not owned).
QList<System *> systems;
FileSystem fs;
ScriptSystem scriptSys;
/// Archive where persistent data should be stored. Written to /home/persist.pack.
/// The archive is owned by the file system.
Archive *persistentData;
UnixInfo unixInfo;
/// The configuration.
Config *config;
void (*terminateFunc)(char const *);
Instance(Public &a, QStringList args)
: Base(a), cmdLine(args), persistentData(0), config(0), terminateFunc(0)
{
singletonApp = &a;
mainThread = QThread::currentThread();
Clock::setAppClock(&clock);
Animation::setClock(&clock);
// Built-in systems.
systems.append(&fs);
systems.append(&scriptSys);
}
~Instance()
{
clock.audienceForTimeChange -= self;
delete config;
Clock::setAppClock(0);
}
void initFileSystem(bool allowPlugins)
{
Folder &binFolder = fs.makeFolder("/bin");
// Initialize the built-in folders. This hooks up the default native
// directories into the appropriate places in the file system.
// All of these are in read-only mode.
#ifdef MACOSX
NativePath appDir = appPath.fileNamePath();
binFolder.attach(new DirectoryFeed(appDir));
fs.makeFolder("/data").attach(new DirectoryFeed(self.nativeBasePath()));
fs.makeFolder("/modules").attach(new DirectoryFeed(self.nativeBasePath() / "modules"));
#elif WIN32
NativePath appDir = appPath.fileNamePath();
fs.makeFolder("/data").attach(new DirectoryFeed(appDir / "..\\data"));
fs.makeFolder("/modules").attach(new DirectoryFeed(appDir / "..\\modules"));
#else // UNIX
fs.makeFolder("/data").attach(new DirectoryFeed(self.nativeBasePath() / "data"));
fs.makeFolder("/modules").attach(new DirectoryFeed(self.nativeBasePath() / "modules"));
#endif
if(allowPlugins)
{
binFolder.attach(new DirectoryFeed(self.nativePluginBinaryPath()));
}
// User's home folder.
fs.makeFolder("/home").attach(new DirectoryFeed(self.nativeHomePath(),
DirectoryFeed::AllowWrite | DirectoryFeed::CreateIfMissing));
// Populate the file system.
fs.refresh();
}
};
App::App(NativePath const &appFilePath, QStringList args)
: d(new Instance(*this, args))
{
// Global time source for animations.
Animation::setClock(&d->clock);
// This instance of LogBuffer is used globally.
LogBuffer::setAppBuffer(d->logBuffer);
// Do not flush the log buffer until we've found out where messages should
// be flushed (Config.log.file).
d->logBuffer.enableFlushing(false);
// Set the log message level.
#ifdef DENG2_DEBUG
LogEntry::Level level = LogEntry::DEBUG;
#else
LogEntry::Level level = LogEntry::MESSAGE;
#endif
try
{
int pos;
if((pos = d->cmdLine.check("-loglevel", 1)) > 0)
{
level = LogEntry::textToLevel(d->cmdLine.at(pos + 1));
}
}
catch(Error const &er)
{
qWarning("%s", er.asText().toLatin1().constData());
}
// Aliases have not been defined at this point.
level = de::max(LogEntry::TRACE,
LogEntry::Level(level
- d->cmdLine.has("-verbose")
- d->cmdLine.has("-v")));
d->logBuffer.enable(level);
d->appPath = appFilePath;
LOG_INFO("Application path: ") << d->appPath;
LOG_INFO("Enabled log entry level: ") << LogEntry::levelToText(level);
#ifdef MACOSX
// When the application is started through Finder, we get a special command
// line argument. The working directory needs to be changed.
if(d->cmdLine.count() >= 2 && d->cmdLine.at(1).beginsWith("-psn"))
{
DirectoryFeed::changeWorkingDir(d->cmdLine.at(0).fileNamePath() + "/..");
}
#endif
}
App::~App()
{
LOG_AS("~App");
d.reset();
singletonApp = 0;
}
void App::setTerminateFunc(void (*func)(char const *))
{
d->terminateFunc = func;
}
void App::handleUncaughtException(String message)
{
LOG_CRITICAL(message);
if(d->terminateFunc) d->terminateFunc(message.toUtf8().constData());
}
bool App::processEvent(Event const &ev)
{
foreach(System *sys, d->systems)
{
if(sys->behavior() & System::ReceivesInputEvents)
{
if(sys->processEvent(ev))
return true;
}
}
return false;
}
void App::timeChanged(Clock const &clock)
{
foreach(System *sys, d->systems)
{
if(sys->behavior() & System::ObservesTime)
{
sys->timeChanged(clock);
}
}
}
bool App::inMainThread()
{
return DENG2_APP->d->mainThread == QThread::currentThread();
}
NativePath App::nativePluginBinaryPath()
{
if(!d->cachedPluginBinaryPath.isEmpty()) return d->cachedPluginBinaryPath;
NativePath path;
#ifdef WIN32
path = d->appPath.fileNamePath() / "plugins";
#else
# ifdef MACOSX
path = d->appPath.fileNamePath() / "../DengPlugins";
# else
path = DENG_LIBRARY_DIR;
# endif
// Also check the system config files.
d->unixInfo.path("libdir", path);
#endif
return (d->cachedPluginBinaryPath = path);
}
NativePath App::nativeHomePath()
{
if(!d->cachedHomePath.isEmpty()) return d->cachedHomePath;
int i;
if((i = d->cmdLine.check("-userdir", 1)))
{
d->cmdLine.makeAbsolutePath(i + 1);
return (d->cachedHomePath = d->cmdLine.at(i + 1));
}
#ifdef MACOSX
NativePath nativeHome = QDir::homePath();
nativeHome = nativeHome / "Library/Application Support/Doomsday Engine/runtime";
#elif WIN32
NativePath nativeHome = appDataPath();
nativeHome = nativeHome / "runtime";
#else // UNIX
NativePath nativeHome = QDir::homePath();
nativeHome = nativeHome / ".doomsday/runtime";
#endif
return (d->cachedHomePath = nativeHome);
}
Archive &App::persistentData()
{
DENG2_ASSERT(DENG2_APP->d->persistentData != 0);
return *DENG2_APP->d->persistentData;
}
NativePath App::currentWorkPath()
{
return NativePath::workPath();
}
bool App::setCurrentWorkPath(NativePath const &cwd)
{
return NativePath::setWorkPath(cwd);
}
NativePath App::nativeBasePath()
{
if(!d->cachedBasePath.isEmpty()) return d->cachedBasePath;
int i;
if((i = d->cmdLine.check("-basedir", 1)))
{
d->cmdLine.makeAbsolutePath(i + 1);
return (d->cachedBasePath = d->cmdLine.at(i + 1));
}
NativePath path;
#ifdef WIN32
path = d->appPath.fileNamePath() / "..";
#else
# ifdef MACOSX
path = d->appPath.fileNamePath() / "../Resources";
# else
path = DENG_BASE_DIR;
# endif
// Also check the system config files.
d->unixInfo.path("basedir", path);
#endif
return (d->cachedBasePath = path);
}
void App::initSubsystems(SubsystemInitFlags flags)
{
bool allowPlugins = !flags.testFlag(DisablePlugins);
d->initFileSystem(allowPlugins);
if(!homeFolder().has("persist.pack") || commandLine().has("-reset"))
{
// Recreate the persistent state data package.
ZipArchive arch;
arch.add("Info", String("# Package for Doomsday's persistent state.\n").toUtf8());
Writer(homeFolder().replaceFile("persist.pack")) << arch;
homeFolder().populate(Folder::PopulateOnlyThisFolder);
}
d->persistentData = &homeFolder().locate<PackageFolder>("persist.pack").archive();
// The configuration.
d->config = new Config("/modules/Config.de");
d->scriptSys.addNativeModule("Config", d->config->names());
d->config->read();
LogBuffer &logBuf = LogBuffer::appBuffer();
// Update the log buffer max entry count: number of items to hold in memory.
logBuf.setMaxEntryCount(d->config->getui("log.bufferSize"));
try
{
// The -out option can be used to override the configured output file.
int pos = 0;
if((pos = commandLine().check("-out", 1)) > 0)
{
logBuf.setOutputFile(String("/home") / commandLine().at(pos + 1));
}
else
{
// Set the log output file.
logBuf.setOutputFile(d->config->gets("log.file"));
}
}
catch(Error const &er)
{
LOG_WARNING("Failed to set log output file:\n" + er.asText());
}
// The level of enabled messages.
/**
* @todo We are presently controlling the log levels depending on build
* configuration, so ignore what the config says.
*/
//logBuf.enable(Log::LogLevel(conf->getui("log.level")));
// We can start flushing now when the destination is known.
logBuf.enableFlushing(true);
if(allowPlugins)
{
#if 0 // not yet handled by libdeng2
// Load the basic plugins.
loadPlugins();
#endif
}
// Update the wall clock time.
d->clock.setTime(Time());
// Now we can start observing progress of time.
d->clock.audienceForTimeChange += this;
LOG_VERBOSE("libdeng2::App %s subsystems initialized.") << Version().asText();
}
void App::addSystem(System &system)
{
d->systems.removeAll(&system);
d->systems.append(&system);
}
void App::removeSystem(System &system)
{
d->systems.removeAll(&system);
}
App &App::app()
{
return *singletonApp;
}
CommandLine &App::commandLine()
{
return DENG2_APP->d->cmdLine;
}
NativePath App::executablePath()
{
return DENG2_APP->d->appPath;
}
#ifdef MACOSX
NativePath App::nativeAppContentsPath()
{
return DENG2_APP->d->appPath/"../..";
}
#endif
FileSystem &App::fileSystem()
{
return DENG2_APP->d->fs;
}
ScriptSystem &de::App::scriptSystem()
{
return DENG2_APP->d->scriptSys;
}
Folder &App::rootFolder()
{
return fileSystem().root();
}
Folder &App::homeFolder()
{
return rootFolder().locate<Folder>("home");
}
Config &App::config()
{
DENG2_ASSERT(DENG2_APP->d->config != 0);
return *DENG2_APP->d->config;
}
UnixInfo &App::unixInfo()
{
return DENG2_APP->d->unixInfo;
}
} // namespace de