refactor: move individual pack version parsing to its own function

This commit is contained in:
flow 2022-04-22 13:23:47 -03:00 committed by flow
parent 67e0214fa5
commit 5c5699bba5
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
2 changed files with 55 additions and 41 deletions

View File

@ -59,12 +59,31 @@ void Modrinth::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
for (auto versionIter : arr) {
auto obj = versionIter.toObject();
auto file = loadIndexedPackVersion(obj);
if(file.fileId.isValid()) // Heuristic to check if the returned value is valid
unsortedVersions.append(file);
}
auto orderSortPredicate = [](const ModPlatform::IndexedVersion& a, const ModPlatform::IndexedVersion& b) -> bool {
// dates are in RFC 3339 format
return a.date > b.date;
};
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
pack.versions = unsortedVersions;
pack.versionsLoaded = true;
}
auto Modrinth::loadIndexedPackVersion(QJsonObject &obj) -> ModPlatform::IndexedVersion
{
ModPlatform::IndexedVersion file;
file.addonId = Json::requireString(obj, "project_id");
file.fileId = Json::requireString(obj, "id");
file.date = Json::requireString(obj, "date_published");
auto versionArray = Json::requireArray(obj, "game_versions");
if (versionArray.empty()) { continue; }
if (versionArray.empty()) {
return {};
}
for (auto mcVer : versionArray) {
file.mcVersion.append(mcVer.toString());
}
@ -100,14 +119,8 @@ void Modrinth::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
if (hash_list.contains(ProviderCaps.hashType(ModPlatform::Provider::MODRINTH)))
file.hash = Json::requireString(hash_list, ProviderCaps.hashType(ModPlatform::Provider::MODRINTH));
unsortedVersions.append(file);
return file;
}
}
auto orderSortPredicate = [](const ModPlatform::IndexedVersion& a, const ModPlatform::IndexedVersion& b) -> bool {
// dates are in RFC 3339 format
return a.date > b.date;
};
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
pack.versions = unsortedVersions;
pack.versionsLoaded = true;
return {};
}

View File

@ -29,5 +29,6 @@ void loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
QJsonArray& arr,
const shared_qobject_ptr<QNetworkAccessManager>& network,
BaseInstance* inst);
auto loadIndexedPackVersion(QJsonObject& obj) -> ModPlatform::IndexedVersion;
} // namespace Modrinth