refactor: move NetJob away from ModModel to ModAPI

This is done so that 1. ModAPI behaves more like an actual API instead
of just a helper, and 2. Allows for more easily creating other mod
providers that may or may not use network tasks (foreshadowing lol)
This commit is contained in:
flow 2022-03-07 16:22:57 -03:00
parent 39bd04f06f
commit f714adf6d2
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
14 changed files with 230 additions and 135 deletions

View File

@ -1,21 +1,30 @@
#pragma once #pragma once
#include <QJsonDocument>
#include <QString> #include <QString>
namespace ModPlatform {
class ListModel;
}
class ModAPI { class ModAPI {
public: protected:
virtual ~ModAPI() = default; using CallerType = ModPlatform::ListModel;
// https://docs.curseforge.com/?http#tocS_ModLoaderType public:
enum ModLoaderType { virtual ~ModAPI() = default;
Any = 0,
Forge = 1,
Cauldron = 2,
LiteLoader = 3,
Fabric = 4
};
inline virtual QString getModSearchURL(int, QString, QString, ModLoaderType, QString) const { return ""; }; // https://docs.curseforge.com/?http#tocS_ModLoaderType
inline virtual QString getVersionsURL(const QString& addonId) const { return ""; }; enum ModLoaderType { Any = 0, Forge = 1, Cauldron = 2, LiteLoader = 3, Fabric = 4 };
inline virtual QString getAuthorURL(const QString& name) const { return ""; };
struct SearchArgs {
int offset;
QString search;
QString sorting;
ModLoaderType mod_loader;
QString version;
};
inline virtual void searchMods(CallerType* caller, SearchArgs&& args) const {};
inline virtual void getVersions(CallerType* caller, const QString& addonId, const QString& debugName = "") const {};
}; };

View File

@ -1,28 +1,93 @@
#pragma once #pragma once
#include "modplatform/ModAPI.h" #include "modplatform/ModAPI.h"
#include "ui/pages/modplatform/ModModel.h"
#include "Application.h"
#include "net/NetJob.h"
class FlameAPI : public ModAPI { class FlameAPI : public ModAPI {
public: public:
inline void searchMods(CallerType* caller, SearchArgs&& args) const override
inline QString getModSearchURL(int index, QString searchFilter, QString sort, ModLoaderType modLoader, QString version) const override
{ {
return QString("https://addons-ecs.forgesvc.net/api/v2/addon/search?" auto netJob = new NetJob(QString("Flame::Search"), APPLICATION->network());
"gameId=432&" "categoryId=0&" "sectionId=6&" auto searchUrl = getModSearchURL(args);
"index=%1&" "pageSize=25&" "searchFilter=%2&" auto response = new QByteArray();
"sort=%3&" "modLoaderType=%4&" "gameVersion=%5") netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), response));
.arg(index)
.arg(searchFilter) QObject::connect(netJob, &NetJob::started, caller, [caller, netJob]{ caller->setActiveJob(netJob); });
.arg(sort) QObject::connect(netJob, &NetJob::failed, caller, &CallerType::searchRequestFailed);
.arg(modLoader) QObject::connect(netJob, &NetJob::succeeded, caller, [caller, response] {
.arg(version); 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 QString getVersionsURL(const QString& addonId) const override 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();
};
private:
inline QString getModSearchURL(SearchArgs& args) const
{
return QString(
"https://addons-ecs.forgesvc.net/api/v2/addon/search?"
"gameId=432&"
"categoryId=0&"
"sectionId=6&"
"index=%1&"
"pageSize=25&"
"searchFilter=%2&"
"sort=%3&"
"modLoaderType=%4&"
"gameVersion=%5")
.arg(args.offset)
.arg(args.search)
.arg(args.sorting)
.arg(args.mod_loader)
.arg(args.version);
};
inline QString getVersionsURL(const QString& addonId) const
{ {
return QString("https://addons-ecs.forgesvc.net/api/v2/addon/%1/files").arg(addonId); return QString("https://addons-ecs.forgesvc.net/api/v2/addon/%1/files").arg(addonId);
}; };
inline QString getAuthorURL(const QString& name) const override { return ""; }; inline QString getAuthorURL(const QString& name) const { return ""; };
}; };

