From e95619fa67239b709522a66b8bb7403bb7b84098 Mon Sep 17 00:00:00 2001 From: Jan Dalheimer Date: Fri, 14 Mar 2014 20:48:57 +0100 Subject: [PATCH] Pull in BaseInstaller related changes from quickmod --- gui/dialogs/OneSixModEditDialog.cpp | 56 ++------------- logic/BaseInstaller.h | 6 ++ logic/ForgeInstaller.cpp | 105 +++++++++++++++++++++++++++- logic/ForgeInstaller.h | 5 +- logic/LiteLoaderInstaller.cpp | 68 +++++++++++++++++- logic/LiteLoaderInstaller.h | 5 +- 6 files changed, 186 insertions(+), 59 deletions(-) diff --git a/gui/dialogs/OneSixModEditDialog.cpp b/gui/dialogs/OneSixModEditDialog.cpp index b5a0d5aa4..7dac68e5b 100644 --- a/gui/dialogs/OneSixModEditDialog.cpp +++ b/gui/dialogs/OneSixModEditDialog.cpp @@ -221,44 +221,9 @@ void OneSixModEditDialog::on_forgeBtn_clicked() m_inst->currentVersionId()); if (vselect.exec() && vselect.selectedVersion()) { - ForgeVersionPtr forgeVersion = - std::dynamic_pointer_cast(vselect.selectedVersion()); - if (!forgeVersion) - return; - auto entry = MMC->metacache()->resolveEntry("minecraftforge", forgeVersion->filename); - if (entry->stale) - { - NetJob *fjob = new NetJob("Forge download"); - fjob->addNetAction(CacheDownload::make(forgeVersion->installer_url, entry)); - ProgressDialog dlg(this); - dlg.exec(fjob); - if (dlg.result() == QDialog::Accepted) - { - // install - QString forgePath = entry->getFullPath(); - ForgeInstaller forge(forgePath, forgeVersion->universal_url); - if (!forge.add(m_inst)) - { - QLOG_ERROR() << "Failure installing forge"; - } - } - else - { - // failed to download forge :/ - } - } - else - { - // install - QString forgePath = entry->getFullPath(); - ForgeInstaller forge(forgePath, forgeVersion->universal_url); - if (!forge.add(m_inst)) - { - QLOG_ERROR() << "Failure installing forge"; - } - } + ProgressDialog dialog(this); + dialog.exec(ForgeInstaller().createInstallTask(m_inst, vselect.selectedVersion(), this)); } - reloadInstanceVersion(); } void OneSixModEditDialog::on_liteloaderBtn_clicked() @@ -281,21 +246,8 @@ void OneSixModEditDialog::on_liteloaderBtn_clicked() m_inst->currentVersionId()); if (vselect.exec() && vselect.selectedVersion()) { - LiteLoaderVersionPtr liteloaderVersion = - std::dynamic_pointer_cast(vselect.selectedVersion()); - if (!liteloaderVersion) - return; - LiteLoaderInstaller liteloader(liteloaderVersion); - if (!liteloader.add(m_inst)) - { - QMessageBox::critical(this, tr("LiteLoader"), - tr("For reasons unknown, the LiteLoader installation failed. " - "Check your MultiMC log files for details.")); - } - else - { - reloadInstanceVersion(); - } + ProgressDialog dialog(this); + dialog.exec(LiteLoaderInstaller().createInstallTask(m_inst, vselect.selectedVersion(), this)); } } diff --git a/logic/BaseInstaller.h b/logic/BaseInstaller.h index c572e0042..d59833cc1 100644 --- a/logic/BaseInstaller.h +++ b/logic/BaseInstaller.h @@ -20,6 +20,10 @@ class OneSixInstance; class QDir; class QString; +class QObject; +class ProgressProvider; +class BaseVersion; +typedef std::shared_ptr BaseVersionPtr; class BaseInstaller { @@ -31,6 +35,8 @@ public: virtual bool add(OneSixInstance *to); virtual bool remove(OneSixInstance *from); + virtual ProgressProvider *createInstallTask(OneSixInstance *instance, BaseVersionPtr version, QObject *parent) = 0; + protected: virtual QString id() const = 0; QString filename(const QString &root) const; diff --git a/logic/ForgeInstaller.cpp b/logic/ForgeInstaller.cpp index 6f238c21f..48bfb8a3d 100644 --- a/logic/ForgeInstaller.cpp +++ b/logic/ForgeInstaller.cpp @@ -24,17 +24,24 @@ #include #include #include "MultiMC.h" +#include "tasks/Task.h" #include "OneSixInstance.h" +#include "lists/ForgeVersionList.h" +#include "gui/dialogs/ProgressDialog.h" #include #include #include #include -ForgeInstaller::ForgeInstaller(QString filename, QString universal_url) +ForgeInstaller::ForgeInstaller() + : BaseInstaller() +{ +} +void ForgeInstaller::prepare(const QString &filename, const QString &universalUrl) { std::shared_ptr newVersion; - m_universal_url = universal_url; + m_universal_url = universalUrl; QuaZip zip(filename); if (!zip.open(QuaZip::mdUnzip)) @@ -111,7 +118,6 @@ ForgeInstaller::ForgeInstaller(QString filename, QString universal_url) m_forge_version = newVersion; realVersionId = m_forge_version->id = installObj.value("minecraft").toString(); } - bool ForgeInstaller::add(OneSixInstance *to) { if (!BaseInstaller::add(to)) @@ -226,3 +232,96 @@ bool ForgeInstaller::add(OneSixInstance *to) return true; } + +class ForgeInstallTask : public Task +{ + Q_OBJECT +public: + ForgeInstallTask(ForgeInstaller *installer, OneSixInstance *instance, BaseVersionPtr version, QObject *parent = 0) + : Task(parent), m_installer(installer), m_instance(instance), m_version(version) + { + } + +protected: + void executeTask() override + { + { + setStatus(tr("Installing forge...")); + ForgeVersionPtr forgeVersion = + std::dynamic_pointer_cast(m_version); + if (!forgeVersion) + { + emitFailed(tr("Unknown error occured")); + return; + } + auto entry = MMC->metacache()->resolveEntry("minecraftforge", forgeVersion->filename); + if (entry->stale) + { + NetJob *fjob = new NetJob("Forge download"); + fjob->addNetAction(CacheDownload::make(forgeVersion->installer_url, entry)); + connect(fjob, &NetJob::progress, [this](qint64 current, qint64 total){setProgress(100 * current / qMax((qint64)1, total));}); + connect(fjob, &NetJob::status, [this](const QString &msg){setStatus(msg);}); + connect(fjob, &NetJob::failed, [this](){emitFailed(tr("Failure to download forge"));}); + connect(fjob, &NetJob::succeeded, [this, entry, forgeVersion]() + { + if (!install(entry, forgeVersion)) + { + QLOG_ERROR() << "Failure installing forge"; + emitFailed(tr("Failure to install forge")); + } + else + { + reload(); + } + }); + } + else + { + if (!install(entry, forgeVersion)) + { + QLOG_ERROR() << "Failure installing forge"; + emitFailed(tr("Failure to install forge")); + } + else + { + reload(); + } + } + } + } + + bool install(const std::shared_ptr &entry, const ForgeVersionPtr &forgeVersion) + { + QString forgePath = entry->getFullPath(); + m_installer->prepare(forgePath, forgeVersion->universal_url); + return m_installer->add(m_instance); + } + void reload() + { + try + { + m_instance->reloadVersion(); + emitSucceeded(); + } + catch (MMCError &e) + { + emitFailed(e.cause()); + } + catch (...) + { + emitFailed(tr("Failed to load the version description file for reasons unknown.")); + } + } + +private: + ForgeInstaller *m_installer; + OneSixInstance *m_instance; + BaseVersionPtr m_version; +}; + +ProgressProvider *ForgeInstaller::createInstallTask(OneSixInstance *instance, BaseVersionPtr version, QObject *parent) +{ + return new ForgeInstallTask(this, instance, version, parent); +} + +#include "ForgeInstaller.moc" diff --git a/logic/ForgeInstaller.h b/logic/ForgeInstaller.h index df029f385..05cc994ba 100644 --- a/logic/ForgeInstaller.h +++ b/logic/ForgeInstaller.h @@ -25,12 +25,15 @@ class VersionFinal; class ForgeInstaller : public BaseInstaller { public: - ForgeInstaller(QString filename, QString universal_url); + ForgeInstaller(); + void prepare(const QString &filename, const QString &universalUrl); bool add(OneSixInstance *to) override; QString id() const override { return "net.minecraftforge"; } + ProgressProvider *createInstallTask(OneSixInstance *instance, BaseVersionPtr version, QObject *parent) override; + private: // the version, read from the installer std::shared_ptr m_forge_version; diff --git a/logic/LiteLoaderInstaller.cpp b/logic/LiteLoaderInstaller.cpp index bb4b07ca6..99cc5643b 100644 --- a/logic/LiteLoaderInstaller.cpp +++ b/logic/LiteLoaderInstaller.cpp @@ -23,12 +23,17 @@ #include "VersionFinal.h" #include "OneSixLibrary.h" #include "OneSixInstance.h" +#include "MultiMC.h" +#include "lists/LiteLoaderVersionList.h" -LiteLoaderInstaller::LiteLoaderInstaller(LiteLoaderVersionPtr version) - : BaseInstaller(), m_version(version) +LiteLoaderInstaller::LiteLoaderInstaller() : BaseInstaller() { } +void LiteLoaderInstaller::prepare(LiteLoaderVersionPtr version) +{ + m_version = version; +} bool LiteLoaderInstaller::add(OneSixInstance *to) { if (!BaseInstaller::add(to)) @@ -84,3 +89,62 @@ bool LiteLoaderInstaller::add(OneSixInstance *to) return true; } + +class LiteLoaderInstallTask : public Task +{ + Q_OBJECT +public: + LiteLoaderInstallTask(LiteLoaderInstaller *installer, OneSixInstance *instance, + BaseVersionPtr version, QObject *parent) + : Task(parent), m_installer(installer), m_instance(instance), m_version(version) + { + } + +protected: + void executeTask() override + { + LiteLoaderVersionPtr liteloaderVersion = + std::dynamic_pointer_cast(m_version); + if (!liteloaderVersion) + { + return; + } + m_installer->prepare(liteloaderVersion); + if (!m_installer->add(m_instance)) + { + emitFailed(tr("For reasons unknown, the LiteLoader installation failed. Check your " + "MultiMC log files for details.")); + } + else + { + try + { + m_instance->reloadVersion(); + emitSucceeded(); + } + catch (MMCError &e) + { + emitFailed(e.cause()); + } + catch (...) + { + emitFailed( + tr("Failed to load the version description file for reasons unknown.")); + } + } + } + +private: + LiteLoaderInstaller *m_installer; + OneSixInstance *m_instance; + BaseVersionPtr m_version; +}; + +ProgressProvider *LiteLoaderInstaller::createInstallTask(OneSixInstance *instance, + BaseVersionPtr version, + QObject *parent) +{ + return new LiteLoaderInstallTask(this, instance, version, parent); +} + +#include "LiteLoaderInstaller.moc" diff --git a/logic/LiteLoaderInstaller.h b/logic/LiteLoaderInstaller.h index 2e0de64a8..3ab5acb25 100644 --- a/logic/LiteLoaderInstaller.h +++ b/logic/LiteLoaderInstaller.h @@ -25,10 +25,13 @@ class LiteLoaderInstaller : public BaseInstaller { public: - LiteLoaderInstaller(LiteLoaderVersionPtr version); + LiteLoaderInstaller(); + void prepare(LiteLoaderVersionPtr version); bool add(OneSixInstance *to) override; + ProgressProvider *createInstallTask(OneSixInstance *instance, BaseVersionPtr version, QObject *parent) override; + private: virtual QString id() const override {