From de2533d389bdf52ddf4faa3029c3fc95f51fed46 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 30 May 2019 18:56:04 -0400 Subject: [PATCH 1/2] game_list_worker: Remove template specializations This is equivalent to specifying two separate functions, so we can just do that. --- src/yuzu/game_list_worker.cpp | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/yuzu/game_list_worker.cpp b/src/yuzu/game_list_worker.cpp index 4d951a4e7..1ccabed59 100644 --- a/src/yuzu/game_list_worker.cpp +++ b/src/yuzu/game_list_worker.cpp @@ -32,11 +32,6 @@ 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") { @@ -70,7 +65,6 @@ QString GetGameListCachedObject(const std::string& filename, const std::string& return generator(); } -template <> std::pair, std::string> GetGameListCachedObject( const std::string& filename, const std::string& ext, const std::function, std::string>()>& generator) { @@ -139,7 +133,7 @@ std::pair, std::string> GetGameListCachedObject( void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, const FileSys::NCA& nca, std::vector& icon, std::string& name) { - std::tie(icon, name) = GetGameListCachedObject, std::string>>( + std::tie(icon, name) = GetGameListCachedObject( 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()); @@ -221,7 +215,7 @@ QList MakeGameListEntry(const std::string& path, const std::stri }; if (UISettings::values.show_add_ons) { - const auto patch_versions = GetGameListCachedObject( + const auto patch_versions = GetGameListCachedObject( fmt::format("{:016X}", patch.GetTitleID()), "pv.txt", [&patch, &loader] { return FormatPatchNameVersions(patch, loader, loader.IsRomFSUpdatable()); }); From d0d97de1e41832aa7d4ab3fa4869498e985d04c7 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 30 May 2019 22:13:38 -0400 Subject: [PATCH 2/2] game_list_worker: Use QFile over our own IOFile instance or std streams Stays consistent in our code with using Qt's provided mechanisms, and also properly handles Unicode paths (which file streams on Windows don't do very well). --- src/yuzu/game_list_worker.cpp | 52 ++++++++++++++++------------------- 1 file changed, 24 insertions(+), 28 deletions(-) diff --git a/src/yuzu/game_list_worker.cpp b/src/yuzu/game_list_worker.cpp index 1ccabed59..4f30e9147 100644 --- a/src/yuzu/game_list_worker.cpp +++ b/src/yuzu/game_list_worker.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include @@ -46,20 +47,17 @@ QString GetGameListCachedObject(const std::string& filename, const std::string& if (!FileUtil::Exists(path)) { const auto str = generator(); - std::ofstream stream(path); - if (stream) { - stream << str.toStdString(); + QFile file{QString::fromStdString(path)}; + if (file.open(QFile::WriteOnly)) { + file.write(str.toUtf8()); } return str; } - std::ifstream stream(path); - - if (stream) { - const std::string out(std::istreambuf_iterator{stream}, - std::istreambuf_iterator{}); - return QString::fromStdString(out); + QFile file{QString::fromStdString(path)}; + if (file.open(QFile::ReadOnly)) { + return QString::fromUtf8(file.readAll()); } return generator(); @@ -82,53 +80,51 @@ std::pair, std::string> GetGameListCachedObject( if (!FileUtil::Exists(path1) || !FileUtil::Exists(path2)) { const auto [icon, nacp] = generator(); - FileUtil::IOFile file1(path1, "wb"); - if (!file1.IsOpen()) { + QFile file1{QString::fromStdString(path1)}; + if (!file1.open(QFile::WriteOnly)) { LOG_ERROR(Frontend, "Failed to open cache file."); return generator(); } - if (!file1.Resize(icon.size())) { + 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()) { + if (file1.write(reinterpret_cast(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) { - stream2 << nacp; + QFile file2{QString::fromStdString(path2)}; + if (file2.open(QFile::WriteOnly)) { + file2.write(nacp.data(), nacp.size()); } return std::make_pair(icon, nacp); } - FileUtil::IOFile file1(path1, "rb"); - std::ifstream stream2(path2); + QFile file1(QString::fromStdString(path1)); + QFile file2(QString::fromStdString(path2)); - if (!file1.IsOpen()) { + if (!file1.open(QFile::ReadOnly)) { LOG_ERROR(Frontend, "Failed to open cache file for reading."); return generator(); } - if (!stream2) { + if (!file2.open(QFile::ReadOnly)) { 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{}); - return std::make_pair(vec, out); + std::vector vec(file1.size()); + if (file1.read(reinterpret_cast(vec.data()), vec.size()) != + static_cast(vec.size())) { + return generator(); } - return generator(); + const auto data = file2.readAll(); + return std::make_pair(vec, data.toStdString()); } void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, const FileSys::NCA& nca,