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:
@ -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
|
||||
|
@ -15,18 +15,68 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QMetaType>
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QCryptographicHash>
|
||||
#include <QString>
|
||||
#include <QUrl>
|
||||
|
||||
class MinecraftInstance;
|
||||
|
||||
namespace Modrinth {
|
||||
|
||||
struct File
|
||||
{
|
||||
QString path;
|
||||
|
||||
QCryptographicHash::Algorithm hashAlgorithm;
|
||||
QByteArray hash;
|
||||
// TODO: should this support multiple download URLs, like the JSON does?
|
||||
QUrl download;
|
||||
};
|
||||
|
||||
struct ModpackExtra {
|
||||
QString body;
|
||||
|
||||
QString sourceUrl;
|
||||
QString wikiUrl;
|
||||
};
|
||||
|
||||
struct ModpackVersion {
|
||||
QString name;
|
||||
QString version;
|
||||
|
||||
QString id;
|
||||
QString project_id;
|
||||
|
||||
QString date;
|
||||
|
||||
QString download_url;
|
||||
};
|
||||
|
||||
struct Modpack {
|
||||
QString id;
|
||||
|
||||
QString name;
|
||||
QString description;
|
||||
QStringList authors;
|
||||
QString iconName;
|
||||
QUrl iconUrl;
|
||||
|
||||
bool versionsLoaded = false;
|
||||
bool extraInfoLoaded = false;
|
||||
|
||||
ModpackExtra extra;
|
||||
QVector<ModpackVersion> versions;
|
||||
};
|
||||
|
||||
void loadIndexedPack(Modpack&, QJsonObject&);
|
||||
void loadIndexedInfo(Modpack&, QJsonObject&);
|
||||
void loadIndexedVersions(Modpack&, QJsonDocument&);
|
||||
auto loadIndexedVersion(QJsonObject&) -> ModpackVersion;
|
||||
|
||||
}
|
||||
|
||||
Q_DECLARE_METATYPE(Modrinth::Modpack);
|
||||
Q_DECLARE_METATYPE(Modrinth::ModpackVersion);
|
||||
|
Reference in New Issue
Block a user