/
packageloader.cpp
447 lines (378 loc) · 12.8 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
/** @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/FS"
#include "de/App"
#include "de/Version"
#include "de/Info"
#include "de/Log"
#include "de/Parser"
#include "de/LinkFile"
#include "de/PackageFeed"
#include <QMap>
#include <QRegExp>
namespace de {
DENG2_PIMPL(PackageLoader)
{
LoadedPackages loaded;
int loadCounter;
Instance(Public *i) : Base(i), loadCounter(0)
{}
~Instance()
{
// 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::ValidationError("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)
{
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.
*
* @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())
{
std::remove_if(found.begin(), found.end(), [&idVer] (File *f) {
Version const pkgVer = f->objectNamespace().gets("package.version");
return (pkgVer != idVer.second); // Ignore other versions.
});
}
// 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("package.version"));
Version const bVer(b->objectNamespace().gets("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_VERBOSE("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") + "\"");
}
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::ValidationError const &er)
{
// Not a loadable package.
qDebug() << i->first << ": Package is invalid:" << er.asText();
}
catch(Parser::SyntaxError const &er)
{
LOG_RES_NOTE("\"%s\": Package has a Doomsday Script syntax error: %s")
<< i->first << er.asText();
}
catch(Info::SyntaxError const &er)
{
// Not a loadable package.
LOG_RES_NOTE("\"%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
}
}
}
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 Instance(this))
{}
bool PackageLoader::isAvailable(String const &packageId) const
{
return d->selectPackage(IdentifierList(packageId)) != nullptr;
}
Package const &PackageLoader::load(String const &packageId)
{
LOG_AS("PackageLoader");
File const *packFile = d->selectPackage(packageId);
if(!packFile)
{
throw NotFoundError("PackageLoader::load",
"Package \"" + packageId + "\" is not available");
}
d->load(packageId, *packFile);
try
{
DENG2_FOR_AUDIENCE2(Load, i)
{
i->packageLoaded(packageId);
}
DENG2_FOR_AUDIENCE2(Activity, i)
{
i->setOfLoadedPackagesChanged();
}
}
catch(Error const &er)
{
// Someone took issue with the loaded package; cancel the load.
unload(packageId);
throw PostLoadError("PackageLoader::load",
"Error during post-load actions of package \"" +
packageId + "\": " + er.asText());
}
return package(packageId);
}
void PackageLoader::unload(String const &packageId)
{
if(isLoaded(packageId))
{
DENG2_FOR_AUDIENCE2(Unload, i)
{
i->aboutToUnloadPackage(packageId);
}
d->unload(packageId);
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.firstKey());
}
}
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;
}
Package const &PackageLoader::package(String const &packageId) const
{
if(!isLoaded(packageId))
{
throw NotFoundError("PackageLoader::package", "Package '" + packageId + "' is not loaded");
}
return *d->loaded[packageId];
}
namespace internal
{
typedef std::pair<File *, int> FileAndOrder;
static bool packageOrderLessThan(FileAndOrder const &a, FileAndOrder const &b) {
return a.second < b.second;
}
}
using namespace internal;
void PackageLoader::sortInPackageOrder(FS::FoundFiles &filesToSort) const
{
// 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(), packageOrderLessThan);
// 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;
d->listPackagesInIndex(App::fileSystem().indexFor(DENG2_TYPE_NAME(Folder)), all);
d->listPackagesInIndex(App::fileSystem().indexFor(DENG2_TYPE_NAME(ArchiveFolder)), all);
d->listPackagesInIndex(App::fileSystem().indexFor(DENG2_TYPE_NAME(LinkFile)), all);
return all;
}
PackageLoader::IdentifierList::IdentifierList(String const &spaceSeparatedIds)
{
static QRegExp anySpace("\\s");
for(auto const &qs : spaceSeparatedIds.split(anySpace, String::SkipEmptyParts))
{
append(qs);
}
}
} // namespace de