From 78f3448e279256e36e320c1c2f30b0c5a81172ff Mon Sep 17 00:00:00 2001 From: LillyJadeKatrin Date: Thu, 25 Jul 2024 08:38:38 -0400 Subject: [PATCH] Convert FilterApprovedPatches to Template --- Source/Core/Core/AchievementManager.cpp | 103 +++++++++++++++--------- Source/Core/Core/AchievementManager.h | 6 ++ 2 files changed, 71 insertions(+), 38 deletions(-) diff --git a/Source/Core/Core/AchievementManager.cpp b/Source/Core/Core/AchievementManager.cpp index 73ace1af34..f4119ca0c3 100644 --- a/Source/Core/Core/AchievementManager.cpp +++ b/Source/Core/Core/AchievementManager.cpp @@ -384,10 +384,11 @@ bool AchievementManager::IsHardcoreModeActive() const return rc_client_is_processing_required(m_client); } -void AchievementManager::FilterApprovedPatches(std::vector& patches, - const std::string& game_ini_id) const +template +void AchievementManager::FilterApprovedIni(std::vector& codes, + const std::string& game_ini_id) const { - if (patches.empty()) + if (codes.empty()) { // There's nothing to verify, so let's save ourselves some work return; @@ -398,46 +399,72 @@ void AchievementManager::FilterApprovedPatches(std::vector& if (!IsHardcoreModeActive()) return; + // Approved codes list failed to hash + if (!m_ini_root->is()) + { + codes.clear(); + return; + } + + for (auto& code : codes) + { + if (code.enabled && !CheckApprovedCode(code, game_ini_id)) + code.enabled = false; + } +} + +template +bool AchievementManager::CheckApprovedCode(const T& code, const std::string& game_ini_id) const +{ + if (!IsHardcoreModeActive()) + return true; + + // Approved codes list failed to hash + if (!m_ini_root->is()) + return false; + const bool known_id = m_ini_root->contains(game_ini_id); - auto patch_itr = patches.begin(); - while (patch_itr != patches.end()) + INFO_LOG_FMT(ACHIEVEMENTS, "Verifying code {}", code.name); + + bool verified = false; + + if (known_id) { - INFO_LOG_FMT(ACHIEVEMENTS, "Verifying patch {}", patch_itr->name); + auto digest = GetCodeHash(code); - bool verified = false; - - if (known_id) - { - auto context = Common::SHA1::CreateContext(); - context->Update(Common::BitCastToArray(static_cast(patch_itr->entries.size()))); - for (const auto& entry : patch_itr->entries) - { - context->Update(Common::BitCastToArray(entry.type)); - context->Update(Common::BitCastToArray(entry.address)); - context->Update(Common::BitCastToArray(entry.value)); - context->Update(Common::BitCastToArray(entry.comparand)); - context->Update(Common::BitCastToArray(entry.conditional)); - } - auto digest = context->Finish(); - - verified = m_ini_root->get(game_ini_id).contains(Common::SHA1::DigestToString(digest)); - } - - if (!verified) - { - patch_itr = patches.erase(patch_itr); - OSD::AddMessage( - fmt::format("Failed to verify patch {} from file {}.", patch_itr->name, game_ini_id), - OSD::Duration::VERY_LONG, OSD::Color::RED); - OSD::AddMessage("Disable hardcore mode to enable this patch.", OSD::Duration::VERY_LONG, - OSD::Color::RED); - } - else - { - patch_itr++; - } + verified = m_ini_root->get(game_ini_id).contains(Common::SHA1::DigestToString(digest)); } + + if (!verified) + { + OSD::AddMessage(fmt::format("Failed to verify code {} from file {}.", code.name, game_ini_id), + OSD::Duration::VERY_LONG, OSD::Color::RED); + OSD::AddMessage("Disable hardcore mode to enable this code.", OSD::Duration::VERY_LONG, + OSD::Color::RED); + } + return verified; +} + +Common::SHA1::Digest AchievementManager::GetCodeHash(const PatchEngine::Patch& patch) const +{ + auto context = Common::SHA1::CreateContext(); + context->Update(Common::BitCastToArray(static_cast(patch.entries.size()))); + for (const auto& entry : patch.entries) + { + context->Update(Common::BitCastToArray(entry.type)); + context->Update(Common::BitCastToArray(entry.address)); + context->Update(Common::BitCastToArray(entry.value)); + context->Update(Common::BitCastToArray(entry.comparand)); + context->Update(Common::BitCastToArray(entry.conditional)); + } + return context->Finish(); +} + +void AchievementManager::FilterApprovedPatches(std::vector& patches, + const std::string& game_ini_id) const +{ + FilterApprovedIni(patches, game_ini_id); } void AchievementManager::SetSpectatorMode() diff --git a/Source/Core/Core/AchievementManager.h b/Source/Core/Core/AchievementManager.h index 174b4abce1..77859b17d1 100644 --- a/Source/Core/Core/AchievementManager.h +++ b/Source/Core/Core/AchievementManager.h @@ -181,6 +181,12 @@ private: void* userdata); void DisplayWelcomeMessage(); + template + void FilterApprovedIni(std::vector& codes, const std::string& game_ini_id) const; + template + bool CheckApprovedCode(const T& code, const std::string& game_ini_id) const; + Common::SHA1::Digest GetCodeHash(const PatchEngine::Patch& patch) const; + static void LeaderboardEntriesCallback(int result, const char* error_message, rc_client_leaderboard_entry_list_t* list, rc_client_t* client, void* userdata);