Move task to another thread

I don't know whether this is the prefered method.

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad 2023-03-03 11:14:57 +00:00
parent f28a7b9d08
commit adcdf28d64
3 changed files with 60 additions and 63 deletions

View File

@ -17,6 +17,7 @@
*/ */
#include "ModrinthPackExportTask.h" #include "ModrinthPackExportTask.h"
#include <qtconcurrentrun.h>
#include <QFileInfoList> #include <QFileInfoList>
#include <QMessageBox> #include <QMessageBox>
#include "MMCZip.h" #include "MMCZip.h"
@ -34,57 +35,59 @@ ModrinthPackExportTask::ModrinthPackExportTask(const QString& name,
void ModrinthPackExportTask::executeTask() void ModrinthPackExportTask::executeTask()
{ {
QFileInfoList files; QtConcurrent::run(QThreadPool::globalInstance(), [this] {
if (!MMCZip::collectFileListRecursively(instance->gameRoot(), nullptr, &files, filter)) { QFileInfoList files;
emitFailed(tr("Could not collect list of files")); if (!MMCZip::collectFileListRecursively(instance->gameRoot(), nullptr, &files, filter)) {
return; emitFailed(tr("Could not collect list of files"));
}
setStatus("Adding files...");
QuaZip zip(output);
if (!zip.open(QuaZip::mdCreate)) {
QFile::remove(output);
emitFailed(tr("Could not create file"));
return;
}
{
QuaZipFile indexFile(&zip);
if (!indexFile.open(QIODevice::WriteOnly, QuaZipNewInfo("modrinth.index.json"))) {
QFile::remove(output);
emitFailed(tr("Could not create index"));
return; return;
} }
indexFile.write(generateIndex());
}
// should exist setStatus("Adding files...");
QDir dotMinecraft(instance->gameRoot());
{ QuaZip zip(output);
size_t i = 0; if (!zip.open(QuaZip::mdCreate)) {
for (const QFileInfo& file : files) { QFile::remove(output);
setProgress(i, files.length()); emitFailed(tr("Could not create file"));
if (!JlCompress::compressFile(&zip, file.absoluteFilePath(), return;
"overrides/" + dotMinecraft.relativeFilePath(file.absoluteFilePath()))) { }
emitFailed(tr("Could not compress %1").arg(file.absoluteFilePath()));
{
QuaZipFile indexFile(&zip);
if (!indexFile.open(QIODevice::WriteOnly, QuaZipNewInfo("modrinth.index.json"))) {
QFile::remove(output);
emitFailed(tr("Could not create index"));
return; return;
} }
i++; indexFile.write(generateIndex());
} }
}
zip.close(); // should exist
QDir dotMinecraft(instance->gameRoot());
if (zip.getZipError() != 0) { {
QFile::remove(output); size_t i = 0;
emitFailed(tr("A zip error occured")); for (const QFileInfo& file : files) {
return; setProgress(i, files.length());
} if (!JlCompress::compressFile(&zip, file.absoluteFilePath(),
"overrides/" + dotMinecraft.relativeFilePath(file.absoluteFilePath()))) {
emitFailed(tr("Could not compress %1").arg(file.absoluteFilePath()));
return;
}
i++;
}
}
emitSucceeded(); zip.close();
if (zip.getZipError() != 0) {
QFile::remove(output);
emitFailed(tr("A zip error occured"));
return;
}
emitSucceeded();
});
} }
QByteArray ModrinthPackExportTask::generateIndex() QByteArray ModrinthPackExportTask::generateIndex()

View File

@ -56,26 +56,22 @@ ExportMrPackDialog::~ExportMrPackDialog()
void ExportMrPackDialog::done(int result) void ExportMrPackDialog::done(int result)
{ {
if (result == Accepted) if (result == Accepted) {
runExport(); const QString filename = FS::RemoveInvalidFilenameChars(ui->name->text());
const QString output =
QFileDialog::getSaveFileName(this, tr("Export %1").arg(ui->name->text()), FS::PathCombine(QDir::homePath(), filename + ".mrpack"),
"Modrinth modpack (*.mrpack *.zip)", nullptr);
if (output.isEmpty())
return;
ModrinthPackExportTask task(ui->name->text(), ui->version->text(), ui->summary->text(), instance, output,
[this](const QString& path) { return proxy->blockedPaths().covers(path); });
ProgressDialog progress(this);
progress.setSkipButton(true, tr("Abort"));
progress.execWithTask(&task);
}
QDialog::done(result); QDialog::done(result);
} }
void ExportMrPackDialog::runExport()
{
const QString filename = FS::RemoveInvalidFilenameChars(ui->name->text());
const QString output =
QFileDialog::getSaveFileName(this, tr("Export %1").arg(ui->name->text()), FS::PathCombine(QDir::homePath(), filename + ".mrpack"),
"Modrinth modpack (*.mrpack *.zip)", nullptr);
if (output.isEmpty())
return;
ModrinthPackExportTask task(ui->name->text(), ui->version->text(), ui->summary->text(), instance, output,
[this](const QString& path) { return proxy->blockedPaths().covers(path); });
ProgressDialog progress(this);
progress.setSkipButton(true, tr("Abort"));
progress.execWithTask(&task);
}

View File

@ -39,6 +39,4 @@ class ExportMrPackDialog : public QDialog {
const InstancePtr instance; const InstancePtr instance;
Ui::ExportMrPackDialog* ui; Ui::ExportMrPackDialog* ui;
PackIgnoreProxy* proxy; PackIgnoreProxy* proxy;
void runExport();
}; };