From 073ac87be2a1a8721c3833c520091f8064813b64 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sat, 18 May 2024 13:26:15 +1000 Subject: [PATCH] GameList: Use string_view for GetEntryForPath() --- src/core/fullscreen_ui.cpp | 8 ++++---- src/core/game_list.cpp | 22 ++++++++++++---------- src/core/game_list.h | 2 +- src/duckstation-qt/gamesummarywidget.cpp | 2 +- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/core/fullscreen_ui.cpp b/src/core/fullscreen_ui.cpp index 8f055d01e..50d5b9fac 100644 --- a/src/core/fullscreen_ui.cpp +++ b/src/core/fullscreen_ui.cpp @@ -2754,7 +2754,7 @@ void FullscreenUI::SwitchToGameSettings() return; auto lock = GameList::GetLock(); - const GameList::Entry* entry = GameList::GetEntryForPath(System::GetDiscPath().c_str()); + const GameList::Entry* entry = GameList::GetEntryForPath(System::GetDiscPath()); if (!entry) { SwitchToGameSettingsForSerial(System::GetGameSerial()); @@ -2767,7 +2767,7 @@ void FullscreenUI::SwitchToGameSettings() void FullscreenUI::SwitchToGameSettingsForPath(const std::string& path) { auto lock = GameList::GetLock(); - const GameList::Entry* entry = GameList::GetEntryForPath(path.c_str()); + const GameList::Entry* entry = GameList::GetEntryForPath(path); if (entry) SwitchToGameSettings(entry); } @@ -5535,7 +5535,7 @@ u32 FullscreenUI::PopulateSaveStateListEntries(const std::string& title, const s bool FullscreenUI::OpenLoadStateSelectorForGame(const std::string& game_path) { auto lock = GameList::GetLock(); - const GameList::Entry* entry = GameList::GetEntryForPath(game_path.c_str()); + const GameList::Entry* entry = GameList::GetEntryForPath(game_path); if (entry) { s_save_state_selector_loading = true; @@ -6823,7 +6823,7 @@ GPUTexture* FullscreenUI::GetCoverForCurrentGame() { auto lock = GameList::GetLock(); - const GameList::Entry* entry = GameList::GetEntryForPath(System::GetDiscPath().c_str()); + const GameList::Entry* entry = GameList::GetEntryForPath(System::GetDiscPath()); if (!entry) return s_fallback_disc_texture.get(); diff --git a/src/core/game_list.cpp b/src/core/game_list.cpp index 60e9a4aba..f5df28d42 100644 --- a/src/core/game_list.cpp +++ b/src/core/game_list.cpp @@ -515,7 +515,7 @@ void GameList::ScanDirectory(const char* path, bool recursive, bool only_cache, } std::unique_lock lock(s_mutex); - if (GetEntryForPath(ffd.FileName.c_str()) || + if (GetEntryForPath(ffd.FileName) || AddFileFromCache(ffd.FileName, ffd.ModificationTime, played_time_map) || only_cache) { continue; @@ -590,13 +590,18 @@ const GameList::Entry* GameList::GetEntryByIndex(u32 index) return (index < s_entries.size()) ? &s_entries[index] : nullptr; } -const GameList::Entry* GameList::GetEntryForPath(const char* path) +const GameList::Entry* GameList::GetEntryForPath(std::string_view path) { - const size_t path_length = std::strlen(path); for (const Entry& entry : s_entries) { - if (entry.path.size() == path_length && StringUtil::Strcasecmp(entry.path.c_str(), path) == 0) + // Use case-insensitive compare on Windows, since it's the same file. +#ifdef _WIN32 + if (StringUtil::EqualNoCase(entry.path, path)) return &entry; +#else + if (entry.path == path) + return &entry; +#endif } return nullptr; @@ -606,11 +611,8 @@ const GameList::Entry* GameList::GetEntryBySerial(std::string_view serial) { for (const Entry& entry : s_entries) { - if (entry.serial.length() == serial.length() && - StringUtil::Strncasecmp(entry.serial.c_str(), serial.data(), serial.length()) == 0) - { + if (entry.serial == serial) return &entry; - } } return nullptr; @@ -1191,7 +1193,7 @@ bool GameList::DownloadCovers(const std::vector& url_templates, boo // make sure it didn't get done already { std::unique_lock lock(s_mutex); - const GameList::Entry* entry = GetEntryForPath(entry_path.c_str()); + const GameList::Entry* entry = GetEntryForPath(entry_path); if (!entry || !GetCoverImagePathForEntry(entry).empty()) { progress->IncrementProgressValue(); @@ -1210,7 +1212,7 @@ bool GameList::DownloadCovers(const std::vector& url_templates, boo return; std::unique_lock lock(s_mutex); - const GameList::Entry* entry = GetEntryForPath(entry_path.c_str()); + const GameList::Entry* entry = GetEntryForPath(entry_path); if (!entry || !GetCoverImagePathForEntry(entry).empty()) return; diff --git a/src/core/game_list.h b/src/core/game_list.h index ed651b0fc..a6b8fe45b 100644 --- a/src/core/game_list.h +++ b/src/core/game_list.h @@ -77,7 +77,7 @@ bool PopulateEntryFromPath(const std::string& path, Entry* entry); // Game list access. It's the caller's responsibility to hold the lock while manipulating the entry in any way. std::unique_lock GetLock(); const Entry* GetEntryByIndex(u32 index); -const Entry* GetEntryForPath(const char* path); +const Entry* GetEntryForPath(std::string_view path); const Entry* GetEntryBySerial(std::string_view serial); const Entry* GetEntryBySerialAndHash(std::string_view serial, u64 hash); std::vector GetDiscSetMembers(std::string_view disc_set_name); diff --git a/src/duckstation-qt/gamesummarywidget.cpp b/src/duckstation-qt/gamesummarywidget.cpp index c57426365..1b64ccf39 100644 --- a/src/duckstation-qt/gamesummarywidget.cpp +++ b/src/duckstation-qt/gamesummarywidget.cpp @@ -140,7 +140,7 @@ void GameSummaryWidget::populateUi(const std::string& path, const std::string& s { auto lock = GameList::GetLock(); - const GameList::Entry* gentry = GameList::GetEntryForPath(path.c_str()); + const GameList::Entry* gentry = GameList::GetEntryForPath(path); if (gentry) m_ui.entryType->setCurrentIndex(static_cast(gentry->type)); }