Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emu: Cache games.yml and only save when necessary #13716

Merged
merged 1 commit into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions rpcs3/Emu/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_library(rpcs3_emu
cache_utils.cpp
games_config.cpp
IdManager.cpp
localized_string.cpp
savestate_utils.cpp
Expand Down
97 changes: 28 additions & 69 deletions rpcs3/Emu/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "Utilities/StrUtil.h"

#include "../Crypto/unself.h"
#include "util/yaml.hpp"
#include "util/logs.hpp"
#include "util/serialization.hpp"

Expand Down Expand Up @@ -875,26 +874,6 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
{
Init(add_only);

// Load game list (maps ABCD12345 IDs to /dev_bdvd/ locations)
YAML::Node games;

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())
{
sys_log.error("Failed to load games.yml: %s", error);
}

games = result;
}

if (!games.IsMap())
{
games.reset();
}

m_state_inspection_savestate = g_cfg.savestate.state_inspection_mode.get();
m_savestate_extension_flags1 = {};

Expand Down Expand Up @@ -956,9 +935,9 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
m_title_id = disc_info;

// Load /dev_bdvd/ from game list if available
if (auto node = games[m_title_id])
if (std::string game_path = m_games_config.get_path(m_title_id); !game_path.empty())
{
disc = node.Scalar();
disc = std::move(game_path);
}
else if (!g_cfg.savestate.state_inspection_mode)
{
Expand Down Expand Up @@ -1070,9 +1049,9 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
std::string title_path;

// const overload does not create new node on failure
if (auto node = std::as_const(games)[m_title_id])
if (std::string game_path = m_games_config.get_path(m_title_id); !game_path.empty())
{
title_path = node.Scalar();
title_path = std::move(game_path);
}

for (std::string test_path :
Expand Down Expand Up @@ -1113,9 +1092,9 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
title_id = title_id.substr(0, title_id.find_first_of('/'));

// Try to load game directory from list if available
if (auto node = (title_id.empty() ? YAML::Node{} : games[title_id]))
if (std::string game_path = m_games_config.get_path(m_title_id); !game_path.empty())
{
disc = node.Scalar();
disc = std::move(game_path);
m_path = disc + argv[0].substr(game0_path.size() + title_id.size());
}
}
Expand Down Expand Up @@ -1541,9 +1520,9 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
if ((is_disc_patch || m_cat == "GD") && bdvd_dir.empty() && disc.empty())
{
// Load /dev_bdvd/ from game list if available
if (auto node = games[m_title_id])
if (std::string game_path = m_games_config.get_path(m_title_id); !game_path.empty())
{
bdvd_dir = node.Scalar();
bdvd_dir = std::move(game_path);
}
else
{
Expand All @@ -1552,35 +1531,6 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
}
}

auto try_register_game_location = [&](const std::string& key, const std::string& loc)
{
// Access or create node if does not exist
auto node = games[key];

if (node && node.Scalar() == loc)
{
// Nothing to do
return true;
}

// Write to node
node = loc;

YAML::Emitter out;
out << games;

fs::pending_file temp(fs::get_config_dir() + "/games.yml");

// Do not update games.yml when TITLE_ID is empty
if (temp.file && temp.file.write(out.c_str(), out.size()), temp.commit())
{
m_games_yml_invalidated = true;
return true;
}

return false;
};

// Check /dev_bdvd/
if (disc.empty() && !bdvd_dir.empty() && fs::is_dir(bdvd_dir))
{
Expand Down Expand Up @@ -1617,7 +1567,11 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
}

// Store /dev_bdvd/ location
if (!try_register_game_location(m_title_id, bdvd_dir))
if (m_games_config.add_game(m_title_id, bdvd_dir))
{
sys_log.notice("Registered BDVD game directory for title '%s': %s", m_title_id, bdvd_dir);
}
else
{
sys_log.error("Failed to save BDVD location of title '%s' (error=%s)", m_title_id, fs::g_tls_error);
}
Expand Down Expand Up @@ -1675,7 +1629,11 @@ game_boot_result Emulator::Load(const std::string& title_id, bool add_only, bool
}

// Add HG games not in HDD0 to games.yml
if (!try_register_game_location(m_title_id, game_dir))
if (m_games_config.add_game(m_title_id, game_dir))
{
sys_log.notice("Registered HG game directory for title '%s': %s", m_title_id, game_dir);
}
else
{
sys_log.error("Failed to save HG game location of title '%s' (error=%s)", m_title_id, fs::g_tls_error);
}
Expand Down Expand Up @@ -3154,15 +3112,12 @@ void Emulator::AddGamesFromDir(const std::string& path)
if (!IsStopped())
return;

m_games_yml_invalidated = false;
m_games_config.set_save_on_dirty(false);

// search dropped path first or else the direct parent to an elf is wrongly skipped
if (const auto error = BootGame(path, "", false, true); error == game_boot_result::no_errors)
{
if (std::exchange(m_games_yml_invalidated, false))
{
sys_log.notice("Registered game directory: %s", path);
}
// Nothing to do
}

// search direct subdirectories, that way we can drop one folder containing all games
Expand All @@ -3177,12 +3132,16 @@ void Emulator::AddGamesFromDir(const std::string& path)

if (const auto error = BootGame(dir_path, "", false, true); error == game_boot_result::no_errors)
{
if (std::exchange(m_games_yml_invalidated, false))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not log it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already logged in load

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, i would have to clear the dirty flag each time which only complicates things for no reason

{
sys_log.notice("Registered game directory: %s", dir_path);
}
// Nothing to do
}
}

