| 1 | // Copyright 2021 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DiscIO/RiivolutionParser.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <sstream> |
| 8 | #include <string> |
| 9 | #include <string_view> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <fmt/format.h> |
| 13 | #include <pugixml.hpp> |
| 14 | |
| 15 | #include "Common/FileSearch.h" |
| 16 | #include "Common/FileUtil.h" |
| 17 | #include "Common/IOFile.h" |
| 18 | #include "Common/StringUtil.h" |
| 19 | #include "DiscIO/GameModDescriptor.h" |
| 20 | #include "DiscIO/RiivolutionPatcher.h" |
| 21 | |
| 22 | namespace DiscIO::Riivolution |
| 23 | { |
| 24 | Patch::~Patch() = default; |
| 25 | |
| 26 | std::optional<Disc> ParseFile(const std::string& filename) |
| 27 | { |
| 28 | ::File::IOFile f(filename, "rb"); |
| 29 | if (!f) |
| 30 | return std::nullopt; |
| 31 | |
| 32 | std::vector<char> data; |
| 33 | data.resize(f.GetSize()); |
| 34 | if (!f.ReadBytes(data.data(), data.size())) |
| 35 | return std::nullopt; |
| 36 | |
| 37 | return ParseString(std::string_view(data.data(), data.size()), filename); |
| 38 | } |
| 39 | |
| 40 | static std::map<std::string, std::string> ReadParams(const pugi::xml_node& node, |
| 41 | std::map<std::string, std::string> params = {}) |
| 42 | { |
| 43 | for (const auto& param_node : node.children("param")) |
| 44 | { |
| 45 | const std::string param_name = param_node.attribute("name").as_string(); |
| 46 | const std::string param_value = param_node.attribute("value").as_string(); |
| 47 | params[param_name] = param_value; |
| 48 | } |
| 49 | return params; |
| 50 | } |
| 51 | |
| 52 | static std::vector<u8> ReadHexString(std::string_view sv) |
| 53 | { |
| 54 | if ((sv.size() % 2) == 1) |
| 55 | return {}; |
| 56 | if (sv.starts_with("0x") || sv.starts_with("0X")) |
| 57 | sv = sv.substr(2); |
| 58 | |
| 59 | std::vector<u8> result; |
| 60 | result.reserve(sv.size() / 2); |
| 61 | while (!sv.empty()) |
| 62 | { |
| 63 | u8 tmp; |
| 64 | if (!TryParse(std::string(sv.substr(0, 2)), &tmp, 16)) |
| 65 | return {}; |
| 66 | result.push_back(tmp); |
| 67 | sv = sv.substr(2); |
| 68 | } |
| 69 | return result; |
| 70 | } |
| 71 | |
| 72 | std::optional<Disc> ParseString(std::string_view xml, std::string xml_path) |
| 73 | { |
| 74 | pugi::xml_document doc; |
| 75 | const auto parse_result = doc.load_buffer(xml.data(), xml.size()); |
| 76 | if (!parse_result) |
| 77 | return std::nullopt; |
| 78 | |
| 79 | const auto wiidisc = doc.child("wiidisc"); |
| 80 | if (!wiidisc) |
| 81 | return std::nullopt; |
| 82 | |
| 83 | Disc disc; |
| 84 | disc.m_xml_path = std::move(xml_path); |
| 85 | disc.m_version = wiidisc.attribute("version").as_int(-1); |
| 86 | if (disc.m_version != 1) |
| 87 | return std::nullopt; |
| 88 | const std::string default_root = wiidisc.attribute("root").as_string(); |
| 89 | |
| 90 | const auto id = wiidisc.child("id"); |
| 91 | if (id) |
| 92 | { |
| 93 | for (const auto& attribute : id.attributes()) |
| 94 | { |
| 95 | const std::string_view attribute_name(attribute.name()); |
| 96 | if (attribute_name == "game") |
| 97 | disc.m_game_filter.m_game = attribute.as_string(); |
| 98 | else if (attribute_name == "developer") |
| 99 | disc.m_game_filter.m_developer = attribute.as_string(); |
| 100 | else if (attribute_name == "disc") |
| 101 | disc.m_game_filter.m_disc = attribute.as_int(-1); |
| 102 | else if (attribute_name == "version") |
| 103 | disc.m_game_filter.m_version = attribute.as_int(-1); |
| 104 | } |
| 105 | |
| 106 | auto xml_regions = id.children("region"); |
| 107 | if (xml_regions.begin() != xml_regions.end()) |
| 108 | { |
| 109 | std::vector<std::string> regions; |
| 110 | for (const auto& region : xml_regions) |
| 111 | regions.push_back(region.attribute("type").as_string()); |
| 112 | disc.m_game_filter.m_regions = std::move(regions); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | const auto options = wiidisc.child("options"); |
| 117 | if (options) |
| 118 | { |
| 119 | for (const auto& section_node : options.children("section")) |
| 120 | { |
| 121 | Section& section = disc.m_sections.emplace_back(); |
| 122 | section.m_name = section_node.attribute("name").as_string(); |
| 123 | for (const auto& option_node : section_node.children("option")) |
| 124 | { |
| 125 | Option& option = section.m_options.emplace_back(); |
| 126 | option.m_id = option_node.attribute("id").as_string(); |
| 127 | option.m_name = option_node.attribute("name").as_string(); |
| 128 | option.m_selected_choice = option_node.attribute("default").as_uint(0); |
| 129 | auto option_params = ReadParams(option_node); |
| 130 | for (const auto& choice_node : option_node.children("choice")) |
| 131 | { |
| 132 | Choice& choice = option.m_choices.emplace_back(); |
| 133 | choice.m_name = choice_node.attribute("name").as_string(); |
| 134 | auto choice_params = ReadParams(choice_node, option_params); |
| 135 | for (const auto& patchref_node : choice_node.children("patch")) |
| 136 | { |
| 137 | PatchReference& patchref = choice.m_patch_references.emplace_back(); |
| 138 | patchref.m_id = patchref_node.attribute("id").as_string(); |
| 139 | patchref.m_params = ReadParams(patchref_node, choice_params); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | for (const auto& macro_node : options.children("macros")) |
| 145 | { |
| 146 | const std::string macro_id = macro_node.attribute("id").as_string(); |
| 147 | for (auto& section : disc.m_sections) |
| 148 | { |
| 149 | auto option_to_clone = std::ranges::find(section.m_options, macro_id, &Option::m_id); |
| 150 | if (option_to_clone == section.m_options.end()) |
| 151 | continue; |
| 152 | |
| 153 | Option cloned_option = *option_to_clone; |
| 154 | cloned_option.m_name = macro_node.attribute("name").as_string(); |
| 155 | for (auto& choice : cloned_option.m_choices) |
| 156 | for (auto& patch_ref : choice.m_patch_references) |
| 157 | patch_ref.m_params = ReadParams(macro_node, patch_ref.m_params); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | const auto patches = wiidisc.children("patch"); |
| 163 | for (const auto& patch_node : patches) |
| 164 | { |
| 165 | Patch& patch = disc.m_patches.emplace_back(); |
| 166 | patch.m_id = patch_node.attribute("id").as_string(); |
| 167 | patch.m_root = patch_node.attribute("root").as_string(); |
| 168 | if (patch.m_root.empty()) |
| 169 | patch.m_root = default_root; |
| 170 | |
| 171 | for (const auto& patch_subnode : patch_node.children()) |
| 172 | { |
| 173 | const std::string_view patch_name(patch_subnode.name()); |
| 174 | if (patch_name == "file" || patch_name == "dolphin_sys_file") |
| 175 | { |
| 176 | auto& file = patch_name == "dolphin_sys_file" ? patch.m_sys_file_patches.emplace_back() : |
| 177 | patch.m_file_patches.emplace_back(); |
| 178 | file.m_disc = patch_subnode.attribute("disc").as_string(); |
| 179 | file.m_external = patch_subnode.attribute("external").as_string(); |
| 180 | file.m_resize = patch_subnode.attribute("resize").as_bool(true); |
| 181 | file.m_create = patch_subnode.attribute("create").as_bool(false); |
| 182 | file.m_offset = patch_subnode.attribute("offset").as_uint(0); |
| 183 | file.m_fileoffset = patch_subnode.attribute("fileoffset").as_uint(0); |
| 184 | file.m_length = patch_subnode.attribute("length").as_uint(0); |
| 185 | } |
| 186 | else if (patch_name == "folder" || patch_name == "dolphin_sys_folder") |
| 187 | { |
| 188 | auto& folder = patch_name == "dolphin_sys_folder" ? |
| 189 | patch.m_sys_folder_patches.emplace_back() : |
| 190 | patch.m_folder_patches.emplace_back(); |
| 191 | folder.m_disc = patch_subnode.attribute("disc").as_string(); |
| 192 | folder.m_external = patch_subnode.attribute("external").as_string(); |
| 193 | folder.m_resize = patch_subnode.attribute("resize").as_bool(true); |
| 194 | folder.m_create = patch_subnode.attribute("create").as_bool(false); |
| 195 | folder.m_recursive = patch_subnode.attribute("recursive").as_bool(true); |
| 196 | folder.m_length = patch_subnode.attribute("length").as_uint(0); |
| 197 | } |
| 198 | else if (patch_name == "savegame") |
| 199 | { |
| 200 | auto& savegame = patch.m_savegame_patches.emplace_back(); |
| 201 | savegame.m_external = patch_subnode.attribute("external").as_string(); |
| 202 | savegame.m_clone = patch_subnode.attribute("clone").as_bool(true); |
| 203 | } |
| 204 | else if (patch_name == "memory") |
| 205 | { |
| 206 | auto& memory = patch.m_memory_patches.emplace_back(); |
| 207 | memory.m_offset = patch_subnode.attribute("offset").as_uint(0); |
| 208 | memory.m_value = ReadHexString(patch_subnode.attribute("value").as_string()); |
| 209 | memory.m_valuefile = patch_subnode.attribute("valuefile").as_string(); |
| 210 | memory.m_original = ReadHexString(patch_subnode.attribute("original").as_string()); |
| 211 | memory.m_ocarina = patch_subnode.attribute("ocarina").as_bool(false); |
| 212 | memory.m_search = patch_subnode.attribute("search").as_bool(false); |
| 213 | memory.m_align = patch_subnode.attribute("align").as_uint(1); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return disc; |
| 219 | } |
| 220 | |
| 221 | static bool CheckRegion(std::span<const std::string> xml_regions, std::string_view game_region) |
| 222 | { |
| 223 | if (xml_regions.empty()) |
| 224 | return true; |
| 225 | |
| 226 | for (const auto& region : xml_regions) |
| 227 | { |
| 228 | if (region == game_region) |
| 229 | return true; |
| 230 | } |
| 231 | |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | bool Disc::IsValidForGame(const std::string& game_id, std::optional<u16> revision, |
| 236 | std::optional<u8> disc_number) const |
| 237 | { |
| 238 | if (game_id.size() != 6) |
| 239 | return false; |
| 240 | |
| 241 | const std::string_view game_id_full = std::string_view(game_id); |
| 242 | const std::string_view game_region = game_id_full.substr(3, 1); |
| 243 | const std::string_view game_developer = game_id_full.substr(4, 2); |
| 244 | const int disc_number_int = std::optional<int>(disc_number).value_or(-1); |
| 245 | const int revision_int = std::optional<int>(revision).value_or(-1); |
| 246 | |
| 247 | if (m_game_filter.m_game && !game_id_full.starts_with(*m_game_filter.m_game)) |
| 248 | return false; |
| 249 | if (m_game_filter.m_developer && game_developer != *m_game_filter.m_developer) |
| 250 | return false; |
| 251 | if (m_game_filter.m_disc && disc_number_int != *m_game_filter.m_disc) |
| 252 | return false; |
| 253 | if (m_game_filter.m_version && revision_int != *m_game_filter.m_version) |
| 254 | return false; |
| 255 | if (m_game_filter.m_regions && !CheckRegion(*m_game_filter.m_regions, game_region)) |
| 256 | return false; |
| 257 | |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | std::vector<Patch> Disc::GeneratePatches(const std::string& game_id) const |
| 262 | { |
| 263 | const std::string_view game_id_full = std::string_view(game_id); |
| 264 | const std::string_view game_id_no_region = game_id_full.substr(0, 3); |
| 265 | const std::string_view game_region = game_id_full.substr(3, 1); |
| 266 | const std::string_view game_developer = game_id_full.substr(4, 2); |
| 267 | |
| 268 | const auto replace_variables = |
| 269 | [&](std::string_view sv, |
| 270 | const std::vector<std::pair<std::string, std::string_view>>& replacements) { |
| 271 | std::string result; |
| 272 | result.reserve(sv.size()); |
| 273 | while (!sv.empty()) |
| 274 | { |
| 275 | bool replaced = false; |
| 276 | for (const auto& r : replacements) |
| 277 | { |
| 278 | if (sv.starts_with(r.first)) |
| 279 | { |
| 280 | for (char c : r.second) |
| 281 | result.push_back(c); |
| 282 | sv = sv.substr(r.first.size()); |
| 283 | replaced = true; |
| 284 | break; |
| 285 | } |
| 286 | } |
| 287 | if (replaced) |
| 288 | continue; |
| 289 | result.push_back(sv[0]); |
| 290 | sv = sv.substr(1); |
| 291 | } |
| 292 | return result; |
| 293 | }; |
| 294 | |
| 295 | // Take only selected patches, replace placeholders in all strings, and return them. |
| 296 | std::vector<Patch> active_patches; |
| 297 | for (const auto& section : m_sections) |
| 298 | { |
| 299 | for (const auto& option : section.m_options) |
| 300 | { |
| 301 | const u32 selected = option.m_selected_choice; |
| 302 | if (selected == 0 || selected > option.m_choices.size()) |
| 303 | continue; |
| 304 | const Choice& choice = option.m_choices[selected - 1]; |
| 305 | for (const auto& patch_ref : choice.m_patch_references) |
| 306 | { |
| 307 | const auto patch = std::ranges::find(m_patches, patch_ref.m_id, &Patch::m_id); |
| 308 | if (patch == m_patches.end()) |
| 309 | continue; |
| 310 | |
| 311 | std::vector<std::pair<std::string, std::string_view>> replacements; |
| 312 | replacements.emplace_back(std::pair{"{$__gameid}", game_id_no_region}); |
| 313 | replacements.emplace_back(std::pair{"{$__region}", game_region}); |
| 314 | replacements.emplace_back(std::pair{"{$__maker}", game_developer}); |
| 315 | for (const auto& param : patch_ref.m_params) |
| 316 | replacements.emplace_back("{$" + param.first + "}", param.second); |
| 317 | |
| 318 | Patch& new_patch = active_patches.emplace_back(*patch); |
| 319 | new_patch.m_root = replace_variables(new_patch.m_root, replacements); |
| 320 | for (auto& file : new_patch.m_file_patches) |
| 321 | { |
| 322 | file.m_disc = replace_variables(file.m_disc, replacements); |
| 323 | file.m_external = replace_variables(file.m_external, replacements); |
| 324 | } |
| 325 | for (auto& folder : new_patch.m_folder_patches) |
| 326 | { |
| 327 | folder.m_disc = replace_variables(folder.m_disc, replacements); |
| 328 | folder.m_external = replace_variables(folder.m_external, replacements); |
| 329 | } |
| 330 | for (auto& savegame : new_patch.m_savegame_patches) |
| 331 | { |
| 332 | savegame.m_external = replace_variables(savegame.m_external, replacements); |
| 333 | } |
| 334 | for (auto& memory : new_patch.m_memory_patches) |
| 335 | { |
| 336 | memory.m_valuefile = replace_variables(memory.m_valuefile, replacements); |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return active_patches; |
| 343 | } |
| 344 | |
| 345 | std::vector<Patch> GenerateRiivolutionPatchesFromGameModDescriptor( |
| 346 | const GameModDescriptorRiivolution& descriptor, const std::string& game_id, |
| 347 | std::optional<u16> revision, std::optional<u8> disc_number) |
| 348 | { |
| 349 | std::vector<Patch> result; |
| 350 | for (const auto& patch_info : descriptor.patches) |
| 351 | { |
| 352 | auto parsed = ParseFile(patch_info.xml); |
| 353 | if (!parsed || !parsed->IsValidForGame(game_id, revision, disc_number)) |
| 354 | continue; |
| 355 | |
| 356 | for (auto& section : parsed->m_sections) |
| 357 | { |
| 358 | for (auto& option : section.m_options) |
| 359 | { |
| 360 | const auto* info = [&]() -> const GameModDescriptorRiivolutionPatchOption* { |
| 361 | for (const auto& o : patch_info.options) |
| 362 | { |
| 363 | if (o.section_name == section.m_name) |
| 364 | { |
| 365 | if (!o.option_id.empty() && o.option_id == option.m_id) |
| 366 | return &o; |
| 367 | if (!o.option_name.empty() && o.option_name == option.m_name) |
| 368 | return &o; |
| 369 | } |
| 370 | } |
| 371 | return nullptr; |
| 372 | }(); |
| 373 | if (info && info->choice <= option.m_choices.size()) |
| 374 | option.m_selected_choice = info->choice; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | for (auto& p : parsed->GeneratePatches(game_id)) |
| 379 | { |
| 380 | p.m_file_data_loader = |
| 381 | std::make_shared<FileDataLoaderHostFS>(patch_info.root, parsed->m_xml_path, p.m_root); |
| 382 | result.emplace_back(std::move(p)); |
| 383 | } |
| 384 | } |
| 385 | return result; |
| 386 | } |
| 387 | |
| 388 | std::vector<Patch> GenerateRiivolutionPatchesFromConfig(const std::string& root_directory, |
| 389 | const std::string& game_id, |
| 390 | std::optional<u16> revision, |
| 391 | std::optional<u8> disc_number) |
| 392 | { |
| 393 | std::vector<Patch> result; |
| 394 | |
| 395 | // The way Riivolution stores settings only makes sense for standard game IDs. |
| 396 | if (!(game_id.size() == 4 || game_id.size() == 6)) |
| 397 | return result; |
| 398 | |
| 399 | const std::optional<Config> config = ParseConfigFile( |
| 400 | fmt::format("{}/riivolution/config/{}.xml", root_directory, game_id.substr(0, 4))); |
| 401 | |
| 402 | for (const std::string& path : Common::DoFileSearch(root_directory + "riivolution", ".xml")) |
| 403 | { |
| 404 | std::optional<Disc> parsed = ParseFile(path); |
| 405 | if (!parsed || !parsed->IsValidForGame(game_id, revision, disc_number)) |
| 406 | continue; |
| 407 | if (config) |
| 408 | ApplyConfigDefaults(&*parsed, *config); |
| 409 | |
| 410 | for (auto& patch : parsed->GeneratePatches(game_id)) |
| 411 | { |
| 412 | patch.m_file_data_loader = |
| 413 | std::make_shared<FileDataLoaderHostFS>(root_directory, parsed->m_xml_path, patch.m_root); |
| 414 | result.emplace_back(std::move(patch)); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return result; |
| 419 | } |
| 420 | |
| 421 | std::optional<Config> ParseConfigFile(const std::string& filename) |
| 422 | { |
| 423 | ::File::IOFile f(filename, "rb"); |
| 424 | if (!f) |
| 425 | return std::nullopt; |
| 426 | |
| 427 | std::vector<char> data; |
| 428 | data.resize(f.GetSize()); |
| 429 | if (!f.ReadBytes(data.data(), data.size())) |
| 430 | return std::nullopt; |
| 431 | |
| 432 | return ParseConfigString(std::string_view(data.data(), data.size())); |
| 433 | } |
| 434 | |
| 435 | std::optional<Config> ParseConfigString(std::string_view xml) |
| 436 | { |
| 437 | pugi::xml_document doc; |
| 438 | const auto parse_result = doc.load_buffer(xml.data(), xml.size()); |
| 439 | if (!parse_result) |
| 440 | return std::nullopt; |
| 441 | |
| 442 | const auto riivolution = doc.child("riivolution"); |
| 443 | if (!riivolution) |
| 444 | return std::nullopt; |
| 445 | |
| 446 | Config config; |
| 447 | config.m_version = riivolution.attribute("version").as_int(-1); |
| 448 | if (config.m_version != 2) |
| 449 | return std::nullopt; |
| 450 | |
| 451 | const auto options = riivolution.children("option"); |
| 452 | for (const auto& option_node : options) |
| 453 | { |
| 454 | auto& option = config.m_options.emplace_back(); |
| 455 | option.m_id = option_node.attribute("id").as_string(); |
| 456 | option.m_default = option_node.attribute("default").as_uint(0); |
| 457 | } |
| 458 | |
| 459 | return config; |
| 460 | } |
| 461 | |
| 462 | std::string WriteConfigString(const Config& config) |
| 463 | { |
| 464 | pugi::xml_document doc; |
| 465 | auto riivolution = doc.append_child("riivolution"); |
| 466 | riivolution.append_attribute("version").set_value(config.m_version); |
| 467 | for (const auto& option : config.m_options) |
| 468 | { |
| 469 | auto option_node = riivolution.append_child("option"); |
| 470 | option_node.append_attribute("id").set_value(option.m_id.c_str()); |
| 471 | option_node.append_attribute("default").set_value(option.m_default); |
| 472 | } |
| 473 | |
| 474 | std::stringstream ss; |
| 475 | doc.print(ss, " "); |
| 476 | return ss.str(); |
| 477 | } |
| 478 | |
| 479 | bool WriteConfigFile(const std::string& filename, const Config& config) |
| 480 | { |
| 481 | auto xml = WriteConfigString(config); |
| 482 | if (xml.empty()) |
| 483 | return false; |
| 484 | |
| 485 | ::File::CreateFullPath(filename); |
| 486 | ::File::IOFile f(filename, "wb"); |
| 487 | if (!f) |
| 488 | return false; |
| 489 | |
| 490 | if (!f.WriteString(xml)) |
| 491 | return false; |
| 492 | |
| 493 | return true; |
| 494 | } |
| 495 | |
| 496 | void ApplyConfigDefaults(Disc* disc, const Config& config) |
| 497 | { |
| 498 | for (const auto& config_option : config.m_options) |
| 499 | { |
| 500 | auto* matching_option = [&]() -> Option* { |
| 501 | for (auto& section : disc->m_sections) |
| 502 | { |
| 503 | for (auto& option : section.m_options) |
| 504 | { |
| 505 | if (option.m_id.empty()) |
| 506 | { |
| 507 | if ((section.m_name + option.m_name) == config_option.m_id) |
| 508 | return &option; |
| 509 | } |
| 510 | else |
| 511 | { |
| 512 | if (option.m_id == config_option.m_id) |
| 513 | return &option; |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | return nullptr; |
| 518 | }(); |
| 519 | if (matching_option) |
| 520 | matching_option->m_selected_choice = config_option.m_default; |
| 521 | } |
| 522 | } |
| 523 | } // namespace DiscIO::Riivolution |