From 58b6e0eb48b4d4b201613f1062796a9af68fe454 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Wed, 20 Jan 2021 18:16:20 +0100 Subject: Increase robustness of mods priority in profile. --- src/profile.h | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'src/profile.h') diff --git a/src/profile.h b/src/profile.h index 04452ff6..ebb69c0e 100644 --- a/src/profile.h +++ b/src/profile.h @@ -261,11 +261,6 @@ public: **/ size_t numMods() const { return m_ModStatus.size(); } - /** - * @return the number of mods that can be enabled and where the priority can be modified - */ - unsigned int numRegularMods() const { return m_NumRegularMods; } - /** * @brief retrieve the mod index based on the priority * @@ -293,17 +288,17 @@ public: **/ void setModsEnabled(const QList &modsToEnable, const QList &modsToDisable); - /** - * change the priority of a mod. Of course this also changes the priority of other mods. - * The priority of the mods in the range ]old, new priority] are shifted so that no gaps - * are possible. - * - * @param index index of the mod to change - * @param newPriority the new priority value - * - * @todo what happens if the new priority is outside the range? - **/ - void setModPriority(unsigned int index, int &newPriority); + // set the priority of a mod, and the priority of other mods in the range + // [old priority, new priority] such that no gaps are possible + // + // the priority is clamped in the range of valid priority (>= 0, and lower than + // the number of "regular" mods) + // + // the function returns true if the priority was changed, or false if the mod + // was already at the given priority (or if the priority of the mod cannot be + // set) + // + bool setModPriority(unsigned int index, int& newPriority); /** * @brief determine if a mod is enabled @@ -364,7 +359,7 @@ signals: **/ void modStatusChanged(QList index); -public slots: +protected slots: // should only be called by DelayedFileWriter, use writeModlist() and writeModlistNow() instead void doWriteModlist(); @@ -374,9 +369,8 @@ private: class ModStatus { friend class Profile; public: - ModStatus() : m_Overwrite(false), m_Enabled(false), m_Priority(-1) {} + ModStatus() : m_Enabled(false), m_Priority(-1) {} private: - bool m_Overwrite; bool m_Enabled; int m_Priority; }; -- cgit v1.3.1 From 946d7fc192d47b542e048d9ec72e65ad982eb09c Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Wed, 20 Jan 2021 18:33:54 +0100 Subject: Add comment for m_NumRegularMods and remove unused methods. --- src/profile.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/profile.h') diff --git a/src/profile.h b/src/profile.h index ebb69c0e..903a8553 100644 --- a/src/profile.h +++ b/src/profile.h @@ -343,6 +343,9 @@ public: void debugDump() const; + + Profile& operator=(const Profile& reference) = delete; + signals: /** @@ -376,9 +379,6 @@ private: }; private: - Profile& operator=(const Profile &reference); // not implemented - - void initTimer(); void updateIndices(); @@ -388,7 +388,6 @@ private: void mergeTweak(const QString &tweakName, const QString &tweakedIni) const; void mergeTweaks(ModInfo::Ptr modInfo, const QString &tweakedIni) const; void touchFile(QString fileName); - void finishChangeStatus() const; static void renameModInList(QFile &modList, const QString &oldName, const QString &newName); @@ -400,11 +399,14 @@ private: const MOBase::IPluginGame *m_GamePlugin; - mutable QByteArray m_LastModlistHash; std::vector m_ModStatus; std::map m_ModIndexByPriority; - unsigned int m_NumRegularMods; + // "regular" here means mods with modifiable priority (i.e. not backups + // or overwrite) + std::size_t m_NumRegularMods; + + mutable QByteArray m_LastModlistHash; MOBase::DelayedFileWriter m_ModListWriter; }; -- cgit v1.3.1 From 62a9d13948e6e299d41eb6876c0f1fbaf0160d79 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Thu, 21 Jan 2021 19:31:05 +0100 Subject: Remove unused priority-related functions from Profile. --- src/profile.cpp | 18 +----------------- src/profile.h | 11 ----------- 2 files changed, 1 insertion(+), 28 deletions(-) (limited to 'src/profile.h') diff --git a/src/profile.cpp b/src/profile.cpp index c45ecd30..caea776c 100644 --- a/src/profile.cpp +++ b/src/profile.cpp @@ -600,17 +600,6 @@ std::vector > Profile::getActiveMods() return result; } - -unsigned int Profile::modIndexByPriority(int priority) const -{ - try { - return m_ModIndexByPriority.at(priority); - } catch (std::out_of_range) { - throw MyException(tr("invalid priority %1").arg(priority)); - } -} - - void Profile::setModEnabled(unsigned int index, bool enabled) { if (index >= m_ModStatus.size()) { @@ -698,7 +687,7 @@ bool Profile::setModPriority(unsigned int index, int &newPriority) return false; } - for (auto& [priority, index] : m_ModIndexByPriority) { + for (const auto& [priority, index] : m_ModIndexByPriority) { if (newPriority < oldPriority && priority >= newPriority && priority < oldPriority) { m_ModStatus.at(index).m_Priority += 1; } @@ -1085,11 +1074,6 @@ void Profile::storeSettingsByArray(const QString &prefix, const QListendArray(); } -int Profile::getPriorityMinimum() const -{ - return m_ModIndexByPriority.begin()->first; -} - bool Profile::forcedLibrariesEnabled(const QString &executable) const { return setting("forced_libraries", executable + "/enabled", false).toBool(); diff --git a/src/profile.h b/src/profile.h index 903a8553..d354dd48 100644 --- a/src/profile.h +++ b/src/profile.h @@ -261,15 +261,6 @@ public: **/ size_t numMods() const { return m_ModStatus.size(); } - /** - * @brief retrieve the mod index based on the priority - * - * @param priority priority to look up - * @return the index of the mod - * @throw std::exception an exception is thrown if there is no mod with the specified priority - **/ - unsigned int modIndexByPriority(int priority) const; - /** * @brief enable or disable a mod * @@ -333,8 +324,6 @@ public: QList settingsByArray(const QString &prefix) const; void storeSettingsByArray(const QString &prefix, const QList &values); - int getPriorityMinimum() const; - bool forcedLibrariesEnabled(const QString &executable) const; void setForcedLibrariesEnabled(const QString &executable, bool enabled); QList determineForcedLibraries(const QString &executable) const; -- cgit v1.3.1 From 1747dcf5632a7887906b9b6d412e95e3af9c8249 Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Thu, 21 Jan 2021 19:42:41 +0100 Subject: INT_MAX -> std::numeric_limits, plus minor clean. --- src/modlistbypriorityproxy.cpp | 2 +- src/organizercore.cpp | 2 +- src/profile.cpp | 2 +- src/profile.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/profile.h') diff --git a/src/modlistbypriorityproxy.cpp b/src/modlistbypriorityproxy.cpp index 7dae7a54..d1d045fc 100644 --- a/src/modlistbypriorityproxy.cpp +++ b/src/modlistbypriorityproxy.cpp @@ -234,7 +234,7 @@ bool ModListByPriorityProxy::canDropMimeData(const QMimeData* data, Qt::DropActi bool hasSeparator = false; unsigned int firstRowIndex = -1; - int firstRowPriority = INT_MAX; + int firstRowPriority = std::numeric_limits::max(); for (auto sourceRow : dropInfo.rows()) { hasSeparator = hasSeparator || ModInfo::getByIndex(sourceRow)->isSeparator(); if (m_sortOrder == Qt::AscendingOrder && m_profile->getModPriority(sourceRow) < firstRowPriority diff --git a/src/organizercore.cpp b/src/organizercore.cpp index d6c22d3c..cf641886 100644 --- a/src/organizercore.cpp +++ b/src/organizercore.cpp @@ -1943,7 +1943,7 @@ std::vector OrganizerCore::fileMapping(const QString &profileName, bool overwriteActive = false; - for (auto mod : profile.getActiveMods()) { + for (const auto& mod : profile.getActiveMods()) { if (std::get<0>(mod).compare("overwrite", Qt::CaseInsensitive) == 0) { continue; } diff --git a/src/profile.cpp b/src/profile.cpp index caea776c..acda465b 100644 --- a/src/profile.cpp +++ b/src/profile.cpp @@ -591,7 +591,7 @@ std::vector > Profile::getActiveMods() if ((iter->second != UINT_MAX) && m_ModStatus[iter->second].m_Enabled) { ModInfo::Ptr modInfo = ModInfo::getByIndex(iter->second); if (modInfo->hasFlag(ModInfo::FLAG_OVERWRITE)) - result.push_back(std::make_tuple(modInfo->internalName(), modInfo->absolutePath(), INT_MAX)); + result.push_back(std::make_tuple(modInfo->internalName(), modInfo->absolutePath(), std::numeric_limits::max())); else result.push_back(std::make_tuple(modInfo->internalName(), modInfo->absolutePath(), m_ModStatus[iter->second].m_Priority)); } diff --git a/src/profile.h b/src/profile.h index d354dd48..d1e3f104 100644 --- a/src/profile.h +++ b/src/profile.h @@ -243,7 +243,7 @@ public: * * @return list of active mods sorted by priority (ascending). "first" is the mod name, "second" is its path **/ - std::vector > getActiveMods(); + std::vector> getActiveMods(); /** * @brief retrieve a mod of the indexes ordered by priority -- cgit v1.3.1 From 7864fc0950729621c0297389f5b47d27e4d41d8f Mon Sep 17 00:00:00 2001 From: Mikaël Capelle Date: Fri, 22 Jan 2021 21:18:54 +0100 Subject: Use constant for minimum/maximum priorities. --- src/modlist.cpp | 5 +---- src/modlistbypriorityproxy.cpp | 2 +- src/modlistviewactions.cpp | 6 +++--- src/profile.h | 12 ++++++++++-- 4 files changed, 15 insertions(+), 10 deletions(-) (limited to 'src/profile.h') diff --git a/src/modlist.cpp b/src/modlist.cpp index 13b2298f..81dc91bf 100644 --- a/src/modlist.cpp +++ b/src/modlist.cpp @@ -1023,14 +1023,11 @@ int ModList::dropPriority(int row, const QModelIndex& parent) const int newPriority = 0; { if (row < 0 || row >= rowCount()) { - newPriority = std::numeric_limits::max(); + newPriority = Profile::MaximumPriority; } else { newPriority = m_Profile->getModPriority(row); } - if (newPriority == -1) { - newPriority = std::numeric_limits::max(); - } } return newPriority; diff --git a/src/modlistbypriorityproxy.cpp b/src/modlistbypriorityproxy.cpp index d1d045fc..24e58ed0 100644 --- a/src/modlistbypriorityproxy.cpp +++ b/src/modlistbypriorityproxy.cpp @@ -234,7 +234,7 @@ bool ModListByPriorityProxy::canDropMimeData(const QMimeData* data, Qt::DropActi bool hasSeparator = false; unsigned int firstRowIndex = -1; - int firstRowPriority = std::numeric_limits::max(); + int firstRowPriority = Profile::MaximumPriority; for (auto sourceRow : dropInfo.rows()) { hasSeparator = hasSeparator || ModInfo::getByIndex(sourceRow)->isSeparator(); if (m_sortOrder == Qt::AscendingOrder && m_profile->getModPriority(sourceRow) < firstRowPriority diff --git a/src/modlistviewactions.cpp b/src/modlistviewactions.cpp index e7e74b16..647aac0b 100644 --- a/src/modlistviewactions.cpp +++ b/src/modlistviewactions.cpp @@ -539,12 +539,12 @@ void ModListViewActions::displayModInformation(ModInfo::Ptr modInfo, unsigned in void ModListViewActions::sendModsToTop(const QModelIndexList& indexes) const { - m_core.modList()->changeModsPriority(indexes, 0); + m_core.modList()->changeModsPriority(indexes, Profile::MinimumPriority); } void ModListViewActions::sendModsToBottom(const QModelIndexList& indexes) const { - m_core.modList()->changeModsPriority(indexes, std::numeric_limits::max()); + m_core.modList()->changeModsPriority(indexes, Profile::MaximumPriority); } void ModListViewActions::sendModsToPriority(const QModelIndexList& indexes) const @@ -580,7 +580,7 @@ void ModListViewActions::sendModsToSeparator(const QModelIndexList& indexes) con if (!result.isEmpty()) { result += "_separator"; - int newPriority = std::numeric_limits::max(); + int newPriority = Profile::MaximumPriority; bool foundSection = false; for (auto mod : m_core.modList()->allModsByProfilePriority()) { unsigned int modIndex = ModInfo::getIndex(mod); diff --git a/src/profile.h b/src/profile.h index d1e3f104..672a2c13 100644 --- a/src/profile.h +++ b/src/profile.h @@ -52,7 +52,14 @@ class Profile : public QObject, public MOBase::IProfile public: - typedef boost::shared_ptr Ptr; + using Ptr = boost::shared_ptr; + +public: + + // the minimum and maximum priority achievable by mods + // + static constexpr int MinimumPriority = 0; + static constexpr int MaximumPriority = std::numeric_limits::max(); public: @@ -283,7 +290,8 @@ public: // [old priority, new priority] such that no gaps are possible // // the priority is clamped in the range of valid priority (>= 0, and lower than - // the number of "regular" mods) + // the number of "regular" mods), you should use MinimumPriority or MaximumPriority + // to send a mod to the "top" or "bottom" of the priority list // // the function returns true if the priority was changed, or false if the mod // was already at the given priority (or if the priority of the mod cannot be -- cgit v1.3.1