Convert FilterApprovedPatches to Template

This commit is contained in:
LillyJadeKatrin 2024-07-25 08:38:38 -04:00
parent 74d129ee12
commit dc49d8cd6b
2 changed files with 71 additions and 38 deletions

View File

@ -384,10 +384,11 @@ bool AchievementManager::IsHardcoreModeActive() const
return rc_client_is_processing_required(m_client);
}
void AchievementManager::FilterApprovedPatches(std::vector<PatchEngine::Patch>& patches,
const std::string& game_ini_id) const
template <typename T>
void AchievementManager::FilterApprovedIni(std::vector<T>& 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<PatchEngine::Patch>&
if (!IsHardcoreModeActive())
return;
// Approved codes list failed to hash
if (!m_ini_root->is<picojson::value::object>())
{
codes.clear();
return;
}
for (auto& code : codes)
{
if (code.enabled && !CheckApprovedCode(code, game_ini_id))
code.enabled = false;
}
}
template <typename T>
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<picojson::value::object>())
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<u8>(static_cast<u64>(patch_itr->entries.size())));
for (const auto& entry : patch_itr->entries)
{
context->Update(Common::BitCastToArray<u8>(entry.type));
context->Update(Common::BitCastToArray<u8>(entry.address));
context->Update(Common::BitCastToArray<u8>(entry.value));
context->Update(Common::BitCastToArray<u8>(entry.comparand));
context->Update(Common::BitCastToArray<u8>(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<u8>(static_cast<u64>(patch.entries.size())));
for (const auto& entry : patch.entries)
{
context->Update(Common::BitCastToArray<u8>(entry.type));
context->Update(Common::BitCastToArray<u8>(entry.address));
context->Update(Common::BitCastToArray<u8>(entry.value));
context->Update(Common::BitCastToArray<u8>(entry.comparand));
context->Update(Common::BitCastToArray<u8>(entry.conditional));
}
return context->Finish();
}
void AchievementManager::FilterApprovedPatches(std::vector<PatchEngine::Patch>& patches,
const std::string& game_ini_id) const
{
FilterApprovedIni(patches, game_ini_id);
}
void AchievementManager::SetSpectatorMode()

View File

@ -181,6 +181,12 @@ private:
void* userdata);
void DisplayWelcomeMessage();
template <typename T>
void FilterApprovedIni(std::vector<T>& codes, const std::string& game_ini_id) const;
template <typename T>
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);