From 46e2ca5475854a1ae58283804d7b712594ecf461 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Sun, 26 May 2019 17:14:09 -0400 Subject: [PATCH] game_list_worker: Add better error handling to caching --- src/yuzu/configuration/config.cpp | 5 +-- src/yuzu/game_list_worker.cpp | 60 ++++++++++++++++++++----------- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index d708d67863..b1942bedc2 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -645,7 +645,8 @@ void Config::ReadUIGamelistValues() { UISettings::values.icon_size = ReadSetting(QStringLiteral("icon_size"), 64).toUInt(); UISettings::values.row_1_text_id = ReadSetting(QStringLiteral("row_1_text_id"), 3).toUInt(); UISettings::values.row_2_text_id = ReadSetting(QStringLiteral("row_2_text_id"), 2).toUInt(); - UISettings::values.cache_game_list = ReadSetting(QStringLiteral("cache_game_list"), true).toBool(); + UISettings::values.cache_game_list = + ReadSetting(QStringLiteral("cache_game_list"), true).toBool(); qt_config->endGroup(); } @@ -1010,7 +1011,7 @@ void Config::SaveUIGamelistValues() { WriteSetting(QStringLiteral("icon_size"), UISettings::values.icon_size, 64); WriteSetting(QStringLiteral("row_1_text_id"), UISettings::values.row_1_text_id, 3); WriteSetting(QStringLiteral("row_2_text_id"), UISettings::values.row_2_text_id, 2); - WriteSetting(QStringLiteral("cache_game_list"), UISettings::values.cache_game_list, true); + WriteSetting(QStringLiteral("cache_game_list"), UISettings::values.cache_game_list, true); qt_config->endGroup(); } diff --git a/src/yuzu/game_list_worker.cpp b/src/yuzu/game_list_worker.cpp index bc1833289d..4d951a4e78 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) {