/
packageloader.cpp
569 lines (487 loc) · 16.6 KB
/
packageloader.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/** @file packageloader.cpp Loads and unloads packages.
*
* @authors Copyright (c) 2014 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>
*/
#include "de/PackageLoader"
#include "de/App"
#include "de/CommandLine"
#include "de/Config"
#include "de/DictionaryValue"
#include "de/FS"
#include "de/Info"
#include "de/LinkFile"
#include "de/Log"
#include "de/PackageFeed"
#include "de/Parser"
#include "de/Version"
#include <QMap>
#include <QRegExp>
namespace de {
static String const VAR_PACKAGE_VERSION("package.version");
DENG2_PIMPL(PackageLoader)
{
LoadedPackages loaded;
int loadCounter;
Impl(Public *i) : Base(i), loadCounter(0)
{}
~Impl()
{
// We own all loaded packages.
qDeleteAll(loaded.values());
}
/**
* Determines if a specific package is currently loaded.
* @param file Package root.
* @return @c true, if loaded.
*/
bool isLoaded(File const &file) const
{
LoadedPackages::const_iterator found = loaded.constFind(Package::identifierForFile(file));
if (found != loaded.constEnd())
{
return &found.value()->file() == &file;
}
return false;
}
static bool shouldIgnoreFile(File const &packageFile)
{
if (Feed const *feed = packageFile.originFeed())
{
// PackageFeed generates links to loaded packages.
if (feed->is<PackageFeed>()) return true;
}
return false;
}
int findAllVariants(String const &packageIdVer, FS::FoundFiles &found) const
{
// The version may be provided optionally, to set the preferred version.
auto idVer = Package::split(packageIdVer);
String const packageId = idVer.first;
QStringList const components = packageId.split('.');
String id;
// The package may actually be inside other packages, so we need to check
// each component of the package identifier.
for (int i = components.size() - 1; i >= 0; --i)
{
id = components.at(i) + (!id.isEmpty()? "." + id : "");
FS::FoundFiles files;
App::fileSystem().findAllOfTypes(StringList()
<< DENG2_TYPE_NAME(Folder)
<< DENG2_TYPE_NAME(ArchiveFolder)
<< DENG2_TYPE_NAME(LinkFile),
id + ".pack", files);
files.remove_if([&packageId] (File *file) {
if (shouldIgnoreFile(*file)) return true;
return Package::identifierForFile(*file) != packageId;
});
std::copy(files.begin(), files.end(), std::back_inserter(found));
}
return int(found.size());
}
/**
* Parses or updates the metadata of a package, and checks it for validity.
* A Package::ValidationError is thrown if the package metadata does not
* comply with the minimum requirements.
*
* @param packFile Package file (".pack" folder).
*/
static void checkPackage(File &packFile)
{
Package::parseMetadata(packFile);
if (!packFile.objectNamespace().has(Package::VAR_PACKAGE))
{
throw Package::NotPackageError("PackageLoader::checkPackage",
packFile.description() + " is not a package");
}
Package::validateMetadata(packFile.objectNamespace().subrecord("package"));
}
File const *selectPackage(IdentifierList const &idList) const
{
for (auto const &id : idList.ids)
{
if (File const *f = selectPackage(id))
return f;
}
return nullptr;
}
/**
* Given a package identifier, pick one of the available versions of the package
* based on predefined criteria.
*
* @param packageId Package identifier. This may optionally include the
* required version number.
*
* @return Selected package, or @c NULL if a version could not be selected.
*/
File const *selectPackage(String const &packageId) const
{
LOG_AS("selectPackage");
FS::FoundFiles found;
if (!findAllVariants(packageId, found))
{
// None found.
return nullptr;
}
// Each must have a version specified.
DENG2_FOR_EACH_CONST(FS::FoundFiles, i, found)
{
checkPackage(**i);
}
// If the identifier includes a version, only accept that specific version.
auto idVer = Package::split(packageId);
if (idVer.second.isValid())
{
found.remove_if([&idVer] (File *f) {
Version const pkgVer = f->objectNamespace().gets(VAR_PACKAGE_VERSION);
return (pkgVer != idVer.second); // Ignore other versions.
});
// Did we run out of candidates?
if (found.empty()) return nullptr;
}
// Sorted descending by version.
found.sort([] (File const *a, File const *b) -> bool
{
// The version must be specified using a format understood by Version.
Version const aVer(a->objectNamespace().gets(VAR_PACKAGE_VERSION));
Version const bVer(b->objectNamespace().gets(VAR_PACKAGE_VERSION));
if (aVer == bVer)
{
// Identifical versions are prioritized by modification time.
return de::cmp(a->status().modifiedAt, b->status().modifiedAt) < 0;
}
return aVer < bVer;
});
LOG_RES_XVERBOSE("Selected '%s': %s") << packageId << found.back()->description();
return found.back();
}
Package &load(String const &packageId, File const &source)
{
if (loaded.contains(packageId))
{
throw AlreadyLoadedError("PackageLoader::load",
"Package '" + packageId + "' is already loaded from \"" +
loaded[packageId]->objectNamespace().gets("path") + "\"");
}
// Loads all the packages listed as required for this package. Throws an
// exception is any are missing.
loadRequirements(source);
// Optional content can be loaded once Config is available so the user's
// preferences are known.
if (App::configExists())
{
loadOptionalContent(source);
}
Package *pkg = new Package(source);
loaded.insert(packageId, pkg);
pkg->setOrder(loadCounter++);
pkg->didLoad();
return *pkg;
}
bool unload(String identifier)
{
LoadedPackages::iterator found = loaded.find(identifier);
if (found == loaded.end()) return false;
Package *pkg = found.value();
pkg->aboutToUnload();
delete pkg;
loaded.remove(identifier);
return true;
}
void listPackagesInIndex(FileIndex const &index, StringList &list)
{
for (auto i = index.begin(); i != index.end(); ++i)
{
if (shouldIgnoreFile(*i->second)) continue;
if (i->first.fileNameExtension() == ".pack")
{
try
{
File &file = *i->second;
String path = file.path();
// The special persistent data package should be ignored.
if (path == "/home/persist.pack") continue;
// Check the metadata.
checkPackage(file);
list.append(path);
}
catch (Package::NotPackageError const &er)
{
// This is usually a .pack folder used only for nesting.
LOG_RES_XVERBOSE("\"%s\": %s") << i->first << er.asText();
}
catch (Package::ValidationError const &er)
{
// Not a loadable package.
LOG_RES_MSG("\"%s\": Package is invalid: %s") << i->first << er.asText();
}
catch (Parser::SyntaxError const &er)
{
LOG_RES_WARNING("\"%s\": Package has a Doomsday Script syntax error: %s")
<< i->first << er.asText();
}
catch (Info::SyntaxError const &er)
{
// Not a loadable package.
LOG_RES_WARNING("\"%s\": Package has a syntax error: %s")
<< i->first << er.asText();
}
/// @todo Store the errors so that the UI can show a list of
/// problematic packages. -jk
}
}
}
void loadRequirements(File const &packageFile)
{
for (String const &reqId : Package::requires(packageFile))
{
if (!self.isLoaded(reqId))
{
self.load(reqId);
}
}
}
void loadOptionalContent(File const &packageFile)
{
// Packages enabled/disabled for use.
DictionaryValue const &selPkgs = Config::get()["fs.selectedPackages"]
.value<DictionaryValue>();
Record const &meta = Package::metadata(packageFile);
// Helper function to determine if a particular pacakge is marked for loading.
auto isPackageSelected = [meta, &selPkgs] (TextValue const &id, bool byDefault) -> bool {
TextValue const key(meta.gets("ID"));
if (selPkgs.contains(key)) {
auto const &sels = selPkgs.element(key).as<DictionaryValue>();
if (sels.contains(id)) {
return sels.element(id).isTrue();
}
}
return byDefault;
};
if (meta.has("recommends"))
{
for (auto const *id : meta.geta("recommends").elements())
{
String const pkgId = id->asText();
if (isPackageSelected(pkgId, true) && !self.isLoaded(pkgId))
{
self.load(pkgId);
}
}
}
if (meta.has("extras"))
{
for (auto const *id : meta.geta("extras").elements())
{
String const pkgId = id->asText();
if (isPackageSelected(pkgId, false) && !self.isLoaded(pkgId))
{
self.load(pkgId);
}
}
}
}
QList<Package *> loadedInOrder() const
{
QList<Package *> pkgs = loaded.values();
qSort(pkgs.begin(), pkgs.end(), [] (Package const *a, Package const *b) {
return a->order() < b->order();
});
return pkgs;
}
DENG2_PIMPL_AUDIENCE(Activity)
DENG2_PIMPL_AUDIENCE(Load)
DENG2_PIMPL_AUDIENCE(Unload)
};
DENG2_AUDIENCE_METHOD(PackageLoader, Activity)
DENG2_AUDIENCE_METHOD(PackageLoader, Load)
DENG2_AUDIENCE_METHOD(PackageLoader, Unload)
PackageLoader::PackageLoader() : d(new Impl(this))
{}
bool PackageLoader::isAvailable(String const &packageId) const
{
return d->selectPackage(IdentifierList(packageId)) != nullptr;
}
File const *PackageLoader::select(String const &packageId) const
{
return d->selectPackage(IdentifierList(packageId));
}
Package const &PackageLoader::load(String const &packageId)
{
LOG_AS("PackageLoader");
File const *packFile = d->selectPackage(IdentifierList(packageId));
if (!packFile)
{
throw NotFoundError("PackageLoader::load",
"Package \"" + packageId + "\" is not available");
}
// Use the identifier computed from the file because @a packageId may
// actually list multiple alternatives.
String const id = Package::identifierForFile(*packFile);
d->load(id, *packFile);
try
{
DENG2_FOR_AUDIENCE2(Load, i)
{
i->packageLoaded(id);
}
DENG2_FOR_AUDIENCE2(Activity, i)
{
i->setOfLoadedPackagesChanged();
}
}
catch (Error const &er)
{
// Someone took issue with the loaded package; cancel the load.
unload(id);
throw PostLoadError("PackageLoader::load",
"Error during post-load actions of package \"" +
id + "\": " + er.asText());
}
return package(id);
}
void PackageLoader::unload(String const &packageId)
{
String id = Package::split(packageId).first;
if (isLoaded(id))
{
DENG2_FOR_AUDIENCE2(Unload, i)
{
i->aboutToUnloadPackage(id);
}
d->unload(id);
DENG2_FOR_AUDIENCE2(Activity, i)
{
i->setOfLoadedPackagesChanged();
}
}
}
void PackageLoader::unloadAll()
{
LOG_AS("PackageLoader");
LOG_RES_MSG("Unloading %i packages") << d->loaded.size();
while (!d->loaded.isEmpty())
{
unload(d->loaded.begin().key());
}
}
bool PackageLoader::isLoaded(String const &packageId) const
{
return d->loaded.contains(packageId);
}
bool PackageLoader::isLoaded(File const &file) const
{
return d->isLoaded(file);
}
PackageLoader::LoadedPackages const &PackageLoader::loadedPackages() const
{
return d->loaded;
}
FS::FoundFiles PackageLoader::loadedPackagesAsFilesInPackageOrder() const
{
QList<Package *> pkgs = d->loadedInOrder();
FS::FoundFiles sorted;
for (auto p : pkgs)
{
sorted.push_back(const_cast<File *>(&p->sourceFile()));
}
return sorted;
}
StringList PackageLoader::loadedPackagesInOrder() const
{
QList<Package *> pkgs = d->loadedInOrder();
StringList ids;
for (auto p : pkgs)
{
ids << p->identifier();
}
return ids;
}
Package const &PackageLoader::package(String const &packageId) const
{
if (!isLoaded(packageId))
{
throw NotFoundError("PackageLoader::package", "Package '" + packageId + "' is not loaded");
}
return *d->loaded[packageId];
}
void PackageLoader::sortInPackageOrder(FS::FoundFiles &filesToSort) const
{
typedef std::pair<File *, int> FileAndOrder;
// Find the packages for files.
QList<FileAndOrder> all;
DENG2_FOR_EACH_CONST(FS::FoundFiles, i, filesToSort)
{
Package const *pkg = 0;
String identifier = Package::identifierForContainerOfFile(**i);
if (isLoaded(identifier))
{
pkg = &package(identifier);
}
all << FileAndOrder(*i, pkg? pkg->order() : -1);
}
// Sort by package order.
std::sort(all.begin(), all.end(), [] (FileAndOrder const &a, FileAndOrder const &b) {
return a.second < b.second;
});
// Put the results back in the given array.
filesToSort.clear();
foreach (FileAndOrder const &f, all)
{
filesToSort.push_back(f.first);
}
}
void PackageLoader::loadFromCommandLine()
{
CommandLine &args = App::commandLine();
for (int p = 0; p < args.count(); )
{
// Find all the -pkg options.
if (!args.matches("-pkg", args.at(p)))
{
++p;
continue;
}
// Load all the specified packages (by identifier, not by path).
while (++p != args.count() && !args.isOption(p))
{
load(args.at(p));
}
}
}
StringList PackageLoader::findAllPackages() const
{
StringList all;
for (QString typeName : QStringList({ DENG2_TYPE_NAME(Folder),
DENG2_TYPE_NAME(ArchiveFolder),
DENG2_TYPE_NAME(LinkFile) }))
{
d->listPackagesInIndex(App::fileSystem().indexFor(typeName), all);
}
return all;
}
PackageLoader &PackageLoader::get()
{
return App::packageLoader();
}
PackageLoader::IdentifierList::IdentifierList(String const &spaceSeparatedIds)
{
static QRegExp anySpace("\\s");
for (auto const &qs : spaceSeparatedIds.split(anySpace, String::SkipEmptyParts))
{
ids.append(qs);
}
}
} // namespace de