Abstract away InstanceCopyPrefs' internals through new getSelectedFiltersAsRegex() function
+ fix typo in comment + remove unused import + add [[nodiscard]] to methods Signed-off-by: Marcelo Hernandez <marcelohdez.inq@gmail.com>
This commit is contained in:
		| @@ -14,3 +14,36 @@ bool InstanceCopyPrefs::allTrue() const | ||||
|         copyServers && | ||||
|         copyMods; | ||||
| } | ||||
|  | ||||
| // Returns a single RegEx string of the selected folders/files to filter out (ex: ".minecraft/saves|.minecraft/server.dat") | ||||
| QString InstanceCopyPrefs::getSelectedFiltersAsRegex() const | ||||
| { | ||||
|     QStringList filters; | ||||
|  | ||||
|     if(!copySaves) | ||||
|         filters << "saves"; | ||||
|  | ||||
|     if(!copyGameOptions) | ||||
|         filters << "options.txt"; | ||||
|  | ||||
|     if(!copyResourcePacks) | ||||
|         filters << "resourcepacks" << "texturepacks"; | ||||
|  | ||||
|     if(!copyShaderPacks) | ||||
|         filters << "shaderpacks"; | ||||
|  | ||||
|     if(!copyServers) | ||||
|         filters << "servers.dat" << "servers.dat_old" << "server-resource-packs"; | ||||
|  | ||||
|     if(!copyMods) | ||||
|         filters << "coremods" << "mods" << "config"; | ||||
|  | ||||
|     // If we have any filters to add, join them as a single regex string to return: | ||||
|     if (!filters.isEmpty()) { | ||||
|         const QString MC_ROOT = "[.]?minecraft/"; | ||||
|         // Ensure first filter starts with root, then join other filters with OR regex before root (ex: ".minecraft/saves|.minecraft/mods"): | ||||
|         return MC_ROOT + filters.join("|" + MC_ROOT); | ||||
|     } | ||||
|  | ||||
|     return {}; | ||||
| } | ||||
|   | ||||
| @@ -5,6 +5,8 @@ | ||||
| #ifndef LAUNCHER_INSTANCECOPYPREFS_H | ||||
| #define LAUNCHER_INSTANCECOPYPREFS_H | ||||
|  | ||||
| #include <QStringList> | ||||
|  | ||||
| struct InstanceCopyPrefs { | ||||
|     bool copySaves = true; | ||||
|     bool keepPlaytime = true; | ||||
| @@ -14,7 +16,8 @@ struct InstanceCopyPrefs { | ||||
|     bool copyServers = true; | ||||
|     bool copyMods = true; | ||||
|  | ||||
|     bool allTrue() const; | ||||
|     [[nodiscard]] bool allTrue() const; | ||||
|     [[nodiscard]] QString getSelectedFiltersAsRegex() const; | ||||
| }; | ||||
|  | ||||
| #endif  // LAUNCHER_INSTANCECOPYPREFS_H | ||||
|   | ||||
| @@ -9,62 +9,16 @@ InstanceCopyTask::InstanceCopyTask(InstancePtr origInstance, const InstanceCopyP | ||||
| { | ||||
|     m_origInstance = origInstance; | ||||
|     m_keepPlaytime = prefs.keepPlaytime; | ||||
|     QString filter; | ||||
|  | ||||
|     if(!prefs.copySaves) | ||||
|     QString filters = prefs.getSelectedFiltersAsRegex(); | ||||
|     if (!filters.isEmpty()) | ||||
|     { | ||||
|         appendToFilter(filter, "saves"); | ||||
|     } | ||||
|  | ||||
|     if(!prefs.copyGameOptions) { | ||||
|         appendToFilter(filter, "options.txt"); | ||||
|     } | ||||
|  | ||||
|     if(!prefs.copyResourcePacks) | ||||
|     { | ||||
|         appendToFilter(filter, "resourcepacks"); | ||||
|         appendToFilter(filter, "texturepacks"); | ||||
|     } | ||||
|  | ||||
|     if(!prefs.copyShaderPacks) | ||||
|     { | ||||
|         appendToFilter(filter, "shaderpacks"); | ||||
|     } | ||||
|  | ||||
|     if(!prefs.copyServers) | ||||
|     { | ||||
|         appendToFilter(filter, "servers.dat"); | ||||
|         appendToFilter(filter, "servers.dat_old"); | ||||
|         appendToFilter(filter, "server-resource-packs"); | ||||
|     } | ||||
|  | ||||
|     if(!prefs.copyMods) | ||||
|     { | ||||
|         appendToFilter(filter, "coremods"); | ||||
|         appendToFilter(filter, "mods"); | ||||
|         appendToFilter(filter, "config"); | ||||
|     } | ||||
|  | ||||
|     if (!filter.isEmpty()) | ||||
|     { | ||||
|         resetFromMatcher(filter); | ||||
|     } | ||||
| } | ||||
|  | ||||
| void InstanceCopyTask::appendToFilter(QString& filter, const QString& append) | ||||
| { | ||||
|     if (!filter.isEmpty()) | ||||
|         filter.append('|'); // OR regex | ||||
|  | ||||
|     filter.append("[.]?minecraft/" + append); | ||||
| } | ||||
|  | ||||
| void InstanceCopyTask::resetFromMatcher(const QString& regexp) | ||||
| { | ||||
|         // Set regex filter: | ||||
|         // FIXME: get this from the original instance type... | ||||
|     auto matcherReal = new RegexpMatcher(regexp); | ||||
|         auto matcherReal = new RegexpMatcher(filters); | ||||
|         matcherReal->caseSensitive(false); | ||||
|         m_matcher.reset(matcherReal); | ||||
|     } | ||||
| } | ||||
|  | ||||
| void InstanceCopyTask::executeTask() | ||||
|   | ||||
| @@ -24,10 +24,6 @@ protected: | ||||
|     void copyAborted(); | ||||
|  | ||||
| private: | ||||
|     // Helper functions to avoid repeating code | ||||
|     static void appendToFilter(QString &filter, const QString &append); | ||||
|     void resetFromMatcher(const QString ®exp); | ||||
|  | ||||
|     /* data */ | ||||
|     InstancePtr m_origInstance; | ||||
|     QFuture<bool> m_copyFuture; | ||||
|   | ||||
| @@ -44,7 +44,6 @@ | ||||
|  | ||||
| #include "BaseVersion.h" | ||||
| #include "icons/IconList.h" | ||||
| #include "tasks/Task.h" | ||||
| #include "BaseInstance.h" | ||||
| #include "InstanceList.h" | ||||
|  | ||||
| @@ -138,7 +137,7 @@ void CopyInstanceDialog::checkAllCheckboxes(const bool& b) | ||||
|     ui->copyModsCheckbox->setChecked(b); | ||||
| } | ||||
|  | ||||
| // Check the "Select all" checkbox checked if all options are already checked: | ||||
| // Check the "Select all" checkbox if all options are already selected: | ||||
| void CopyInstanceDialog::updateSelectAllCheckbox() | ||||
| { | ||||
|     ui->selectAllCheckbox->blockSignals(true); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Marcelo Hernandez
					Marcelo Hernandez