rpcsx/rpcs3/Emu/games_config.cpp

113 lines
1.8 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "games_config.h"
#include "util/logs.hpp"
#include "util/yaml.hpp"
#include "Utilities/File.h"
LOG_CHANNEL(cfg_log, "CFG");
games_config::games_config()
{
load();
}
games_config::~games_config()
{
if (m_dirty)
{
save();
}
}
std::string games_config::get_path(const std::string& title_id) const
{
if (title_id.empty())
{
return {};
}
if (const auto it = m_games.find(title_id); it != m_games.cend())
{
return it->second;
}
return {};
}
bool games_config::add_game(const std::string& key, const std::string& path)
{
// Access or create node if does not exist
if (auto it = m_games.find(key); it != m_games.end())
{
if (it->second == path)
{
// Nothing to do
return true;
}
it->second = path;
}
else
{
m_games.emplace(key, path);
}
m_dirty = true;
if (m_save_on_dirty)
{
return save();
}
return true;
}
bool games_config::save()
{
YAML::Emitter out;
out << m_games;
fs::pending_file temp(fs::get_config_dir() + "/games.yml");
if (temp.file && temp.file.write(out.c_str(), out.size()), temp.commit())
{
m_dirty = false;
return true;
}
cfg_log.error("Failed to save games.yml: %s", fs::g_tls_error);
return false;
}
void games_config::load()
{
m_games.clear();
if (fs::file f{fs::get_config_dir() + "/games.yml", fs::read + fs::create})
{
auto [result, error] = yaml_load(f.to_string());
if (!error.empty())
{
cfg_log.error("Failed to load games.yml: %s", error);
}
if (!result.IsMap())
{
if (!result.IsNull())
{
cfg_log.error("Failed to load games.yml: type %d not a map", result.Type());
}
return;
}
for (const auto& entry : result)
{
if (!entry.first.Scalar().empty() && entry.second.IsScalar() && !entry.second.Scalar().empty())
{
m_games.emplace(entry.first.Scalar(), entry.second.Scalar());
}
}
}
}