|
3 | 3 | #include <functional> |
4 | 4 | #include <vector> |
5 | 5 |
|
6 | | -#include <QDir> |
| 6 | +#include <QFileDialog> |
| 7 | +#include <QLabel> |
7 | 8 | #include <QMainWindow> |
| 9 | +#include <QProgressBar> |
| 10 | +#include <QStatusBar> |
| 11 | +#include <QThread> |
8 | 12 | #include <vpkpp/vpkpp.h> |
9 | 13 |
|
10 | 14 | #include "dialogs/PackFileOptionsDialog.h" |
11 | 15 | #include "plugins/previews/IVPKEditPreviewPlugin.h" |
12 | 16 |
|
13 | 17 | class QAction; |
14 | 18 | class QJsonObject; |
15 | | -class QLabel; |
16 | 19 | class QLineEdit; |
17 | 20 | class QMenu; |
18 | 21 | class QNetworkAccessManager; |
19 | 22 | class QNetworkReply; |
20 | | -class QProgressBar; |
21 | 23 | class QSettings; |
22 | | -class QThread; |
23 | 24 |
|
24 | 25 | struct EntryContextMenuData; |
25 | 26 | class EntryTree; |
26 | 27 | class FileViewer; |
27 | 28 |
|
28 | | -class Window : public QMainWindow { |
| 29 | +class IndeterminateProgressWorker : public QObject { |
29 | 30 | Q_OBJECT; |
30 | 31 |
|
31 | | - friend class SavePackFileWorker; |
32 | | - friend class ExtractPackFileWorker; |
33 | | - |
34 | 32 | public: |
35 | | - explicit Window(QWidget* parent = nullptr); |
36 | | - |
37 | | - void newPackFile(std::string_view typeGUID, bool fromDirectory, const QString& startPath, const QString& name, const QString& extension); |
38 | | - |
39 | | - void newBMZ(bool fromDirectory, const QString& startPath = QString()); |
40 | | - |
41 | | - void newFGP(bool fromDirectory, const QString& startPath = QString()); |
42 | | - |
43 | | - void newFPX(bool fromDirectory, const QString& startPath = QString()); |
| 33 | + IndeterminateProgressWorker() = default; |
44 | 34 |
|
45 | | - void newPAK(bool fromDirectory, const QString& startPath = QString()); |
| 35 | + void run(const std::function<void()>& fn); |
46 | 36 |
|
47 | | - void newPCK(bool fromDirectory, const QString& startPath = QString()); |
| 37 | + signals: |
| 38 | + void taskFinished(); |
| 39 | +}; |
48 | 40 |
|
49 | | - void newVPK(bool fromDirectory, const QString& startPath = QString()); |
| 41 | +class Window : public QMainWindow { |
| 42 | + Q_OBJECT; |
50 | 43 |
|
51 | | - void newVPK_VTMB(bool fromDirectory, const QString& startPath = QString()); |
| 44 | + friend class SavePackFileWorker; |
| 45 | + friend class ExtractPackFileWorker; |
52 | 46 |
|
53 | | - void newWAD3(bool fromDirectory, const QString& startPath = QString()); |
| 47 | +public: |
| 48 | + explicit Window(QWidget* parent = nullptr); |
54 | 49 |
|
55 | | - void newZIP(bool fromDirectory, const QString& startPath = QString()); |
| 50 | + template<typename T> |
| 51 | + requires requires { {T::create("")} -> std::same_as<std::unique_ptr<vpkpp::PackFile>>; } |
| 52 | + void newPackFile(bool fromDirectory, const QString& startPath, const QString& name, std::string_view extension, PackFileOptionsShowForTypeFlags flags = PackFileOptionsShowForTypeFlags::ORDINARY) { |
| 53 | + if (this->isWindowModified() && this->promptUserToKeepModifications()) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + auto options = PackFileOptionsDialog::getForNew(flags, fromDirectory, this); |
| 58 | + if (!options) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + auto dirPath = fromDirectory ? QFileDialog::getExistingDirectory(this, QObject::tr("Use This Folder"), startPath) : ""; |
| 63 | + if (fromDirectory && dirPath.isEmpty()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + QString saveFilePath; |
| 68 | + if (fromDirectory) { |
| 69 | + saveFilePath = std::filesystem::path{dirPath.toLocal8Bit().constData()}.parent_path().string().c_str(); |
| 70 | + saveFilePath += QDir::separator(); |
| 71 | + if constexpr (std::same_as<T, vpkpp::FPX> || std::same_as<T, vpkpp::VPK>) { |
| 72 | + saveFilePath += std::filesystem::path{dirPath.toLocal8Bit().constData()}.stem().string().c_str() + ((options->vpk_saveSingleFile || dirPath.endsWith("_dir") ? "" : "_dir") + extension); |
| 73 | + } else { |
| 74 | + saveFilePath += std::filesystem::path{dirPath.toLocal8Bit().constData()}.stem().string().c_str() + extension; |
| 75 | + } |
| 76 | + } |
| 77 | + auto packFilePath = QFileDialog::getSaveFileName(this, QObject::tr("Save New Pack File"), fromDirectory ? saveFilePath : startPath, name + " (*" + extension.data() + ")"); |
| 78 | + if (packFilePath.isEmpty()) { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + std::unique_ptr<vpkpp::PackFile> out; |
| 83 | + if constexpr (std::same_as<T, vpkpp::VPK_VTMB>) { |
| 84 | + const auto basePath = std::filesystem::path{packFilePath.toLocal8Bit().constData()}; |
| 85 | + const auto packFilePathStr = basePath.parent_path().string() + "/pack000" + std::string{vpkpp::VPK_VTMB_EXTENSION}; |
| 86 | + packFilePath = packFilePathStr.c_str(); |
| 87 | + } |
| 88 | + out = T::create(packFilePath.toLocal8Bit().constData()); |
| 89 | + |
| 90 | + if constexpr (std::same_as<T, vpkpp::FPX> || std::same_as<T, vpkpp::VPK>) { |
| 91 | + if (auto* vpk = dynamic_cast<vpkpp::VPK*>(out.get())) { |
| 92 | + vpk->setChunkSize(options->vpk_chunkSize); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + if (!fromDirectory) { |
| 97 | + this->loadPackFile(packFilePath, std::move(out)); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + // Set up progress bar |
| 102 | + this->statusText->hide(); |
| 103 | + this->statusProgressBar->show(); |
| 104 | + this->statusBar()->show(); |
| 105 | + |
| 106 | + // Show progress bar is busy |
| 107 | + this->statusProgressBar->setValue(0); |
| 108 | + this->statusProgressBar->setRange(0, 0); |
| 109 | + |
| 110 | + this->freezeActions(true); |
| 111 | + |
| 112 | + // Set up thread |
| 113 | + this->createPackFileFromDirWorkerThread = new QThread(this); |
| 114 | + auto* worker = new IndeterminateProgressWorker(); |
| 115 | + worker->moveToThread(this->createPackFileFromDirWorkerThread); |
| 116 | + QObject::connect(this->createPackFileFromDirWorkerThread, &QThread::started, worker, [worker, packFilePath, dirPath, options_=*options] { |
| 117 | + worker->run([packFilePath, dirPath, options_] { |
| 118 | + if (const auto packFile = vpkpp::PackFile::open(packFilePath.toLocal8Bit().constData())) { |
| 119 | + if constexpr (std::same_as<T, vpkpp::FPX> || std::same_as<T, vpkpp::VPK>) { |
| 120 | + if (auto* vpk = dynamic_cast<vpkpp::VPK*>(packFile.get())) { |
| 121 | + vpk->setChunkSize(options_.vpk_chunkSize); |
| 122 | + } |
| 123 | + } |
| 124 | + packFile->addDirectory("", dirPath.toLocal8Bit().constData(), { |
| 125 | + .zip_compressionType = vpkpp::EntryCompressionType::NO_COMPRESS, |
| 126 | + .zip_compressionStrength = 0, |
| 127 | + .vpk_preloadBytes = 0, |
| 128 | + .vpk_saveToDirectory = options_.vpk_saveSingleFile, |
| 129 | + }); |
| 130 | + packFile->bake("", {}, nullptr); |
| 131 | + } |
| 132 | + }); |
| 133 | + }); |
| 134 | + QObject::connect(worker, &IndeterminateProgressWorker::taskFinished, this, [this, packFilePath, options_=*options] { |
| 135 | + // Kill thread |
| 136 | + this->createPackFileFromDirWorkerThread->quit(); |
| 137 | + this->createPackFileFromDirWorkerThread->wait(); |
| 138 | + delete this->createPackFileFromDirWorkerThread; |
| 139 | + this->createPackFileFromDirWorkerThread = nullptr; |
| 140 | + |
| 141 | + // loadPackFile freezes them right away again |
| 142 | + // this->freezeActions(false); |
| 143 | + this->loadPackFile(packFilePath); |
| 144 | + if constexpr (std::same_as<T, vpkpp::FPX> || std::same_as<T, vpkpp::VPK>) { |
| 145 | + if (auto* vpk = dynamic_cast<vpkpp::VPK*>(packFile.get())) { |
| 146 | + vpk->setChunkSize(options_.vpk_chunkSize); |
| 147 | + } |
| 148 | + } |
| 149 | + }); |
| 150 | + this->createPackFileFromDirWorkerThread->start(); |
| 151 | + } |
56 | 152 |
|
57 | 153 | void openDir(const QString& startPath = QString(), const QString& dirPath = QString()); |
58 | 154 |
|
@@ -206,18 +302,6 @@ class Window : public QMainWindow { |
206 | 302 | void resetStatusBar(); |
207 | 303 | }; |
208 | 304 |
|
209 | | -class IndeterminateProgressWorker : public QObject { |
210 | | - Q_OBJECT; |
211 | | - |
212 | | -public: |
213 | | - IndeterminateProgressWorker() = default; |
214 | | - |
215 | | - void run(const std::function<void()>& fn); |
216 | | - |
217 | | -signals: |
218 | | - void taskFinished(); |
219 | | -}; |
220 | | - |
221 | 305 | class SavePackFileWorker : public QObject { |
222 | 306 | Q_OBJECT; |
223 | 307 |
|
|
0 commit comments