game_list_worker: Add better error handling to caching

This commit is contained in:
Zach Hilman 2019-05-26 17:14:09 -04:00
parent 944c07ac7d
commit 46e2ca5475
2 changed files with 42 additions and 23 deletions

View File

@ -645,7 +645,8 @@ void Config::ReadUIGamelistValues() {
UISettings::values.icon_size = ReadSetting(QStringLiteral("icon_size"), 64).toUInt(); 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_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.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(); qt_config->endGroup();
} }

View File

@ -39,11 +39,12 @@ T GetGameListCachedObject(const std::string& filename, const std::string& ext,
template <> template <>
QString GetGameListCachedObject(const std::string& filename, const std::string& ext, QString GetGameListCachedObject(const std::string& filename, const std::string& ext,
const std::function<QString()>& generator) { const std::function<QString()>& generator) {
if (!UISettings::values.cache_game_list || filename == "0000000000000000") if (!UISettings::values.cache_game_list || filename == "0000000000000000") {
return generator(); return generator();
}
const auto& path = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + "game_list" + const auto path = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + "game_list" +
DIR_SEP + filename + "." + ext; DIR_SEP + filename + '.' + ext;
FileUtil::CreateFullPath(path); FileUtil::CreateFullPath(path);
@ -51,10 +52,10 @@ QString GetGameListCachedObject(const std::string& filename, const std::string&
const auto str = generator(); const auto str = generator();
std::ofstream stream(path); std::ofstream stream(path);
if (stream) if (stream) {
stream << str.toStdString(); stream << str.toStdString();
}
stream.close();
return str; return str;
} }
@ -63,7 +64,6 @@ QString GetGameListCachedObject(const std::string& filename, const std::string&
if (stream) { if (stream) {
const std::string out(std::istreambuf_iterator<char>{stream}, const std::string out(std::istreambuf_iterator<char>{stream},
std::istreambuf_iterator<char>{}); std::istreambuf_iterator<char>{});
stream.close();
return QString::fromStdString(out); return QString::fromStdString(out);
} }
@ -74,13 +74,14 @@ template <>
std::pair<std::vector<u8>, std::string> GetGameListCachedObject( std::pair<std::vector<u8>, std::string> GetGameListCachedObject(
const std::string& filename, const std::string& ext, const std::string& filename, const std::string& ext,
const std::function<std::pair<std::vector<u8>, std::string>()>& generator) { const std::function<std::pair<std::vector<u8>, std::string>()>& generator) {
if (!UISettings::values.cache_game_list || filename == "0000000000000000") if (!UISettings::values.cache_game_list || filename == "0000000000000000") {
return generator(); return generator();
}
const auto& path1 = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + const auto path1 = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + "game_list" +
"game_list" + DIR_SEP + filename + ".jpeg"; DIR_SEP + filename + ".jpeg";
const auto& path2 = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + const auto path2 = FileUtil::GetUserPath(FileUtil::UserPath::CacheDir) + DIR_SEP + "game_list" +
"game_list" + DIR_SEP + filename + ".appname.txt"; DIR_SEP + filename + ".appname.txt";
FileUtil::CreateFullPath(path1); FileUtil::CreateFullPath(path1);
@ -88,28 +89,48 @@ std::pair<std::vector<u8>, std::string> GetGameListCachedObject(
const auto [icon, nacp] = generator(); const auto [icon, nacp] = generator();
FileUtil::IOFile file1(path1, "wb"); FileUtil::IOFile file1(path1, "wb");
file1.Resize(icon.size()); if (!file1.IsOpen()) {
file1.WriteBytes(icon.data(), icon.size()); 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); std::ofstream stream2(path2, std::ios::out);
if (stream2) if (stream2) {
stream2 << nacp; stream2 << nacp;
}
file1.Close();
stream2.close();
return std::make_pair(icon, nacp); return std::make_pair(icon, nacp);
} }
FileUtil::IOFile file1(path1, "rb"); FileUtil::IOFile file1(path1, "rb");
std::ifstream stream2(path2); 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<u8> vec(file1.GetSize()); std::vector<u8> vec(file1.GetSize());
file1.ReadBytes(vec.data(), vec.size()); file1.ReadBytes(vec.data(), vec.size());
if (stream2 && !vec.empty()) { if (stream2 && !vec.empty()) {
const std::string out(std::istreambuf_iterator<char>{stream2}, const std::string out(std::istreambuf_iterator<char>{stream2},
std::istreambuf_iterator<char>{}); std::istreambuf_iterator<char>{});
stream2.close();
return std::make_pair(vec, out); return std::make_pair(vec, out);
} }
@ -118,14 +139,11 @@ std::pair<std::vector<u8>, std::string> GetGameListCachedObject(
void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, const FileSys::NCA& nca, void GetMetadataFromControlNCA(const FileSys::PatchManager& patch_manager, const FileSys::NCA& nca,
std::vector<u8>& icon, std::string& name) { std::vector<u8>& icon, std::string& name) {
auto res = GetGameListCachedObject<std::pair<std::vector<u8>, std::string>>( std::tie(icon, name) = GetGameListCachedObject<std::pair<std::vector<u8>, std::string>>(
fmt::format("{:016X}", patch_manager.GetTitleID()), {}, [&patch_manager, &nca] { fmt::format("{:016X}", patch_manager.GetTitleID()), {}, [&patch_manager, &nca] {
const auto [nacp, icon_f] = patch_manager.ParseControlNCA(nca); const auto [nacp, icon_f] = patch_manager.ParseControlNCA(nca);
return std::make_pair(icon_f->ReadAllBytes(), nacp->GetApplicationName()); 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) { bool HasSupportedFileExtension(const std::string& file_name) {