refactor: de-duplicate common code in network mod APIs
This commit is contained in:
parent
f714adf6d2
commit
16bfafa29e
@ -492,6 +492,16 @@ set(META_SOURCES
|
||||
meta/Index.h
|
||||
)
|
||||
|
||||
set(API_SOURCES
|
||||
modplatform/ModAPI.h
|
||||
|
||||
modplatform/flame/FlameAPI.h
|
||||
modplatform/modrinth/ModrinthAPI.h
|
||||
|
||||
modplatform/helpers/NetworkModAPI.h
|
||||
modplatform/helpers/NetworkModAPI.cpp
|
||||
)
|
||||
|
||||
set(FTB_SOURCES
|
||||
modplatform/legacy_ftb/PackFetchTask.h
|
||||
modplatform/legacy_ftb/PackFetchTask.cpp
|
||||
@ -572,6 +582,7 @@ set(LOGIC_SOURCES
|
||||
${TOOLS_SOURCES}
|
||||
${META_SOURCES}
|
||||
${ICONS_SOURCES}
|
||||
${API_SOURCES}
|
||||
${FTB_SOURCES}
|
||||
${FLAME_SOURCES}
|
||||
${MODRINTH_SOURCES}
|
||||
|
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QString>
|
||||
|
||||
namespace ModPlatform {
|
||||
@ -25,6 +24,6 @@ class ModAPI {
|
||||
QString version;
|
||||
};
|
||||
|
||||
inline virtual void searchMods(CallerType* caller, SearchArgs&& args) const {};
|
||||
inline virtual void getVersions(CallerType* caller, const QString& addonId, const QString& debugName = "") const {};
|
||||
virtual void searchMods(CallerType* caller, SearchArgs&& args) const = 0;
|
||||
virtual void getVersions(CallerType* caller, const QString& addonId) const = 0;
|
||||
};
|
||||
|
@ -1,67 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include "modplatform/ModAPI.h"
|
||||
#include "ui/pages/modplatform/ModModel.h"
|
||||
|
||||
#include "Application.h"
|
||||
#include "net/NetJob.h"
|
||||
|
||||
class FlameAPI : public ModAPI {
|
||||
public:
|
||||
inline void searchMods(CallerType* caller, SearchArgs&& args) const override
|
||||
{
|
||||
auto netJob = new NetJob(QString("Flame::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 = "Flame") 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();
|
||||
};
|
||||
#include "modplatform/helpers/NetworkModAPI.h"
|
||||
|
||||
class FlameAPI : public NetworkModAPI {
|
||||
private:
|
||||
inline QString getModSearchURL(SearchArgs& args) const
|
||||
{
|
||||
@ -88,6 +29,4 @@ class FlameAPI : public ModAPI {
|
||||
{
|
||||
return QString("https://addons-ecs.forgesvc.net/api/v2/addon/%1/files").arg(addonId);
|
||||
};
|
||||
|
||||
inline QString getAuthorURL(const QString& name) const { return ""; };
|
||||
};
|
||||
|
60
launcher/modplatform/helpers/NetworkModAPI.cpp
Normal file
60
launcher/modplatform/helpers/NetworkModAPI.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
#include "NetworkModAPI.h"
|
||||
|
||||
#include "ui/pages/modplatform/ModModel.h"
|
||||
|
||||
#include "Application.h"
|
||||
#include "net/NetJob.h"
|
||||
|
||||
void NetworkModAPI::searchMods(CallerType* caller, SearchArgs&& args) const
|
||||
{
|
||||
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 " << caller->debugName() << " at " << parse_error.offset
|
||||
<< " reason: " << parse_error.errorString();
|
||||
qWarning() << *response;
|
||||
return;
|
||||
}
|
||||
|
||||
caller->searchRequestFinished(doc);
|
||||
});
|
||||
|
||||
netJob->start();
|
||||
};
|
||||
|
||||
void NetworkModAPI::getVersions(CallerType* caller, const QString& addonId) const
|
||||
{
|
||||
auto netJob = new NetJob(QString("%1::ModVersions(%2)").arg(caller->debugName()).arg(addonId), APPLICATION->network());
|
||||
auto response = new QByteArray();
|
||||
|
||||
netJob->addNetAction(Net::Download::makeByteArray(getVersionsURL(addonId), response));
|
||||
|
||||
QObject::connect(netJob, &NetJob::succeeded, caller, [response, 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 " << caller->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();
|
||||
};
|
13
launcher/modplatform/helpers/NetworkModAPI.h
Normal file
13
launcher/modplatform/helpers/NetworkModAPI.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "modplatform/ModAPI.h"
|
||||
|
||||
class NetworkModAPI : public ModAPI {
|
||||
public:
|
||||
void searchMods(CallerType* caller, SearchArgs&& args) const override;
|
||||
void getVersions(CallerType* caller, const QString& addonId) const override;
|
||||
|
||||
protected:
|
||||
virtual QString getModSearchURL(SearchArgs& args) const = 0;
|
||||
virtual QString getVersionsURL(const QString& addonId) const = 0;
|
||||
};
|
@ -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;
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -53,6 +53,11 @@ QVariant ListModel::data(const QModelIndex& index, int role) const
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
QString ListModel::debugName() const
|
||||
{
|
||||
return m_parent->debugName();
|
||||
}
|
||||
|
||||
void ListModel::logoLoaded(QString logo, QIcon out)
|
||||
{
|
||||
m_loadingLogos.removeAll(logo);
|
||||
@ -99,8 +104,9 @@ void ListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallbac
|
||||
}
|
||||
}
|
||||
|
||||
void ListModel::requestModVersions(ModPlatform::IndexedPack const& current) {
|
||||
m_parent->apiProvider()->getVersions(this, current.addonId.toString(), m_parent->debugName());
|
||||
void ListModel::requestModVersions(ModPlatform::IndexedPack const& current)
|
||||
{
|
||||
m_parent->apiProvider()->getVersions(this, current.addonId.toString());
|
||||
}
|
||||
|
||||
void ListModel::performPaginatedSearch()
|
||||
|
@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include <qjsondocument.h>
|
||||
#include <QAbstractListModel>
|
||||
|
||||
#include "modplatform/ModAPI.h"
|
||||
@ -24,6 +23,9 @@ class ListModel : public QAbstractListModel {
|
||||
int rowCount(const QModelIndex& parent) const override;
|
||||
int columnCount(const QModelIndex& parent) const override;
|
||||
|
||||
QString debugName() const;
|
||||
|
||||
/* Retrieve information from the model at a given index with the given role */
|
||||
QVariant data(const QModelIndex& index, int role) const override;
|
||||
Qt::ItemFlags flags(const QModelIndex& index) const override;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user