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
14 changed files with 230 additions and 135 deletions

View File

@ -91,30 +91,16 @@ void ListModel::fetchMore(const QModelIndex& parent)
void ListModel::getLogo(const QString& logo, const QString& logoUrl, LogoCallback callback)
{
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 {
requestLogo(logo, logoUrl);
}
}
void ListModel::requestModVersions(ModPlatform::IndexedPack const& current)
{
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::requestModVersions(ModPlatform::IndexedPack const& current) {
m_parent->apiProvider()->getVersions(this, current.addonId.toString(), m_parent->debugName());
}
void ListModel::performPaginatedSearch()
@ -124,16 +110,9 @@ void ListModel::performPaginatedSearch()
->getPackProfile()
->getComponentVersion("net.fabricmc.fabric-loader")
.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));
jobPtr = netJob;
jobPtr->start();
QObject::connect(netJob, &NetJob::succeeded, this, &ListModel::searchRequestFinished);
QObject::connect(netJob, &NetJob::failed, this, &ListModel::searchRequestFailed);
m_parent->apiProvider()->searchMods(
this, { nextSearchOffset, currentSearchTerm, getSorts()[currentSort], hasFabric ? ModAPI::Fabric : ModAPI::Forge, mcVersion });
}
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) {
// 409 Gone, notify user to update
QMessageBox::critical(nullptr, tr("Error"),
QString("%1 %2")
.arg(m_parent->displayName())
.arg(tr("API version too old!\nPlease update PolyMC!")));
QString("%1 %2").arg(m_parent->displayName()).arg(tr("API version too old!\nPlease update PolyMC!")));
// self-destruct
((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)
{
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());
job->addNetAction(Net::Download::makeCached(QUrl(url), entry));

View File

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

View File

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

View File

@ -11,18 +11,10 @@ ListModel::ListModel(FlameModPage* parent) : ModPlatform::ListModel(parent) {}
ListModel::~ListModel() {}
void FlameMod::ListModel::searchRequestFinished()
void FlameMod::ListModel::searchRequestFinished(QJsonDocument& doc)
{
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;
auto packs = doc.array();
for(auto packRaw : packs) {

View File

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

View File

@ -36,23 +36,17 @@ FlameModPage::FlameModPage(ModDownloadDialog* dialog, BaseInstance* instance)
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) {
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();
try {
FlameMod::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance);
} catch (const JSONValidationError& e) {
qDebug() << *response;
qDebug() << doc;
qWarning() << "Error while reading Flame mod version: " << e.cause();
}
auto packProfile = ((MinecraftInstance*)m_instance)->getPackProfile();

View File

@ -22,5 +22,5 @@ class FlameModPage : public ModPage {
bool shouldDisplay() const override;
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() {}
void Modrinth::ListModel::searchRequestFinished()
void Modrinth::ListModel::searchRequestFinished(QJsonDocument& doc)
{
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;
auto packs = doc.object().value("hits").toArray();
for (auto packRaw : packs) {

View File

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

View File

@ -35,22 +35,15 @@ ModrinthPage::ModrinthPage(ModDownloadDialog* dialog, BaseInstance* instance)
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; }
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;
}
QJsonArray arr = doc.array();
QJsonArray arr = response.array();
try {
Modrinth::loadIndexedPackVersions(current, arr, APPLICATION->network(), m_instance);
} catch (const JSONValidationError& e) {
qDebug() << *response;
qDebug() << response;
qWarning() << "Error while reading Modrinth mod version: " << e.cause();
}
auto packProfile = ((MinecraftInstance*)m_instance)->getPackProfile();

View File

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