| 1 | // Copyright 2024 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 5 | |
| 6 | #include <map> |
| 7 | #include <string> |
| 8 | #include <string_view> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <fmt/format.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | #include <picojson.h> |
| 14 | |
| 15 | #include "Common/BitUtils.h" |
| 16 | #include "Common/CommonPaths.h" |
| 17 | #include "Common/Crypto/SHA1.h" |
| 18 | #include "Common/FileUtil.h" |
| 19 | #include "Common/IniFile.h" |
| 20 | #include "Common/JsonUtil.h" |
| 21 | #include "Core/AchievementApprovedHash.h" |
| 22 | #include "Core/ActionReplay.h" |
| 23 | #include "Core/GeckoCode.h" |
| 24 | #include "Core/GeckoCodeConfig.h" |
| 25 | #include "Core/PatchEngine.h" |
| 26 | |
| 27 | struct GameHashes |
| 28 | { |
| 29 | std::string game_title; |
| 30 | std::map<std::string /*hash*/, std::string /*patch name*/> hashes; |
| 31 | }; |
| 32 | |
| 33 | using AllowList = std::map<std::string /*ID*/, GameHashes>; |
| 34 | |
| 35 | template <typename T> |
| 36 | void ReadVerified(const Common::IniFile& ini, const std::string& filename, |
| 37 | const std::string& section, bool enabled, std::vector<T>* codes); |
| 38 | |
| 39 | TEST(PatchAllowlist, VerifyHashes) |
| 40 | { |
| 41 | // Iterate over GameSettings directory |
| 42 | picojson::object new_allowlist; |
| 43 | std::string cur_directory = File::GetExeDirectory() |
| 44 | #if defined(__APPLE__) |
| 45 | + DIR_SEP "Tests" // FIXME: Ugly hack. |
| 46 | #endif |
| 47 | ; |
| 48 | std::string sys_directory = cur_directory + DIR_SEP "Sys"; |
| 49 | auto directory = |
| 50 | File::ScanDirectoryTree(fmt::format("{}{}GameSettings", sys_directory, DIR_SEP), false); |
| 51 | for (const auto& file : directory.children) |
| 52 | { |
| 53 | // Load ini file |
| 54 | picojson::object approved; |
| 55 | Common::IniFile ini_file; |
| 56 | ini_file.Load(file.physicalName, true); |
| 57 | std::string game_id = file.virtualName.substr(0, file.virtualName.find_first_of('.')); |
| 58 | std::vector<PatchEngine::Patch> patches; |
| 59 | PatchEngine::LoadPatchSection("OnFrame", &patches, ini_file, Common::IniFile()); |
| 60 | std::vector<Gecko::GeckoCode> geckos = Gecko::LoadCodes(Common::IniFile(), ini_file); |
| 61 | std::vector<ActionReplay::ARCode> action_replays = |
| 62 | ActionReplay::LoadCodes(Common::IniFile(), ini_file); |
| 63 | // Filter patches for RetroAchievements approved |
| 64 | ReadVerified<PatchEngine::Patch>(ini_file, game_id, "Patches_RetroAchievements_Verified", true, |
| 65 | &patches); |
| 66 | ReadVerified<Gecko::GeckoCode>(ini_file, game_id, "Gecko_RetroAchievements_Verified", true, |
| 67 | &geckos); |
| 68 | ReadVerified<ActionReplay::ARCode>(ini_file, game_id, "AR_RetroAchievements_Verified", true, |
| 69 | &action_replays); |
| 70 | // Iterate over approved patches |
| 71 | for (const auto& patch : patches) |
| 72 | { |
| 73 | if (!patch.enabled) |
| 74 | continue; |
| 75 | // Hash patch |
| 76 | auto context = Common::SHA1::CreateContext(); |
| 77 | context->Update(Common::BitCastToArray<u8>(static_cast<u64>(patch.entries.size()))); |
| 78 | for (const auto& entry : patch.entries) |
| 79 | { |
| 80 | context->Update(Common::BitCastToArray<u8>(entry.type)); |
| 81 | context->Update(Common::BitCastToArray<u8>(entry.address)); |
| 82 | context->Update(Common::BitCastToArray<u8>(entry.value)); |
| 83 | context->Update(Common::BitCastToArray<u8>(entry.comparand)); |
| 84 | context->Update(Common::BitCastToArray<u8>(entry.conditional)); |
| 85 | } |
| 86 | auto digest = context->Finish(); |
| 87 | approved[patch.name] = picojson::value(Common::SHA1::DigestToString(digest)); |
| 88 | } |
| 89 | // Iterate over approved geckos |
| 90 | for (const auto& code : geckos) |
| 91 | { |
| 92 | if (!code.enabled) |
| 93 | continue; |
| 94 | // Hash patch |
| 95 | auto context = Common::SHA1::CreateContext(); |
| 96 | context->Update(Common::BitCastToArray<u8>(static_cast<u64>(code.codes.size()))); |
| 97 | for (const auto& entry : code.codes) |
| 98 | { |
| 99 | context->Update(Common::BitCastToArray<u8>(entry.address)); |
| 100 | context->Update(Common::BitCastToArray<u8>(entry.data)); |
| 101 | } |
| 102 | auto digest = context->Finish(); |
| 103 | approved[code.name] = picojson::value(Common::SHA1::DigestToString(digest)); |
| 104 | } |
| 105 | // Iterate over approved AR codes |
| 106 | for (const auto& code : action_replays) |
| 107 | { |
| 108 | if (!code.enabled) |
| 109 | continue; |
| 110 | // Hash patch |
| 111 | auto context = Common::SHA1::CreateContext(); |
| 112 | context->Update(Common::BitCastToArray<u8>(static_cast<u64>(code.ops.size()))); |
| 113 | for (const auto& entry : code.ops) |
| 114 | { |
| 115 | context->Update(Common::BitCastToArray<u8>(entry.cmd_addr)); |
| 116 | context->Update(Common::BitCastToArray<u8>(entry.value)); |
| 117 | } |
| 118 | auto digest = context->Finish(); |
| 119 | approved[code.name] = picojson::value(Common::SHA1::DigestToString(digest)); |
| 120 | } |
| 121 | // Add approved patches and codes to tree |
| 122 | if (!approved.empty()) |
| 123 | new_allowlist[game_id] = picojson::value(approved); |
| 124 | } |
| 125 | |
| 126 | // Hash new allowlist |
| 127 | std::string new_allowlist_str = picojson::value(new_allowlist).serialize(); |
| 128 | auto context = Common::SHA1::CreateContext(); |
| 129 | context->Update(new_allowlist_str); |
| 130 | auto digest = context->Finish(); |
| 131 | if (digest != ACHIEVEMENT_APPROVED_LIST_HASH) |
| 132 | { |
| 133 | ADD_FAILURE() << "Approved list hash does not match the one in AchievementApprovedHash.h." |
| 134 | << std::endl |
| 135 | << "Please update ACHIEVEMENT_APPROVED_LIST_HASH to the following:" << std::endl |
| 136 | << Common::SHA1::DigestToSource(digest); |
| 137 | } |
| 138 | // Compare with old allowlist |
| 139 | std::string old_allowlist; |
| 140 | std::string error; |
| 141 | const auto& list_filepath = |
| 142 | fmt::format("{}{}{}", sys_directory, DIR_SEP, ACHIEVEMENT_APPROVED_LIST_FILENAME); |
| 143 | if (!File::ReadFileToString(list_filepath, old_allowlist) || old_allowlist != new_allowlist_str) |
| 144 | { |
| 145 | static constexpr std::string_view NEW_ACHIEVEMENT_APPROVED_LIST_FILENAME = |
| 146 | "New-ApprovedInis.json"; |
| 147 | const auto& new_list_filepath = |
| 148 | fmt::format("{}{}{}", sys_directory, DIR_SEP, NEW_ACHIEVEMENT_APPROVED_LIST_FILENAME); |
| 149 | if (!JsonToFile(new_list_filepath, picojson::value(new_allowlist), false)) |
| 150 | { |
| 151 | ADD_FAILURE() << "Failed to write new approved list to " << list_filepath; |
| 152 | } |
| 153 | ADD_FAILURE() << "Approved list needs to be updated. Please run this test in your" << std::endl |
| 154 | << "local environment and copy" << std::endl |
| 155 | << new_list_filepath << std::endl |
| 156 | << "to Data/Sys/ApprovedInis.json to pass this test."; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | template <typename T> |
| 161 | void ReadVerified(const Common::IniFile& ini, const std::string& filename, |
| 162 | const std::string& section, bool enabled, std::vector<T>* codes) |
| 163 | { |
| 164 | for (auto& code : *codes) |
| 165 | code.enabled = false; |
| 166 | |
| 167 | std::vector<std::string> lines; |
| 168 | ini.GetLines(section, &lines, false); |
| 169 | |
| 170 | for (const std::string& line : lines) |
| 171 | { |
| 172 | if (line.empty() || line[0] != '$') |
| 173 | continue; |
| 174 | |
| 175 | bool found = false; |
| 176 | for (T& code : *codes) |
| 177 | { |
| 178 | // Exclude the initial '$' from the comparison. |
| 179 | if (line.compare(1, std::string::npos, code.name) == 0) |
| 180 | { |
| 181 | code.enabled = enabled; |
| 182 | found = true; |
| 183 | } |
| 184 | } |
| 185 | if (!found) |
| 186 | { |
| 187 | // Report: approved patch in ini doesn't actually exist |
| 188 | ADD_FAILURE() << "Code with approval not found" << std::endl |
| 189 | << "Game ID: " << filename << std::endl |
| 190 | << "Name: \"" << line << "\""; |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | #endif // USE_RETRO_ACHIEVEMENTS |