diff options
Diffstat (limited to 'api/logic/minecraft/curse')
-rw-r--r-- | api/logic/minecraft/curse/FileResolvingTask.cpp | 64 | ||||
-rw-r--r-- | api/logic/minecraft/curse/FileResolvingTask.h | 32 | ||||
-rw-r--r-- | api/logic/minecraft/curse/PackManifest.cpp | 63 | ||||
-rw-r--r-- | api/logic/minecraft/curse/PackManifest.h | 45 |
4 files changed, 204 insertions, 0 deletions
diff --git a/api/logic/minecraft/curse/FileResolvingTask.cpp b/api/logic/minecraft/curse/FileResolvingTask.cpp new file mode 100644 index 00000000..13308202 --- /dev/null +++ b/api/logic/minecraft/curse/FileResolvingTask.cpp @@ -0,0 +1,64 @@ +#include "FileResolvingTask.h" +#include "Json.h" + +const char * metabase = "https://cursemeta.dries007.net"; + +Curse::FileResolvingTask::FileResolvingTask(QVector<Curse::File>& toProcess) + : m_toProcess(toProcess) +{ +} + +void Curse::FileResolvingTask::executeTask() +{ + m_dljob.reset(new NetJob("Curse file resolver")); + results.resize(m_toProcess.size()); + int index = 0; + for(auto & file: m_toProcess) + { + auto projectIdStr = QString::number(file.projectId); + auto fileIdStr = QString::number(file.fileId); + QString metaurl = QString("%1/%2/%3.json").arg(metabase, projectIdStr, fileIdStr); + auto dl = Net::Download::makeByteArray(QUrl(metaurl), &results[index]); + m_dljob->addNetAction(dl); + index ++; + } + connect(m_dljob.get(), &NetJob::finished, this, &Curse::FileResolvingTask::netJobFinished); + m_dljob->start(); +} + +void Curse::FileResolvingTask::netJobFinished() +{ + bool failed = false; + int index = 0; + for(auto & bytes: results) + { + try + { + auto doc = Json::requireDocument(bytes); + auto obj = Json::requireObject(doc); + // result code signifies true failure. + if(obj.contains("code")) + { + failed = true; + continue; + } + auto & out = m_toProcess[index]; + out.fileName = Json::requireString(obj, "FileNameOnDisk"); + out.url = Json::requireString(obj, "DownloadURL"); + out.resolved = true; + } + catch(JSONValidationError & e) + { + failed = true; + } + index++; + } + if(!failed) + { + emitSucceeded(); + } + else + { + emitFailed(tr("Some curse ID resolving tasks failed.")); + } +} diff --git a/api/logic/minecraft/curse/FileResolvingTask.h b/api/logic/minecraft/curse/FileResolvingTask.h new file mode 100644 index 00000000..b7ca85d3 --- /dev/null +++ b/api/logic/minecraft/curse/FileResolvingTask.h @@ -0,0 +1,32 @@ +#pragma once + +#include "tasks/Task.h" +#include "net/NetJob.h" +#include "PackManifest.h" + +#include "multimc_logic_export.h" + +namespace Curse +{ +class MULTIMC_LOGIC_EXPORT FileResolvingTask : public Task +{ + Q_OBJECT +public: + explicit FileResolvingTask(QVector<Curse::File> &toProcess); + const QVector<Curse::File> &getResults() const + { + return m_toProcess; + } + +protected: + virtual void executeTask() override; + +protected slots: + void netJobFinished(); + +private: /* data */ + QVector<Curse::File> m_toProcess; + QVector<QByteArray> results; + NetJobPtr m_dljob; +}; +} diff --git a/api/logic/minecraft/curse/PackManifest.cpp b/api/logic/minecraft/curse/PackManifest.cpp new file mode 100644 index 00000000..a4ea703b --- /dev/null +++ b/api/logic/minecraft/curse/PackManifest.cpp @@ -0,0 +1,63 @@ +#include "PackManifest.h" +#include "Json.h" + +static void loadFileV1(Curse::File & f, QJsonObject & file) +{ + f.projectId = Json::requireInteger(file, "projectID"); + f.fileId = Json::requireInteger(file, "fileID"); + f.required = Json::requireBoolean(file, "required"); +} + +static void loadModloaderV1(Curse::Modloader & m, QJsonObject & modLoader) +{ + m.id = Json::requireString(modLoader, "id"); + m.primary = Json::ensureBoolean(modLoader, "primary", false); +} + +static void loadMinecraftV1(Curse::Minecraft & m, QJsonObject & minecraft) +{ + m.version = Json::requireString(minecraft, "version"); + auto arr = Json::ensureArray(minecraft, "modLoaders", QJsonArray()); + for (const auto & item : arr) + { + auto obj = Json::requireObject(item); + Curse::Modloader loader; + loadModloaderV1(loader, obj); + m.modLoaders.append(loader); + } +} + +static void loadManifestV1(Curse::Manifest & m, QJsonObject & manifest) +{ + auto mc = Json::requireObject(manifest, "minecraft"); + loadMinecraftV1(m.minecraft, mc); + m.name = Json::requireString(manifest, "name"); + m.version = Json::requireString(manifest, "version"); + m.author = Json::requireString(manifest, "author"); + auto arr = Json::ensureArray(manifest, "files", QJsonArray()); + for (const auto & item : arr) + { + auto obj = Json::requireObject(item); + Curse::File file; + loadFileV1(file, obj); + m.files.append(file); + } + m.overrides = Json::ensureString(manifest, "overrides", "overrides"); +} + +void Curse::loadManifest(Curse::Manifest & m, const QString &filepath) +{ + auto doc = Json::requireDocument(filepath); + auto obj = Json::requireObject(doc); + m.manifestType = Json::requireString(obj, "manifestType"); + if(m.manifestType != "minecraftModpack") + { + throw JSONValidationError("Not a Curse modpack manifest!"); + } + m.manifestVersion = Json::requireInteger(obj, "manifestVersion"); + if(m.manifestVersion != 1) + { + throw JSONValidationError(QString("Unknown manifest version (%1)").arg(m.manifestVersion)); + } + loadManifestV1(m, obj); +} diff --git a/api/logic/minecraft/curse/PackManifest.h b/api/logic/minecraft/curse/PackManifest.h new file mode 100644 index 00000000..8b9602a4 --- /dev/null +++ b/api/logic/minecraft/curse/PackManifest.h @@ -0,0 +1,45 @@ +#pragma once + +#include <QString> +#include <QVector> + +namespace Curse +{ +struct File +{ + int projectId = 0; + int fileId = 0; + bool required = true; + + // our + bool resolved = false; + QString fileName; + QString url; +}; + +struct Modloader +{ + QString id; + bool primary = false; +}; + +struct Minecraft +{ + QString version; + QVector<Curse::Modloader> modLoaders; +}; + +struct Manifest +{ + QString manifestType; + int manifestVersion = 0; + Curse::Minecraft minecraft; + QString name; + QString version; + QString author; + QVector<Curse::File> files; + QString overrides; +}; + +void loadManifest(Curse::Manifest & m, const QString &filepath); +} |