pollymc/launcher/modplatform/flame/FlameModIndex.cpp

96 lines
3.9 KiB
C++
Raw Normal View History

2022-01-16 15:50:21 +05:30
#include "FlameModIndex.h"
2022-01-16 15:50:21 +05:30
#include "Json.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
#include "net/NetJob.h"
2022-01-16 15:50:21 +05:30
void FlameMod::loadIndexedPack(ModPlatform::IndexedPack& pack, QJsonObject& obj)
2022-01-16 15:50:21 +05:30
{
pack.addonId = Json::requireInteger(obj, "id");
pack.name = Json::requireString(obj, "name");
pack.websiteUrl = Json::ensureString(obj, "websiteUrl", "");
pack.description = Json::ensureString(obj, "summary", "");
bool thumbnailFound = false;
auto attachments = Json::requireArray(obj, "attachments");
for (auto attachmentRaw : attachments) {
2022-01-16 15:50:21 +05:30
auto attachmentObj = Json::requireObject(attachmentRaw);
bool isDefault = attachmentObj.value("isDefault").toBool(false);
if (isDefault) {
2022-01-16 15:50:21 +05:30
thumbnailFound = true;
pack.logoName = Json::requireString(attachmentObj, "title");
pack.logoUrl = Json::requireString(attachmentObj, "thumbnailUrl");
break;
}
}
if (!thumbnailFound) { throw JSONValidationError(QString("Pack without an icon, skipping: %1").arg(pack.name)); }
2022-01-16 15:50:21 +05:30
auto authors = Json::requireArray(obj, "authors");
for (auto authorIter : authors) {
2022-01-16 15:50:21 +05:30
auto author = Json::requireObject(authorIter);
ModPlatform::ModpackAuthor packAuthor;
2022-01-16 15:50:21 +05:30
packAuthor.name = Json::requireString(author, "name");
packAuthor.url = Json::requireString(author, "url");
pack.authors.append(packAuthor);
}
}
void FlameMod::loadIndexedPackVersions(ModPlatform::IndexedPack& pack,
QJsonArray& arr,
const shared_qobject_ptr<QNetworkAccessManager>& network,
BaseInstance* inst)
2022-01-16 15:50:21 +05:30
{
QVector<ModPlatform::IndexedVersion> unsortedVersions;
bool hasFabric = !(dynamic_cast<MinecraftInstance*>(inst))->getPackProfile()->getComponentVersion("net.fabricmc.fabric-loader").isEmpty();
QString mcVersion = (dynamic_cast<MinecraftInstance*>(inst))->getPackProfile()->getComponentVersion("net.minecraft");
2022-01-16 15:50:21 +05:30
for (auto versionIter : arr) {
2022-01-16 15:50:21 +05:30
auto obj = versionIter.toObject();
2022-01-16 15:50:21 +05:30
auto versionArray = Json::requireArray(obj, "gameVersion");
if (versionArray.isEmpty()) { continue; }
ModPlatform::IndexedVersion file;
for (auto mcVer : versionArray) {
2022-01-16 15:50:21 +05:30
file.mcVersion.append(mcVer.toString());
}
file.addonId = pack.addonId;
file.fileId = Json::requireInteger(obj, "id");
file.date = Json::requireString(obj, "fileDate");
2022-01-16 15:50:21 +05:30
file.version = Json::requireString(obj, "displayName");
file.downloadUrl = Json::requireString(obj, "downloadUrl");
file.fileName = Json::requireString(obj, "fileName");
auto modules = Json::requireArray(obj, "modules");
bool is_valid_fabric_version = false;
for (auto m : modules) {
auto fname = Json::requireString(m.toObject(), "foldername");
// FIXME: This does not work properly when a mod supports more than one mod loader, since
// they bundle the meta files for all of them in the same arquive, even when that version
// doesn't support the given mod loader.
if (hasFabric) {
if (fname == "fabric.mod.json") {
is_valid_fabric_version = true;
2022-01-16 15:50:21 +05:30
break;
}
} else
break;
// NOTE: Since we're not validating forge versions, we can just skip this loop.
2022-01-16 15:50:21 +05:30
}
if (hasFabric && !is_valid_fabric_version) continue;
2022-01-16 15:50:21 +05:30
unsortedVersions.append(file);
}
auto orderSortPredicate = [](const ModPlatform::IndexedVersion& a, const ModPlatform::IndexedVersion& b) -> bool {
// dates are in RFC 3339 format
2022-01-16 15:50:21 +05:30
return a.date > b.date;
};
std::sort(unsortedVersions.begin(), unsortedVersions.end(), orderSortPredicate);
pack.versions = unsortedVersions;
pack.versionsLoaded = true;
2022-02-02 02:53:34 +05:30
}