2022-01-14 14:13:42 +05:30
|
|
|
#include "ModrinthPackIndex.h"
|
2022-03-03 07:31:23 +05:30
|
|
|
#include "ModrinthAPI.h"
|
2022-01-14 14:13:42 +05:30
|
|
|
|
|
|
|
#include "Json.h"
|
2022-01-15 14:55:24 +05:30
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
2022-03-03 03:05:59 +05:30
|
|
|
#include "net/NetJob.h"
|
2022-01-15 14:55:24 +05:30
|
|
|
|
2022-03-03 07:31:23 +05:30
|
|
|
static ModrinthAPI api;
|
|
|
|
|
2022-03-03 03:05:59 +05:30
|
|
|
void Modrinth::loadIndexedPack(ModPlatform::IndexedPack& pack, QJsonObject& obj)
|
2022-01-14 14:13:42 +05:30
|
|
|
{
|
2022-01-31 21:48:11 +05:30
|
|
|
pack.addonId = Json::requireString(obj, "project_id");
|
2022-01-14 14:13:42 +05:30
|
|
|
pack.name = Json::requireString(obj, "title");
|
|
|
|
pack.websiteUrl = Json::ensureString(obj, "page_url", "");
|
|
|
|
pack.description = Json::ensureString(obj, "description", "");
|
|
|
|
|
|
|
|
pack.logoUrl = Json::requireString(obj, "icon_url");
|
2022-03-03 03:05:59 +05:30
|
|
|
pack.logoName = pack.addonId.toString();
|
2022-01-14 14:13:42 +05:30
|
|
|
|
2022-03-03 03:05:59 +05:30
|
|
|
ModPlatform::ModpackAuthor modAuthor;
|
2022-01-24 11:53:01 +05:30
|
|
|
modAuthor.name = Json::requireString(obj, "author");
|
2022-03-03 07:31:23 +05:30
|
|
|
modAuthor.url = api.getAuthorURL(modAuthor.name);
|
2022-03-03 03:05:59 +05:30
|
|
|
pack.authors.append(modAuthor);
|
2022-01-14 14:13:42 +05:30
|
|
|
}
|
|
|
|
|
2022-03-03 03:05:59 +05:30
|
|
|
void Modrinth::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
|
|
|
|
QJsonArray& arr,
|
|
|
|
const shared_qobject_ptr<QNetworkAccessManager>& network,
|
|
|
|
BaseInstance* inst)
|
2022-01-14 14:13:42 +05:30
|
|
|
{
|
2022-03-03 03:05:59 +05:30
|
|
|
QVector<ModPlatform::IndexedVersion> unsortedVersions;
|
|
|
|
bool hasFabric = !((MinecraftInstance*)inst)->getPackProfile()->getComponentVersion("net.fabricmc.fabric-loader").isEmpty();
|
|
|
|
QString mcVersion = ((MinecraftInstance*)inst)->getPackProfile()->getComponentVersion("net.minecraft");
|
2022-01-15 14:55:24 +05:30
|
|
|
|
2022-03-03 03:05:59 +05:30
|
|
|
for (auto versionIter : arr) {
|
2022-01-14 14:13:42 +05:30
|
|
|
auto obj = versionIter.toObject();
|
2022-03-03 03:05:59 +05:30
|
|
|
ModPlatform::IndexedVersion file;
|
|
|
|
file.addonId = Json::requireString(obj, "project_id");
|
2022-01-14 14:13:42 +05:30
|
|
|
file.fileId = Json::requireString(obj, "id");
|
|
|
|
file.date = Json::requireString(obj, "date_published");
|
|
|
|
auto versionArray = Json::requireArray(obj, "game_versions");
|
2022-03-03 03:05:59 +05:30
|
|
|
if (versionArray.empty()) { continue; }
|
|
|
|
for (auto mcVer : versionArray) {
|
2022-01-15 13:21:47 +05:30
|
|
|
file.mcVersion.append(mcVer.toString());
|
|
|
|
}
|
2022-03-03 03:05:59 +05:30
|
|
|
auto loaders = Json::requireArray(obj, "loaders");
|
|
|
|
for (auto loader : loaders) {
|
2022-01-15 13:21:47 +05:30
|
|
|
file.loaders.append(loader.toString());
|
|
|
|
}
|
2022-01-14 14:13:42 +05:30
|
|
|
file.version = Json::requireString(obj, "name");
|
2022-01-15 14:55:24 +05:30
|
|
|
|
|
|
|
auto files = Json::requireArray(obj, "files");
|
|
|
|
int i = 0;
|
2022-03-03 03:05:59 +05:30
|
|
|
while (files.count() > 1 && i < files.count()) {
|
|
|
|
// try to resolve the correct file
|
2022-01-15 14:55:24 +05:30
|
|
|
auto parent = files[i].toObject();
|
|
|
|
auto fileName = Json::requireString(parent, "filename");
|
2022-03-03 03:05:59 +05:30
|
|
|
// avoid grabbing "dev" files
|
|
|
|
if (fileName.contains("javadocs", Qt::CaseInsensitive) || fileName.contains("sources", Qt::CaseInsensitive)) {
|
2022-01-15 14:55:24 +05:30
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-03 03:05:59 +05:30
|
|
|
// grab the correct mod loader
|
|
|
|
if (fileName.contains("forge", Qt::CaseInsensitive) || fileName.contains("fabric", Qt::CaseInsensitive)) {
|
|
|
|
if (hasFabric) {
|
|
|
|
if (fileName.contains("forge", Qt::CaseInsensitive)) {
|
2022-01-15 14:55:24 +05:30
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
2022-03-03 03:05:59 +05:30
|
|
|
} else {
|
|
|
|
if (fileName.contains("fabric", Qt::CaseInsensitive)) {
|
2022-01-15 14:55:24 +05:30
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
auto parent = files[i].toObject();
|
2022-03-03 03:05:59 +05:30
|
|
|
if (parent.contains("url")) {
|
2022-02-02 02:26:52 +05:30
|
|
|
file.downloadUrl = Json::requireString(parent, "url");
|
|
|
|
file.fileName = Json::requireString(parent, "filename");
|
2022-01-15 13:21:47 +05:30
|
|
|
|
2022-02-02 02:26:52 +05:30
|
|
|
unsortedVersions.append(file);
|
|
|
|
}
|
2022-01-14 14:13:42 +05:30
|
|
|
}
|
2022-03-03 03:05:59 +05:30
|
|
|
auto orderSortPredicate = [](const ModPlatform::IndexedVersion& a, const ModPlatform::IndexedVersion& b) -> bool {
|
|
|
|
// dates are in RFC 3339 format
|
2022-01-14 14:13:42 +05:30
|
|
|
return a.date > b.date;
|
|
|
|
};
|
|
|
|
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
|
|
|
|
pack.versions = unsortedVersions;
|
|
|
|
pack.versionsLoaded = true;
|
|
|
|
}
|