GameDB: Add a mutex, and load on demand instead.

This commit is contained in:
Henrik Rydgård 2024-01-15 17:17:02 +01:00
parent c1a7235767
commit dc4f22d1a4
3 changed files with 25 additions and 10 deletions

View file

@ -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<std::string_view> &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<GameDBInfo> *infos) {
// Not a game.
return false;
}
std::lock_guard<std::mutex> guard(loadMutex_);
LoadIfNeeded();
for (auto &line : lines_) {
for (auto &serial : line.serials) {
// Ignore version and stuff for now

View file

@ -4,6 +4,7 @@
#include <string>
#include <vector>
#include <string_view>
#include <mutex>
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<GameDBInfo> *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<Line> lines_;
std::vector<std::string_view> columns_;
bool loaded_ = false;
std::mutex loadMutex_;
};
extern GameDB g_gameDB;

View file

@ -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;
}