fix java download tasks

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2024-06-16 15:09:49 +03:00
parent 94fd02a34f
commit 8d3e6667cd
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
4 changed files with 49 additions and 41 deletions

View File

@ -50,21 +50,17 @@ void ArchiveDownloadTask::executeTask()
download->addNetAction(action); download->addNetAction(action);
auto fullPath = entry->getFullPath(); auto fullPath = entry->getFullPath();
connect(download.get(), &NetJob::finished, [download, this] { connect(download.get(), &Task::failed, this, &ArchiveDownloadTask::emitFailed);
disconnect(this, &Task::aborted, download.get(), &NetJob::abort);
download->deleteLater();
});
connect(download.get(), &NetJob::failed, this, &ArchiveDownloadTask::emitFailed);
connect(this, &Task::aborted, download.get(), &NetJob::abort);
connect(download.get(), &Task::progress, this, &ArchiveDownloadTask::setProgress); connect(download.get(), &Task::progress, this, &ArchiveDownloadTask::setProgress);
connect(download.get(), &Task::stepProgress, this, &ArchiveDownloadTask::propagateStepProgress); connect(download.get(), &Task::stepProgress, this, &ArchiveDownloadTask::propagateStepProgress);
connect(download.get(), &Task::status, this, &ArchiveDownloadTask::setStatus); connect(download.get(), &Task::status, this, &ArchiveDownloadTask::setStatus);
connect(download.get(), &Task::details, this, &ArchiveDownloadTask::setDetails); connect(download.get(), &Task::details, this, &ArchiveDownloadTask::setDetails);
connect(download.get(), &NetJob::succeeded, [this, fullPath] { connect(download.get(), &Task::succeeded, [this, fullPath] {
// This should do all of the extracting and creating folders // This should do all of the extracting and creating folders
extractJava(fullPath); extractJava(fullPath);
}); });
download->start(); m_task = download;
m_task->start();
} }
void ArchiveDownloadTask::extractJava(QString input) void ArchiveDownloadTask::extractJava(QString input)
@ -102,34 +98,40 @@ void ArchiveDownloadTask::extractJava(QString input)
emitFailed("Empty archive"); emitFailed("Empty archive");
return; return;
} }
auto zipTask = makeShared<MMCZip::ExtractZipTask>(zip, m_final_path, files[0]); m_task = makeShared<MMCZip::ExtractZipTask>(zip, m_final_path, files[0]);
auto progressStep = std::make_shared<TaskStepProgress>(); auto progressStep = std::make_shared<TaskStepProgress>();
connect(zipTask.get(), &Task::finished, this, [this, progressStep] { connect(m_task.get(), &Task::finished, this, [this, progressStep] {
progressStep->state = TaskStepState::Succeeded; progressStep->state = TaskStepState::Succeeded;
stepProgress(*progressStep); stepProgress(*progressStep);
}); });
connect(this, &Task::aborted, zipTask.get(), &Task::abort); connect(m_task.get(), &Task::succeeded, this, &ArchiveDownloadTask::emitSucceeded);
connect(zipTask.get(), &Task::finished, [zipTask, this] { disconnect(this, &Task::aborted, zipTask.get(), &Task::abort); }); connect(m_task.get(), &Task::aborted, this, &ArchiveDownloadTask::emitAborted);
connect(m_task.get(), &Task::failed, this, [this, progressStep](QString reason) {
connect(zipTask.get(), &Task::succeeded, this, &ArchiveDownloadTask::emitSucceeded);
connect(zipTask.get(), &Task::aborted, this, &ArchiveDownloadTask::emitAborted);
connect(zipTask.get(), &Task::failed, this, [this, progressStep](QString reason) {
progressStep->state = TaskStepState::Failed; progressStep->state = TaskStepState::Failed;
stepProgress(*progressStep); stepProgress(*progressStep);
emitFailed(reason); emitFailed(reason);
}); });
connect(zipTask.get(), &Task::stepProgress, this, &ArchiveDownloadTask::propagateStepProgress); connect(m_task.get(), &Task::stepProgress, this, &ArchiveDownloadTask::propagateStepProgress);
connect(zipTask.get(), &Task::progress, this, [this, progressStep](qint64 current, qint64 total) { connect(m_task.get(), &Task::progress, this, [this, progressStep](qint64 current, qint64 total) {
progressStep->update(current, total); progressStep->update(current, total);
stepProgress(*progressStep); stepProgress(*progressStep);
}); });
connect(zipTask.get(), &Task::status, this, [this, progressStep](QString status) { connect(m_task.get(), &Task::status, this, [this, progressStep](QString status) {
progressStep->status = status; progressStep->status = status;
stepProgress(*progressStep); stepProgress(*progressStep);
}); });
zipTask->start(); m_task->start();
} }
bool ArchiveDownloadTask::abort()
{
auto aborted = canAbort();
if (m_task)
aborted = m_task->abort();
emitAborted();
return aborted;
};
} // namespace Java } // namespace Java

View File

@ -30,6 +30,7 @@ class ArchiveDownloadTask : public Task {
[[nodiscard]] bool canAbort() const override { return true; } [[nodiscard]] bool canAbort() const override { return true; }
void executeTask() override; void executeTask() override;
virtual bool abort() override;
private slots: private slots:
void extractJava(QString input); void extractJava(QString input);
@ -39,5 +40,6 @@ class ArchiveDownloadTask : public Task {
QString m_final_path; QString m_final_path;
QString m_checksum_type; QString m_checksum_type;
QString m_checksum_hash; QString m_checksum_hash;
Task::Ptr m_task;
}; };
} // namespace Java } // namespace Java

