feat: add mod index updating to ModDownloadTask

This makes ModDownloadTask into a SequentialTask with 2 subtasks:
Downloading the mod files and updating the index with the new
information.

The index updating is done first so that, in the future, we
can prompt the user before download if, for instance, we discover
there's another version already installed.
This commit is contained in:
flow 2022-04-13 19:18:28 -03:00 committed by flow
parent b30b88716e
commit c86c719e1a
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
3 changed files with 25 additions and 28 deletions

View File

@ -1,24 +1,28 @@
#include "ModDownloadTask.h"
#include "Application.h"
#include "minecraft/mod/LocalModUpdateTask.h"
ModDownloadTask::ModDownloadTask(const QUrl sourceUrl,const QString filename, const std::shared_ptr<ModFolderModel> mods)
: m_sourceUrl(sourceUrl), mods(mods), filename(filename) {
}
ModDownloadTask::ModDownloadTask(ModPlatform::IndexedPack mod, ModPlatform::IndexedVersion version, const std::shared_ptr<ModFolderModel> mods)
: m_mod(mod), m_mod_version(version), mods(mods)
{
m_update_task.reset(new LocalModUpdateTask(mods->dir(), m_mod, m_mod_version));
void ModDownloadTask::executeTask() {
setStatus(tr("Downloading mod:\n%1").arg(m_sourceUrl.toString()));
addTask(m_update_task);
m_filesNetJob.reset(new NetJob(tr("Mod download"), APPLICATION->network()));
m_filesNetJob->addNetAction(Net::Download::makeFile(m_sourceUrl, mods->dir().absoluteFilePath(filename)));
m_filesNetJob->setStatus(tr("Downloading mod:\n%1").arg(m_mod_version.downloadUrl));
m_filesNetJob->addNetAction(Net::Download::makeFile(m_mod_version.downloadUrl, mods->dir().absoluteFilePath(getFilename())));
connect(m_filesNetJob.get(), &NetJob::succeeded, this, &ModDownloadTask::downloadSucceeded);
connect(m_filesNetJob.get(), &NetJob::progress, this, &ModDownloadTask::downloadProgressChanged);
connect(m_filesNetJob.get(), &NetJob::failed, this, &ModDownloadTask::downloadFailed);
m_filesNetJob->start();
addTask(m_filesNetJob);
}
void ModDownloadTask::downloadSucceeded()
{
emitSucceeded();
m_filesNetJob.reset();
}
@ -32,8 +36,3 @@ void ModDownloadTask::downloadProgressChanged(qint64 current, qint64 total)
{
emit progress(current, total);
}
bool ModDownloadTask::abort() {
return m_filesNetJob->abort();
}

View File

@ -1,28 +1,26 @@
#pragma once
#include "QObjectPtr.h"
#include "tasks/Task.h"
#include "minecraft/mod/LocalModUpdateTask.h"
#include "modplatform/ModIndex.h"
#include "tasks/SequentialTask.h"
#include "minecraft/mod/ModFolderModel.h"
#include "net/NetJob.h"
#include <QUrl>
class ModDownloadTask : public Task {
class ModDownloadTask : public SequentialTask {
Q_OBJECT
public:
explicit ModDownloadTask(const QUrl sourceUrl, const QString filename, const std::shared_ptr<ModFolderModel> mods);
const QString& getFilename() const { return filename; }
public slots:
bool abort() override;
protected:
//! Entry point for tasks.
void executeTask() override;
explicit ModDownloadTask(ModPlatform::IndexedPack mod, ModPlatform::IndexedVersion version, const std::shared_ptr<ModFolderModel> mods);
const QString& getFilename() const { return m_mod_version.fileName; }
private:
QUrl m_sourceUrl;
NetJob::Ptr m_filesNetJob;
ModPlatform::IndexedPack m_mod;
ModPlatform::IndexedVersion m_mod_version;
const std::shared_ptr<ModFolderModel> mods;
const QString filename;
NetJob::Ptr m_filesNetJob;
LocalModUpdateTask::Ptr m_update_task;
void downloadProgressChanged(qint64 current, qint64 total);

View File

@ -150,7 +150,7 @@ void ModPage::onModSelected()
if (dialog->isModSelected(current.name, version.fileName)) {
dialog->removeSelectedMod(current.name);
} else {
dialog->addSelectedMod(current.name, new ModDownloadTask(version.downloadUrl, version.fileName, dialog->mods));
dialog->addSelectedMod(current.name, new ModDownloadTask(current, version, dialog->mods));
}
updateSelectionButton();