Refator task to work with multiple providers

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97
2023-04-20 00:57:09 +03:00
parent fac33498db
commit c1490cd627
12 changed files with 220 additions and 280 deletions

View File

@ -23,8 +23,10 @@
#include <QPushButton>
#include <algorithm>
#include <memory>
#include "Application.h"
#include "QObjectPtr.h"
#include "ResourceDownloadTask.h"
#include "minecraft/mod/ModFolderModel.h"
@ -32,7 +34,10 @@
#include "minecraft/mod/ShaderPackFolderModel.h"
#include "minecraft/mod/TexturePackFolderModel.h"
#include "minecraft/mod/tasks/GetModDependenciesTask.h"
#include "modplatform/ModIndex.h"
#include "ui/dialogs/CustomMessageBox.h"
#include "ui/dialogs/ProgressDialog.h"
#include "ui/dialogs/ReviewMessageBox.h"
#include "ui/pages/modplatform/ResourcePage.h"
@ -122,30 +127,38 @@ void ResourceDownloadDialog::connectButtons()
void ResourceDownloadDialog::confirm()
{
auto keys = m_selected.keys();
keys.sort(Qt::CaseInsensitive);
auto confirm_dialog = ReviewMessageBox::create(this, tr("Confirm %1 to download").arg(resourcesString()));
confirm_dialog->retranslateUi(resourcesString());
if (auto model = dynamic_cast<ModFolderModel*>(getBaseModel().get()); model) {
QList<ModPlatform::IndexedVersion> selectedVers;
for (auto& task : keys) {
auto selected = m_selected.constFind(task).value();
selectedVers.append(selected->getVersion());
}
if (auto task = getModDependenciesTask(); task) {
connect(task.get(), &Task::failed, this,
[&](QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->exec(); });
auto dir = model->indexDir();
auto dependencies = m_selectedPage->getDependecies(dir, selectedVers);
connect(task.get(), &Task::succeeded, this, [&]() {
QStringList warnings = task->warnings();
if (warnings.count()) {
CustomMessageBox::selectable(this, tr("Warnings"), warnings.join('\n'), QMessageBox::Warning)->exec();
}
});
for (auto dep : dependencies) {
dep.is_currently_selected = true;
auto pack = ModPlatform::IndexedPack{ dep.addonId, ModPlatform::ResourceProvider::FLAME, dep.fileName, dep.fileName };
m_selected.insert(dep.fileName, makeShared<ResourceDownloadTask>(pack, dep, getBaseModel(), true));
}
// Check for updates
ProgressDialog progress_dialog(this);
progress_dialog.setSkipButton(true, tr("Abort"));
progress_dialog.setWindowTitle(tr("Checking for dependencies..."));
auto ret = progress_dialog.execWithTask(task.get());
keys = m_selected.keys();
// If the dialog was skipped / some download error happened
if (ret == QDialog::DialogCode::Rejected) {
QMetaObject::invokeMethod(this, "reject", Qt::QueuedConnection);
return;
} else
for (auto dep : task->getDependecies()) {
addResource(dep->pack, dep->version, true);
}
}
auto keys = m_selected.keys();
keys.sort(Qt::CaseInsensitive);
for (auto& task : keys) {
auto selected = m_selected.constFind(task).value();
confirm_dialog->appendResource({ task, selected->getFilename(), selected->getCustomPath() });
@ -173,6 +186,7 @@ ResourcePage* ResourceDownloadDialog::getSelectedPage()
void ResourceDownloadDialog::addResource(ModPlatform::IndexedPack& pack, ModPlatform::IndexedVersion& ver, bool is_indexed)
{
qWarning() << "DebugName: " << pack.name;
removeResource(pack, ver);
ver.is_currently_selected = true;
@ -256,6 +270,21 @@ QList<BasePage*> ModDownloadDialog::getPages()
return pages;
}
GetModDependenciesTask::Ptr ModDownloadDialog::getModDependenciesTask()
{
if (auto model = dynamic_cast<ModFolderModel*>(getBaseModel().get()); model) {
auto keys = m_selected.keys();
QList<std::shared_ptr<GetModDependenciesTask::PackDependecny>> selectedVers;
for (auto& task : keys) {
auto selected = m_selected.constFind(task).value();
selectedVers.append(std::make_shared<GetModDependenciesTask::PackDependecny>(selected->getPack(), selected->getVersion()));
}
return makeShared<GetModDependenciesTask>(this, m_instance, model, selectedVers);
}
return nullptr;
};
ResourcePackDownloadDialog::ResourcePackDownloadDialog(QWidget* parent,
const std::shared_ptr<ResourcePackFolderModel>& resource_packs,
BaseInstance* instance)

View File

@ -25,7 +25,9 @@
#include <QLayout>
#include "QObjectPtr.h"
#include "minecraft/mod/tasks/GetModDependenciesTask.h"
#include "modplatform/ModIndex.h"
#include "tasks/Task.h"
#include "ui/pages/BasePageProvider.h"
class BaseInstance;
@ -80,6 +82,8 @@ class ResourceDownloadDialog : public QDialog, public BasePageProvider {
protected:
[[nodiscard]] virtual QString geometrySaveKey() const { return ""; }
[[nodiscard]] virtual GetModDependenciesTask::Ptr getModDependenciesTask() { return nullptr; }
protected:
const std::shared_ptr<ResourceFolderModel> m_base_model;
@ -92,8 +96,6 @@ class ResourceDownloadDialog : public QDialog, public BasePageProvider {
QHash<QString, DownloadTaskPtr> m_selected;
};
class ModDownloadDialog final : public ResourceDownloadDialog {
Q_OBJECT
@ -106,6 +108,7 @@ class ModDownloadDialog final : public ResourceDownloadDialog {
[[nodiscard]] QString geometrySaveKey() const override { return "ModDownloadGeometry"; }
QList<BasePage*> getPages() override;
GetModDependenciesTask::Ptr getModDependenciesTask() override;
private:
BaseInstance* m_instance;
@ -135,8 +138,8 @@ class TexturePackDownloadDialog final : public ResourceDownloadDialog {
public:
explicit TexturePackDownloadDialog(QWidget* parent,
const std::shared_ptr<TexturePackFolderModel>& resource_packs,
BaseInstance* instance);
const std::shared_ptr<TexturePackFolderModel>& resource_packs,
BaseInstance* instance);
~TexturePackDownloadDialog() override = default;
//: String that gets appended to the texture pack download dialog title ("Download " + resourcesString())
@ -153,9 +156,7 @@ class ShaderPackDownloadDialog final : public ResourceDownloadDialog {
Q_OBJECT
public:
explicit ShaderPackDownloadDialog(QWidget* parent,
const std::shared_ptr<ShaderPackFolderModel>& shader_packs,
BaseInstance* instance);
explicit ShaderPackDownloadDialog(QWidget* parent, const std::shared_ptr<ShaderPackFolderModel>& shader_packs, BaseInstance* instance);
~ShaderPackDownloadDialog() override = default;
//: String that gets appended to the shader pack download dialog title ("Download " + resourcesString())

View File

@ -449,46 +449,4 @@ void ResourceModel::infoRequestSucceeded(QJsonDocument& doc, ModPlatform::Indexe
emit projectInfoUpdated();
}
QList<ModPlatform::IndexedVersion> ResourceModel::getDependecies(QDir& dir, QList<ModPlatform::IndexedVersion> selected)
{
auto task = std::make_unique<GetModDependenciesTask>(
dir, selected,
[this](const ModPlatform::Dependency& dependency, std::function<void(const ModPlatform::IndexedVersion&)> succeeded) -> Task::Ptr {
auto args{ createDependecyArguments(dependency) };
auto callbacks{ createDependecyCallbacks() };
// Use default if no callbacks are set
if (!callbacks.on_succeed)
callbacks.on_succeed = [this, dependency, succeeded](auto& doc, auto& pack) {
ModPlatform::IndexedVersion ver;
try {
QJsonArray arr;
if (dependency.version.length() != 0 && doc.isObject()) {
arr.append(doc.object());
} else {
arr = doc.isObject() ? Json::ensureArray(doc.object(), "data") : doc.array();
}
ver = loadDependencyVersions(dependency, arr);
if (!ver.addonId.isValid()) {
qWarning() << "Error while reading " << debugName() << " resource version empty ";
qDebug() << doc;
return;
}
} catch (const JSONValidationError& e) {
qDebug() << doc;
qWarning() << "Error while reading " << debugName() << " resource version: " << e.cause();
return;
}
succeeded(ver);
};
return m_api->getDependencyVersion(std::move(args), std::move(callbacks));
});
task->start();
return task->getDependecies();
};
} // namespace ResourceDownload

View File

@ -85,8 +85,6 @@ class ResourceModel : public QAbstractListModel {
/** Gets the icon at the URL for the given index. If it's not fetched yet, fetch it and update when fisinhed. */
std::optional<QIcon> getIcon(QModelIndex&, const QUrl&);
QList<ModPlatform::IndexedVersion> getDependecies(QDir& dir, QList<ModPlatform::IndexedVersion> m_selected);
protected:
/** Resets the model's data. */
void clearData();

View File

@ -405,9 +405,4 @@ void ResourcePage::openUrl(const QUrl& url)
QDesktopServices::openUrl(url);
}
QList<ModPlatform::IndexedVersion> ResourcePage::getDependecies(QDir& dir, QList<ModPlatform::IndexedVersion> selected)
{
return m_model->getDependecies(dir, selected);
};
} // namespace ResourceDownload

View File

@ -75,8 +75,6 @@ class ResourcePage : public QWidget, public BasePage {
virtual void addResourceToDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
virtual void removeResourceFromDialog(ModPlatform::IndexedPack&, ModPlatform::IndexedVersion&);
QList<ModPlatform::IndexedVersion> getDependecies(QDir& dir, QList<ModPlatform::IndexedVersion> m_selected);
protected slots:
virtual void triggerSearch() {}