Change to use future

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad 2023-05-02 14:23:27 +01:00
parent 709736d3f9
commit e0380960fd
2 changed files with 40 additions and 25 deletions

View File

@ -62,9 +62,12 @@ bool ModrinthPackExportTask::abort()
return true; return true;
} }
pendingAbort = true; if (buildZipFuture.isRunning()) {
buildZipFuture.cancel();
return true; return true;
} }
return false;
}
void ModrinthPackExportTask::collectFiles() void ModrinthPackExportTask::collectFiles()
{ {
@ -190,44 +193,37 @@ void ModrinthPackExportTask::parseApiResponse(const QByteArray* response)
void ModrinthPackExportTask::buildZip() void ModrinthPackExportTask::buildZip()
{ {
static_cast<void>(QtConcurrent::run(QThreadPool::globalInstance(), [this]() {
setStatus("Adding files..."); setStatus("Adding files...");
buildZipFuture = QtConcurrent::run(QThreadPool::globalInstance(), [this]() {
QuaZip zip(output); QuaZip zip(output);
if (!zip.open(QuaZip::mdCreate)) { if (!zip.open(QuaZip::mdCreate)) {
QFile::remove(output); QFile::remove(output);
emitFailed(tr("Could not create file")); return BuildZipResult(tr("Could not create file"));
return;
} }
if (pendingAbort) { if (buildZipFuture.isCanceled())
QMetaObject::invokeMethod(this, &ModrinthPackExportTask::emitAborted, Qt::QueuedConnection); return BuildZipResult();
return;
}
QuaZipFile indexFile(&zip); QuaZipFile indexFile(&zip);
if (!indexFile.open(QIODevice::WriteOnly, QuaZipNewInfo("modrinth.index.json"))) { if (!indexFile.open(QIODevice::WriteOnly, QuaZipNewInfo("modrinth.index.json"))) {
QFile::remove(output); QFile::remove(output);
QMetaObject::invokeMethod( return BuildZipResult(tr("Could not create index"));
this, [this]() { emitFailed(tr("Could not create index")); }, Qt::QueuedConnection);
return;
} }
indexFile.write(generateIndex()); indexFile.write(generateIndex());
size_t progress = 0; size_t progress = 0;
for (const QFileInfo& file : files) { for (const QFileInfo& file : files) {
if (pendingAbort) { if (buildZipFuture.isCanceled()) {
QFile::remove(output); QFile::remove(output);
QMetaObject::invokeMethod(this, &ModrinthPackExportTask::emitAborted, Qt::QueuedConnection); return BuildZipResult();
return;
} }
setProgress(progress, files.length()); setProgress(progress, files.length());
const QString relative = gameRoot.relativeFilePath(file.absoluteFilePath()); const QString relative = gameRoot.relativeFilePath(file.absoluteFilePath());
if (!resolvedFiles.contains(relative) && !JlCompress::compressFile(&zip, file.absoluteFilePath(), "overrides/" + relative)) { if (!resolvedFiles.contains(relative) && !JlCompress::compressFile(&zip, file.absoluteFilePath(), "overrides/" + relative)) {
QFile::remove(output); QFile::remove(output);
QMetaObject::invokeMethod( return BuildZipResult(tr("Could not read and compress %1").arg(relative));
this, [this, relative]() { emitFailed(tr("Could not read and compress %1").arg(relative)); }, Qt::QueuedConnection);
return;
} }
progress++; progress++;
} }
@ -236,13 +232,26 @@ void ModrinthPackExportTask::buildZip()
if (zip.getZipError() != 0) { if (zip.getZipError() != 0) {
QFile::remove(output); QFile::remove(output);
QMetaObject::invokeMethod( return BuildZipResult(tr("A zip error occurred"));
this, [this]() { emitFailed(tr("A zip error occurred")); }, Qt::QueuedConnection);
return;
} }
QMetaObject::invokeMethod(this, &ModrinthPackExportTask::emitSucceeded, Qt::QueuedConnection); return BuildZipResult();
})); });
connect(&buildZipWatcher, &QFutureWatcher<BuildZipResult>::finished, this, &ModrinthPackExportTask::finish);
buildZipWatcher.setFuture(buildZipFuture);
}
void ModrinthPackExportTask::finish()
{
if (buildZipFuture.isCanceled())
emitAborted();
else {
const BuildZipResult result = buildZipFuture.result();
if (result.has_value())
emitFailed(result.value());
else
emitSucceeded();
}
} }
QByteArray ModrinthPackExportTask::generateIndex() QByteArray ModrinthPackExportTask::generateIndex()

View File

@ -18,6 +18,8 @@
#pragma once #pragma once
#include <QFuture>
#include <QFutureWatcher>
#include "BaseInstance.h" #include "BaseInstance.h"
#include "MMCZip.h" #include "MMCZip.h"
#include "minecraft/MinecraftInstance.h" #include "minecraft/MinecraftInstance.h"
@ -54,18 +56,22 @@ class ModrinthPackExportTask : public Task {
const QString output; const QString output;
const MMCZip::FilterFunction filter; const MMCZip::FilterFunction filter;
typedef std::optional<QString> BuildZipResult;
ModrinthAPI api; ModrinthAPI api;
QFileInfoList files; QFileInfoList files;
QMap<QString, QString> pendingHashes; QMap<QString, QString> pendingHashes;
QMap<QString, ResolvedFile> resolvedFiles; QMap<QString, ResolvedFile> resolvedFiles;
Task::Ptr task; Task::Ptr task;
bool pendingAbort = false; QFuture<BuildZipResult> buildZipFuture;
QFutureWatcher<BuildZipResult> buildZipWatcher;
void collectFiles(); void collectFiles();
void collectHashes(); void collectHashes();
void makeApiRequest(); void makeApiRequest();
void parseApiResponse(const QByteArray* response); void parseApiResponse(const QByteArray* response);
void buildZip(); void buildZip();
void finish();
QByteArray generateIndex(); QByteArray generateIndex();
}; };