pollymc/launcher/modplatform/flame/PackManifest.cpp

99 lines
3.6 KiB
C++
Raw Normal View History

#include "PackManifest.h"
#include "Json.h"
2022-05-08 12:52:50 +05:30
static void loadFileV1(Flame::File& f, QJsonObject& file)
{
2018-07-15 18:21:05 +05:30
f.projectId = Json::requireInteger(file, "projectID");
f.fileId = Json::requireInteger(file, "fileID");
f.required = Json::ensureBoolean(file, QString("required"), true);
}
2022-05-08 12:52:50 +05:30
static void loadModloaderV1(Flame::Modloader& m, QJsonObject& modLoader)
{
2018-07-15 18:21:05 +05:30
m.id = Json::requireString(modLoader, "id");
m.primary = Json::ensureBoolean(modLoader, QString("primary"), false);
}
2022-05-08 12:52:50 +05:30
static void loadMinecraftV1(Flame::Minecraft& m, QJsonObject& minecraft)
{
2018-07-15 18:21:05 +05:30
m.version = Json::requireString(minecraft, "version");
// extra libraries... apparently only used for a custom Minecraft launcher in the 1.2.5 FTB retro pack
// intended use is likely hardcoded in the 'Flame' client, the manifest says nothing
m.libraries = Json::ensureString(minecraft, QString("libraries"), QString());
auto arr = Json::ensureArray(minecraft, "modLoaders", QJsonArray());
2022-05-08 12:52:50 +05:30
for (QJsonValueRef item : arr) {
2018-07-15 18:21:05 +05:30
auto obj = Json::requireObject(item);
Flame::Modloader loader;
loadModloaderV1(loader, obj);
m.modLoaders.append(loader);
}
}
2022-05-08 12:52:50 +05:30
static void loadManifestV1(Flame::Manifest& m, QJsonObject& manifest)
{
2018-07-15 18:21:05 +05:30
auto mc = Json::requireObject(manifest, "minecraft");
loadMinecraftV1(m.minecraft, mc);
m.name = Json::ensureString(manifest, QString("name"), "Unnamed");
m.version = Json::ensureString(manifest, QString("version"), QString());
2022-05-08 12:52:50 +05:30
m.author = Json::ensureString(manifest, QString("author"), "Anonymous");
2018-07-15 18:21:05 +05:30
auto arr = Json::ensureArray(manifest, "files", QJsonArray());
2022-05-08 12:52:50 +05:30
for (QJsonValueRef item : arr) {
2018-07-15 18:21:05 +05:30
auto obj = Json::requireObject(item);
Flame::File file;
loadFileV1(file, obj);
m.files.append(file);
}
m.overrides = Json::ensureString(manifest, "overrides", "overrides");
}
2022-05-08 12:52:50 +05:30
void Flame::loadManifest(Flame::Manifest& m, const QString& filepath)
{
2018-07-15 18:21:05 +05:30
auto doc = Json::requireDocument(filepath);
auto obj = Json::requireObject(doc);
m.manifestType = Json::requireString(obj, "manifestType");
2022-05-08 12:52:50 +05:30
if (m.manifestType != "minecraftModpack") {
2018-07-15 18:21:05 +05:30
throw JSONValidationError("Not a modpack manifest!");
}
m.manifestVersion = Json::requireInteger(obj, "manifestVersion");
2022-05-08 12:52:50 +05:30
if (m.manifestVersion != 1) {
2018-07-15 18:21:05 +05:30
throw JSONValidationError(QString("Unknown manifest version (%1)").arg(m.manifestVersion));
}
loadManifestV1(m, obj);
}
bool Flame::File::parseFromBytes(const QByteArray& bytes)
{
auto doc = Json::requireDocument(bytes);
2022-05-08 12:52:50 +05:30
if (!doc.isObject()) {
throw JSONValidationError(QString("data is not an object? that's not supposed to happen"));
}
2022-05-08 12:52:50 +05:30
auto obj = Json::ensureObject(doc.object(), "data");
fileName = Json::requireString(obj, "fileName");
// This is a piece of a Flame project JSON pulled out into the file metadata (here) for convenience
// It is also optional
2022-05-08 12:52:50 +05:30
type = File::Type::SingleFile;
if (fileName.endsWith(".zip")) {
// this is probably a resource pack
targetFolder = "resourcepacks";
} else {
// this is probably a mod, dunno what else could modpacks download
targetFolder = "mods";
}
2022-05-21 01:36:36 +05:30
QString rawUrl = Json::ensureString(obj, "downloadUrl");
if(rawUrl.isEmpty()){
2022-05-21 00:49:19 +05:30
//either there somehow is an emtpy string as a link, or it's null either way it's invalid
//soft failing
return false;
}
url = QUrl(rawUrl, QUrl::TolerantMode);
if (!url.isValid()) {
throw JSONValidationError(QString("Invalid URL: %1").arg(rawUrl));
}
resolved = true;
return true;
}