aboutsummaryrefslogtreecommitdiff
path: root/launcher/modplatform/modrinth
diff options
context:
space:
mode:
authorflow <thiagodonato300@gmail.com>2022-03-07 16:46:08 -0300
committerflow <thiagodonato300@gmail.com>2022-03-07 17:45:28 -0300
commit16bfafa29e2cb54e1553c813cab0fff5203f8c60 (patch)
tree6ecb63a86a07b066f71fb0f4442555b4c29c528b /launcher/modplatform/modrinth
parentf714adf6d2cc94f20ba37f2776d0d61e22267f0e (diff)
downloadPrismLauncher-16bfafa29e2cb54e1553c813cab0fff5203f8c60.tar.gz
PrismLauncher-16bfafa29e2cb54e1553c813cab0fff5203f8c60.tar.bz2
PrismLauncher-16bfafa29e2cb54e1553c813cab0fff5203f8c60.zip
refactor: de-duplicate common code in network mod APIs
Diffstat (limited to 'launcher/modplatform/modrinth')
-rw-r--r--launcher/modplatform/modrinth/ModrinthAPI.h74
1 files changed, 10 insertions, 64 deletions
diff --git a/launcher/modplatform/modrinth/ModrinthAPI.h b/launcher/modplatform/modrinth/ModrinthAPI.h
index 84cc561b..1cc51ff2 100644
--- a/launcher/modplatform/modrinth/ModrinthAPI.h
+++ b/launcher/modplatform/modrinth/ModrinthAPI.h
@@ -1,73 +1,15 @@
#pragma once
-#include "modplatform/ModAPI.h"
-#include "ui/pages/modplatform/ModModel.h"
-
-#include "Application.h"
-#include "net/NetJob.h"
+#include "modplatform/helpers/NetworkModAPI.h"
#include <QDebug>
-class ModrinthAPI : public ModAPI {
+class ModrinthAPI : public NetworkModAPI {
public:
- inline void searchMods(CallerType* caller, SearchArgs&& args) const override
- {
- auto netJob = new NetJob(QString("Modrinth::Search"), APPLICATION->network());
- auto searchUrl = getModSearchURL(args);
-
- auto response = new QByteArray();
- netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), response));
-
- QObject::connect(netJob, &NetJob::started, caller, [caller, netJob]{ caller->setActiveJob(netJob); });
- QObject::connect(netJob, &NetJob::failed, caller, &CallerType::searchRequestFailed);
- QObject::connect(netJob, &NetJob::succeeded, caller, [caller, response] {
- QJsonParseError parse_error;
- QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
- if (parse_error.error != QJsonParseError::NoError) {
- qWarning() << "Error while parsing JSON response from Modrinth at " << parse_error.offset
- << " reason: " << parse_error.errorString();
- qWarning() << *response;
- return;
- }
-
- caller->searchRequestFinished(doc);
- });
-
- netJob->start();
- };
-
- inline void getVersions(CallerType* caller, const QString& addonId, const QString& debugName = "Modrinth") const override
- {
- auto netJob = new NetJob(QString("%1::ModVersions(%2)").arg(debugName).arg(addonId), APPLICATION->network());
- auto response = new QByteArray();
-
- netJob->addNetAction(Net::Download::makeByteArray(getVersionsURL(addonId), response));
-
- QObject::connect(netJob, &NetJob::succeeded, caller, [response, debugName, caller, addonId] {
- QJsonParseError parse_error;
- QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
- if (parse_error.error != QJsonParseError::NoError) {
- qWarning() << "Error while parsing JSON response from " << debugName << " at " << parse_error.offset
- << " reason: " << parse_error.errorString();
- qWarning() << *response;
- return;
- }
-
- caller->versionRequestSucceeded(doc, addonId);
- });
-
- QObject::connect(netJob, &NetJob::finished, caller, [response, netJob] {
- netJob->deleteLater();
- delete response;
- });
-
- netJob->start();
- };
-
inline QString getAuthorURL(const QString& name) const { return "https://modrinth.com/user/" + name; };
private:
- inline QString getModSearchURL(SearchArgs& args) const
+ inline QString getModSearchURL(SearchArgs& args) const override
{
if (!validateModLoader(args.mod_loader)) {
qWarning() << "Modrinth only have Forge and Fabric-compatible mods!";
@@ -88,13 +30,11 @@ class ModrinthAPI : public ModAPI {
.arg(args.version);
};
- inline QString getVersionsURL(const QString& addonId) const
+ inline QString getVersionsURL(const QString& addonId) const override
{
return QString("https://api.modrinth.com/v2/project/%1/version").arg(addonId);
};
- inline bool validateModLoader(ModLoaderType modLoader) const { return modLoader == Any || modLoader == Forge || modLoader == Fabric; }
-
inline QString getModLoaderString(ModLoaderType modLoader) const
{
switch (modLoader) {
@@ -108,4 +48,10 @@ class ModrinthAPI : public ModAPI {
return "";
}
}
+
+ inline bool validateModLoader(ModLoaderType modLoader) const
+ {
+ return modLoader == Any || modLoader == Forge || modLoader == Fabric;
+ }
+
};