Skip to content

Commit

Permalink
libcore|FS: Selecting alternative packages, versioned IDs
Browse files Browse the repository at this point in the history
PackageLoader can be given a space-separated list of alternative
package IDs. PackageLoader will check them all in order and use the
first one that is found.

If a version is included in the package ID, only that specific
version will be selected by PackageLoader.
  • Loading branch information
skyjake committed Jan 20, 2016
1 parent 9899f6d commit 2527f5f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
8 changes: 8 additions & 0 deletions doomsday/sdk/libcore/include/de/filesys/packageloader.h
Expand Up @@ -65,6 +65,14 @@ class DENG2_PUBLIC PackageLoader

typedef QMap<String, Package *> LoadedPackages;

/**
* Utility for dealing with space-separated lists of identifiers.
*/
struct DENG2_PUBLIC IdentifierList : public StringList
{
IdentifierList(String const &spaceSeparatedIds);
};

public:
PackageLoader();

Expand Down
33 changes: 32 additions & 1 deletion doomsday/sdk/libcore/src/filesys/packageloader.cpp
Expand Up @@ -27,6 +27,7 @@
#include "de/PackageFeed"

#include <QMap>
#include <QRegExp>

namespace de {

Expand Down Expand Up @@ -123,6 +124,16 @@ DENG2_PIMPL(PackageLoader)
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.
Expand All @@ -148,6 +159,17 @@ DENG2_PIMPL(PackageLoader)
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.
Expand Down Expand Up @@ -254,7 +276,7 @@ PackageLoader::PackageLoader() : d(new Instance(this))

bool PackageLoader::isAvailable(String const &packageId) const
{
return d->selectPackage(packageId) != nullptr;
return d->selectPackage(IdentifierList(packageId)) != nullptr;
}

Package const &PackageLoader::load(String const &packageId)
Expand Down Expand Up @@ -413,4 +435,13 @@ StringList PackageLoader::findAllPackages() const
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

0 comments on commit 2527f5f

Please sign in to comment.