View File

@ -1,50 +1,111 @@
#pragma once #pragma once
#include "modplatform/ModAPI.h" #include "modplatform/ModAPI.h"
#include "ui/pages/modplatform/ModModel.h"
#include "Application.h"
#include "net/NetJob.h"
#include <QDebug> #include <QDebug>
class ModrinthAPI : public ModAPI { class ModrinthAPI : public ModAPI {
public: public:
inline QString getModSearchURL(int offset, QString query, QString sort, ModLoaderType modLoader, QString version) const override inline void searchMods(CallerType* caller, SearchArgs&& args) const override
{ {
if(!validateModLoader(modLoader)){ 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
{
if (!validateModLoader(args.mod_loader)) {
qWarning() << "Modrinth only have Forge and Fabric-compatible mods!"; qWarning() << "Modrinth only have Forge and Fabric-compatible mods!";
return ""; return "";
} }
return QString("https://api.modrinth.com/v2/search?" return QString(
"offset=%1&" "limit=25&" "query=%2&" "index=%3&" "https://api.modrinth.com/v2/search?"
"facets=[[\"categories:%4\"],[\"versions:%5\"],[\"project_type:mod\"]]") "offset=%1&"
.arg(offset) "limit=25&"
.arg(query) "query=%2&"
.arg(sort) "index=%3&"
.arg(getModLoaderString(modLoader)) "facets=[[\"categories:%4\"],[\"versions:%5\"],[\"project_type:mod\"]]")
.arg(version); .arg(args.offset)
.arg(args.search)
.arg(args.sorting)
.arg(getModLoaderString(args.mod_loader))
.arg(args.version);
}; };
inline QString getVersionsURL(const QString& addonId) const override inline QString getVersionsURL(const QString& addonId) const
{ {
return QString("https://api.modrinth.com/v2/project/%1/version").arg(addonId); return QString("https://api.modrinth.com/v2/project/%1/version").arg(addonId);
}; };
inline QString getAuthorURL(const QString& name) const override { return "https://modrinth.com/user/" + name; }; inline bool validateModLoader(ModLoaderType modLoader) const { return modLoader == Any || modLoader == Forge || modLoader == Fabric; }
private: inline QString getModLoaderString(ModLoaderType modLoader) const
inline bool validateModLoader(ModLoaderType modLoader) const{ {
return modLoader == Any || modLoader == Forge || modLoader == Fabric; switch (modLoader) {
} case Any:
return "fabric, forge";
inline QString getModLoaderString(ModLoaderType modLoader) const{ case Forge:
switch(modLoader){ return "forge";
case Any: case Fabric:
return "fabric, forge"; return "fabric";
case Forge: default:
return "forge"; return "";
case Fabric:
return "fabric";
default:
return "";
} }
} }
}; };

View File

@ -91,30 +91,16 @@ void ListModel::fetchMore(const QModelIndex& parent)
void ListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback) void ListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback)
{ {
if (m_logoMap.contains(logo)) { if (m_logoMap.contains(logo)) {
callback(APPLICATION->metacache()->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath()); callback(APPLICATION->metacache()
->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)))
->getFullPath());
} else { } else {
requestLogo(logo, logoUrl); requestLogo(logo, logoUrl);
} }
} }
void ListModel::requestModVersions(ModPlatform::IndexedPack const& current) void ListModel::requestModVersions(ModPlatform::IndexedPack const& current) {
{ m_parent->apiProvider()->getVersions(this, current.addonId.toString(), m_parent->debugName());
auto netJob = new NetJob(QString("%1::ModVersions(%2)").arg(m_parent->debugName()).arg(current.name), APPLICATION->network());
auto response = new QByteArray();
QString addonId = current.addonId.toString();
netJob->addNetAction(Net::Download::makeByteArray(m_parent->apiProvider()->getVersionsURL(addonId), response));
QObject::connect(netJob, &NetJob::succeeded, this, [this, response, addonId]{
m_parent->onRequestVersionsSucceeded(m_parent, response, addonId);
});
QObject::connect(netJob, &NetJob::finished, this, [response, netJob] {
netJob->deleteLater();
delete response;
});
netJob->start();
} }
void ListModel::performPaginatedSearch() void ListModel::performPaginatedSearch()
@ -124,16 +110,9 @@ void ListModel::performPaginatedSearch()
->getPackProfile() ->getPackProfile()
->getComponentVersion("net.fabricmc.fabric-loader") ->getComponentVersion("net.fabricmc.fabric-loader")
.isEmpty(); .isEmpty();
auto netJob = new NetJob(QString("%1::Search").arg(m_parent->debugName()), APPLICATION->network());
auto searchUrl = m_parent->apiProvider()->getModSearchURL(
nextSearchOffset, currentSearchTerm, getSorts()[currentSort], hasFabric ? ModAPI::Fabric : ModAPI::Forge, mcVersion);
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), &response)); m_parent->apiProvider()->searchMods(
jobPtr = netJob; this, { nextSearchOffset, currentSearchTerm, getSorts()[currentSort], hasFabric ? ModAPI::Fabric : ModAPI::Forge, mcVersion });
jobPtr->start();
QObject::connect(netJob, &NetJob::succeeded, this, &ListModel::searchRequestFinished);
QObject::connect(netJob, &NetJob::failed, this, &ListModel::searchRequestFailed);
} }
void ListModel::searchWithTerm(const QString& term, const int sort) void ListModel::searchWithTerm(const QString& term, const int sort)
@ -160,9 +139,7 @@ void ListModel::searchRequestFailed(QString reason)
if (jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) { if (jobPtr->first()->m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 409) {
// 409 Gone, notify user to update // 409 Gone, notify user to update
QMessageBox::critical(nullptr, tr("Error"), QMessageBox::critical(nullptr, tr("Error"),
QString("%1 %2") QString("%1 %2").arg(m_parent->displayName()).arg(tr("API version too old!\nPlease update PolyMC!")));
.arg(m_parent->displayName())
.arg(tr("API version too old!\nPlease update PolyMC!")));
// self-destruct // self-destruct
((ModDownloadDialog*)((ModPage*)parent())->parentWidget())->reject(); ((ModDownloadDialog*)((ModPage*)parent())->parentWidget())->reject();
} }
@ -180,11 +157,17 @@ void ListModel::searchRequestFailed(QString reason)
} }
} }
void ListModel::versionRequestSucceeded(QJsonDocument doc, QString addonId)
{
m_parent->onRequestVersionsSucceeded(doc, addonId);
}
void ListModel::requestLogo(QString logo, QString url) void ListModel::requestLogo(QString logo, QString url)
{ {
if (m_loadingLogos.contains(logo) || m_failedLogos.contains(logo)) { return; } if (m_loadingLogos.contains(logo) || m_failedLogos.contains(logo)) { return; }
MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0))); MetaEntryPtr entry =
APPLICATION->metacache()->resolveEntry(m_parent->metaEntryBase(), QString("logos/%1").arg(logo.section(".", 0, 0)));
auto job = new NetJob(QString("%1 Icon Download %2").arg(m_parent->debugName()).arg(logo), APPLICATION->network()); auto job = new NetJob(QString("%1 Icon Download %2").arg(m_parent->debugName()).arg(logo), APPLICATION->network());
job->addNetAction(Net::Download::makeCached(QUrl(url), entry)); job->addNetAction(Net::Download::makeCached(QUrl(url), entry));

