/
app.h
450 lines (380 loc) · 12.8 KB
/
app.h
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
/*
* The Doomsday Engine Project -- libcore
*
* Copyright © 2010-2017 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 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 Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#ifndef LIBCORE_APP_H
#define LIBCORE_APP_H
#include "../libcore.h"
#include "de/Clock"
#include "de/FileIndex"
#include "de/Package"
/**
* Macro for conveniently accessing the de::App singleton instance.
*/
#define DE_APP (&de::App::app())
#if defined (DE_ASSERT_IN_MAIN_THREAD)
# undef DE_ASSERT_IN_MAIN_THREAD
#endif
#define DE_ASSERT_IN_MAIN_THREAD() DE_ASSERT(de::App::inMainThread())
namespace de {
class Archive;
class ArchiveFolder;
class CommandLine;
class Config;
class Event;
class FileSystem;
class Folder;
class LogBuffer;
class LogFilter;
class MetadataBank;
class Module;
class Path;
class NativePath;
class PackageLoader;
class ScriptSystem;
class System;
class UnixInfo;
namespace filesys { class RemoteFeedRelay; }
namespace game { class Game; }
/**
* Represents the application and its subsystems. This is the common
* denominator (and abstract base class) for GUI and non-GUI apps. de::App is
* not usable on its own; instead you must use one of the derived variants.
* @ingroup core
*
* @see GuiApp, TextApp
*/
class DE_PUBLIC App : DE_OBSERVES(Clock, TimeChange)
{
public:
enum SubsystemInitFlag {
DefaultSubsystems = 0x0,
//DisablePlugins = 0x1,
DisablePersistentData = 0x2
};
using SubsystemInitFlags = Flags;
/// Attempting to access persistent data when it has been disabled at init.
/// @ingroup errors
DE_ERROR(PersistentDataNotAvailable);
/// Asset with given identifier was not found. @ingroup errors
DE_ERROR(AssetNotFoundError);
/**
* Notified when application startup has been fully completed.
*/
DE_AUDIENCE(StartupComplete, void appStartupCompleted())
static const char *ORG_NAME; ///< Name of the author/organization.
static const char *ORG_DOMAIN; ///< Network domain of the author/organization.
static const char *APP_NAME; ///< Name of the application, as presented to humans.
static const char *APP_VERSION; ///< Version of the application.
static const char *CONFIG_PATH;
static const char *UNIX_HOME;
public:
/**
* Construct an App instance. The application will not be fully usable
* until initSubsystems() has been called -- you should call
* initSubsystems() as soon as possible after construction. Never throws an
* exception.
*
* @param appFilePath Path of the application binary.
* @param args Arguments.
*/
App(const StringList &args);
virtual ~App();
/**
* Metadata about the application (META_* as keys).
*/
Record &metadata();
/**
* Returns the application metadata.
*/
const Record &metadata() const;
/**
* Add a new package to be loaded at initialization time. Call this before
* initSubsystems().
*
* @param identifier Package identifier.
*/
void addInitPackage(const String &identifier);
/**
* Sets the path of the configuration script that will be automatically run if needed
* during application launch. The script governs the contents of the special
* persistent Config module. @see Config
*
* This method must be called before initSubsystems().
*
* @param path Location of the @em Config.ds script file. The default path of the
* script is "/modules/Config.ds".
*/
// void setConfigScript(const Path &path);
/**
* Sets the name of the application. Derived classes should call this from their
* implementation of setMetadata().
*
* @param appName Application name. Defaults to "Doomsday Engine".
*/
// void setName(const String &appName);
/**
* Sets the Unix-style home folder name. For instance, ".doomsday" could be used.
*
* @param name Name of the (usually hidden) user-specific home data folder.
*/
void setUnixHomeFolderName(const String &name);
String unixHomeFolderName() const;
/**
* Returns the home folder name without the possible dot in the beginning.
*/
String unixEtcFolderName() const;
/**
* Returns the reverse domain name of the application. This is based on the
* ORG_DOMAIN metadata and the Unix home folder name.
*/
String reverseDomainIdentifier() const;
/**
* Sets a callback to be called when an uncaught exception occurs.
*/
void setTerminateFunc(void (*func)(const char *msg));
/**
* Finishes App construction by initializing all the application's
* subsystems. This includes Config and FS. Has to be called manually in
* the application's initialization routine. An exception will be thrown if
* initialization cannot be successfully completed.
*
* @param flags How to/which subsystems to initialize.
*/
virtual void initSubsystems(SubsystemInitFlags subsystemInitflags = DefaultSubsystems);
/**
* Adds a system to the application. The order of systems is preserved; the
* system added last will be notified of time changes last and will receive
* input events last (if others don't eat them).
*
* @param system System. Ownership kept by caller. The caller is
* responsible for making sure the system has been
* initialized properly.
*/
void addSystem(System &system);
/**
* Removes a system from the application.
*
* @param system System to remove.
*/
void removeSystem(System &system);
void notifyStartupComplete();
/**
* Determines if an instance of App currently exists.
*/
static bool appExists();
static App &app();
static LogFilter &logFilter();
/**
* Returns the command line used to start the application.
*/
static CommandLine &commandLine();
/**
* Returns the absolute native path of the application executable.
*/
static NativePath executableFile();
/**
* Returns the absolute native path of the directory where the application executable is located.
*/
static NativePath executableDir();
#ifdef MACOSX
/**
* Returns the native path of the application bundle contents.
*/
NativePath nativeAppContentsPath();
#endif
/**
* Returns the native path of the data base folder.
*
* The base folder is the location where all the common data files are located, e.g.,
* /usr/share/doomsday on Linux.
*/
NativePath nativeBasePath();
#if 0
#if !defined (DE_STATIC_LINK)
/**
* Returns the native path of where to load binaries (plugins). This
* is where "/bin" points to.
*/
NativePath nativePluginBinaryPath();
#endif
#endif
/**
* Returns the native path where user-specific runtime files should be
* placed (this is where "/home" points to). The user can override the
* location using the @em -userdir command line option.
*/
NativePath nativeHomePath();
/**
* Returns the archive for storing persistent engine state into. Typically
* the contents are updated when subsystems are being shut down. When the
* file system is being shut down, the <code>/home/persist.pack</code>
* package is written to disk.
*
* @return Persistent data archive.
*/
static const Archive &persistentData();
/**
* Returns the persistent data as a mutable archive. Accessing the entries in
* a mutable archive will automatically update their timestamps.
*
* @return Persistent data archive (allowing changes).
*/
static Archive &mutablePersistentData();
static bool hasPersistentData();
static ArchiveFolder &persistPackFolder();
/**
* Returns the application's current native working directory.
*/
static NativePath currentWorkPath();
/**
* Returns a directory where temporary files can be written.
*/
static NativePath tempPath();
/**
* Returns a native directory for caching non-user-specific (native) files.
*/
static NativePath cachePath();
/**
* Changes the application's current native working directory.
*
* @param cwd New working directory for the application.
*
* @return @c true, if the current working directory was changed,
* otherwise @c false.
*/
static bool setCurrentWorkPath(const NativePath &cwd);
/**
* Returns the application's file system.
*/
static FileSystem &fileSystem();
/**
* Returns the application's metadata cache.
*/
static MetadataBank &metadataBank();
/**
* Returns the root folder of the file system.
*/
static Folder &rootFolder();
/**
* Returns the /home folder.
*/
static Folder &homeFolder();
/**
* Returns the application's package loader.
*/
static PackageLoader &packageLoader();
/**
* Returns the remote feed relay that manages connections to remote file repositories.
*/
static filesys::RemoteFeedRelay &remoteFeedRelay();
/**
* Convenience method for finding all files matching a certain name or partial path
* from all loaded packages.
*
* @param partialPath File name or partial path.
* @param files Resulting list of found files.
*
* @return Number of files found.
*/
static int findInPackages(const String &partialPath, FileIndex::FoundFiles &files);
/**
* Checks if an asset exists.
*
* @param identifier Identifier.
*
* @return @c true, if assetInfo() can be called.
*/
static bool assetExists(const String &identifier);
/**
* Retrieves the namespace of an asset.
*
* @param identifier Identifier.
*
* @return Asset namespace accessor.
*/
static Package::Asset asset(const String &identifier);
/**
* Returns the application's script system.
*/
static ScriptSystem &scriptSystem();
static bool configExists();
/**
* Returns the configuration.
*/
static Config &config();
/**
* Returns a configuration variable.
*
* @param name Name of the variable.
*
* @return Variable.
*/
static Variable &config(const String &name);
/**
* Returns the web API URL. Always includes the protocol and ends with a slash.
* @return Configured web API URL.
*/
static String apiUrl();
/**
* Returns the Unix system-level configuration preferences.
*/
static UnixInfo &unixInfo();
/**
* Requests engine shutdown by calling the specified termination callback
* (see setTerminateFunc()). Called when an exception is caught at the
* de::App level, at which point there is no way to gracefully handle it
* and the application has to be shut down.
*
* This should not be called directly. From C++ code, one should throw an
* exception in unrecoverable error situations. From C code, one should
* call the App_FatalError() function.
*
* @param message Error message to be shown to the user.
*/
void handleUncaughtException(const String& message);
/*
* Events received from the operating system should be passed here; the
* application will make sure all subsystems get a chance to process them.
*/
// virtual bool processEvent(const Event &);
/**
* Informs all the subsystems about advancement of time. Subsystems will be
* notified in the order they were added with addSystem(). This will be
* automatically called by the application clock when time changes.
*/
void timeChanged(const Clock &);
public:
/**
* Determines if the currently executing thread is the application's main
* thread.
*/
static bool inMainThread();
protected:
/**
* Returns the native path of the directory where the application can store
* user-specific data. This is usually not the same as the user's native
* home folder.
*
* @return Application data path.
*/
virtual NativePath appDataPath() const = 0;
private:
DE_PRIVATE(d)
};
} // namespace de
#endif // LIBCORE_APP_H