Skip to content

Commit

Permalink
Resources|libdoomsday: Use the best-scored match when detecting data …
Browse files Browse the repository at this point in the history
…bundles

In case many files match a condition, link to the best-scored file.

By default, prefer files over folders when detecting data bundles.
The type can also be explicitly requested using the "fileType" criterion
in databundles.dei.
  • Loading branch information
skyjake committed May 25, 2016
1 parent d7ac2de commit a9d8cea
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
9 changes: 9 additions & 0 deletions doomsday/apps/libdoomsday/src/resource/bundles.cpp
Expand Up @@ -173,6 +173,15 @@ Bundles::MatchResult Bundles::match(DataBundle const &bundle) const
}
}

// Match the file type.
String fileType = def->keyValue(QStringLiteral("fileType"));
if(fileType.isEmpty()) fileType = "file"; // prefer files by default
if((!fileType.compareWithoutCase(QStringLiteral("file")) && source.status().type() == File::Status::FILE) ||
(!fileType.compareWithoutCase(QStringLiteral("folder")) && source.status().type() == File::Status::FOLDER))
{
++score;
}

// Match the file size.
String fileSize = def->keyValue(QStringLiteral("fileSize"));
if(!fileSize.isEmpty() && fileSize.toUInt() == source.size())
Expand Down
17 changes: 15 additions & 2 deletions doomsday/apps/libdoomsday/src/resource/databundle.cpp
Expand Up @@ -146,7 +146,8 @@ DENG2_PIMPL(DataBundle)

// Finally, make a link that represents the package.
if(auto chosen = chooseUniqueLinkPathAndVersion(dataFile, packageId,
matched.packageVersion))
matched.packageVersion,
matched.bestScore))
{
LOGDEV_RES_VERBOSE("Linking %s as %s") << dataFile.path() << chosen.path;

Expand Down Expand Up @@ -198,7 +199,8 @@ DENG2_PIMPL(DataBundle)

PathAndVersion chooseUniqueLinkPathAndVersion(File const &dataFile,
String const &packageId,
Version const &packageVersion)
Version const &packageVersion,
dint bundleScore)
{
for(int attempt = 0; attempt < 3; ++attempt)
{
Expand Down Expand Up @@ -243,6 +245,17 @@ DENG2_PIMPL(DataBundle)
{
return PathAndVersion(linkPath, version);
}
else
{
// This could still be a better scored match.
Record const &pkgInfo = bundleFolder().locate<File const>(linkPath).objectNamespace();
if(bundleScore > pkgInfo.geti("bundleScore"))
{
// Forget about the previous link.
bundleFolder().removeFile(linkPath);
return PathAndVersion(linkPath, version);
}
}
}

// Unique path & version not available. This version of the package is probably
Expand Down

0 comments on commit a9d8cea

Please sign in to comment.