View File

@ -1,9 +1,10 @@
#pragma once #pragma once
#include <qjsondocument.h>
#include <QAbstractListModel> #include <QAbstractListModel>
#include "modplatform/ModIndex.h"
#include "modplatform/ModAPI.h" #include "modplatform/ModAPI.h"
#include "modplatform/ModIndex.h"
#include "net/NetJob.h" #include "net/NetJob.h"
class ModPage; class ModPage;
@ -26,6 +27,8 @@ class ListModel : public QAbstractListModel {
QVariant data(const QModelIndex& index, int role) const override; QVariant data(const QModelIndex& index, int role) const override;
Qt::ItemFlags flags(const QModelIndex& index) const override; Qt::ItemFlags flags(const QModelIndex& index) const override;
void setActiveJob(NetJob::Ptr ptr) { jobPtr = ptr; }
bool canFetchMore(const QModelIndex& parent) const override; bool canFetchMore(const QModelIndex& parent) const override;
void fetchMore(const QModelIndex& parent) override; void fetchMore(const QModelIndex& parent) override;
@ -34,10 +37,14 @@ class ListModel : public QAbstractListModel {
virtual void requestModVersions(const ModPlatform::IndexedPack& current); virtual void requestModVersions(const ModPlatform::IndexedPack& current);
protected slots: public slots:
virtual void searchRequestFinished() = 0; virtual void searchRequestFinished(QJsonDocument& doc) = 0;
void searchRequestFailed(QString reason); void searchRequestFailed(QString reason);
void versionRequestSucceeded(QJsonDocument doc, QString addonId);
protected slots:
void logoFailed(QString logo); void logoFailed(QString logo);
void logoLoaded(QString logo, QIcon out); void logoLoaded(QString logo, QIcon out);

View File

@ -37,7 +37,7 @@ class ModPage : public QWidget, public BasePage {
virtual bool shouldDisplay() const override = 0; virtual bool shouldDisplay() const override = 0;
const ModAPI* apiProvider() const { return api.get(); }; const ModAPI* apiProvider() const { return api.get(); };
virtual void onRequestVersionsSucceeded(ModPage*, QByteArray*, QString) = 0; virtual void onRequestVersionsSucceeded(QJsonDocument&, QString) = 0;
void openedImpl() override; void openedImpl() override;
bool eventFilter(QObject* watched, QEvent* event) override; bool eventFilter(QObject* watched, QEvent* event) override;

View File

@ -11,18 +11,10 @@ ListModel::ListModel(FlameModPage* parent) : ModPlatform::ListModel(parent) {}
ListModel::~ListModel() {} ListModel::~ListModel() {}
void FlameMod::ListModel::searchRequestFinished() void FlameMod::ListModel::searchRequestFinished(QJsonDocument& doc)
{ {
jobPtr.reset(); jobPtr.reset();
QJsonParseError parse_error;
QJsonDocument doc = QJsonDocument::fromJson(response, &parse_error);
if(parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from Flame at " << parse_error.offset << " reason: " << parse_error.errorString();
qWarning() << response;
return;
}
QList<ModPlatform::IndexedPack> newList; QList<ModPlatform::IndexedPack> newList;
auto packs = doc.array(); auto packs = doc.array();
for(auto packRaw : packs) { for(auto packRaw : packs) {

View File

@ -30,7 +30,7 @@ class ListModel : public ModPlatform::ListModel {
virtual ~ListModel(); virtual ~ListModel();
private slots: private slots:
void searchRequestFinished() override; void searchRequestFinished(QJsonDocument& doc) override;
private: private:
const char** getSorts() const override; const char** getSorts() const override;

View File

@ -36,23 +36,17 @@ FlameModPage::FlameModPage(ModDownloadDialog* dialog, BaseInstance* instance)
bool FlameModPage::shouldDisplay() const { return true; } bool FlameModPage::shouldDisplay() const { return true; }
void FlameModPage::onRequestVersionsSucceeded(ModPage* instance, QByteArray* response, QString addonId) void FlameModPage::onRequestVersionsSucceeded(QJsonDocument& doc, QString addonId)
{ {
if (addonId != current.addonId) { if (addonId != current.addonId) {
return; // wrong request return; // wrong request
} }
QJsonParseError parse_error;
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
qWarning() << "Error while parsing JSON response from Flame at " << parse_error.offset << " reason: " << parse_error.errorString();
qWarning() << *response;
return;
}
QJsonArray arr = doc.array(); QJsonArray arr = doc.array();
try { try {
FlameMod::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance); FlameMod::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance);
} catch (const JSONValidationError& e) { } catch (const JSONValidationError& e) {
qDebug() << *response; qDebug() << doc;
qWarning() << "Error while reading Flame mod version: " << e.cause(); qWarning() << "Error while reading Flame mod version: " << e.cause();
} }
auto packProfile = ((MinecraftInstance*)m_instance)->getPackProfile(); auto packProfile = ((MinecraftInstance*)m_instance)->getPackProfile();

View File

@ -22,5 +22,5 @@ class FlameModPage : public ModPage {
bool shouldDisplay() const override; bool shouldDisplay() const override;
private: private:
void onRequestVersionsSucceeded(ModPage*, QByteArray*, QString) override; void onRequestVersionsSucceeded(QJsonDocument&, QString) override;
}; };

View File

@ -10,19 +10,10 @@ ListModel::ListModel(ModrinthPage* parent) : ModPlatform::ListModel(parent) {}
ListModel::~ListModel() {} ListModel::~ListModel() {}
void Modrinth::ListModel::searchRequestFinished() void Modrinth::ListModel::searchRequestFinished(QJsonDocument& doc)
{ {
jobPtr.reset(); jobPtr.reset();
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;
}
QList<ModPlatform::IndexedPack> newList; QList<ModPlatform::IndexedPack> newList;
auto packs = doc.object().value("hits").toArray(); auto packs = doc.object().value("hits").toArray();
for (auto packRaw : packs) { for (auto packRaw : packs) {

View File

@ -29,8 +29,8 @@ class ListModel : public ModPlatform::ListModel {
ListModel(ModrinthPage* parent); ListModel(ModrinthPage* parent);
virtual ~ListModel(); virtual ~ListModel();
private slots: public slots:
void searchRequestFinished() override; void searchRequestFinished(QJsonDocument& doc) override;
private: private:
const char** getSorts() const override; const char** getSorts() const override;

View File

@ -35,22 +35,15 @@ ModrinthPage::ModrinthPage(ModDownloadDialog* dialog, BaseInstance* instance)
bool ModrinthPage::shouldDisplay() const { return true; } bool ModrinthPage::shouldDisplay() const { return true; }
void ModrinthPage::onRequestVersionsSucceeded(ModPage* instance, QByteArray* response, QString addonId) void ModrinthPage::onRequestVersionsSucceeded(QJsonDocument& response, QString addonId)
{ {
if (addonId != current.addonId) { return; } if (addonId != current.addonId) { return; }
QJsonParseError parse_error;
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error); QJsonArray arr = response.array();
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;
}
QJsonArray arr = doc.array();
try { try {
Modrinth::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance); Modrinth::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance);
} catch (const JSONValidationError& e) { } catch (const JSONValidationError& e) {
qDebug() << *response; qDebug() << response;
qWarning() << "Error while reading Modrinth mod version: " << e.cause(); qWarning() << "Error while reading Modrinth mod version: " << e.cause();
} }
auto packProfile = ((MinecraftInstance*)m_instance)->getPackProfile(); auto packProfile = ((MinecraftInstance*)m_instance)->getPackProfile();

View File

@ -22,5 +22,5 @@ class ModrinthPage : public ModPage {
bool shouldDisplay() const override; bool shouldDisplay() const override;
private: private:
void onRequestVersionsSucceeded(ModPage*, QByteArray*, QString) override; void onRequestVersionsSucceeded(QJsonDocument&, QString) override;
}; };