From f95bdb5088a4264b7174f22e95d5409b5e297c16 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Tue, 23 Apr 2019 09:08:38 -0400 Subject: game_list: Implement caching for game list Preserves list of add ons and the icon, which are the two costliest parts of game list population. --- src/yuzu/game_list_worker.cpp | 106 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 7 deletions(-) (limited to 'src/yuzu/game_list_worker.cpp') diff --git a/src/yuzu/game_list_worker.cpp b/src/yuzu/game_list_worker.cpp index 82d2826ba..bc1833289 100644 --- a/src/yuzu/game_list_worker.cpp +++ b/src/yuzu/game_list_worker.cpp @@ -9,6 +9,7 @@ #include #include +#include #include "common/common_paths.h" #include "common/file_util.h" @@ -30,13 +31,101 @@ #include "yuzu/ui_settings.h" namespace { + +template +T GetGameListCachedObject(const std::string& filename, const std::string& ext, + const std::function& generator); + +template <> +QString GetGameListCachedObject(const std::string& filename, const std::string& ext, + const std::function& generator) { + if (!UISettings::values.cache_game_list || filename == "0000000000000000") + return generator(); + + const auto& path = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + "game_list" + + DIR_SEP + filename + "." + ext; + + FileUtil::CreateFullPath(path); + + if (!FileUtil::Exists(path)) { + const auto str = generator(); + + std::ofstream stream(path); + if (stream) + stream << str.toStdString(); + + stream.close(); + return str; + } + + std::ifstream stream(path); + + if (stream) { + const std::string out(std::istreambuf_iterator{stream}, + std::istreambuf_iterator{}); + stream.close(); + return QString::fromStdString(out); + } + + return generator(); +} + +template <> +std::pair, std::string> GetGameListCachedObject( + const std::string& filename, const std::string& ext, + const std::function, std::string>()>& generator) { + if (!UISettings::values.cache_game_list || filename == "0000000000000000") + return generator(); + + const auto& path1 = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + + "game_list" + DIR_SEP + filename + ".jpeg"; + const auto& path2 = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + + "game_list" + DIR_SEP + filename + ".appname.txt"; + + FileUtil::CreateFullPath(path1); + + if (!FileUtil::Exists(path1) || !FileUtil::Exists(path2)) { + const auto [icon, nacp] = generator(); + + FileUtil::IOFile file1(path1, "wb"); + file1.Resize(icon.size()); + file1.WriteBytes(icon.data(), icon.size()); + + std::ofstream stream2(path2, std::ios::out); + if (stream2) + stream2 << nacp; + + file1.Close(); + stream2.close(); + return std::make_pair(icon, nacp); + } + + FileUtil::IOFile file1(path1, "rb"); + std::ifstream stream2(path2); + + std::vector vec(file1.GetSize()); + file1.ReadBytes(vec.data(), vec.size()); + + if (stream2 && !vec.empty()) { + const std::string out(std::istreambuf_iterator{stream2}, + std::istreambuf_iterator{}); + stream2.close(); + return std::make_pair(vec, out); + } + + return generator(); +} + void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, const FileSys::NCA& nca, std::vector& icon, std::string& name) { - auto [nacp, icon_file] = patch_manager.ParseControlNCA(nca); - if (icon_file != nullptr) - icon = icon_file->ReadAllBytes(); - if (nacp != nullptr) - name = nacp->GetApplicationName(); + auto res = GetGameListCachedObject, std::string>>( + fmt::format("{:016X}", patch_manager.GetTitleID()), {}, [&patch_manager, &nca] { + const auto [nacp, icon_f] = patch_manager.ParseControlNCA(nca); + return std::make_pair(icon_f->ReadAllBytes(), nacp->GetApplicationName()); + }); + + icon = std::move(res.first); + name = std::move(res.second); } bool HasSupportedFileExtension(const std::string& file_name) { @@ -114,8 +203,11 @@ QList MakeGameListEntry(const std::string& path, const std::stri }; if (UISettings::values.show_add_ons) { - list.insert( - 2, new GameListItem(FormatPatchNameVersions(patch, loader, loader.IsRomFSUpdatable()))); + const auto patch_versions = GetGameListCachedObject( + fmt::format("{:016X}", patch.GetTitleID()), "pv.txt", [&patch, &loader] { + return FormatPatchNameVersions(patch, loader, loader.IsRomFSUpdatable()); + }); + list.insert(2, new GameListItem(patch_versions)); } return list; -- cgit v1.2.3 From 46e2ca5475854a1ae58283804d7b712594ecf461 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 26 May 2019 17:14:09 -0400 Subject: game_list_worker: Add better error handling to caching --- src/yuzu/game_list_worker.cpp | 60 ++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 21 deletions(-) (limited to 'src/yuzu/game_list_worker.cpp') diff --git a/src/yuzu/game_list_worker.cpp b/src/yuzu/game_list_worker.cpp index bc1833289..4d951a4e7 100644 --- a/src/yuzu/game_list_worker.cpp +++ b/src/yuzu/game_list_worker.cpp @@ -39,11 +39,12 @@ T GetGameListCachedObject(const std::string& filename, const std::string& ext, template <> QString GetGameListCachedObject(const std::string& filename, const std::string& ext, const std::function& generator) { - if (!UISettings::values.cache_game_list || filename == "0000000000000000") + if (!UISettings::values.cache_game_list || filename == "0000000000000000") { return generator(); + } - const auto& path = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + "game_list" + - DIR_SEP + filename + "." + ext; + const auto path = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + "game_list" + + DIR_SEP + filename + '.' + ext; FileUtil::CreateFullPath(path); @@ -51,10 +52,10 @@ QString GetGameListCachedObject(const std::string& filename, const std::string& const auto str = generator(); std::ofstream stream(path); - if (stream) + if (stream) { stream << str.toStdString(); + } - stream.close(); return str; } @@ -63,7 +64,6 @@ QString GetGameListCachedObject(const std::string& filename, const std::string& if (stream) { const std::string out(std::istreambuf_iterator{stream}, std::istreambuf_iterator{}); - stream.close(); return QString::fromStdString(out); } @@ -74,13 +74,14 @@ template <> std::pair, std::string> GetGameListCachedObject( const std::string& filename, const std::string& ext, const std::function, std::string>()>& generator) { - if (!UISettings::values.cache_game_list || filename == "0000000000000000") + if (!UISettings::values.cache_game_list || filename == "0000000000000000") { return generator(); + } - const auto& path1 = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + - "game_list" + DIR_SEP + filename + ".jpeg"; - const auto& path2 = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + - "game_list" + DIR_SEP + filename + ".appname.txt"; + const auto path1 = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + "game_list" + + DIR_SEP + filename + ".jpeg"; + const auto path2 = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + "game_list" + + DIR_SEP + filename + ".appname.txt"; FileUtil::CreateFullPath(path1); @@ -88,28 +89,48 @@ std::pair, std::string> GetGameListCachedObject( const auto [icon, nacp] = generator(); FileUtil::IOFile file1(path1, "wb"); - file1.Resize(icon.size()); - file1.WriteBytes(icon.data(), icon.size()); + if (!file1.IsOpen()) { + LOG_ERROR(Frontend, "Failed to open cache file."); + return generator(); + } + + if (!file1.Resize(icon.size())) { + LOG_ERROR(Frontend, "Failed to resize cache file to necessary size."); + return generator(); + } + + if (file1.WriteBytes(icon.data(), icon.size()) != icon.size()) { + LOG_ERROR(Frontend, "Failed to write data to cache file."); + return generator(); + } std::ofstream stream2(path2, std::ios::out); - if (stream2) + if (stream2) { stream2 << nacp; + } - file1.Close(); - stream2.close(); return std::make_pair(icon, nacp); } FileUtil::IOFile file1(path1, "rb"); std::ifstream stream2(path2); + if (!file1.IsOpen()) { + LOG_ERROR(Frontend, "Failed to open cache file for reading."); + return generator(); + } + + if (!stream2) { + LOG_ERROR(Frontend, "Failed to open cache file for reading."); + return generator(); + } + std::vector vec(file1.GetSize()); file1.ReadBytes(vec.data(), vec.size()); if (stream2 && !vec.empty()) { const std::string out(std::istreambuf_iterator{stream2}, std::istreambuf_iterator{}); - stream2.close(); return std::make_pair(vec, out); } @@ -118,14 +139,11 @@ std::pair, std::string> GetGameListCachedObject( void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, const FileSys::NCA& nca, std::vector& icon, std::string& name) { - auto res = GetGameListCachedObject, std::string>>( + std::tie(icon, name) = GetGameListCachedObject, std::string>>( fmt::format("{:016X}", patch_manager.GetTitleID()), {}, [&patch_manager, &nca] { const auto [nacp, icon_f] = patch_manager.ParseControlNCA(nca); return std::make_pair(icon_f->ReadAllBytes(), nacp->GetApplicationName()); }); - - icon = std::move(res.first); - name = std::move(res.second); } bool HasSupportedFileExtension(const std::string& file_name) { -- cgit v1.2.3