Fixed a couple of custom collection issues when changing the game counter metadata entry.

Also made some general code cleanup.
This commit is contained in:
Leon Styhre 2022-09-25 13:03:39 +02:00
parent fcbe612a0b
commit ca52a1d7c5

View file

@ -386,7 +386,7 @@ void CollectionSystemsManager::updateCollectionSystem(FileData* file, Collection
} }
} }
else { else {
bool addGame = false; bool addGame {false};
// We didn't find the entry in the collection, so we need to check if we should add it. // We didn't find the entry in the collection, so we need to check if we should add it.
if ((name == "recent" && file->metadata.get("playcount") > "0" && if ((name == "recent" && file->metadata.get("playcount") > "0" &&
file->getCountAsGame() && includeFileInAutoCollections(file)) || file->getCountAsGame() && includeFileInAutoCollections(file)) ||
@ -503,7 +503,7 @@ void CollectionSystemsManager::deleteCollectionFiles(FileData* file)
const bool CollectionSystemsManager::isThemeGenericCollectionCompatible( const bool CollectionSystemsManager::isThemeGenericCollectionCompatible(
bool genericCustomCollections) bool genericCustomCollections)
{ {
std::vector<std::string> cfgSys = getCollectionThemeFolders(genericCustomCollections); std::vector<std::string> cfgSys {getCollectionThemeFolders(genericCustomCollections)};
for (auto sysIt = cfgSys.cbegin(); sysIt != cfgSys.cend(); ++sysIt) { for (auto sysIt = cfgSys.cbegin(); sysIt != cfgSys.cend(); ++sysIt) {
if (!themeFolderExists(*sysIt)) if (!themeFolderExists(*sysIt))
return false; return false;
@ -542,7 +542,7 @@ std::string CollectionSystemsManager::getValidNewCollectionName(const std::strin
name = Utils::String::trim(name); name = Utils::String::trim(name);
if (index == 0) { if (index == 0) {
size_t remove = std::string::npos; size_t remove {std::string::npos};
// Get valid name. // Get valid name.
while ((remove = name.find_first_not_of( while ((remove = name.find_first_not_of(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-[]()' ")) != "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-[]()' ")) !=
@ -637,8 +637,8 @@ const bool CollectionSystemsManager::inCustomCollection(const std::string& colle
auto collectionEntry = mCustomCollectionSystemsData.find(collectionName); auto collectionEntry = mCustomCollectionSystemsData.find(collectionName);
if (collectionEntry != mCustomCollectionSystemsData.end()) { if (collectionEntry != mCustomCollectionSystemsData.end()) {
const std::unordered_map<std::string, FileData*>& children = const std::unordered_map<std::string, FileData*>& children {
collectionEntry->second.system->getRootFolder()->getChildrenByFilename(); collectionEntry->second.system->getRootFolder()->getChildrenByFilename()};
return children.find(gameFile->getFullPath()) != children.cend(); return children.find(gameFile->getFullPath()) != children.cend();
} }
return false; return false;
@ -647,7 +647,7 @@ const bool CollectionSystemsManager::inCustomCollection(const std::string& colle
const bool CollectionSystemsManager::toggleGameInCollection(FileData* file) const bool CollectionSystemsManager::toggleGameInCollection(FileData* file)
{ {
if (file->getType() == GAME) { if (file->getType() == GAME) {
bool adding = true; bool adding {true};
std::string name {file->getName()}; std::string name {file->getName()};
std::string sysName {mEditingCollection}; std::string sysName {mEditingCollection};
if (mIsEditingCustom) { if (mIsEditingCustom) {
@ -660,7 +660,7 @@ const bool CollectionSystemsManager::toggleGameInCollection(FileData* file)
FileData* rootFolder {sysData->getRootFolder()}; FileData* rootFolder {sysData->getRootFolder()};
const std::unordered_map<std::string, FileData*>& children { const std::unordered_map<std::string, FileData*>& children {
rootFolder->getChildrenByFilename()}; rootFolder->getChildrenByFilename()};
bool found = children.find(key) != children.cend(); bool found {children.find(key) != children.cend()};
FileFilterIndex* fileIndex {sysData->getIndex()}; FileFilterIndex* fileIndex {sysData->getIndex()};
SystemData* systemViewToUpdate {getSystemToView(sysData)}; SystemData* systemViewToUpdate {getSystemToView(sysData)};
@ -772,17 +772,17 @@ FileData* CollectionSystemsManager::updateCollectionFolderMetadata(SystemData* s
gamesList = rootFolder->getChildrenListToDisplay(); gamesList = rootFolder->getChildrenListToDisplay();
} }
unsigned int gameCount {static_cast<unsigned int>(gamesList.size())}; int gameCount {static_cast<int>(gamesList.size())};
// If there is more than 1 game in the collection, then randomize the example game names. // If there is more than 1 game in the collection, then randomize the example game names.
if (gameCount > 1) { if (gameCount > 1) {
std::random_device randDev; std::random_device randDev;
// Mersenne Twister pseudorandom number generator. // Mersenne Twister pseudorandom number generator.
std::mt19937 engine {randDev()}; std::mt19937 engine {randDev()};
unsigned int target; int target;
for (unsigned int i = 0; i < 3; ++i) { for (int i = 0; i < 3; ++i) {
std::uniform_int_distribution<int> uniform_dist(0, gameCount - 1 - i); std::uniform_int_distribution<int> uniform_dist {0, gameCount - 1 - i};
target = uniform_dist(engine); target = uniform_dist(engine);
gamesListRandom.push_back(gamesList[target]); gamesListRandom.push_back(gamesList[target]);
std::vector<FileData*>::iterator it {(gamesList.begin() + target)}; std::vector<FileData*>::iterator it {(gamesList.begin() + target)};
@ -939,8 +939,8 @@ void CollectionSystemsManager::deleteCustomCollection(const std::string& collect
void CollectionSystemsManager::reactivateCustomCollectionEntry(FileData* game) void CollectionSystemsManager::reactivateCustomCollectionEntry(FileData* game)
{ {
std::string gamePath = Utils::FileSystem::getFileName(game->getFullPath()); std::string gamePath {
gamePath = "%ROMPATH%/" + game->getSystemName() + "/" + gamePath; Utils::String::replace(game->getFullPath(), FileData::getROMDirectory(), "%ROMPATH%/")};
// Try to read from all custom collection configuration files to see if there are any // Try to read from all custom collection configuration files to see if there are any
// matching entries for the game passed as the parameter. If so, then enable it in each // matching entries for the game passed as the parameter. If so, then enable it in each
@ -949,6 +949,8 @@ void CollectionSystemsManager::reactivateCustomCollectionEntry(FileData* game)
for (std::map<std::string, CollectionSystemData, StringComparator>::const_iterator it = for (std::map<std::string, CollectionSystemData, StringComparator>::const_iterator it =
mCustomCollectionSystemsData.cbegin(); mCustomCollectionSystemsData.cbegin();
it != mCustomCollectionSystemsData.cend(); ++it) { it != mCustomCollectionSystemsData.cend(); ++it) {
if (!it->second.isEnabled)
continue;
std::string path {getCustomCollectionConfigPath(it->first)}; std::string path {getCustomCollectionConfigPath(it->first)};
if (Utils::FileSystem::exists(path)) { if (Utils::FileSystem::exists(path)) {
#if defined(_WIN64) #if defined(_WIN64)
@ -1220,8 +1222,16 @@ void CollectionSystemsManager::populateCustomCollection(CollectionSystemData* sy
std::unordered_map<std::string, FileData*>::const_iterator it = allFilesMap.find(gameKey); std::unordered_map<std::string, FileData*>::const_iterator it = allFilesMap.find(gameKey);
if (it != allFilesMap.cend()) { if (it != allFilesMap.cend()) {
CollectionFileData* newGame = new CollectionFileData(it->second, newSys); CollectionFileData* newGame = new CollectionFileData(it->second, newSys);
rootFolder->addChild(newGame); if (!newGame->getCountAsGame()) {
index->addToIndex(newGame); LOG(LogWarning)
<< "File \"" << gameKey
<< "\" does not exist, is hidden, or is not counted as a game, ignoring entry";
delete newGame;
}
else {
rootFolder->addChild(newGame);
index->addToIndex(newGame);
}
} }
else { else {
LOG(LogWarning) LOG(LogWarning)
@ -1286,8 +1296,8 @@ void CollectionSystemsManager::addEnabledCollectionsToDisplayedSystems(
rootFolder->getSortTypeFromString(rootFolder->getSortTypeString()), rootFolder->getSortTypeFromString(rootFolder->getSortTypeString()),
Settings::getInstance()->getBool("FavFirstCustom")); Settings::getInstance()->getBool("FavFirstCustom"));
// Jump to the first row of the game list, assuming it's not empty. // Jump to the first row of the game list, assuming it's not empty.
GamelistView* gameList = GamelistView* gameList {
ViewController::getInstance()->getGamelistView((it->second.system)).get(); ViewController::getInstance()->getGamelistView((it->second.system)).get()};
if (!gameList->getCursor()->isPlaceHolder()) { if (!gameList->getCursor()->isPlaceHolder()) {
gameList->setCursor(gameList->getFirstEntry()); gameList->setCursor(gameList->getFirstEntry());
} }
@ -1319,9 +1329,9 @@ std::vector<std::string> CollectionSystemsManager::getSystemsFromConfig()
pugi::xml_document doc; pugi::xml_document doc;
#if defined(_WIN64) #if defined(_WIN64)
pugi::xml_parse_result res = doc.load_file(Utils::String::stringToWideString(path).c_str()); pugi::xml_parse_result res {doc.load_file(Utils::String::stringToWideString(path).c_str())};
#else #else
pugi::xml_parse_result res = doc.load_file(path.c_str()); pugi::xml_parse_result res {doc.load_file(path.c_str())};
#endif #endif
if (!res) if (!res)
@ -1333,7 +1343,7 @@ std::vector<std::string> CollectionSystemsManager::getSystemsFromConfig()
if (!systemList) if (!systemList)
return systems; return systems;
for (pugi::xml_node system = systemList.child("system"); system; for (pugi::xml_node system {systemList.child("system")}; system;
system = system.next_sibling("system")) { system = system.next_sibling("system")) {
// Theme folder. // Theme folder.
std::string themeFolder {system.child("theme").text().get()}; std::string themeFolder {system.child("theme").text().get()};
@ -1437,8 +1447,8 @@ void CollectionSystemsManager::trimCollectionCount(FileData* rootFolder, int lim
{ {
SystemData* curSys {rootFolder->getSystem()}; SystemData* curSys {rootFolder->getSystem()};
while (static_cast<int>(rootFolder->getChildrenListToDisplay().size()) > limit) { while (static_cast<int>(rootFolder->getChildrenListToDisplay().size()) > limit) {
CollectionFileData* gameToRemove = CollectionFileData* gameToRemove {
(CollectionFileData*)rootFolder->getChildrenListToDisplay().back(); (CollectionFileData*)rootFolder->getChildrenListToDisplay().back()};
ViewController::getInstance()->getGamelistView(curSys).get()->remove(gameToRemove, false); ViewController::getInstance()->getGamelistView(curSys).get()->remove(gameToRemove, false);
} }
// Also update the lists of last played and most played games as these could otherwise // Also update the lists of last played and most played games as these could otherwise