Added progress bar to Prism instance export
Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
parent
251055302e
commit
cadb7142f0
@ -2,6 +2,7 @@
|
||||
/*
|
||||
* PolyMC - Minecraft Launcher
|
||||
* 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
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -41,9 +42,11 @@
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDebug>
|
||||
#include <QtConcurrentRun>
|
||||
|
||||
namespace MMCZip {
|
||||
// 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());
|
||||
modZip.open(QuaZip::mdUnzip);
|
||||
@ -86,7 +89,7 @@ bool MMCZip::mergeZipFiles(QuaZip* into, QFileInfo from, QSet<QString>& containe
|
||||
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);
|
||||
if (!directory.exists())
|
||||
@ -109,7 +112,7 @@ bool MMCZip::compressDirFiles(QuaZip* zip, QString dir, QFileInfoList files, boo
|
||||
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);
|
||||
QDir().mkpath(QFileInfo(fileCompressed).absolutePath());
|
||||
@ -130,7 +133,7 @@ bool MMCZip::compressDirFiles(QString fileCompressed, QString dir, QFileInfoList
|
||||
}
|
||||
|
||||
// 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);
|
||||
if (!zipOut.open(QuaZip::mdCreate)) {
|
||||
@ -175,14 +178,14 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
|
||||
dir.cdUp();
|
||||
QString parent_dir = dir.absolutePath();
|
||||
auto files = QFileInfoList();
|
||||
MMCZip::collectFileListRecursively(what_to_zip, nullptr, &files, nullptr);
|
||||
collectFileListRecursively(what_to_zip, nullptr, &files, nullptr);
|
||||
|
||||
for (auto e : files) {
|
||||
if (addedFiles.contains(e.filePath()))
|
||||
files.removeAll(e);
|
||||
}
|
||||
|
||||
if (!MMCZip::compressDirFiles(&zipOut, parent_dir, files)) {
|
||||
if (!compressDirFiles(&zipOut, parent_dir, files)) {
|
||||
zipOut.close();
|
||||
QFile::remove(targetJarPath);
|
||||
qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
|
||||
@ -216,7 +219,7 @@ bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const
|
||||
}
|
||||
|
||||
// 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);
|
||||
for (auto&& fileName : rootDir.entryList(QDir::Files)) {
|
||||
@ -240,7 +243,7 @@ QString MMCZip::findFolderOfFileInZip(QuaZip* zip, const QString& what, const QS
|
||||
}
|
||||
|
||||
// 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);
|
||||
for (auto fileName : rootDir.entryList(QDir::Files)) {
|
||||
@ -256,7 +259,7 @@ bool MMCZip::findFilesInZip(QuaZip* zip, const QString& what, QStringList& resul
|
||||
}
|
||||
|
||||
// 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);
|
||||
|
||||
@ -328,13 +331,13 @@ std::optional<QStringList> MMCZip::extractSubDir(QuaZip* zip, const QString& sub
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// ours
|
||||
std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString dir)
|
||||
std::optional<QStringList> extractDir(QString fileCompressed, QString dir)
|
||||
{
|
||||
QuaZip zip(fileCompressed);
|
||||
if (!zip.open(QuaZip::mdUnzip)) {
|
||||
@ -347,11 +350,11 @@ std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString di
|
||||
;
|
||||
return std::nullopt;
|
||||
}
|
||||
return MMCZip::extractSubDir(&zip, "", dir);
|
||||
return extractSubDir(&zip, "", dir);
|
||||
}
|
||||
|
||||
// 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);
|
||||
if (!zip.open(QuaZip::mdUnzip)) {
|
||||
@ -364,11 +367,11 @@ std::optional<QStringList> MMCZip::extractDir(QString fileCompressed, QString su
|
||||
;
|
||||
return std::nullopt;
|
||||
}
|
||||
return MMCZip::extractSubDir(&zip, subdir, dir);
|
||||
return extractSubDir(&zip, subdir, dir);
|
||||
}
|
||||
|
||||
// ours
|
||||
bool MMCZip::extractFile(QString fileCompressed, QString file, QString target)
|
||||
bool extractFile(QString fileCompressed, QString file, QString target)
|
||||
{
|
||||
QuaZip zip(fileCompressed);
|
||||
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();
|
||||
return false;
|
||||
}
|
||||
return MMCZip::extractRelFile(&zip, file, target);
|
||||
return extractRelFile(&zip, file, target);
|
||||
}
|
||||
|
||||
bool MMCZip::collectFileListRecursively(const QString& rootDir,
|
||||
const QString& subDir,
|
||||
QFileInfoList* files,
|
||||
MMCZip::FilterFunction excludeFilter)
|
||||
bool collectFileListRecursively(const QString& rootDir, const QString& subDir, QFileInfoList* files, FilterFunction excludeFilter)
|
||||
{
|
||||
QDir rootDirectory(rootDir);
|
||||
if (!rootDirectory.exists())
|
||||
@ -417,7 +417,52 @@ bool MMCZip::collectFileListRecursively(const QString& rootDir,
|
||||
continue;
|
||||
}
|
||||
|
||||
files->append(e); // we want the original paths for MMCZip::compressDirFiles
|
||||
files->append(e); // we want the original paths for compressDirFiles
|
||||
}
|
||||
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
|
@ -2,6 +2,7 @@
|
||||
/*
|
||||
* PolyMC - Minecraft Launcher
|
||||
* 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
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -35,14 +36,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <quazip.h>
|
||||
#include <quazip/JlCompress.h>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QSet>
|
||||
#include <QString>
|
||||
#include <functional>
|
||||
#include "minecraft/mod/Mod.h"
|
||||
|
||||
#include <quazip/JlCompress.h>
|
||||
#include <optional>
|
||||
#include "minecraft/mod/Mod.h"
|
||||
#include "tasks/Task.h"
|
||||
|
||||
namespace MMCZip {
|
||||
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
|
||||
*/
|
||||
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
|
||||
|
@ -3,6 +3,7 @@
|
||||
* Prism Launcher - Minecraft Launcher
|
||||
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
||||
* 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
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -41,6 +42,9 @@
|
||||
#include <QFileSystemModel>
|
||||
#include <QMessageBox>
|
||||
#include "FileIgnoreProxy.h"
|
||||
#include "QObjectPtr.h"
|
||||
#include "ui/dialogs/CustomMessageBox.h"
|
||||
#include "ui/dialogs/ProgressDialog.h"
|
||||
#include "ui_ExportInstanceDialog.h"
|
||||
|
||||
#include <FileSystem.h>
|
||||
@ -119,14 +123,15 @@ void SaveIcon(InstancePtr m_instance)
|
||||
pixmap.save(FS::PathCombine(m_instance->instanceRoot(), iconKey + ".png"));
|
||||
}
|
||||
|
||||
bool ExportInstanceDialog::doExport()
|
||||
void ExportInstanceDialog::doExport()
|
||||
{
|
||||
auto name = FS::RemoveInvalidFilenameChars(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);
|
||||
if (output.isEmpty()) {
|
||||
return false;
|
||||
QDialog::done(QDialog::Rejected);
|
||||
return;
|
||||
}
|
||||
|
||||
SaveIcon(m_instance);
|
||||
@ -135,26 +140,31 @@ bool ExportInstanceDialog::doExport()
|
||||
if (!MMCZip::collectFileListRecursively(m_instance->instanceRoot(), nullptr, &files,
|
||||
std::bind(&FileIgnoreProxy::filterFile, proxyModel, std::placeholders::_1))) {
|
||||
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)) {
|
||||
QMessageBox::warning(this, tr("Error"), tr("Unable to export instance"));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
auto task = makeShared<MMCZip::ExportToZipTask>(output, m_instance->instanceRoot(), files, "", true);
|
||||
|
||||
connect(task.get(), &Task::failed, this, [this, output](QString reason) {
|
||||
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
|
||||
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)
|
||||
{
|
||||
savePackIgnore();
|
||||
if (result == QDialog::Accepted) {
|
||||
if (doExport()) {
|
||||
QDialog::done(QDialog::Accepted);
|
||||
return;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
doExport();
|
||||
return;
|
||||
}
|
||||
QDialog::done(result);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
/*
|
||||
* Prism Launcher - Minecraft Launcher
|
||||
* 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
|
||||
* 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);
|
||||
|
||||
private:
|
||||
bool doExport();
|
||||
void doExport();
|
||||
void loadPackIgnore();
|
||||
void savePackIgnore();
|
||||
QString ignoreFileName();
|
||||
|
Loading…
Reference in New Issue
Block a user