From 1a4c2fc4aebb0fb314a8d478caefbda65a5c7562 Mon Sep 17 00:00:00 2001
From: Leon Styhre <leon@leonstyhre.com>
Date: Thu, 31 Dec 2020 19:44:24 +0100
Subject: [PATCH] Fixed an issue where flagging a game not to be counted could
 lead to a crash.

---
 es-app/src/CollectionSystemsManager.cpp | 11 +++++++----
 es-app/src/guis/GuiMetaDataEd.cpp       |  5 +++--
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/es-app/src/CollectionSystemsManager.cpp b/es-app/src/CollectionSystemsManager.cpp
index 9e9db245e..8245b5f75 100644
--- a/es-app/src/CollectionSystemsManager.cpp
+++ b/es-app/src/CollectionSystemsManager.cpp
@@ -304,7 +304,6 @@ void CollectionSystemsManager::refreshCollectionSystems(FileData* file)
         realSys.isEnabled = true;
         realSys.isPopulated = true;
         realSys.decl.isCustom = false;
-
         updateCollectionSystem(file, realSys);
     }
 
@@ -343,7 +342,7 @@ void CollectionSystemsManager::updateCollectionSystem(FileData* file, Collection
         else
             mFavoritesSorting = Settings::getInstance()->getBool("FavoritesFirst");
 
-        const std::unordered_map<std::string, FileData*>&children =
+        const std::unordered_map<std::string, FileData*>& children =
                 curSys->getRootFolder()->getChildrenByFilename();
 
         bool found = children.find(key) != children.cend();
@@ -365,8 +364,12 @@ void CollectionSystemsManager::updateCollectionSystem(FileData* file, Collection
             }
             else if (curSys->isCollection() && !file->getCountAsGame()) {
                 // If the countasgame flag has been set to false, then remove the game.
-                ViewController::get()->
-                        getGameListView(curSys).get()->remove(collectionEntry, false);
+                if (curSys->isGroupedCustomCollection())
+                    ViewController::get()->getGameListView(curSys->getRootFolder()->getParent()->
+                            getSystem()).get()->remove(collectionEntry, false);
+                else
+                    ViewController::get()->
+                            getGameListView(curSys).get()->remove(collectionEntry, false);
             }
 
             else {
diff --git a/es-app/src/guis/GuiMetaDataEd.cpp b/es-app/src/guis/GuiMetaDataEd.cpp
index cdb7a6855..2fce9966b 100644
--- a/es-app/src/guis/GuiMetaDataEd.cpp
+++ b/es-app/src/guis/GuiMetaDataEd.cpp
@@ -385,8 +385,9 @@ void GuiMetaDataEd::save()
     if (mSavedCallback)
         mSavedCallback();
 
-    // Update respective Collection Entries.
-    CollectionSystemsManager::get()->refreshCollectionSystems(mScraperParams.game);
+    // Update all collections where the game is present.
+    if (mScraperParams.game->getType() == GAME)
+        CollectionSystemsManager::get()->refreshCollectionSystems(mScraperParams.game);
 
     mScraperParams.system->onMetaDataSavePoint();