refactor: move individual pack version parsing to its own function
This commit is contained in:
parent
67e0214fa5
commit
5c5699bba5
@ -59,12 +59,31 @@ void Modrinth::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
|
|||||||
|
|
||||||
for (auto versionIter : arr) {
|
for (auto versionIter : arr) {
|
||||||
auto obj = versionIter.toObject();
|
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;
|
ModPlatform::IndexedVersion file;
|
||||||
|
|
||||||
file.addonId = Json::requireString(obj, "project_id");
|
file.addonId = Json::requireString(obj, "project_id");
|
||||||
file.fileId = Json::requireString(obj, "id");
|
file.fileId = Json::requireString(obj, "id");
|
||||||
file.date = Json::requireString(obj, "date_published");
|
file.date = Json::requireString(obj, "date_published");
|
||||||
auto versionArray = Json::requireArray(obj, "game_versions");
|
auto versionArray = Json::requireArray(obj, "game_versions");
|
||||||
if (versionArray.empty()) { continue; }
|
if (versionArray.empty()) {
|
||||||
|
return {};
|
||||||
|
}
|
||||||
for (auto mcVer : versionArray) {
|
for (auto mcVer : versionArray) {
|
||||||
file.mcVersion.append(mcVer.toString());
|
file.mcVersion.append(mcVer.toString());
|
||||||
}
|
}
|
||||||
@ -81,12 +100,12 @@ void Modrinth::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
|
|||||||
// Will default to the last one if there's no primary (though I think Modrinth requires that
|
// Will default to the last one if there's no primary (though I think Modrinth requires that
|
||||||
// at least one file is primary, idk)
|
// at least one file is primary, idk)
|
||||||
// NOTE: files.count() is 1-indexed, so we need to subtract 1 to become 0-indexed
|
// NOTE: files.count() is 1-indexed, so we need to subtract 1 to become 0-indexed
|
||||||
while (i < files.count() - 1){
|
while (i < files.count() - 1) {
|
||||||
auto parent = files[i].toObject();
|
auto parent = files[i].toObject();
|
||||||
auto fileName = Json::requireString(parent, "filename");
|
auto fileName = Json::requireString(parent, "filename");
|
||||||
|
|
||||||
// Grab the primary file, if available
|
// Grab the primary file, if available
|
||||||
if(Json::requireBoolean(parent, "primary"))
|
if (Json::requireBoolean(parent, "primary"))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
@ -97,17 +116,11 @@ void Modrinth::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
|
|||||||
file.downloadUrl = Json::requireString(parent, "url");
|
file.downloadUrl = Json::requireString(parent, "url");
|
||||||
file.fileName = Json::requireString(parent, "filename");
|
file.fileName = Json::requireString(parent, "filename");
|
||||||
auto hash_list = Json::requireObject(parent, "hashes");
|
auto hash_list = Json::requireObject(parent, "hashes");
|
||||||
if(hash_list.contains(ProviderCaps.hashType(ModPlatform::Provider::MODRINTH)))
|
if (hash_list.contains(ProviderCaps.hashType(ModPlatform::Provider::MODRINTH)))
|
||||||
file.hash = Json::requireString(hash_list, 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 {
|
return {};
|
||||||
// dates are in RFC 3339 format
|
|
||||||
return a.date > b.date;
|
|
||||||
};
|
|
||||||
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
|
|
||||||
pack.versions = unsortedVersions;
|
|
||||||
pack.versionsLoaded = true;
|
|
||||||
}
|
}
|
||||||
|
@ -29,5 +29,6 @@ void loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
|
|||||||
QJsonArray& arr,
|
QJsonArray& arr,
|
||||||
const shared_qobject_ptr<QNetworkAccessManager>& network,
|
const shared_qobject_ptr<QNetworkAccessManager>& network,
|
||||||
BaseInstance* inst);
|
BaseInstance* inst);
|
||||||
|
auto loadIndexedPackVersion(QJsonObject& obj) -> ModPlatform::IndexedVersion;
|
||||||
|
|
||||||
} // namespace Modrinth
|
} // namespace Modrinth
|
||||||
|
Loading…
x
Reference in New Issue
Block a user