feat: add modrinth pack downloading

Things that don't work / work poorly (there's more for sure but those
are the evident ones):
    - Icons are broken in the import dialog
    - No way to search for private packs
    - Icons are not downloaded when downloading a mod
    - No support for multiple download URLs
    - Probably a lot more...
This commit is contained in:
flow
2022-05-14 20:17:05 -03:00
parent 31988f0529
commit 4fda35b466
7 changed files with 729 additions and 36 deletions

View File

@ -14,3 +14,87 @@
*/
#include "ModrinthPackManifest.h"
#include "Json.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
namespace Modrinth {
void loadIndexedPack(Modpack& pack, QJsonObject& obj)
{
pack.id = Json::ensureString(obj, "project_id");
pack.name = Json::ensureString(obj, "title");
pack.description = Json::ensureString(obj, "description");
pack.authors << Json::ensureString(obj, "author");
pack.iconName = QString("modrinth_%1").arg(Json::ensureString(obj, "slug"));
pack.iconUrl = Json::ensureString(obj, "icon_url");
}
void loadIndexedInfo(Modpack& pack, QJsonObject& obj)
{
pack.extra.body = Json::ensureString(obj, "body");
pack.extra.sourceUrl = Json::ensureString(obj, "source_url");
pack.extra.wikiUrl = Json::ensureString(obj, "wiki_url");
pack.extraInfoLoaded = true;
}
void loadIndexedVersions(Modpack& pack, QJsonDocument& doc)
{
QVector<ModpackVersion> unsortedVersions;
auto arr = Json::requireArray(doc);
for (auto versionIter : arr) {
auto obj = Json::requireObject(versionIter);
auto file = loadIndexedVersion(obj);
if(!file.id.isEmpty()) // Heuristic to check if the returned value is valid
unsortedVersions.append(file);
}
auto orderSortPredicate = [](const ModpackVersion& a, const ModpackVersion& b) -> bool {
// dates are in RFC 3339 format
return a.date > b.date;
};
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
pack.versions.swap(unsortedVersions);
pack.versionsLoaded = true;
}
auto loadIndexedVersion(QJsonObject &obj) -> ModpackVersion
{
ModpackVersion file;
file.name = Json::requireString(obj, "name");
file.version = Json::requireString(obj, "version_number");
file.id = Json::requireString(obj, "id");
file.project_id = Json::requireString(obj, "project_id");
file.date = Json::requireString(obj, "date_published");
auto files = Json::requireArray(obj, "files");
for (auto file_iter : files) {
File indexed_file;
auto parent = Json::requireObject(file_iter);
if (!Json::ensureBoolean(parent, "primary", false)) {
continue;
}
file.download_url = Json::requireString(parent, "url");
break;
}
if(file.download_url.isEmpty())
return {};
return file;
}
} // namespace Modrinth