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.
SkinUtils.cpp
SkinUtils.h
PackIgnoreProxy.cpp
PackIgnoreProxy.h
FileIgnoreProxy.cpp
FileIgnoreProxy.h
# GUI - setup wizard
ui/setupwizard/SetupWizard.h

View File

@ -34,7 +34,7 @@
* limitations under the License.
*/
#include "PackIgnoreProxy.h"
#include "FileIgnoreProxy.h"
#include <QDebug>
#include <QFileSystemModel>
@ -44,9 +44,9 @@
#include "SeparatorPrefixTree.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.
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());
if (!fsm) {
@ -79,7 +79,7 @@ bool PackIgnoreProxy::lessThan(const QModelIndex& left, const QModelIndex& 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())
return Qt::NoItemFlags;
@ -96,7 +96,7 @@ Qt::ItemFlags PackIgnoreProxy::flags(const QModelIndex& index) const
return flags;
}
QVariant PackIgnoreProxy::data(const QModelIndex& index, int role) const
QVariant FileIgnoreProxy::data(const QModelIndex& index, int role) const
{
QModelIndex sourceIndex = mapToSource(index);
@ -116,7 +116,7 @@ QVariant PackIgnoreProxy::data(const QModelIndex& index, int role) const
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) {
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);
}
QString PackIgnoreProxy::relPath(const QString& path) const
QString FileIgnoreProxy::relPath(const QString& path) const
{
QString prefix = QDir().absoluteFilePath(root);
prefix += '/';
@ -137,7 +137,7 @@ QString PackIgnoreProxy::relPath(const QString& path) const
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());
@ -225,7 +225,7 @@ bool PackIgnoreProxy::setFilterState(QModelIndex index, Qt::CheckState state)
return true;
}
bool PackIgnoreProxy::shouldExpand(QModelIndex index)
bool FileIgnoreProxy::shouldExpand(QModelIndex index)
{
QModelIndex sourceIndex = mapToSource(index);
QFileSystemModel* fsm = qobject_cast<QFileSystemModel*>(sourceModel());
@ -240,7 +240,7 @@ bool PackIgnoreProxy::shouldExpand(QModelIndex index)
return false;
}
void PackIgnoreProxy::setBlockedPaths(QStringList paths)
void FileIgnoreProxy::setBlockedPaths(QStringList paths)
{
beginResetModel();
blocked.clear();
@ -248,12 +248,12 @@ void PackIgnoreProxy::setBlockedPaths(QStringList paths)
endResetModel();
}
const SeparatorPrefixTree<'/'>& PackIgnoreProxy::blockedPaths() const
const SeparatorPrefixTree<'/'>& FileIgnoreProxy::blockedPaths() const
{
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)

View File

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

View File

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

View File

@ -37,6 +37,11 @@ class ModrinthPackExportTask : public Task {
bool abort() override;
private:
struct ResolvedFile {
QString sha1, sha512, url;
int size;
};
static const QStringList PREFIXES;
// inputs
@ -47,17 +52,13 @@ class ModrinthPackExportTask : public Task {
ModrinthAPI api;
QFileInfoList files;
QMap<QString, QString> fileHashes;
QMap<QString, QString> pendingHashes;
QMap<QString, ResolvedFile> resolvedFiles;
Task::Ptr task;
struct ResolvedFile {
QString sha1, sha512, url;
int size;
};
void collectFiles();
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);
auto model = new QFileSystemModel(this);
auto root = instance->instanceRoot();
proxyModel = new PackIgnoreProxy(root, this);
proxyModel = new FileIgnoreProxy(root, this);
loadPackIgnore();
proxyModel->setSourceModel(model);
ui->treeView->setModel(proxyModel);

View File

@ -18,7 +18,7 @@
#include <QDialog>
#include <QModelIndex>
#include <memory>
#include "PackIgnoreProxy.h"
#include "FileIgnoreProxy.h"
class BaseInstance;
typedef std::shared_ptr<BaseInstance> InstancePtr;
@ -47,7 +47,7 @@ private:
private:
Ui::ExportInstanceDialog *ui;
InstancePtr m_instance;
PackIgnoreProxy * proxyModel;
FileIgnoreProxy * proxyModel;
private slots:
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);
// use the game root - everything outside cannot be exported
QString root = instance->gameRoot();
proxy = new PackIgnoreProxy(root, this);
proxy = new FileIgnoreProxy(root, this);
proxy->setSourceModel(model);
ui->treeView->setModel(proxy);
ui->treeView->setRootIndex(proxy->mapFromSource(model->index(root)));
@ -58,16 +58,15 @@ void ExportMrPackDialog::done(int result)
{
if (result == Accepted) {
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 pack (*.mrpack *.zip)", nullptr);
const QString output = QFileDialog::getSaveFileName(this, tr("Export %1").arg(ui->name->text()),
FS::PathCombine(QDir::homePath(), filename + ".mrpack"),
"Modrinth pack (*.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"));
if (progress.execWithTask(&task) != QDialog::Accepted)

View File

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