From 966d2616be5d9f757f4555101c9b873021fc888d Mon Sep 17 00:00:00 2001
From: Leon Styhre <leon@leonstyhre.com>
Date: Sat, 25 Sep 2021 11:02:27 +0200
Subject: [PATCH] Added support for defining custom system sorting using the
 <systemsortname> tag.

---
 es-app/src/CollectionSystemsManager.cpp       |  4 ++--
 es-app/src/CollectionSystemsManager.h         |  2 +-
 es-app/src/SystemData.cpp                     | 19 ++++++++++++++++---
 es-app/src/SystemData.h                       |  3 +++
 .../src/guis/GuiCollectionSystemsOptions.cpp  |  4 ++--
 5 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/es-app/src/CollectionSystemsManager.cpp b/es-app/src/CollectionSystemsManager.cpp
index 282b880a2..b804698a7 100644
--- a/es-app/src/CollectionSystemsManager.cpp
+++ b/es-app/src/CollectionSystemsManager.cpp
@@ -925,7 +925,7 @@ SystemData* CollectionSystemsManager::addNewCustomCollection(std::string name)
     CollectionSystemDecl decl = mCollectionSystemDeclsIndex[myCollectionsName];
     decl.themeFolder = name;
     decl.name = name;
-    decl.longName = name;
+    decl.fullName = name;
 
     return createNewCollectionEntry(name, decl, true, true);
 }
@@ -1113,7 +1113,7 @@ SystemData* CollectionSystemsManager::createNewCollectionEntry(std::string name,
                                                                bool index,
                                                                bool custom)
 {
-    SystemData* newSys = new SystemData(name, sysDecl.longName, mCollectionEnvData,
+    SystemData* newSys = new SystemData(name, sysDecl.fullName, "", mCollectionEnvData,
                                         sysDecl.themeFolder, true, custom);
 
     CollectionSystemData newCollectionData;
diff --git a/es-app/src/CollectionSystemsManager.h b/es-app/src/CollectionSystemsManager.h
index df976ef93..ff31f88d7 100644
--- a/es-app/src/CollectionSystemsManager.h
+++ b/es-app/src/CollectionSystemsManager.h
@@ -43,7 +43,7 @@ enum CollectionSystemType {
 struct CollectionSystemDecl {
     CollectionSystemType type;
     std::string name;
-    std::string longName;
+    std::string fullName;
     std::string themeFolder;
     bool isCustom;
 };
diff --git a/es-app/src/SystemData.cpp b/es-app/src/SystemData.cpp
index adaa2c9b6..1ca0cae81 100644
--- a/es-app/src/SystemData.cpp
+++ b/es-app/src/SystemData.cpp
@@ -178,12 +178,14 @@ void FindRules::loadFindRules()
 
 SystemData::SystemData(const std::string& name,
                        const std::string& fullName,
+                       const std::string& sortName,
                        SystemEnvironmentData* envData,
                        const std::string& themeFolder,
                        bool CollectionSystem,
                        bool CustomCollectionSystem)
     : mName(name)
     , mFullName(fullName)
+    , mSortName(sortName)
     , mEnvData(envData)
     , mThemeFolder(themeFolder)
     , mIsCollectionSystem(CollectionSystem)
@@ -438,11 +440,13 @@ bool SystemData::loadConfig()
              system = system.next_sibling("system")) {
             std::string name;
             std::string fullname;
+            std::string sortName;
             std::string path;
             std::string themeFolder;
 
             name = system.child("name").text().get();
             fullname = system.child("fullname").text().get();
+            sortName = system.child("systemsortname").text().get();
             path = system.child("path").text().get();
 
             auto nameFindFunc = [&] {
@@ -583,6 +587,15 @@ bool SystemData::loadConfig()
                 continue;
             }
 
+            if (sortName == "") {
+                sortName = fullname;
+            }
+            else {
+                LOG(LogDebug) << "SystemData::loadConfig(): System \"" << name
+                              << "\" has a <systemsortname> tag set, sorting as \"" << sortName
+                              << "\" instead of \"" << fullname << "\"";
+            }
+
             // Convert path to generic directory seperators.
             path = Utils::FileSystem::getGenericPath(path);
 
@@ -601,7 +614,7 @@ bool SystemData::loadConfig()
             envData->mLaunchCommands = commands;
             envData->mPlatformIds = platformIds;
 
-            SystemData* newSys = new SystemData(name, fullname, envData, themeFolder);
+            SystemData* newSys = new SystemData(name, fullname, sortName, envData, themeFolder);
             bool onlyHidden = false;
 
             // If the option to show hidden games has been disabled, then check whether all
@@ -630,9 +643,9 @@ bool SystemData::loadConfig()
         }
     }
 
-    // Sort systems by their full names.
+    // Sort systems by sortName, which will normally be the same as the full name.
     std::sort(std::begin(sSystemVector), std::end(sSystemVector),
-              [](SystemData* a, SystemData* b) { return a->getFullName() < b->getFullName(); });
+              [](SystemData* a, SystemData* b) { return a->getSortName() < b->getSortName(); });
 
     // Don't load any collections if there are no systems available.
     if (sSystemVector.size() > 0)