m_games_config.set_save_on_dirty(true);

if (m_games_config.is_dirty() && !m_games_config.save())
{
sys_log.error("Failed to save games.yml after adding games");
}
}

bool Emulator::IsPathInsideDir(std::string_view path, std::string_view dir) const
Expand Down
8 changes: 7 additions & 1 deletion rpcs3/Emu/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "util/types.hpp"
#include "util/atomic.hpp"
#include "Utilities/bit_set.h"
#include "games_config.h"
#include <functional>
#include <memory>
#include <string>
Expand Down Expand Up @@ -119,7 +120,7 @@ class Emulator final
atomic_t<u64> m_pause_amend_time{0}; // increased when resumed
atomic_t<u64> m_stop_ctr{0}; // Increments when emulation is stopped

bool m_games_yml_invalidated = false;
games_config m_games_config;

video_renderer m_default_renderer;
std::string m_default_graphics_adapter;
Expand Down Expand Up @@ -287,6 +288,11 @@ class Emulator final
return m_usr;
}

const games_config& GetGamesConfig() const
{
return m_games_config;
}

// Get deserialization manager
utils::serial* DeserialManager() const;

Expand Down
112 changes: 112 additions & 0 deletions rpcs3/Emu/games_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#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());
}
}
}
}
28 changes: 28 additions & 0 deletions rpcs3/Emu/games_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include <map>

class games_config
{
public:
games_config();
virtual ~games_config();

void set_save_on_dirty(bool enabled) { m_save_on_dirty = enabled; }

const std::map<std::string, std::string>& get_games() const { return m_games; }
bool is_dirty() const { return m_dirty; }

std::string get_path(const std::string& title_id) const;

bool add_game(const std::string& key, const std::string& path);
bool save();

private:
void load();

std::map<std::string, std::string> m_games;

bool m_dirty = false;
bool m_save_on_dirty = true;
};
2 changes: 2 additions & 0 deletions rpcs3/emucore.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<ClCompile Include="Emu\Cell\Modules\libfs_utility_init.cpp" />
<ClCompile Include="Emu\Cell\Modules\sys_crashdump.cpp" />
<ClCompile Include="Emu\Cell\Modules\HLE_PATCHES.cpp" />
<ClCompile Include="Emu\games_config.cpp" />
<ClCompile Include="Emu\Io\camera_config.cpp" />
<ClCompile Include="Emu\Io\recording_config.cpp" />
<ClCompile Include="Emu\Io\Turntable.cpp" />
Expand Down Expand Up @@ -497,6 +498,7 @@
<ClInclude Include="Emu\Cell\Modules\libfs_utility_init.h" />
<ClInclude Include="Emu\Cell\Modules\sys_crashdump.h" />
<ClInclude Include="Emu\CPU\sse2neon.h" />
<ClInclude Include="Emu\games_config.h" />
<ClInclude Include="Emu\Io\camera_config.h" />
<ClInclude Include="Emu\Io\camera_handler_base.h" />
<ClInclude Include="Emu\Io\music_handler_base.h" />
Expand Down
6 changes: 6 additions & 0 deletions rpcs3/emucore.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,9 @@
<ClCompile Include="Emu\RSX\Overlays\overlay_manager.cpp">
<Filter>Emu\GPU\RSX\Overlays</Filter>
</ClCompile>
<ClCompile Include="Emu\games_config.cpp">
<Filter>Emu</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Crypto\aes.h">
Expand Down Expand Up @@ -2311,6 +2314,9 @@
<ClInclude Include="io_buffer.h">
<Filter>Emu\GPU\RSX\Common</Filter>
</ClInclude>
<ClInclude Include="Emu\games_config.h">
<Filter>Emu</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="Emu\RSX\Program\GLSLSnippets\GPUDeswizzle.glsl">
Expand Down
Loading