refactor: change some ResourceAPI from NetJob to Task

This makes it easier to create resource apis that aren't network-based.

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2023-01-03 13:58:27 -03:00
parent e62e1d9701
commit ba677a8cb7
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
17 changed files with 75 additions and 67 deletions

View File

@ -28,6 +28,10 @@ class shared_qobject_ptr : public QSharedPointer<T> {
constexpr shared_qobject_ptr(const shared_qobject_ptr<Derived>& other) : QSharedPointer<T>(other)
{}
template <typename Derived>
constexpr shared_qobject_ptr(const QSharedPointer<Derived>& other) : QSharedPointer<T>(other)
{}
void reset() { QSharedPointer<T>::reset(); }
void reset(const shared_qobject_ptr<T>& other)
{

View File

@ -13,8 +13,6 @@
#include "modplatform/modrinth/ModrinthAPI.h"
#include "modplatform/modrinth/ModrinthPackIndex.h"
#include "net/NetJob.h"
static ModPlatform::ProviderCapabilities ProviderCaps;
static ModrinthAPI modrinth_api;
@ -107,7 +105,7 @@ void EnsureMetadataTask::executeTask()
}
}
NetJob::Ptr version_task;
Task::Ptr version_task;
switch (m_provider) {
case (ModPlatform::ResourceProvider::MODRINTH):
@ -127,7 +125,7 @@ void EnsureMetadataTask::executeTask()
};
connect(version_task.get(), &Task::finished, this, [this, invalidade_leftover] {
NetJob::Ptr project_task;
Task::Ptr project_task;
switch (m_provider) {
case (ModPlatform::ResourceProvider::MODRINTH):
@ -149,7 +147,7 @@ void EnsureMetadataTask::executeTask()
m_current_task = nullptr;
});
m_current_task = project_task.get();
m_current_task = project_task;
project_task->start();
});
@ -164,7 +162,7 @@ void EnsureMetadataTask::executeTask()
setStatus(tr("Requesting metadata information from %1 for '%2'...")
.arg(ProviderCaps.readableName(m_provider), m_mods.begin().value()->name()));
m_current_task = version_task.get();
m_current_task = version_task;
version_task->start();
}
@ -210,7 +208,7 @@ void EnsureMetadataTask::emitFail(Mod* m, QString key, RemoveFromList remove)
// Modrinth
NetJob::Ptr EnsureMetadataTask::modrinthVersionsTask()
Task::Ptr EnsureMetadataTask::modrinthVersionsTask()
{
auto hash_type = ProviderCaps.hashType(ModPlatform::ResourceProvider::MODRINTH).first();
@ -221,7 +219,7 @@ NetJob::Ptr EnsureMetadataTask::modrinthVersionsTask()
if (!ver_task)
return {};
connect(ver_task.get(), &NetJob::succeeded, this, [this, response] {
connect(ver_task.get(), &Task::succeeded, this, [this, response] {
QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
@ -260,14 +258,14 @@ NetJob::Ptr EnsureMetadataTask::modrinthVersionsTask()
return ver_task;
}
NetJob::Ptr EnsureMetadataTask::modrinthProjectsTask()
Task::Ptr EnsureMetadataTask::modrinthProjectsTask()
{
QHash<QString, QString> addonIds;
for (auto const& data : m_temp_versions)
addonIds.insert(data.addonId.toString(), data.hash);
auto response = new QByteArray();
NetJob::Ptr proj_task;
Task::Ptr proj_task;
if (addonIds.isEmpty()) {
qWarning() << "No addonId found!";
@ -281,7 +279,7 @@ NetJob::Ptr EnsureMetadataTask::modrinthProjectsTask()
if (!proj_task)
return {};
connect(proj_task.get(), &NetJob::succeeded, this, [this, response, addonIds] {
connect(proj_task.get(), &Task::succeeded, this, [this, response, addonIds] {
QJsonParseError parse_error{};
auto doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {
@ -335,7 +333,7 @@ NetJob::Ptr EnsureMetadataTask::modrinthProjectsTask()
}
// Flame
NetJob::Ptr EnsureMetadataTask::flameVersionsTask()
Task::Ptr EnsureMetadataTask::flameVersionsTask()
{
auto* response = new QByteArray();
@ -400,7 +398,7 @@ NetJob::Ptr EnsureMetadataTask::flameVersionsTask()
return ver_task;
}
NetJob::Ptr EnsureMetadataTask::flameProjectsTask()
Task::Ptr EnsureMetadataTask::flameProjectsTask()
{
QHash<QString, QString> addonIds;
for (auto const& hash : m_mods.keys()) {
@ -414,7 +412,7 @@ NetJob::Ptr EnsureMetadataTask::flameProjectsTask()
}
auto response = new QByteArray();
NetJob::Ptr proj_task;
Task::Ptr proj_task;
if (addonIds.isEmpty()) {
qWarning() << "No addonId found!";
@ -428,7 +426,7 @@ NetJob::Ptr EnsureMetadataTask::flameProjectsTask()
if (!proj_task)
return {};
connect(proj_task.get(), &NetJob::succeeded, this, [this, response, addonIds] {
connect(proj_task.get(), &Task::succeeded, this, [this, response, addonIds] {
QJsonParseError parse_error{};
auto doc = QJsonDocument::fromJson(*response, &parse_error);
if (parse_error.error != QJsonParseError::NoError) {

View File

@ -28,11 +28,11 @@ class EnsureMetadataTask : public Task {
private:
// FIXME: Move to their own namespace
auto modrinthVersionsTask() -> NetJob::Ptr;
auto modrinthProjectsTask() -> NetJob::Ptr;
auto modrinthVersionsTask() -> Task::Ptr;
auto modrinthProjectsTask() -> Task::Ptr;
auto flameVersionsTask() -> NetJob::Ptr;
auto flameProjectsTask() -> NetJob::Ptr;
auto flameVersionsTask() -> Task::Ptr;
auto flameProjectsTask() -> Task::Ptr;
// Helpers
enum class RemoveFromList {
@ -61,5 +61,5 @@ class EnsureMetadataTask : public Task {
QHash<QString, ModPlatform::IndexedVersion> m_temp_versions;
ConcurrentTask* m_hashing_task;
NetJob* m_current_task;
Task::Ptr m_current_task;
};

View File

@ -35,6 +35,7 @@
#pragma once
#include <QDebug>
#include <QList>
#include <QString>
@ -44,7 +45,7 @@
#include "../Version.h"
#include "modplatform/ModIndex.h"
#include "net/NetJob.h"
#include "tasks/Task.h"
/* Simple class with a common interface for interacting with APIs */
class ResourceAPI {
@ -113,28 +114,28 @@ class ResourceAPI {
[[nodiscard]] virtual auto getSortingMethods() const -> QList<SortingMethod> = 0;
public slots:
[[nodiscard]] virtual NetJob::Ptr searchProjects(SearchArgs&&, SearchCallbacks&&) const
[[nodiscard]] virtual Task::Ptr searchProjects(SearchArgs&&, SearchCallbacks&&) const
{
qWarning() << "TODO";
return nullptr;
}
[[nodiscard]] virtual NetJob::Ptr getProject(QString addonId, QByteArray* response) const
[[nodiscard]] virtual Task::Ptr getProject(QString addonId, QByteArray* response) const
{
qWarning() << "TODO";
return nullptr;
}
[[nodiscard]] virtual NetJob::Ptr getProjects(QStringList addonIds, QByteArray* response) const
[[nodiscard]] virtual Task::Ptr getProjects(QStringList addonIds, QByteArray* response) const
{
qWarning() << "TODO";
return nullptr;
}
[[nodiscard]] virtual NetJob::Ptr getProjectInfo(ProjectInfoArgs&&, ProjectInfoCallbacks&&) const
[[nodiscard]] virtual Task::Ptr getProjectInfo(ProjectInfoArgs&&, ProjectInfoCallbacks&&) const
{
qWarning() << "TODO";
return nullptr;
}
[[nodiscard]] virtual NetJob::Ptr getProjectVersions(VersionSearchArgs&&, VersionSearchCallbacks&&) const
[[nodiscard]] virtual Task::Ptr getProjectVersions(VersionSearchArgs&&, VersionSearchCallbacks&&) const
{
qWarning() << "TODO";
return nullptr;

View File

@ -7,7 +7,7 @@
#include "net/Upload.h"
auto FlameAPI::matchFingerprints(const QList<uint>& fingerprints, QByteArray* response) -> NetJob::Ptr
Task::Ptr FlameAPI::matchFingerprints(const QList<uint>& fingerprints, QByteArray* response)
{
auto* netJob = new NetJob(QString("Flame::MatchFingerprints"), APPLICATION->network());
@ -167,7 +167,7 @@ auto FlameAPI::getLatestVersion(VersionSearchArgs&& args) -> ModPlatform::Indexe
return ver;
}
NetJob::Ptr FlameAPI::getProjects(QStringList addonIds, QByteArray* response) const
Task::Ptr FlameAPI::getProjects(QStringList addonIds, QByteArray* response) const
{
auto* netJob = new NetJob(QString("Flame::GetProjects"), APPLICATION->network());
@ -190,7 +190,7 @@ NetJob::Ptr FlameAPI::getProjects(QStringList addonIds, QByteArray* response) co
return netJob;
}
NetJob::Ptr FlameAPI::getFiles(const QStringList& fileIds, QByteArray* response) const
Task::Ptr FlameAPI::getFiles(const QStringList& fileIds, QByteArray* response) const
{
auto* netJob = new NetJob(QString("Flame::GetFiles"), APPLICATION->network());

View File

@ -10,9 +10,9 @@ class FlameAPI : public NetworkResourceAPI {
auto getLatestVersion(VersionSearchArgs&& args) -> ModPlatform::IndexedVersion;
NetJob::Ptr getProjects(QStringList addonIds, QByteArray* response) const override;
NetJob::Ptr matchFingerprints(const QList<uint>& fingerprints, QByteArray* response);
NetJob::Ptr getFiles(const QStringList& fileIds, QByteArray* response) const;
Task::Ptr getProjects(QStringList addonIds, QByteArray* response) const override;
Task::Ptr matchFingerprints(const QList<uint>& fingerprints, QByteArray* response);
Task::Ptr getFiles(const QStringList& fileIds, QByteArray* response) const;
[[nodiscard]] auto getSortingMethods() const -> QList<ResourceAPI::SortingMethod> override;

View File

@ -183,7 +183,7 @@ bool FlameCreationTask::updateInstance()
QEventLoop loop;
connect(job.get(), &NetJob::succeeded, this, [this, raw_response, fileIds, old_inst_dir, &old_files, old_minecraft_dir] {
connect(job.get(), &Task::succeeded, this, [this, raw_response, fileIds, old_inst_dir, &old_files, old_minecraft_dir] {
// Parse the API response
QJsonParseError parse_error{};
auto doc = QJsonDocument::fromJson(*raw_response, &parse_error);
@ -225,7 +225,7 @@ bool FlameCreationTask::updateInstance()
m_files_to_remove.append(old_minecraft_dir.absoluteFilePath(relative_path));
}
});
connect(job.get(), &NetJob::finished, &loop, &QEventLoop::quit);
connect(job.get(), &Task::finished, &loop, &QEventLoop::quit);
m_process_update_file_info_job = job;
job->start();

View File

@ -86,7 +86,7 @@ class FlameCreationTask final : public InstanceCreationTask {
Flame::Manifest m_pack;
// Handle to allow aborting
NetJob::Ptr m_process_update_file_info_job = nullptr;
Task::Ptr m_process_update_file_info_job = nullptr;
NetJob::Ptr m_files_job = nullptr;
QString m_managed_id, m_managed_version_id;

View File

@ -5,7 +5,7 @@
#include "modplatform/ModIndex.h"
NetJob::Ptr NetworkResourceAPI::searchProjects(SearchArgs&& args, SearchCallbacks&& callbacks) const
Task::Ptr NetworkResourceAPI::searchProjects(SearchArgs&& args, SearchCallbacks&& callbacks) const
{
auto search_url_optional = getSearchURL(args);
if (!search_url_optional.has_value()) {
@ -50,7 +50,7 @@ NetJob::Ptr NetworkResourceAPI::searchProjects(SearchArgs&& args, SearchCallback
return netJob;
}
NetJob::Ptr NetworkResourceAPI::getProjectInfo(ProjectInfoArgs&& args, ProjectInfoCallbacks&& callbacks) const
Task::Ptr NetworkResourceAPI::getProjectInfo(ProjectInfoArgs&& args, ProjectInfoCallbacks&& callbacks) const
{
auto response = new QByteArray();
auto job = getProject(args.pack.addonId.toString(), response);
@ -71,7 +71,7 @@ NetJob::Ptr NetworkResourceAPI::getProjectInfo(ProjectInfoArgs&& args, ProjectIn
return job;
}
NetJob::Ptr NetworkResourceAPI::getProjectVersions(VersionSearchArgs&& args, VersionSearchCallbacks&& callbacks) const
Task::Ptr NetworkResourceAPI::getProjectVersions(VersionSearchArgs&& args, VersionSearchCallbacks&& callbacks) const
{
auto versions_url_optional = getVersionsURL(args);
if (!versions_url_optional.has_value())
@ -104,7 +104,7 @@ NetJob::Ptr NetworkResourceAPI::getProjectVersions(VersionSearchArgs&& args, Ver
return netJob;
}
NetJob::Ptr NetworkResourceAPI::getProject(QString addonId, QByteArray* response) const
Task::Ptr NetworkResourceAPI::getProject(QString addonId, QByteArray* response) const
{
auto project_url_optional = getInfoURL(addonId);
if (!project_url_optional.has_value())

View File

@ -4,12 +4,12 @@
class NetworkResourceAPI : public ResourceAPI {
public:
NetJob::Ptr searchProjects(SearchArgs&&, SearchCallbacks&&) const override;
Task::Ptr searchProjects(SearchArgs&&, SearchCallbacks&&) const override;
NetJob::Ptr getProject(QString addonId, QByteArray* response) const override;
Task::Ptr getProject(QString addonId, QByteArray* response) const override;
NetJob::Ptr getProjectInfo(ProjectInfoArgs&&, ProjectInfoCallbacks&&) const override;
NetJob::Ptr getProjectVersions(VersionSearchArgs&&, VersionSearchCallbacks&&) const override;
Task::Ptr getProjectInfo(ProjectInfoArgs&&, ProjectInfoCallbacks&&) const override;
Task::Ptr getProjectVersions(VersionSearchArgs&&, VersionSearchCallbacks&&) const override;
protected:
[[nodiscard]] virtual auto getSearchURL(SearchArgs const& args) const -> std::optional<QString> = 0;

View File

@ -4,7 +4,7 @@
#include "Json.h"
#include "net/Upload.h"
auto ModrinthAPI::currentVersion(QString hash, QString hash_format, QByteArray* response) -> NetJob::Ptr
Task::Ptr ModrinthAPI::currentVersion(QString hash, QString hash_format, QByteArray* response)
{
auto* netJob = new NetJob(QString("Modrinth::GetCurrentVersion"), APPLICATION->network());
@ -16,7 +16,7 @@ auto ModrinthAPI::currentVersion(QString hash, QString hash_format, QByteArray*
return netJob;
}
auto ModrinthAPI::currentVersions(const QStringList& hashes, QString hash_format, QByteArray* response) -> NetJob::Ptr
Task::Ptr ModrinthAPI::currentVersions(const QStringList& hashes, QString hash_format, QByteArray* response)
{
auto* netJob = new NetJob(QString("Modrinth::GetCurrentVersions"), APPLICATION->network());
@ -35,11 +35,11 @@ auto ModrinthAPI::currentVersions(const QStringList& hashes, QString hash_format
return netJob;
}
auto ModrinthAPI::latestVersion(QString hash,
QString hash_format,
std::optional<std::list<Version>> mcVersions,
std::optional<ModLoaderTypes> loaders,
QByteArray* response) -> NetJob::Ptr
Task::Ptr ModrinthAPI::latestVersion(QString hash,
QString hash_format,
std::optional<std::list<Version>> mcVersions,
std::optional<ModLoaderTypes> loaders,
QByteArray* response)
{
auto* netJob = new NetJob(QString("Modrinth::GetLatestVersion"), APPLICATION->network());
@ -67,11 +67,11 @@ auto ModrinthAPI::latestVersion(QString hash,
return netJob;
}
auto ModrinthAPI::latestVersions(const QStringList& hashes,
QString hash_format,
std::optional<std::list<Version>> mcVersions,
std::optional<ModLoaderTypes> loaders,
QByteArray* response) -> NetJob::Ptr
Task::Ptr ModrinthAPI::latestVersions(const QStringList& hashes,
QString hash_format,
std::optional<std::list<Version>> mcVersions,
std::optional<ModLoaderTypes> loaders,
QByteArray* response)
{
auto* netJob = new NetJob(QString("Modrinth::GetLatestVersions"), APPLICATION->network());
@ -101,14 +101,17 @@ auto ModrinthAPI::latestVersions(const QStringList& hashes,
return netJob;
}
NetJob::Ptr ModrinthAPI::getProjects(QStringList addonIds, QByteArray* response) const
Task::Ptr ModrinthAPI::getProjects(QStringList addonIds, QByteArray* response) const
{
auto netJob = new NetJob(QString("Modrinth::GetProjects"), APPLICATION->network());
auto searchUrl = getMultipleModInfoURL(addonIds);
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), response));
QObject::connect(netJob, &NetJob::finished, [response, netJob] { delete response; netJob->deleteLater(); });
QObject::connect(netJob, &NetJob::finished, [response, netJob] {
delete response;
netJob->deleteLater();
});
return netJob;
}

View File

@ -28,25 +28,25 @@ class ModrinthAPI : public NetworkResourceAPI {
public:
auto currentVersion(QString hash,
QString hash_format,
QByteArray* response) -> NetJob::Ptr;
QByteArray* response) -> Task::Ptr;
auto currentVersions(const QStringList& hashes,
QString hash_format,
QByteArray* response) -> NetJob::Ptr;
QByteArray* response) -> Task::Ptr;
auto latestVersion(QString hash,
QString hash_format,
std::optional<std::list<Version>> mcVersions,
std::optional<ModLoaderTypes> loaders,
QByteArray* response) -> NetJob::Ptr;
QByteArray* response) -> Task::Ptr;
auto latestVersions(const QStringList& hashes,
QString hash_format,
std::optional<std::list<Version>> mcVersions,
std::optional<ModLoaderTypes> loaders,
QByteArray* response) -> NetJob::Ptr;
QByteArray* response) -> Task::Ptr;
NetJob::Ptr getProjects(QStringList addonIds, QByteArray* response) const override;
Task::Ptr getProjects(QStringList addonIds, QByteArray* response) const override;
public:
[[nodiscard]] auto getSortingMethods() const -> QList<ResourceAPI::SortingMethod> override;

View File

@ -175,7 +175,7 @@ void ModrinthCheckUpdate::executeTask()
setStatus(tr("Waiting for the API response from Modrinth..."));
setProgress(1, 3);
m_net_job = job.get();
m_net_job = qSharedPointerObjectCast<NetJob, Task>(job);
job->start();
lock.exec();

View File

@ -19,5 +19,5 @@ class ModrinthCheckUpdate : public CheckUpdateTask {
void executeTask() override;
private:
NetJob* m_net_job = nullptr;
NetJob::Ptr m_net_job = nullptr;
};

View File

@ -12,6 +12,8 @@
#include "modplatform/flame/FlameAPI.h"
#include "modplatform/flame/FlamePackIndex.h"
#include "net/NetJob.h"
#include "ui/pages/BasePage.h"
#include <QWidget>

View File

@ -225,7 +225,7 @@ void ResourceModel::clearData()
endResetModel();
}
void ResourceModel::runSearchJob(NetJob::Ptr ptr)
void ResourceModel::runSearchJob(Task::Ptr ptr)
{
m_current_search_job = ptr;
m_current_search_job->start();

View File

@ -80,7 +80,7 @@ class ResourceModel : public QAbstractListModel {
/** Resets the model's data. */
void clearData();
void runSearchJob(NetJob::Ptr);
void runSearchJob(Task::Ptr);
void runInfoJob(Task::Ptr);
[[nodiscard]] auto getCurrentSortingMethodByIndex() const -> std::optional<ResourceAPI::SortingMethod>;
@ -111,7 +111,7 @@ class ResourceModel : public QAbstractListModel {
std::unique_ptr<ResourceAPI> m_api;
// Job for searching for new entries
shared_qobject_ptr<NetJob> m_current_search_job;
shared_qobject_ptr<Task> m_current_search_job;
// Job for fetching versions and extra info on existing entries
ConcurrentTask m_current_info_job;