More refactoring

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad 2023-03-04 19:55:38 +00:00
parent 87384182a1
commit 970ec8187c
9 changed files with 50 additions and 47 deletions

View File

@ -666,8 +666,8 @@ SET(LAUNCHER_SOURCES
# FIXME: maybe find a better home for this. # FIXME: maybe find a better home for this.
SkinUtils.cpp SkinUtils.cpp
SkinUtils.h SkinUtils.h
PackIgnoreProxy.cpp FileIgnoreProxy.cpp
PackIgnoreProxy.h FileIgnoreProxy.h
# GUI - setup wizard # GUI - setup wizard
ui/setupwizard/SetupWizard.h ui/setupwizard/SetupWizard.h

View File

@ -34,7 +34,7 @@
* limitations under the License. * limitations under the License.
*/ */
#include "PackIgnoreProxy.h" #include "FileIgnoreProxy.h"
#include <QDebug> #include <QDebug>
#include <QFileSystemModel> #include <QFileSystemModel>
@ -44,9 +44,9 @@
#include "SeparatorPrefixTree.h" #include "SeparatorPrefixTree.h"
#include "StringUtils.h" #include "StringUtils.h"
PackIgnoreProxy::PackIgnoreProxy(QString root, QObject* parent) : QSortFilterProxyModel(parent), root(root) {} FileIgnoreProxy::FileIgnoreProxy(QString root, QObject* parent) : QSortFilterProxyModel(parent), root(root) {}
// NOTE: Sadly, we have to do sorting ourselves. // NOTE: Sadly, we have to do sorting ourselves.
bool PackIgnoreProxy::lessThan(const QModelIndex& left, const QModelIndex& right) const bool FileIgnoreProxy::lessThan(const QModelIndex& left, const QModelIndex& right) const
{ {
QFileSystemModel* fsm = qobject_cast<QFileSystemModel*>(sourceModel()); QFileSystemModel* fsm = qobject_cast<QFileSystemModel*>(sourceModel());
if (!fsm) { if (!fsm) {
@ -79,7 +79,7 @@ bool PackIgnoreProxy::lessThan(const QModelIndex& left, const QModelIndex& right
return QSortFilterProxyModel::lessThan(left, right); return QSortFilterProxyModel::lessThan(left, right);
} }
Qt::ItemFlags PackIgnoreProxy::flags(const QModelIndex& index) const Qt::ItemFlags FileIgnoreProxy::flags(const QModelIndex& index) const
{ {
if (!index.isValid()) if (!index.isValid())
return Qt::NoItemFlags; return Qt::NoItemFlags;
@ -96,7 +96,7 @@ Qt::ItemFlags PackIgnoreProxy::flags(const QModelIndex& index) const
return flags; return flags;
} }
QVariant PackIgnoreProxy::data(const QModelIndex& index, int role) const QVariant FileIgnoreProxy::data(const QModelIndex& index, int role) const
{ {
QModelIndex sourceIndex = mapToSource(index); QModelIndex sourceIndex = mapToSource(index);
@ -116,7 +116,7 @@ QVariant PackIgnoreProxy::data(const QModelIndex& index, int role) const
return sourceIndex.data(role); return sourceIndex.data(role);
} }
bool PackIgnoreProxy::setData(const QModelIndex& index, const QVariant& value, int role) bool FileIgnoreProxy::setData(const QModelIndex& index, const QVariant& value, int role)
{ {
if (index.column() == 0 && role == Qt::CheckStateRole) { if (index.column() == 0 && role == Qt::CheckStateRole) {
Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt()); Qt::CheckState state = static_cast<Qt::CheckState>(value.toInt());
@ -127,7 +127,7 @@ bool PackIgnoreProxy::setData(const QModelIndex& index, const QVariant& value, i
return QSortFilterProxyModel::sourceModel()->setData(sourceIndex, value, role); return QSortFilterProxyModel::sourceModel()->setData(sourceIndex, value, role);
} }
QString PackIgnoreProxy::relPath(const QString& path) const QString FileIgnoreProxy::relPath(const QString& path) const
{ {
QString prefix = QDir().absoluteFilePath(root); QString prefix = QDir().absoluteFilePath(root);
prefix += '/'; prefix += '/';
@ -137,7 +137,7 @@ QString PackIgnoreProxy::relPath(const QString& path) const
return path.mid(prefix.size()); return path.mid(prefix.size());
} }
bool PackIgnoreProxy::setFilterState(QModelIndex index, Qt::CheckState state) bool FileIgnoreProxy::setFilterState(QModelIndex index, Qt::CheckState state)
{ {
QFileSystemModel* fsm = qobject_cast<QFileSystemModel*>(sourceModel()); QFileSystemModel* fsm = qobject_cast<QFileSystemModel*>(sourceModel());
@ -225,7 +225,7 @@ bool PackIgnoreProxy::setFilterState(QModelIndex index, Qt::CheckState state)
return true; return true;
} }
bool PackIgnoreProxy::shouldExpand(QModelIndex index) bool FileIgnoreProxy::shouldExpand(QModelIndex index)
{ {
QModelIndex sourceIndex = mapToSource(index); QModelIndex sourceIndex = mapToSource(index);
QFileSystemModel* fsm = qobject_cast<QFileSystemModel*>(sourceModel()); QFileSystemModel* fsm = qobject_cast<QFileSystemModel*>(sourceModel());
@ -240,7 +240,7 @@ bool PackIgnoreProxy::shouldExpand(QModelIndex index)
return false; return false;
} }
void PackIgnoreProxy::setBlockedPaths(QStringList paths) void FileIgnoreProxy::setBlockedPaths(QStringList paths)
{ {
beginResetModel(); beginResetModel();
blocked.clear(); blocked.clear();
@ -248,12 +248,12 @@ void PackIgnoreProxy::setBlockedPaths(QStringList paths)
endResetModel(); endResetModel();
} }
const SeparatorPrefixTree<'/'>& PackIgnoreProxy::blockedPaths() const const SeparatorPrefixTree<'/'>& FileIgnoreProxy::blockedPaths() const
{ {
return blocked; return blocked;
} }
bool PackIgnoreProxy::filterAcceptsColumn(int source_column, const QModelIndex& source_parent) const bool FileIgnoreProxy::filterAcceptsColumn(int source_column, const QModelIndex& source_parent) const
{ {
Q_UNUSED(source_parent) Q_UNUSED(source_parent)

View File

@ -39,11 +39,11 @@
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
#include "SeparatorPrefixTree.h" #include "SeparatorPrefixTree.h"
class PackIgnoreProxy : public QSortFilterProxyModel { class FileIgnoreProxy : public QSortFilterProxyModel {
Q_OBJECT Q_OBJECT
public: public:
PackIgnoreProxy(QString root, QObject* parent); FileIgnoreProxy(QString root, QObject* parent);
// NOTE: Sadly, we have to do sorting ourselves. // NOTE: Sadly, we have to do sorting ourselves.
bool lessThan(const QModelIndex& left, const QModelIndex& right) const; bool lessThan(const QModelIndex& left, const QModelIndex& right) const;

View File

@ -27,6 +27,7 @@
#include "MMCZip.h" #include "MMCZip.h"
#include "minecraft/MinecraftInstance.h" #include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h" #include "minecraft/PackProfile.h"
#include "minecraft/mod/Mod.h"
#include "modplatform/modrinth/ModrinthAPI.h" #include "modplatform/modrinth/ModrinthAPI.h"
const QStringList ModrinthPackExportTask::PREFIXES = QStringList({ "mods", "coremods", "resourcepacks", "texturepacks", "shaderpacks" }); const QStringList ModrinthPackExportTask::PREFIXES = QStringList({ "mods", "coremods", "resourcepacks", "texturepacks", "shaderpacks" });
@ -47,13 +48,14 @@ void ModrinthPackExportTask::executeTask()
collectFiles(); collectFiles();
QByteArray* response = new QByteArray; QByteArray* response = new QByteArray;
task = api.currentVersions(fileHashes.values(), "sha512", response); task = api.currentVersions(pendingHashes.values(), "sha512", response);
connect(task.get(), &NetJob::succeeded, [this, response]() { parseApiResponse(response); }); connect(task.get(), &NetJob::succeeded, [this, response]() { parseApiResponse(response); });
connect(task.get(), &NetJob::failed, this, &ModrinthPackExportTask::emitFailed); connect(task.get(), &NetJob::failed, this, &ModrinthPackExportTask::emitFailed);
task->start(); task->start();
} }
bool ModrinthPackExportTask::abort() { bool ModrinthPackExportTask::abort()
{
if (!task.isNull() && task->abort()) { if (!task.isNull() && task->abort()) {
task = nullptr; task = nullptr;
emitFailed(tr("Aborted")); emitFailed(tr("Aborted"));
@ -71,7 +73,8 @@ void ModrinthPackExportTask::collectFiles()
return; return;
} }
fileHashes.clear(); pendingHashes.clear();
resolvedFiles.clear();
QDir mc(instance->gameRoot()); QDir mc(instance->gameRoot());
for (QFileInfo file : files) { for (QFileInfo file : files) {
@ -97,18 +100,16 @@ void ModrinthPackExportTask::collectFiles()
continue; continue;
} }
fileHashes[relative] = hash.result().toHex(); pendingHashes[relative] = hash.result().toHex();
} }
} }
void ModrinthPackExportTask::parseApiResponse(QByteArray* response) void ModrinthPackExportTask::parseApiResponse(QByteArray* response)
{ {
QMap<QString, ResolvedFile> resolved;
try { try {
QJsonDocument doc = Json::requireDocument(*response); QJsonDocument doc = Json::requireDocument(*response);
QMapIterator<QString, QString> iterator(fileHashes); QMapIterator<QString, QString> iterator(pendingHashes);
while (iterator.hasNext()) { while (iterator.hasNext()) {
iterator.next(); iterator.next();
@ -121,18 +122,20 @@ void ModrinthPackExportTask::parseApiResponse(QByteArray* response)
[&iterator](const QJsonValue& file) { return file["hashes"]["sha512"] == iterator.value(); }); [&iterator](const QJsonValue& file) { return file["hashes"]["sha512"] == iterator.value(); });
fileIter != files.end()) { fileIter != files.end()) {
// map the file to the url // map the file to the url
resolved[iterator.key()] = ResolvedFile{ fileIter->toObject()["hashes"].toObject()["sha1"].toString(), iterator.value(), resolvedFiles[iterator.key()] =
fileIter->toObject()["url"].toString(), fileIter->toObject()["size"].toInt() }; ResolvedFile{ fileIter->toObject()["hashes"].toObject()["sha1"].toString(), iterator.value(),
fileIter->toObject()["url"].toString(), fileIter->toObject()["size"].toInt() };
} }
} }
} catch (Json::JsonException& e) { } catch (Json::JsonException& e) {
qWarning() << "Failed to parse versions response" << e.what(); qWarning() << "Failed to parse versions response" << e.what();
} }
pendingHashes.clear();
buildZip(resolved); buildZip();
} }
void ModrinthPackExportTask::buildZip(const QMap<QString, ResolvedFile>& resolvedFiles) void ModrinthPackExportTask::buildZip()
{ {
setStatus("Adding files..."); setStatus("Adding files...");
QuaZip zip(output); QuaZip zip(output);
@ -148,7 +151,7 @@ void ModrinthPackExportTask::buildZip(const QMap<QString, ResolvedFile>& resolve
emitFailed(tr("Could not create index")); emitFailed(tr("Could not create index"));
return; return;
} }
indexFile.write(generateIndex(resolvedFiles)); indexFile.write(generateIndex());
QDir mc(instance->gameRoot()); QDir mc(instance->gameRoot());
size_t i = 0; size_t i = 0;
@ -171,7 +174,7 @@ void ModrinthPackExportTask::buildZip(const QMap<QString, ResolvedFile>& resolve
emitSucceeded(); emitSucceeded();
} }
QByteArray ModrinthPackExportTask::generateIndex(const QMap<QString, ResolvedFile>& resolvedFiles) QByteArray ModrinthPackExportTask::generateIndex()
{ {
QJsonObject obj; QJsonObject obj;
obj["formatVersion"] = 1; obj["formatVersion"] = 1;

View File

@ -37,6 +37,11 @@ class ModrinthPackExportTask : public Task {
bool abort() override; bool abort() override;
private: private:
struct ResolvedFile {
QString sha1, sha512, url;
int size;
};
static const QStringList PREFIXES; static const QStringList PREFIXES;
// inputs // inputs
@ -47,17 +52,13 @@ class ModrinthPackExportTask : public Task {
ModrinthAPI api; ModrinthAPI api;
QFileInfoList files; QFileInfoList files;
QMap<QString, QString> fileHashes; QMap<QString, QString> pendingHashes;
QMap<QString, ResolvedFile> resolvedFiles;
Task::Ptr task; Task::Ptr task;
struct ResolvedFile {
QString sha1, sha512, url;
int size;
};
void collectFiles(); void collectFiles();
void parseApiResponse(QByteArray* response); void parseApiResponse(QByteArray* response);
void buildZip(const QMap<QString, ResolvedFile>& resolvedFiles); void buildZip();
QByteArray generateIndex(const QMap<QString, ResolvedFile>& resolvedFiles); QByteArray generateIndex();
}; };

View File

@ -57,7 +57,7 @@ ExportInstanceDialog::ExportInstanceDialog(InstancePtr instance, QWidget *parent
ui->setupUi(this); ui->setupUi(this);
auto model = new QFileSystemModel(this); auto model = new QFileSystemModel(this);
auto root = instance->instanceRoot(); auto root = instance->instanceRoot();
proxyModel = new PackIgnoreProxy(root, this); proxyModel = new FileIgnoreProxy(root, this);
loadPackIgnore(); loadPackIgnore();
proxyModel->setSourceModel(model); proxyModel->setSourceModel(model);
ui->treeView->setModel(proxyModel); ui->treeView->setModel(proxyModel);

View File

@ -18,7 +18,7 @@
#include <QDialog> #include <QDialog>
#include <QModelIndex> #include <QModelIndex>
#include <memory> #include <memory>
#include "PackIgnoreProxy.h" #include "FileIgnoreProxy.h"
class BaseInstance; class BaseInstance;
typedef std::shared_ptr<BaseInstance> InstancePtr; typedef std::shared_ptr<BaseInstance> InstancePtr;
@ -47,7 +47,7 @@ private:
private: private:
Ui::ExportInstanceDialog *ui; Ui::ExportInstanceDialog *ui;
InstancePtr m_instance; InstancePtr m_instance;
PackIgnoreProxy * proxyModel; FileIgnoreProxy * proxyModel;
private slots: private slots:
void rowsInserted(QModelIndex parent, int top, int bottom); void rowsInserted(QModelIndex parent, int top, int bottom);

View File

@ -37,7 +37,7 @@ ExportMrPackDialog::ExportMrPackDialog(InstancePtr instance, QWidget* parent)
auto model = new QFileSystemModel(this); auto model = new QFileSystemModel(this);
// use the game root - everything outside cannot be exported // use the game root - everything outside cannot be exported
QString root = instance->gameRoot(); QString root = instance->gameRoot();
proxy = new PackIgnoreProxy(root, this); proxy = new FileIgnoreProxy(root, this);
proxy->setSourceModel(model); proxy->setSourceModel(model);
ui->treeView->setModel(proxy); ui->treeView->setModel(proxy);
ui->treeView->setRootIndex(proxy->mapFromSource(model->index(root))); ui->treeView->setRootIndex(proxy->mapFromSource(model->index(root)));
@ -58,16 +58,15 @@ void ExportMrPackDialog::done(int result)
{ {
if (result == Accepted) { if (result == Accepted) {
const QString filename = FS::RemoveInvalidFilenameChars(ui->name->text()); const QString filename = FS::RemoveInvalidFilenameChars(ui->name->text());
const QString output = const QString output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(ui->name->text()),
QFileDialog::getSaveFileName(this, tr("Export %1").arg(ui->name->text()), FS::PathCombine(QDir::homePath(), filename + ".mrpack"), FS::PathCombine(QDir::homePath(), filename + ".mrpack"),
"Modrinth pack (*.mrpack *.zip)", nullptr); "Modrinth pack (*.mrpack *.zip)", nullptr);
if (output.isEmpty()) if (output.isEmpty())
return; return;
ModrinthPackExportTask task(ui->name->text(), ui->version->text(), ui->summary->text(), instance, output, ModrinthPackExportTask task(ui->name->text(), ui->version->text(), ui->summary->text(), instance, output,
[this](const QString& path) { return proxy->blockedPaths().covers(path); }); [this](const QString& path) { return proxy->blockedPaths().covers(path); });
ProgressDialog progress(this); ProgressDialog progress(this);
progress.setSkipButton(true, tr("Abort")); progress.setSkipButton(true, tr("Abort"));
if (progress.execWithTask(&task) != QDialog::Accepted) if (progress.execWithTask(&task) != QDialog::Accepted)

View File

@ -20,7 +20,7 @@
#include <QDialog> #include <QDialog>
#include "BaseInstance.h" #include "BaseInstance.h"
#include "PackIgnoreProxy.h" #include "FileIgnoreProxy.h"
namespace Ui { namespace Ui {
class ExportMrPackDialog; class ExportMrPackDialog;
@ -38,5 +38,5 @@ class ExportMrPackDialog : public QDialog {
private: private:
const InstancePtr instance; const InstancePtr instance;
Ui::ExportMrPackDialog* ui; Ui::ExportMrPackDialog* ui;
PackIgnoreProxy* proxy; FileIgnoreProxy* proxy;
}; };