diff --git a/es-app/src/FileData.cpp b/es-app/src/FileData.cpp index 412d496cf..4d1424a57 100644 --- a/es-app/src/FileData.cpp +++ b/es-app/src/FileData.cpp @@ -47,7 +47,7 @@ FileData::FileData(FileType type, system->hasPlatformId(PlatformIds::SNK_NEO_GEO)) && metadata.getType() != FOLDER_METADATA) { // If it's a MAME or Neo Geo game, expand the game name accordingly. - metadata.set("name", MameNames::getInstance()->getCleanName(getCleanName())); + metadata.set("name", MameNames::getInstance().getCleanName(getCleanName())); } else { if (metadata.getType() == FOLDER_METADATA && Utils::FileSystem::isHidden(mPath)) { @@ -414,7 +414,7 @@ const bool FileData::isArcadeAsset() const const std::string stem = Utils::FileSystem::getStem(mPath); return ((mSystem && (mSystem->hasPlatformId(PlatformIds::ARCADE) || mSystem->hasPlatformId(PlatformIds::SNK_NEO_GEO))) && - (MameNames::getInstance()->isBios(stem) || MameNames::getInstance()->isDevice(stem))); + (MameNames::getInstance().isBios(stem) || MameNames::getInstance().isDevice(stem))); } const bool FileData::isArcadeGame() const @@ -422,7 +422,7 @@ const bool FileData::isArcadeGame() const const std::string stem = Utils::FileSystem::getStem(mPath); return ((mSystem && (mSystem->hasPlatformId(PlatformIds::ARCADE) || mSystem->hasPlatformId(PlatformIds::SNK_NEO_GEO))) && - (!MameNames::getInstance()->isBios(stem) && !MameNames::getInstance()->isDevice(stem))); + (!MameNames::getInstance().isBios(stem) && !MameNames::getInstance().isDevice(stem))); } void FileData::addChild(FileData* file) diff --git a/es-app/src/guis/GuiGameScraper.cpp b/es-app/src/guis/GuiGameScraper.cpp index 4e4eebf57..5e9eab8c2 100644 --- a/es-app/src/guis/GuiGameScraper.cpp +++ b/es-app/src/guis/GuiGameScraper.cpp @@ -38,9 +38,9 @@ GuiGameScraper::GuiGameScraper(Window* window, else { if (params.game->isArcadeGame() && Settings::getInstance()->getString("Scraper") == "thegamesdb") - scrapeName = - Utils::FileSystem::getFileName(mSearchParams.game->getPath()) + " (" + - MameNames::getInstance()->getCleanName(mSearchParams.game->getCleanName()) + ")"; + scrapeName = Utils::FileSystem::getFileName(mSearchParams.game->getPath()) + " (" + + MameNames::getInstance().getCleanName(mSearchParams.game->getCleanName()) + + ")"; else scrapeName = Utils::FileSystem::getFileName(mSearchParams.game->getPath()); } diff --git a/es-app/src/guis/GuiGamelistOptions.cpp b/es-app/src/guis/GuiGamelistOptions.cpp index d73e41256..8026eaf8e 100644 --- a/es-app/src/guis/GuiGamelistOptions.cpp +++ b/es-app/src/guis/GuiGamelistOptions.cpp @@ -397,8 +397,8 @@ void GuiGamelistOptions::openMetaDataEd() if (it->key == "name") { if (file->isArcadeGame()) { // If it's a MAME or Neo Geo game, expand the game name accordingly. - file->metadata.set( - it->key, MameNames::getInstance()->getCleanName(file->getCleanName())); + file->metadata.set(it->key, + MameNames::getInstance().getCleanName(file->getCleanName())); } else { file->metadata.set(it->key, file->getDisplayName()); diff --git a/es-app/src/guis/GuiScraperMulti.cpp b/es-app/src/guis/GuiScraperMulti.cpp index fc2b5c828..09b35b520 100644 --- a/es-app/src/guis/GuiScraperMulti.cpp +++ b/es-app/src/guis/GuiScraperMulti.cpp @@ -230,7 +230,7 @@ void GuiScraperMulti::doNextSearch() Settings::getInstance()->getString("Scraper") == "thegamesdb") scrapeName = Utils::FileSystem::getFileName(mSearchQueue.front().game->getPath()) + " (" + - MameNames::getInstance()->getCleanName(mSearchQueue.front().game->getCleanName()) + + MameNames::getInstance().getCleanName(mSearchQueue.front().game->getCleanName()) + ")"; else scrapeName = Utils::FileSystem::getFileName(mSearchQueue.front().game->getPath()); diff --git a/es-app/src/guis/GuiScraperSearch.cpp b/es-app/src/guis/GuiScraperSearch.cpp index 5a6d78a3f..528fdb0f4 100644 --- a/es-app/src/guis/GuiScraperSearch.cpp +++ b/es-app/src/guis/GuiScraperSearch.cpp @@ -826,7 +826,7 @@ void GuiScraperSearch::openInputScreen(ScraperSearchParams& params) // required as TheGamesDB does not support searches using the short MAME names. if (params.game->isArcadeGame() && Settings::getInstance()->getString("Scraper") == "thegamesdb") - searchString = MameNames::getInstance()->getCleanName(params.game->getCleanName()); + searchString = MameNames::getInstance().getCleanName(params.game->getCleanName()); else searchString = params.game->getCleanName(); } @@ -859,7 +859,7 @@ bool GuiScraperSearch::saveMetadata(const ScraperSearchResult& result, // Get the default name, which is either the MAME name or the name of the physical file // or directory. if (scrapedGame->isArcadeGame()) - defaultName = MameNames::getInstance()->getCleanName(scrapedGame->getCleanName()); + defaultName = MameNames::getInstance().getCleanName(scrapedGame->getCleanName()); else defaultName = Utils::FileSystem::getStem(scrapedGame->getFileName()); diff --git a/es-app/src/main.cpp b/es-app/src/main.cpp index 248c68ea3..480f4652d 100644 --- a/es-app/src/main.cpp +++ b/es-app/src/main.cpp @@ -596,7 +596,7 @@ int main(int argc, char* argv[]) } AudioManager::getInstance(); - MameNames::init(); + MameNames::getInstance(); loadSystemsReturnCode loadSystemsStatus = loadSystemConfigFile(); if (loadSystemsStatus) { @@ -701,7 +701,6 @@ int main(int argc, char* argv[]) delete window.peekGui(); window.deinit(); - MameNames::deinit(); CollectionSystemsManager::deinit(); SystemData::deleteSystems(); NavigationSounds::getInstance().deinit(); diff --git a/es-app/src/scrapers/GamesDBJSONScraper.cpp b/es-app/src/scrapers/GamesDBJSONScraper.cpp index 592592fe0..1646464f3 100644 --- a/es-app/src/scrapers/GamesDBJSONScraper.cpp +++ b/es-app/src/scrapers/GamesDBJSONScraper.cpp @@ -157,7 +157,7 @@ void thegamesdb_generate_json_scraper_requests( // arcade game and if so expand to the full game name. This is required as // TheGamesDB has issues with searching using the short MAME names. if (params.game->isArcadeGame()) - cleanName = MameNames::getInstance()->getCleanName(params.game->getCleanName()); + cleanName = MameNames::getInstance().getCleanName(params.game->getCleanName()); else cleanName = params.game->getCleanName(); } diff --git a/es-core/src/MameNames.cpp b/es-core/src/MameNames.cpp index a3ff474e3..7672b30a0 100644 --- a/es-core/src/MameNames.cpp +++ b/es-core/src/MameNames.cpp @@ -14,33 +14,14 @@ #include "Log.h" #include "resources/ResourceManager.h" #include "utils/FileSystemUtil.h" -#include "utils/StringUtil.h" #include #include -MameNames* MameNames::sInstance = nullptr; - -void MameNames::init() +MameNames& MameNames::getInstance() { - if (!sInstance) - sInstance = new MameNames(); -} - -void MameNames::deinit() -{ - if (sInstance) { - delete sInstance; - sInstance = nullptr; - } -} - -MameNames* MameNames::getInstance() -{ - if (!sInstance) - sInstance = new MameNames(); - - return sInstance; + static MameNames instance; + return instance; } MameNames::MameNames() @@ -68,9 +49,8 @@ MameNames::MameNames() for (pugi::xml_node gameNode = doc.child("game"); gameNode; gameNode = gameNode.next_sibling("game")) { - NamePair namePair = {gameNode.child("mamename").text().get(), - gameNode.child("realname").text().get()}; - mNamePairs.push_back(namePair); + mNamePairs[gameNode.child("mamename").text().get()] = + gameNode.child("realname").text().get(); } // Read BIOS file. @@ -96,7 +76,7 @@ MameNames::MameNames() for (pugi::xml_node biosNode = doc.child("bios"); biosNode; biosNode = biosNode.next_sibling("bios")) { std::string bios = biosNode.text().get(); - mMameBioses.push_back(bios); + mMameBioses.emplace_back(bios); } // Read device file. @@ -122,53 +102,6 @@ MameNames::MameNames() for (pugi::xml_node deviceNode = doc.child("device"); deviceNode; deviceNode = deviceNode.next_sibling("device")) { std::string device = deviceNode.text().get(); - mMameDevices.push_back(device); + mMameDevices.emplace_back(device); } } - -std::string MameNames::getRealName(const std::string& _mameName) -{ - size_t start = 0; - size_t end = mNamePairs.size(); - - while (start < end) { - const size_t index = (start + end) / 2; - const int compare = strcmp(mNamePairs[index].mameName.c_str(), _mameName.c_str()); - - if (compare < 0) - start = index + 1; - else if (compare > 0) - end = index; - else - return mNamePairs[index].realName; - } - - return _mameName; -} - -std::string MameNames::getCleanName(const std::string& _mameName) -{ - std::string cleanName = Utils::String::removeParenthesis(getRealName(_mameName)); - return cleanName; -} - -const bool MameNames::find(std::vector devices, const std::string& name) -{ - size_t start = 0; - size_t end = devices.size(); - - while (start < end) { - if (!strcmp(devices[start].c_str(), name.c_str())) - return true; - const size_t index = (start + end) / 2; - const int compare = strcmp(devices[index].c_str(), name.c_str()); - - if (compare < 0) - start = index + 1; - else if (compare > 0) - end = index; - else - return true; - } - return false; -} diff --git a/es-core/src/MameNames.h b/es-core/src/MameNames.h index 6ef25cf09..84b28e059 100644 --- a/es-core/src/MameNames.h +++ b/es-core/src/MameNames.h @@ -12,45 +12,50 @@ #ifndef ES_CORE_MAMENAMES_H #define ES_CORE_MAMENAMES_H +#include "utils/StringUtil.h" + +#include +#include #include #include -// Expand MAME names to full game names. +// Expand MAME names to full game names and lookup device and BIOS entries. class MameNames { public: - static void init(); - static void deinit(); - static MameNames* getInstance(); - std::string getRealName(const std::string& _mameName); - std::string getCleanName(const std::string& _mameName); - const bool isBios(const std::string& _biosName) + static MameNames& getInstance(); + + std::string getRealName(const std::string& mameName) { - return MameNames::find(mMameBioses, _biosName); + std::string name = mNamePairs[mameName]; + if (name == "") + return mameName; + else + return name; } - const bool isDevice(const std::string& _deviceName) + + std::string getCleanName(const std::string& mameName) { - return MameNames::find(mMameDevices, _deviceName); + return Utils::String::removeParenthesis(getRealName(mameName)); + } + + const bool isBios(const std::string& biosName) + { + return std::find(mMameBioses.cbegin(), mMameBioses.cend(), biosName) != mMameBioses.cend(); + } + + const bool isDevice(const std::string& deviceName) + { + return std::find(mMameDevices.cbegin(), mMameDevices.cend(), deviceName) != + mMameDevices.cend(); } private: - struct NamePair { - std::string mameName; - std::string realName; - }; - - typedef std::vector namePairVector; - MameNames(); - ~MameNames() {} - static MameNames* sInstance; - - namePairVector mNamePairs; + std::unordered_map mNamePairs; std::vector mMameBioses; std::vector mMameDevices; - - const bool find(const std::vector devices, const std::string& name); }; #endif // ES_CORE_MAMENAMES_H diff --git a/es-core/src/Platform.cpp b/es-core/src/Platform.cpp index 4484c0570..8b54b08a7 100644 --- a/es-core/src/Platform.cpp +++ b/es-core/src/Platform.cpp @@ -262,7 +262,6 @@ void emergencyShutdown() { LOG(LogError) << "Critical - Performing emergency shutdown..."; - MameNames::deinit(); // Most of the SDL deinitialization is done in Renderer. Renderer::deinit(); Log::flush();