View File

@ -51,18 +51,13 @@ void ManifestDownloadTask::executeTask()
} }
download->addNetAction(action); download->addNetAction(action);
connect(download.get(), &NetJob::finished, [download, this] { connect(download.get(), &Task::failed, this, &ManifestDownloadTask::emitFailed);
disconnect(this, &Task::aborted, download.get(), &NetJob::abort);
download->deleteLater();
});
connect(download.get(), &NetJob::failed, this, &ManifestDownloadTask::emitFailed);
connect(this, &Task::aborted, download.get(), &NetJob::abort);
connect(download.get(), &Task::progress, this, &ManifestDownloadTask::setProgress); connect(download.get(), &Task::progress, this, &ManifestDownloadTask::setProgress);
connect(download.get(), &Task::stepProgress, this, &ManifestDownloadTask::propagateStepProgress); connect(download.get(), &Task::stepProgress, this, &ManifestDownloadTask::propagateStepProgress);
connect(download.get(), &Task::status, this, &ManifestDownloadTask::setStatus); connect(download.get(), &Task::status, this, &ManifestDownloadTask::setStatus);
connect(download.get(), &Task::details, this, &ManifestDownloadTask::setDetails); connect(download.get(), &Task::details, this, &ManifestDownloadTask::setDetails);
connect(download.get(), &NetJob::succeeded, [files, this] { connect(download.get(), &Task::succeeded, [files, this] {
QJsonParseError parse_error{}; QJsonParseError parse_error{};
QJsonDocument doc = QJsonDocument::fromJson(*files, &parse_error); QJsonDocument doc = QJsonDocument::fromJson(*files, &parse_error);
if (parse_error.error != QJsonParseError::NoError) { if (parse_error.error != QJsonParseError::NoError) {
@ -73,7 +68,8 @@ void ManifestDownloadTask::executeTask()
} }
downloadJava(doc); downloadJava(doc);
}); });
download->start(); m_task = download;
m_task->start();
} }
void ManifestDownloadTask::downloadJava(const QJsonDocument& doc) void ManifestDownloadTask::downloadJava(const QJsonDocument& doc)
@ -107,7 +103,7 @@ void ManifestDownloadTask::downloadJava(const QJsonDocument& doc)
} }
} }
} }
auto elementDownload = new NetJob("JRE::FileDownload", APPLICATION->network()); auto elementDownload = makeShared<NetJob>("JRE::FileDownload", APPLICATION->network());
for (const auto& file : toDownload) { for (const auto& file : toDownload) {
auto dl = Net::Download::makeFile(file.url, file.path); auto dl = Net::Download::makeFile(file.url, file.path);
if (!file.hash.isEmpty()) { if (!file.hash.isEmpty()) {
@ -119,18 +115,24 @@ void ManifestDownloadTask::downloadJava(const QJsonDocument& doc)
} }
elementDownload->addNetAction(dl); elementDownload->addNetAction(dl);
} }
connect(elementDownload, &NetJob::finished, [elementDownload, this] {
disconnect(this, &Task::aborted, elementDownload, &NetJob::abort);
elementDownload->deleteLater();
});
connect(elementDownload, &NetJob::failed, this, &ManifestDownloadTask::emitFailed);
connect(elementDownload, &Task::progress, this, &ManifestDownloadTask::setProgress);
connect(elementDownload, &Task::stepProgress, this, &ManifestDownloadTask::propagateStepProgress);
connect(elementDownload, &Task::status, this, &ManifestDownloadTask::setStatus);
connect(elementDownload, &Task::details, this, &ManifestDownloadTask::setDetails);
connect(this, &Task::aborted, elementDownload, &NetJob::abort); connect(elementDownload.get(), &Task::failed, this, &ManifestDownloadTask::emitFailed);
connect(elementDownload, &NetJob::succeeded, [this] { emitSucceeded(); }); connect(elementDownload.get(), &Task::progress, this, &ManifestDownloadTask::setProgress);
elementDownload->start(); connect(elementDownload.get(), &Task::stepProgress, this, &ManifestDownloadTask::propagateStepProgress);
connect(elementDownload.get(), &Task::status, this, &ManifestDownloadTask::setStatus);
connect(elementDownload.get(), &Task::details, this, &ManifestDownloadTask::setDetails);
connect(elementDownload.get(), &Task::succeeded, this, &ManifestDownloadTask::emitSucceeded);
m_task = elementDownload;
m_task->start();
} }
bool ManifestDownloadTask::abort()
{
auto aborted = canAbort();
if (m_task)
aborted = m_task->abort();
emitAborted();
return aborted;
};
} // namespace Java } // namespace Java

View File

@ -31,6 +31,7 @@ class ManifestDownloadTask : public Task {
[[nodiscard]] bool canAbort() const override { return true; } [[nodiscard]] bool canAbort() const override { return true; }
void executeTask() override; void executeTask() override;
virtual bool abort() override;
private slots: private slots:
void downloadJava(const QJsonDocument& doc); void downloadJava(const QJsonDocument& doc);
@ -40,5 +41,6 @@ class ManifestDownloadTask : public Task {
QString m_final_path; QString m_final_path;
QString m_checksum_type; QString m_checksum_type;
QString m_checksum_hash; QString m_checksum_hash;
Task::Ptr m_task;
}; };
} // namespace Java } // namespace Java