From 58d2c15ffa4a966b40ba2f741c90e1d32fdbe106 Mon Sep 17 00:00:00 2001 From: flow Date: Fri, 14 Oct 2022 14:36:48 -0300 Subject: [PATCH] feat: add functionality to MR modpack update in the page :D Signed-off-by: flow --- launcher/InstanceImportTask.cpp | 2 + launcher/ui/InstanceWindow.cpp | 6 ++ .../ui/pages/instance/ManagedPackPage.cpp | 73 ++++++++++++++++--- launcher/ui/pages/instance/ManagedPackPage.h | 28 +++++-- 4 files changed, 95 insertions(+), 14 deletions(-) diff --git a/launcher/InstanceImportTask.cpp b/launcher/InstanceImportTask.cpp index 5f459649..f5ef250e 100644 --- a/launcher/InstanceImportTask.cpp +++ b/launcher/InstanceImportTask.cpp @@ -264,6 +264,7 @@ void InstanceImportTask::processFlame() inst_creation_task->setName(*this); inst_creation_task->setIcon(m_instIcon); inst_creation_task->setGroup(m_instGroup); + inst_creation_task->setConfirmUpdate(shouldConfirmUpdate()); connect(inst_creation_task, &Task::succeeded, this, [this, inst_creation_task] { setOverride(inst_creation_task->shouldOverride()); @@ -328,6 +329,7 @@ void InstanceImportTask::processModrinth() inst_creation_task->setName(*this); inst_creation_task->setIcon(m_instIcon); inst_creation_task->setGroup(m_instGroup); + inst_creation_task->setConfirmUpdate(shouldConfirmUpdate()); connect(inst_creation_task, &Task::succeeded, this, [this, inst_creation_task] { setOverride(inst_creation_task->shouldOverride()); diff --git a/launcher/ui/InstanceWindow.cpp b/launcher/ui/InstanceWindow.cpp index 09ce0d67..c62b370f 100644 --- a/launcher/ui/InstanceWindow.cpp +++ b/launcher/ui/InstanceWindow.cpp @@ -132,6 +132,12 @@ InstanceWindow::InstanceWindow(InstancePtr instance, QWidget *parent) { connect(m_instance.get(), &BaseInstance::statusChanged, this, &InstanceWindow::on_instanceStatusChanged); } + + // add ourself as the modpack page's instance window + { + static_cast(m_container->getPage("managed_pack"))->setInstanceWindow(this); + } + show(); } diff --git a/launcher/ui/pages/instance/ManagedPackPage.cpp b/launcher/ui/pages/instance/ManagedPackPage.cpp index 7c51cf38..348dd857 100644 --- a/launcher/ui/pages/instance/ManagedPackPage.cpp +++ b/launcher/ui/pages/instance/ManagedPackPage.cpp @@ -6,10 +6,17 @@ #include "Application.h" #include "BuildConfig.h" +#include "InstanceImportTask.h" +#include "InstanceList.h" +#include "InstanceTask.h" #include "Json.h" #include "modplatform/modrinth/ModrinthPackManifest.h" +#include "ui/InstanceWindow.h" +#include "ui/dialogs/CustomMessageBox.h" +#include "ui/dialogs/ProgressDialog.h" + /** This is just to override the combo box popup behavior so that the combo box doesn't take the whole screen. * ... thanks Qt. */ @@ -33,14 +40,15 @@ class NoBigComboBoxStyle : public QProxyStyle { ManagedPackPage* ManagedPackPage::createPage(BaseInstance* inst, QString type, QWidget* parent) { if (type == "modrinth") - return new ModrinthManagedPackPage(inst, parent); + return new ModrinthManagedPackPage(inst, nullptr, parent); if (type == "flame") - return new FlameManagedPackPage(inst, parent); + return new FlameManagedPackPage(inst, nullptr, parent); - return new GenericManagedPackPage(inst, parent); + return new GenericManagedPackPage(inst, nullptr, parent); } -ManagedPackPage::ManagedPackPage(BaseInstance* inst, QWidget* parent) : QWidget(parent), ui(new Ui::ManagedPackPage), m_inst(inst) +ManagedPackPage::ManagedPackPage(BaseInstance* inst, InstanceWindow* instance_window, QWidget* parent) + : QWidget(parent), m_instance_window(instance_window), ui(new Ui::ManagedPackPage), m_inst(inst) { Q_ASSERT(inst); @@ -92,10 +100,37 @@ bool ManagedPackPage::shouldDisplay() const return m_inst->isManagedPack(); } -ModrinthManagedPackPage::ModrinthManagedPackPage(BaseInstance* inst, QWidget* parent) : ManagedPackPage(inst, parent) +bool ManagedPackPage::runUpdateTask(InstanceTask* task) +{ + Q_ASSERT(task); + + unique_qobject_ptr wrapped_task(APPLICATION->instances()->wrapInstanceTask(task)); + + connect(task, &Task::failed, + [this](QString reason) { CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show(); }); + connect(task, &Task::succeeded, [this, task]() { + QStringList warnings = task->warnings(); + if (warnings.count()) + CustomMessageBox::selectable(this, tr("Warnings"), warnings.join('\n'), QMessageBox::Warning)->show(); + }); + connect(task, &Task::aborted, [this] { + CustomMessageBox::selectable(this, tr("Task aborted"), tr("The task has been aborted by the user."), QMessageBox::Information) + ->show(); + }); + + ProgressDialog loadDialog(this); + loadDialog.setSkipButton(true, tr("Abort")); + loadDialog.execWithTask(task); + + return task->wasSuccessful(); +} + +ModrinthManagedPackPage::ModrinthManagedPackPage(BaseInstance* inst, InstanceWindow* instance_window, QWidget* parent) + : ManagedPackPage(inst, instance_window, parent) { Q_ASSERT(inst->isManagedPack()); connect(ui->versionsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(suggestVersion())); + connect(ui->updateButton, &QPushButton::pressed, this, &ModrinthManagedPackPage::update); } void ModrinthManagedPackPage::parseManagedPack() @@ -166,15 +201,35 @@ void ModrinthManagedPackPage::suggestVersion() ui->changelogTextBrowser->setText(version.changelog); } -FlameManagedPackPage::FlameManagedPackPage(BaseInstance* inst, QWidget* parent) : ManagedPackPage(inst, parent) +void ModrinthManagedPackPage::update() +{ + auto index = ui->versionsComboBox->currentIndex(); + auto version = m_pack.versions.at(index); + + auto extracted = new InstanceImportTask(version.download_url, this); + + InstanceName inst_name(m_inst->getManagedPackName(), version.version); + inst_name.setName(m_inst->name().replace(m_inst->getManagedPackVersionName(), version.version)); + extracted->setName(inst_name); + + extracted->setGroup(APPLICATION->instances()->getInstanceGroup(m_inst->id())); + extracted->setIcon(m_inst->iconKey()); + extracted->setConfirmUpdate(false); + + auto did_succeed = runUpdateTask(extracted); + + if (m_instance_window && did_succeed) + m_instance_window->close(); +} + +FlameManagedPackPage::FlameManagedPackPage(BaseInstance* inst, InstanceWindow* instance_window, QWidget* parent) + : ManagedPackPage(inst, instance_window, parent) { Q_ASSERT(inst->isManagedPack()); connect(ui->versionsComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(suggestVersion())); } -void FlameManagedPackPage::parseManagedPack() -{ -} +void FlameManagedPackPage::parseManagedPack() {} QString FlameManagedPackPage::url() const { diff --git a/launcher/ui/pages/instance/ManagedPackPage.h b/launcher/ui/pages/instance/ManagedPackPage.h index 1a756d33..dc6ae118 100644 --- a/launcher/ui/pages/instance/ManagedPackPage.h +++ b/launcher/ui/pages/instance/ManagedPackPage.h @@ -13,6 +13,9 @@ namespace Ui { class ManagedPackPage; } +class InstanceTask; +class InstanceWindow; + class ManagedPackPage : public QWidget, public BasePage { Q_OBJECT @@ -45,15 +48,28 @@ class ManagedPackPage : public QWidget, public BasePage { */ [[nodiscard]] virtual QString url() const { return {}; }; + void setInstanceWindow(InstanceWindow* window) { m_instance_window = window; } + public slots: /** Gets the current version selection and update the changelog. */ virtual void suggestVersion() {}; - protected: - ManagedPackPage(BaseInstance* inst, QWidget* parent = nullptr); + virtual void update() {}; protected: + ManagedPackPage(BaseInstance* inst, InstanceWindow* instance_window, QWidget* parent = nullptr); + + /** Run the InstanceTask, with a progress dialog and all. + * Similar to MainWindow::instanceFromInstanceTask + * + * Returns whether the task was successful. + */ + bool runUpdateTask(InstanceTask*); + + protected: + InstanceWindow* m_instance_window = nullptr; + Ui::ManagedPackPage* ui; BaseInstance* m_inst; @@ -65,7 +81,7 @@ class GenericManagedPackPage final : public ManagedPackPage { Q_OBJECT public: - GenericManagedPackPage(BaseInstance* inst, QWidget* parent = nullptr) : ManagedPackPage(inst, parent) {} + GenericManagedPackPage(BaseInstance* inst, InstanceWindow* instance_window, QWidget* parent = nullptr) : ManagedPackPage(inst, instance_window, parent) {} ~GenericManagedPackPage() override = default; }; @@ -73,7 +89,7 @@ class ModrinthManagedPackPage final : public ManagedPackPage { Q_OBJECT public: - ModrinthManagedPackPage(BaseInstance* inst, QWidget* parent = nullptr); + ModrinthManagedPackPage(BaseInstance* inst, InstanceWindow* instance_window, QWidget* parent = nullptr); ~ModrinthManagedPackPage() override = default; void parseManagedPack() override; @@ -82,6 +98,8 @@ class ModrinthManagedPackPage final : public ManagedPackPage { public slots: void suggestVersion() override; + void update() override; + private: Modrinth::Modpack m_pack; ModrinthAPI m_api; @@ -91,7 +109,7 @@ class FlameManagedPackPage final : public ManagedPackPage { Q_OBJECT public: - FlameManagedPackPage(BaseInstance* inst, QWidget* parent = nullptr); + FlameManagedPackPage(BaseInstance* inst, InstanceWindow* instance_window, QWidget* parent = nullptr); ~FlameManagedPackPage() override = default; void parseManagedPack() override;