Added progress bar to Prism instance export

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2023-07-16 20:57:57 +03:00
parent 251055302e
commit cadb7142f0
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
4 changed files with 136 additions and 39 deletions

View File

@ -2,6 +2,7 @@
/* /*
* PolyMC - Minecraft Launcher * PolyMC - Minecraft Launcher
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net> * Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -41,9 +42,11 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QtConcurrentRun>
namespace MMCZip {
// ours // ours
bool MMCZip::mergeZipFiles(QuaZip* into, QFileInfo from, QSet<QString>& contained, const FilterFunction filter) bool mergeZipFiles(QuaZip* into, QFileInfo from, QSet<QString>& contained, const FilterFunction filter)
{ {
QuaZip modZip(from.filePath()); QuaZip modZip(from.filePath());
modZip.open(QuaZip::mdUnzip); modZip.open(QuaZip::mdUnzip);
@ -86,7 +89,7 @@ bool MMCZip::mergeZipFiles(QuaZip* into, QFileInfo from, QSet<QString>& containe
return true; return true;
} }
bool MMCZip::compressDirFiles(QuaZip* zip, QString dir, QFileInfoList files, bool followSymlinks) bool compressDirFiles(QuaZip* zip, QString dir, QFileInfoList files, bool followSymlinks)
{ {
QDir directory(dir); QDir directory(dir);
if (!directory.exists()) if (!directory.exists())
@ -109,7 +112,7 @@ bool MMCZip::compressDirFiles(QuaZip* zip, QString dir, QFileInfoList files, boo
return true; return true;
} }
bool MMCZip::compressDirFiles(QString fileCompressed, QString dir, QFileInfoList files, bool followSymlinks) bool compressDirFiles(QString fileCompressed, QString dir, QFileInfoList files, bool followSymlinks)
{ {
QuaZip zip(fileCompressed); QuaZip zip(fileCompressed);
QDir().mkpath(QFileInfo(fileCompressed).absolutePath()); QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
@ -130,7 +133,7 @@ bool MMCZip::compressDirFiles(QString fileCompressed, QString dir, QFileInfoList
} }
// ours // ours
bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod*>& mods) bool createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod*>& mods)
{ {
QuaZip zipOut(targetJarPath); QuaZip zipOut(targetJarPath);
if (!zipOut.open(QuaZip::mdCreate)) { if (!zipOut.open(QuaZip::mdCreate)) {
@ -175,14 +178,14 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
dir.cdUp(); dir.cdUp();
QString parent_dir = dir.absolutePath(); QString parent_dir = dir.absolutePath();
auto files = QFileInfoList(); auto files = QFileInfoList();
MMCZip::collectFileListRecursively(what_to_zip, nullptr, &files, nullptr); collectFileListRecursively(what_to_zip, nullptr, &files, nullptr);
for (auto e : files) { for (auto e : files) {
if (addedFiles.contains(e.filePath())) if (addedFiles.contains(e.filePath()))
files.removeAll(e); files.removeAll(e);
} }
if (!MMCZip::compressDirFiles(&zipOut, parent_dir, files)) { if (!compressDirFiles(&zipOut, parent_dir, files)) {
zipOut.close(); zipOut.close();
QFile::remove(targetJarPath); QFile::remove(targetJarPath);
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar."; qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
@ -216,7 +219,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
} }
// ours // ours
QString MMCZip::findFolderOfFileInZip(QuaZip* zip, const QString& what, const QStringList& ignore_paths, const QString& root) QString findFolderOfFileInZip(QuaZip* zip, const QString& what, const QStringList& ignore_paths, const QString& root)
{ {
QuaZipDir rootDir(zip, root); QuaZipDir rootDir(zip, root);
for (auto&& fileName : rootDir.entryList(QDir::Files)) { for (auto&& fileName : rootDir.entryList(QDir::Files)) {
@ -240,7 +243,7 @@ QString MMCZip::findFolderOfFileInZip(QuaZip* zip, const QString& what, const QS
} }
// ours // ours
bool MMCZip::findFilesInZip(QuaZip* zip, const QString& what, QStringList& result, const QString& root) bool findFilesInZip(QuaZip* zip, const QString& what, QStringList& result, const QString& root)
{ {
QuaZipDir rootDir(zip, root); QuaZipDir rootDir(zip, root);
for (auto fileName : rootDir.entryList(QDir::Files)) { for (auto fileName : rootDir.entryList(QDir::Files)) {
@ -256,7 +259,7 @@ bool MMCZip::findFilesInZip(QuaZip* zip, const QString& what, QStringList& resul
} }
// ours // ours
std::optional<QStringList> MMCZip::extractSubDir(QuaZip* zip, const QString& subdir, const QString& target) std::optional<QStringList> extractSubDir(QuaZip* zip, const QString& subdir, const QString& target)
{ {
auto target_top_dir = QUrl::fromLocalFile(target); auto target_top_dir = QUrl::fromLocalFile(target);
@ -328,13 +331,13 @@ std::optional<QStringList> MMCZip::extractSubDir(QuaZip* zip, const QString& sub
} }
// ours // ours
bool MMCZip::extractRelFile(QuaZip* zip, const QString& file, const QString& target) bool extractRelFile(QuaZip* zip, const QString& file, const QString& target)
{ {
return JlCompress::extractFile(zip, file, target); return JlCompress::extractFile(zip, file, target);
} }
// ours // ours
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString dir) std::optional<QStringList> extractDir(QString fileCompressed, QString dir)
{ {
QuaZip zip(fileCompressed); QuaZip zip(fileCompressed);
if (!zip.open(QuaZip::mdUnzip)) { if (!zip.open(QuaZip::mdUnzip)) {
@ -347,11 +350,11 @@ std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString di
; ;
return std::nullopt; return std::nullopt;
} }
return MMCZip::extractSubDir(&zip, "", dir); return extractSubDir(&zip, "", dir);
} }
// ours // ours
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString subdir, QString dir) std::optional<QStringList> extractDir(QString fileCompressed, QString subdir, QString dir)
{ {
QuaZip zip(fileCompressed); QuaZip zip(fileCompressed);
if (!zip.open(QuaZip::mdUnzip)) { if (!zip.open(QuaZip::mdUnzip)) {
@ -364,11 +367,11 @@ std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString su
; ;
return std::nullopt; return std::nullopt;
} }
return MMCZip::extractSubDir(&zip, subdir, dir); return extractSubDir(&zip, subdir, dir);
} }
// ours // ours
bool MMCZip::extractFile(QString fileCompressed, QString file, QString target) bool extractFile(QString fileCompressed, QString file, QString target)
{ {
QuaZip zip(fileCompressed); QuaZip zip(fileCompressed);
if (!zip.open(QuaZip::mdUnzip)) { if (!zip.open(QuaZip::mdUnzip)) {
@ -380,13 +383,10 @@ bool MMCZip::extractFile(QString fileCompressed, QString file, QString target)
qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError(); qWarning() << "Could not open archive for unzipping:" << fileCompressed << "Error:" << zip.getZipError();
return false; return false;
} }
return MMCZip::extractRelFile(&zip, file, target); return extractRelFile(&zip, file, target);
} }
bool MMCZip::collectFileListRecursively(const QString& rootDir, bool collectFileListRecursively(const QString& rootDir, const QString& subDir, QFileInfoList* files, FilterFunction excludeFilter)
const QString& subDir,
QFileInfoList* files,
MMCZip::FilterFunction excludeFilter)
{ {
QDir rootDirectory(rootDir); QDir rootDirectory(rootDir);
if (!rootDirectory.exists()) if (!rootDirectory.exists())
@ -417,7 +417,52 @@ bool MMCZip::collectFileListRecursively(const QString& rootDir,
continue; continue;
} }
files->append(e); // we want the original paths for MMCZip::compressDirFiles files->append(e); // we want the original paths for compressDirFiles
} }
return true; return true;
} }
void ExportToZipTask::executeTask()
{
(void)QtConcurrent::run(QThreadPool::globalInstance(), [this]() {
setStatus("Adding files...");
setProgress(0, m_files.length());
if (!m_dir.exists()) {
emitFailed(tr("Folder doesn't exist"));
return;
}
if (!m_output->isOpen() && !m_output->open(QuaZip::mdCreate)) {
emitFailed(tr("Could not create file"));
return;
}
for (const QFileInfo& file : m_files) {
if (!isRunning())
return;
auto absolute = file.absoluteFilePath();
auto relative = m_dir.relativeFilePath(absolute);
setStatus("Compresing: " + relative);
setProgress(m_progress + 1, m_progressTotal);
if (m_followSymlinks) {
if (file.isSymLink())
absolute = file.symLinkTarget();
else
absolute = file.canonicalFilePath();
}
if (!JlCompress::compressFile(m_output, absolute, m_destinationPrefix + relative)) {
emitFailed(tr("Could not read and compress %1").arg(relative));
return;
}
}
m_output->close();
if (m_output->getZipError() != 0) {
emitFailed(tr("A zip error occurred"));
return;
}
emitSucceeded();
});
}
} // namespace MMCZip

View File

@ -2,6 +2,7 @@
/* /*
* PolyMC - Minecraft Launcher * PolyMC - Minecraft Launcher
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net> * Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -35,14 +36,16 @@
#pragma once #pragma once
#include <quazip.h>
#include <quazip/JlCompress.h>
#include <QDir>
#include <QFileInfo> #include <QFileInfo>
#include <QSet> #include <QSet>
#include <QString> #include <QString>
#include <functional> #include <functional>
#include "minecraft/mod/Mod.h"
#include <quazip/JlCompress.h>
#include <optional> #include <optional>
#include "minecraft/mod/Mod.h"
#include "tasks/Task.h"
namespace MMCZip { namespace MMCZip {
using FilterFunction = std::function<bool(const QString&)>; using FilterFunction = std::function<bool(const QString&)>;
@ -139,4 +142,42 @@ bool extractFile(QString fileCompressed, QString file, QString dir);
* \return true for success or false for failure * \return true for success or false for failure
*/ */
bool collectFileListRecursively(const QString& rootDir, const QString& subDir, QFileInfoList* files, FilterFunction excludeFilter); bool collectFileListRecursively(const QString& rootDir, const QString& subDir, QFileInfoList* files, FilterFunction excludeFilter);
class ExportToZipTask : public Task {
public:
ExportToZipTask(QuaZip* output,
QDir dir,
QFileInfoList files,
QString destinationPrefix = "",
bool followSymlinks = false,
bool cleanUp = false)
: m_output(output)
, m_dir(dir)
, m_files(files)
, m_destinationPrefix(destinationPrefix)
, m_followSymlinks(followSymlinks)
, m_cleanUp(cleanUp)
{
setAbortable(true);
};
ExportToZipTask(QString outputPath, QString dir, QFileInfoList files, QString destinationPrefix = "", bool followSymlinks = false)
: ExportToZipTask(new QuaZip(outputPath), QDir(dir), files, destinationPrefix, followSymlinks, true){};
virtual ~ExportToZipTask()
{
if (m_cleanUp)
delete m_output;
}
protected:
virtual void executeTask() override;
private:
QuaZip* m_output;
QDir m_dir;
QFileInfoList m_files;
QString m_destinationPrefix;
bool m_followSymlinks;
bool m_cleanUp;
};
} // namespace MMCZip } // namespace MMCZip