diff --git a/es-app/src/SystemData.h b/es-app/src/SystemData.h
index 8dd7bd4fd..a89eb4586 100644
--- a/es-app/src/SystemData.h
+++ b/es-app/src/SystemData.h
@@ -62,6 +62,7 @@ class SystemData
 public:
     SystemData(const std::string& name,
                const std::string& fullName,
+               const std::string& sortName,
                SystemEnvironmentData* envData,
                const std::string& themeFolder,
                bool CollectionSystem = false,
@@ -72,6 +73,7 @@ public:
     FileData* getRootFolder() const { return mRootFolder; }
     const std::string& getName() const { return mName; }
     const std::string& getFullName() const { return mFullName; }
+    const std::string& getSortName() const { return mSortName; }
     const std::string& getStartPath() const { return mEnvData->mStartPath; }
     const std::vector<std::string>& getExtensions() const { return mEnvData->mSearchExtensions; }
     const std::string& getThemeFolder() const { return mThemeFolder; }
@@ -152,6 +154,7 @@ public:
 private:
     std::string mName;
     std::string mFullName;
+    std::string mSortName;
     SystemEnvironmentData* mEnvData;
     std::string mAlternativeEmulator;
     std::string mThemeFolder;
diff --git a/es-app/src/guis/GuiCollectionSystemsOptions.cpp b/es-app/src/guis/GuiCollectionSystemsOptions.cpp
index 59cc0052e..2d1e31059 100644
--- a/es-app/src/guis/GuiCollectionSystemsOptions.cpp
+++ b/es-app/src/guis/GuiCollectionSystemsOptions.cpp
@@ -52,7 +52,7 @@ GuiCollectionSystemsOptions::GuiCollectionSystemsOptions(Window* window, std::st
     for (std::map<std::string, CollectionSystemData, stringComparator>::const_iterator it =
              autoSystems.cbegin();
          it != autoSystems.cend(); it++)
-        collection_systems_auto->add(it->second.decl.longName, it->second.decl.name,
+        collection_systems_auto->add(it->second.decl.fullName, it->second.decl.name,
                                      it->second.isEnabled);
     addWithLabel("AUTOMATIC GAME COLLECTIONS", collection_systems_auto);
     addSaveFunc([this, autoSystems] {
@@ -101,7 +101,7 @@ GuiCollectionSystemsOptions::GuiCollectionSystemsOptions(Window* window, std::st
     for (std::map<std::string, CollectionSystemData, stringComparator>::const_iterator it =
              customSystems.cbegin();
          it != customSystems.cend(); it++)
-        collection_systems_custom->add(it->second.decl.longName, it->second.decl.name,
+        collection_systems_custom->add(it->second.decl.fullName, it->second.decl.name,
                                        it->second.isEnabled);
 
     addWithLabel("CUSTOM GAME COLLECTIONS", collection_systems_custom);