diff --git a/Core/Util/GameDB.cpp b/Core/Util/GameDB.cpp index c29066b95e..804aa055a0 100644 --- a/Core/Util/GameDB.cpp +++ b/Core/Util/GameDB.cpp @@ -7,6 +7,8 @@ GameDB g_gameDB; +static const char *const g_gameDBFilename = "redump.csv"; + static void SplitCSVLine(const std::string_view str, std::vector &result) { result.clear(); @@ -57,11 +59,19 @@ static void SplitSV(std::string_view strv, char delim, bool removeWhiteSpace, st } } -bool GameDB::LoadFromVFS(VFSInterface &vfs, const char *filename) { +void GameDB::LoadIfNeeded() { + if (loaded_) { + // Already loaded + return; + } + + loaded_ = true; + size_t size; - uint8_t *data = vfs.ReadFile(filename, &size); + uint8_t *data = g_VFS.ReadFile(g_gameDBFilename, &size); if (!data) - return false; + return; + contents_ = std::string((const char *)data, size); delete[] data; @@ -99,7 +109,6 @@ bool GameDB::LoadFromVFS(VFSInterface &vfs, const char *filename) { line.size = items[sizeColumn]; lines_.push_back(line); } - return true; } size_t GameDB::GetColumnIndex(std::string_view name) const { @@ -128,7 +137,11 @@ bool GameDB::GetGameInfos(std::string_view id, std::vector *infos) { // Not a game. return false; } - + + std::lock_guard guard(loadMutex_); + + LoadIfNeeded(); + for (auto &line : lines_) { for (auto &serial : line.serials) { // Ignore version and stuff for now diff --git a/Core/Util/GameDB.h b/Core/Util/GameDB.h index d2c24f97a5..5d211da724 100644 --- a/Core/Util/GameDB.h +++ b/Core/Util/GameDB.h @@ -4,6 +4,7 @@ #include #include #include +#include class VFSInterface; @@ -17,10 +18,12 @@ struct GameDBInfo { class GameDB { public: - bool LoadFromVFS(VFSInterface &vfs, const char *filename); + // Warning: Linear search. Don't call it every frame if possible. bool GetGameInfos(std::string_view id, std::vector *infos); private: + // Call under lock. + void LoadIfNeeded(); size_t GetColumnIndex(std::string_view name) const; struct Line { @@ -36,6 +39,9 @@ private: std::string contents_; std::vector lines_; std::vector columns_; + bool loaded_ = false; + + std::mutex loadMutex_; }; extern GameDB g_gameDB; diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index c21a2f7609..e418b69022 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -784,10 +784,6 @@ void NativeInit(int argc, const char *argv[], const char *savegame_dir, const ch // Initialize retro achievements runtime. Achievements::Initialize(); -#if !defined(__LIBRETRO__) - g_gameDB.LoadFromVFS(g_VFS, "redump.csv"); -#endif - // Must be done restarting by now. restarting = false; }