View File

@ -3,6 +3,7 @@
* Prism Launcher - Minecraft Launcher * Prism Launcher - Minecraft Launcher
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net> * Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me> * Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -41,6 +42,9 @@
#include <QFileSystemModel> #include <QFileSystemModel>
#include <QMessageBox> #include <QMessageBox>
#include "FileIgnoreProxy.h" #include "FileIgnoreProxy.h"
#include "QObjectPtr.h"
#include "ui/dialogs/CustomMessageBox.h"
#include "ui/dialogs/ProgressDialog.h"
#include "ui_ExportInstanceDialog.h" #include "ui_ExportInstanceDialog.h"
#include <FileSystem.h> #include <FileSystem.h>
@ -119,14 +123,15 @@ void SaveIcon(InstancePtr m_instance)
pixmap.save(FS::PathCombine(m_instance->instanceRoot(), iconKey + ".png")); pixmap.save(FS::PathCombine(m_instance->instanceRoot(), iconKey + ".png"));
} }
bool ExportInstanceDialog::doExport() void ExportInstanceDialog::doExport()
{ {
auto name = FS::RemoveInvalidFilenameChars(m_instance->name()); auto name = FS::RemoveInvalidFilenameChars(m_instance->name());
const QString output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(m_instance->name()), const QString output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(m_instance->name()),
FS::PathCombine(QDir::homePath(), name + ".zip"), "Zip (*.zip)", nullptr); FS::PathCombine(QDir::homePath(), name + ".zip"), "Zip (*.zip)", nullptr);
if (output.isEmpty()) { if (output.isEmpty()) {
return false; QDialog::done(QDialog::Rejected);
return;
} }
SaveIcon(m_instance); SaveIcon(m_instance);
@ -135,26 +140,31 @@ bool ExportInstanceDialog::doExport()
if (!MMCZip::collectFileListRecursively(m_instance->instanceRoot(), nullptr, &files, if (!MMCZip::collectFileListRecursively(m_instance->instanceRoot(), nullptr, &files,
std::bind(&FileIgnoreProxy::filterFile, proxyModel, std::placeholders::_1))) { std::bind(&FileIgnoreProxy::filterFile, proxyModel, std::placeholders::_1))) {
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance")); QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));
return false; QDialog::done(QDialog::Rejected);
return;
} }
if (!MMCZip::compressDirFiles(output, m_instance->instanceRoot(), files, true)) { auto task = makeShared<MMCZip::ExportToZipTask>(output, m_instance->instanceRoot(), files, "", true);
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));
return false; connect(task.get(), &Task::failed, this, [this, output](QString reason) {
} CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
return true; QFile::remove(output);
});
connect(task.get(), &Task::aborted, this, [output] { QFile::remove(output); });
connect(task.get(), &Task::finished, this, [task] { task->deleteLater(); });
ProgressDialog progress(this);
progress.setSkipButton(true, tr("Abort"));
auto result = progress.execWithTask(task.get());
QDialog::done(result);
} }
void ExportInstanceDialog::done(int result) void ExportInstanceDialog::done(int result)
{ {
savePackIgnore(); savePackIgnore();
if (result == QDialog::Accepted) { if (result == QDialog::Accepted) {
if (doExport()) { doExport();
QDialog::done(QDialog::Accepted); return;
return;
} else {
return;
}
} }
QDialog::done(result); QDialog::done(result);
} }

View File

@ -2,6 +2,7 @@
/* /*
* Prism Launcher - Minecraft Launcher * Prism Launcher - Minecraft Launcher
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me> * Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -58,7 +59,7 @@ class ExportInstanceDialog : public QDialog {
virtual void done(int result); virtual void done(int result);
private: private:
bool doExport(); void doExport();
void loadPackIgnore(); void loadPackIgnore();
void savePackIgnore(); void savePackIgnore();
QString ignoreFileName(); QString ignoreFileName();