Skip to content

Commit

Permalink
FS|libcore: Finding all available packages
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Aug 20, 2015
1 parent 304e273 commit 98ca7dc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
20 changes: 13 additions & 7 deletions doomsday/sdk/libcore/src/filesys/package.cpp
Expand Up @@ -31,6 +31,8 @@ static String const PACKAGE("package");
static String const PACKAGE_ORDER("package.__order__");
static String const PACKAGE_IMPORT_PATH("package.importPath");

static String const VAR_ID("ID");

Package::Asset::Asset(Record const &rec) : RecordAccessor(rec) {}

Package::Asset::Asset(Record const *rec) : RecordAccessor(rec) {}
Expand Down Expand Up @@ -230,7 +232,7 @@ void Package::parseMetadata(File &packageFile) // static
if(!needParse) return;

// The package identifier and path are automatically set.
metadata.set("id", identifierForFile(packageFile));
metadata.set(VAR_ID, identifierForFile(packageFile));
metadata.set("path", packageFile.path());

// Check for a ScriptedInfo source.
Expand Down Expand Up @@ -266,8 +268,13 @@ void Package::parseMetadata(File &packageFile) // static

void Package::validateMetadata(Record const &packageInfo)
{
if(!packageInfo.has(VAR_ID))
{
throw ValidationError("Package::validateMetadata", "Not a package");
}

// A domain is required in all package identifiers.
DotPath const ident(packageInfo.gets("id"));
DotPath const ident(packageInfo.gets(VAR_ID));

if(ident.segmentCount() <= 1)
{
Expand All @@ -286,16 +293,15 @@ void Package::validateMetadata(Record const &packageInfo)
.arg(packageInfo.gets("path")));
}

char const *required[] = { "title", "version", "license", "tags", 0 };

for(int i = 0; required[i]; ++i)
static char const *required[] = { "title", "version", "license", "tags" };
for(char const *req : required)
{
if(!packageInfo.has(required[i]))
if(!packageInfo.has(req))
{
throw IncompleteMetadataError("Package::validateMetadata",
QString("Package \"%1\" does not have '%2' in its metadata")
.arg(packageInfo.gets("path"))
.arg(required[i]));
.arg(req));
}
}
}
Expand Down
40 changes: 31 additions & 9 deletions doomsday/sdk/libcore/src/filesys/packageloader.cpp
Expand Up @@ -104,6 +104,19 @@ DENG2_PIMPL(PackageLoader)
return int(found.size());
}

/**
* Parses or updates the metadata of a package, and checks it for validity.
* A 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);
Package::validateMetadata(packFile.info().subrecord("package"));
}

/**
* Given a package identifier, pick one of the available versions of the package
* based on predefined criteria.
Expand All @@ -126,9 +139,7 @@ DENG2_PIMPL(PackageLoader)
// Each must have a version specified.
DENG2_FOR_EACH_CONST(FS::FoundFiles, i, found)
{
File *pkg = *i;
Package::parseMetadata(*pkg);
Package::validateMetadata(pkg->info().subrecord("package"));
checkPackage(**i);
}

found.sort(ascendingPackagesByLatest);
Expand Down Expand Up @@ -173,12 +184,23 @@ DENG2_PIMPL(PackageLoader)
{
if(i->first.fileNameExtension() == ".pack")
{
String path = i->second->path();

// The special persistent data package should be ignored.
if(path == "/home/persist.pack") continue;

list.append(path);
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 &)
{
// Not a loadable package.
}
}
}
}
Expand Down

0 comments on commit 98ca7dc

Please sign in to comment.