From 53069205faf4063e838bab4beea5089d6ec2a0af Mon Sep 17 00:00:00 2001 From: Jan Dalheimer Date: Sat, 8 Feb 2014 17:22:26 +0100 Subject: [PATCH] Allow overriding the order in which patches are applied --- gui/dialogs/OneSixModEditDialog.cpp | 122 ++++++++++++++++++++++++++++ gui/dialogs/OneSixModEditDialog.h | 6 ++ gui/dialogs/OneSixModEditDialog.ui | 28 +++++++ logic/OneSixVersion.cpp | 9 ++ logic/OneSixVersion.h | 3 + logic/OneSixVersionBuilder.cpp | 68 ++++++++++++++++ logic/OneSixVersionBuilder.h | 4 + 7 files changed, 240 insertions(+) diff --git a/gui/dialogs/OneSixModEditDialog.cpp b/gui/dialogs/OneSixModEditDialog.cpp index 1b61771d..9e585de5 100644 --- a/gui/dialogs/OneSixModEditDialog.cpp +++ b/gui/dialogs/OneSixModEditDialog.cpp @@ -39,6 +39,18 @@ #include "logic/lists/ForgeVersionList.h" #include "logic/ForgeInstaller.h" #include "logic/LiteLoaderInstaller.h" +#include "logic/OneSixVersionBuilder.h" + +template +QMap invert(const QMap &in) +{ + QMap out; + for (auto it = in.begin(); it != in.end(); ++it) + { + out.insert(it.value(), it.key()); + } + return out; +} OneSixModEditDialog::OneSixModEditDialog(OneSixInstance *inst, QWidget *parent) : QDialog(parent), ui(new Ui::OneSixModEditDialog), m_inst(inst) @@ -129,6 +141,87 @@ void OneSixModEditDialog::on_removeLibraryBtn_clicked() } } +void OneSixModEditDialog::on_resetLibraryOrderBtn_clicked() +{ + QDir(m_inst->instanceRoot()).remove("order.json"); + m_inst->reloadVersion(this); +} +void OneSixModEditDialog::on_moveLibraryUpBtn_clicked() +{ + + QMap order = getExistingOrder(); + if (order.size() < 2 || ui->libraryTreeView->selectionModel()->selectedIndexes().isEmpty()) + { + return; + } + const int ourRow = ui->libraryTreeView->selectionModel()->selectedIndexes().first().row(); + const QString ourId = m_version->versionFileId(ourRow); + const int ourOrder = order[ourId]; + if (ourId.isNull() || ourId.startsWith("org.multimc.")) + { + return; + } + + QMap sortedOrder = invert(order); + + QList sortedOrders = sortedOrder.keys(); + const int ourIndex = sortedOrders.indexOf(ourOrder); + if (ourIndex <= 0) + { + return; + } + const int ourNewOrder = sortedOrders.at(ourIndex - 1); + order[ourId] = ourNewOrder; + order[sortedOrder[sortedOrders[ourIndex - 1]]] = ourOrder; + + if (!OneSixVersionBuilder::writeOverrideOrders(order, m_inst)) + { + QMessageBox::critical(this, tr("Error"), tr("Couldn't save the new order")); + } + else + { + m_inst->reloadVersion(this); + ui->libraryTreeView->selectionModel()->select(m_version->index(ourRow - 1), QItemSelectionModel::SelectCurrent); + } +} +void OneSixModEditDialog::on_moveLibraryDownBtn_clicked() +{ + QMap order = getExistingOrder(); + if (order.size() < 2 || ui->libraryTreeView->selectionModel()->selectedIndexes().isEmpty()) + { + return; + } + const int ourRow = ui->libraryTreeView->selectionModel()->selectedIndexes().first().row(); + const QString ourId = m_version->versionFileId(ourRow); + const int ourOrder = order[ourId]; + if (ourId.isNull() || ourId.startsWith("org.multimc.")) + { + return; + } + + QMap sortedOrder = invert(order); + + QList sortedOrders = sortedOrder.keys(); + const int ourIndex = sortedOrders.indexOf(ourOrder); + if ((ourIndex + 1) >= sortedOrders.size()) + { + return; + } + const int ourNewOrder = sortedOrders.at(ourIndex + 1); + order[ourId] = ourNewOrder; + order[sortedOrder[sortedOrders[ourIndex + 1]]] = ourOrder; + + if (!OneSixVersionBuilder::writeOverrideOrders(order, m_inst)) + { + QMessageBox::critical(this, tr("Error"), tr("Couldn't save the new order")); + } + else + { + m_inst->reloadVersion(this); + ui->libraryTreeView->selectionModel()->select(m_version->index(ourRow + 1), QItemSelectionModel::SelectCurrent); + } +} + void OneSixModEditDialog::on_forgeBtn_clicked() { if (QDir(m_inst->instanceRoot()).exists("custom.json")) @@ -250,6 +343,35 @@ bool OneSixModEditDialog::resourcePackListFilter(QKeyEvent *keyEvent) return QDialog::eventFilter(ui->resPackTreeView, keyEvent); } +QMap OneSixModEditDialog::getExistingOrder() const +{ + + QMap order; + // default + { + for (OneSixVersion::VersionFile file : m_version->versionFiles) + { + if (file.id.startsWith("org.multimc.")) + { + continue; + } + order.insert(file.id, file.order); + } + } + // overriden + { + QMap overridenOrder = OneSixVersionBuilder::readOverrideOrders(m_inst); + for (auto id : order.keys()) + { + if (overridenOrder.contains(id)) + { + order[id] = overridenOrder[id]; + } + } + } + return order; +} + bool OneSixModEditDialog::eventFilter(QObject *obj, QEvent *ev) { if (ev->type() != QEvent::KeyPress) diff --git a/gui/dialogs/OneSixModEditDialog.h b/gui/dialogs/OneSixModEditDialog.h index a7def967..f44b336b 100644 --- a/gui/dialogs/OneSixModEditDialog.h +++ b/gui/dialogs/OneSixModEditDialog.h @@ -47,6 +47,9 @@ slots: void on_liteloaderBtn_clicked(); void on_reloadLibrariesBtn_clicked(); void on_removeLibraryBtn_clicked(); + void on_resetLibraryOrderBtn_clicked(); + void on_moveLibraryUpBtn_clicked(); + void on_moveLibraryDownBtn_clicked(); void updateVersionControls(); void disableVersionControls(); @@ -62,6 +65,9 @@ private: std::shared_ptr m_resourcepacks; EnabledItemFilter *main_model; OneSixInstance *m_inst; + + QMap getExistingOrder() const; + public slots: void loaderCurrent(QModelIndex current, QModelIndex previous); diff --git a/gui/dialogs/OneSixModEditDialog.ui b/gui/dialogs/OneSixModEditDialog.ui index b97fa323..eaf8f7fd 100644 --- a/gui/dialogs/OneSixModEditDialog.ui +++ b/gui/dialogs/OneSixModEditDialog.ui @@ -108,6 +108,34 @@ + + + + Reset order + + + + + + + Qt::Horizontal + + + + + + + Move up + + + + + + + Move down + + + diff --git a/logic/OneSixVersion.cpp b/logic/OneSixVersion.cpp index e19683d8..fb32f3a8 100644 --- a/logic/OneSixVersion.cpp +++ b/logic/OneSixVersion.cpp @@ -81,6 +81,15 @@ bool OneSixVersion::canRemove(const int index) const return false; } +QString OneSixVersion::versionFileId(const int index) const +{ + if (index < 0 || index >= versionFiles.size()) + { + return QString(); + } + return versionFiles.at(index).id; +} + bool OneSixVersion::remove(const int index) { if (canRemove(index)) diff --git a/logic/OneSixVersion.h b/logic/OneSixVersion.h index 516f153c..ba7695d5 100644 --- a/logic/OneSixVersion.h +++ b/logic/OneSixVersion.h @@ -44,6 +44,8 @@ public: bool canRemove(const int index) const; + QString versionFileId(const int index) const; + public slots: bool remove(const int index); @@ -123,6 +125,7 @@ public: QString version; QString mcVersion; QString filename; + int order; }; QList versionFiles; diff --git a/logic/OneSixVersionBuilder.cpp b/logic/OneSixVersionBuilder.cpp index 62a5ca43..bbd33ddc 100644 --- a/logic/OneSixVersionBuilder.cpp +++ b/logic/OneSixVersionBuilder.cpp @@ -763,6 +763,7 @@ struct VersionFile versionFile.version = this->version; versionFile.mcVersion = mcVersion; versionFile.filename = filename; + versionFile.order = order; version->versionFiles.append(versionFile); isError = false; @@ -858,6 +859,7 @@ bool OneSixVersionBuilder::build(const bool onlyVanilla) // patches/ { // load all, put into map for ordering, apply in the right order + QMap overrideOrder = readOverrideOrders(m_instance); QMap> files; for (auto info : patches.entryInfoList(QStringList() << "*.json", QDir::Files)) @@ -868,6 +870,15 @@ bool OneSixVersionBuilder::build(const bool onlyVanilla) { return false; } + if (overrideOrder.contains(file.fileId)) + { + file.order = overrideOrder.value(file.fileId); + } + if (files.contains(file.order)) + { + QLOG_ERROR() << file.fileId << "has the same order as" << files[file.order].second.fileId; + return false; + } files.insert(file.order, qMakePair(info.fileName(), file)); } for (auto order : files.keys()) @@ -1007,3 +1018,60 @@ bool OneSixVersionBuilder::read(const QFileInfo &fileInfo, const bool requireOrd } return true; } + +QMap OneSixVersionBuilder::readOverrideOrders(OneSixInstance *instance) +{ + QMap out; + if (QDir(instance->instanceRoot()).exists("order.json")) + { + QFile orderFile(instance->instanceRoot() + "/order.json"); + if (!orderFile.open(QFile::ReadOnly)) + { + QLOG_ERROR() << "Couldn't open" << orderFile.fileName() << " for reading:" << orderFile.errorString(); + QLOG_WARN() << "Ignoring overriden order"; + } + else + { + QJsonParseError error; + QJsonDocument doc = QJsonDocument::fromJson(orderFile.readAll(), &error); + if (error.error != QJsonParseError::NoError || !doc.isObject()) + { + QLOG_ERROR() << "Couldn't parse" << orderFile.fileName() << ":" << error.errorString(); + QLOG_WARN() << "Ignoring overriden order"; + } + else + { + QJsonObject obj = doc.object(); + for (auto it = obj.begin(); it != obj.end(); ++it) + { + if (it.key().startsWith("org.multimc.")) + { + continue; + } + out.insert(it.key(), it.value().toDouble()); + } + } + } + } + return out; +} +bool OneSixVersionBuilder::writeOverrideOrders(const QMap &order, OneSixInstance *instance) +{ + QJsonObject obj; + for (auto it = order.cbegin(); it != order.cend(); ++it) + { + if (it.key().startsWith("org.multimc.")) + { + continue; + } + obj.insert(it.key(), it.value()); + } + QFile orderFile(instance->instanceRoot() + "/order.json"); + if (!orderFile.open(QFile::WriteOnly)) + { + QLOG_ERROR() << "Couldn't open" << orderFile.fileName() << "for writing:" << orderFile.errorString(); + return false; + } + orderFile.write(QJsonDocument(obj).toJson(QJsonDocument::Indented)); + return true; +} diff --git a/logic/OneSixVersionBuilder.h b/logic/OneSixVersionBuilder.h index f0d69bf6..ab0966df 100644 --- a/logic/OneSixVersionBuilder.h +++ b/logic/OneSixVersionBuilder.h @@ -16,6 +16,7 @@ #pragma once #include +#include class OneSixVersion; class OneSixInstance; @@ -30,6 +31,8 @@ class OneSixVersionBuilder public: static bool build(OneSixVersion *version, OneSixInstance *instance, QWidget *widgetParent, const bool onlyVanilla); static bool read(OneSixVersion *version, const QJsonObject &obj); + static QMap readOverrideOrders(OneSixInstance *instance); + static bool writeOverrideOrders(const QMap &order, OneSixInstance *instance); private: OneSixVersion *m_version; @@ -40,4 +43,5 @@ private: bool read(const QJsonObject &obj); bool read(const QFileInfo &fileInfo, const bool requireOrder, VersionFile *out); + };