RiivolutionParser.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2021 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <map>
  5. #include <memory>
  6. #include <optional>
  7. #include <string>
  8. #include <string_view>
  9. #include <vector>
  10. #include "Common/CommonTypes.h"
  11. namespace DiscIO
  12. {
  13. struct GameModDescriptorRiivolution;
  14. }
  15. namespace DiscIO::Riivolution
  16. {
  17. class FileDataLoader;
  18. // Data to determine the game patches are valid for.
  19. struct GameFilter
  20. {
  21. std::optional<std::string> m_game;
  22. std::optional<std::string> m_developer;
  23. std::optional<int> m_disc;
  24. std::optional<int> m_version;
  25. std::optional<std::vector<std::string>> m_regions;
  26. };
  27. // Which patches will get activated by selecting a Choice in the Riivolution GUI.
  28. struct PatchReference
  29. {
  30. std::string m_id;
  31. std::map<std::string, std::string> m_params;
  32. };
  33. // A single choice within an Option in the Riivolution GUI.
  34. struct Choice
  35. {
  36. std::string m_name;
  37. std::vector<PatchReference> m_patch_references;
  38. };
  39. // A single option presented to the user in the Riivolution GUI.
  40. struct Option
  41. {
  42. std::string m_name;
  43. std::string m_id;
  44. std::vector<Choice> m_choices;
  45. // The currently selected patch choice in the m_choices vector.
  46. // Note that this index is 1-based; 0 means no choice is selected and this Option is disabled.
  47. u32 m_selected_choice = 0;
  48. };
  49. // A single page of options presented to the user in the Riivolution GUI.
  50. struct Section
  51. {
  52. std::string m_name;
  53. std::vector<Option> m_options;
  54. };
  55. // Replaces, adds, or modifies a file on disc.
  56. struct File
  57. {
  58. // Path of the file on disc to modify.
  59. std::string m_disc;
  60. // Path of the file on SD card to use for modification.
  61. std::string m_external;
  62. // If true, the file on the disc is truncated if the external file end is before the disc file
  63. // end. If false, the bytes after the external file end stay as they were.
  64. bool m_resize = true;
  65. // If true, a new file is created if it does not already exist at the disc path. Otherwise this
  66. // modification is ignored if the file does not exist on disc.
  67. bool m_create = false;
  68. // Offset of where to start replacing bytes in the on-disc file.
  69. u32 m_offset = 0;
  70. // Offset of where to start reading bytes in the external file.
  71. u32 m_fileoffset = 0;
  72. // Amount of bytes to copy from the external file to the disc file.
  73. // If left zero, the entire file (starting at fileoffset) is used.
  74. u32 m_length = 0;
  75. };
  76. // Adds or modifies a folder on disc.
  77. struct Folder
  78. {
  79. // Path of the folder on disc to modify.
  80. // Can be left empty to replace files matching the filename without specifying the folder.
  81. std::string m_disc;
  82. // Path of the folder on SD card to use for modification.
  83. std::string m_external;
  84. // Like File::m_resize but for each file in the folder.
  85. bool m_resize = true;
  86. // Like File::m_create but for each file in the folder.
  87. bool m_create = false;
  88. // Whether to also traverse subdirectories. (TODO: of the disc folder? external folder? both?)
  89. bool m_recursive = true;
  90. // Like File::m_length but for each file in the folder.
  91. u32 m_length = 0;
  92. };
  93. // Redirects the save file from the Wii NAND to a folder on SD card.
  94. struct Savegame
  95. {
  96. // The folder on SD card to use for the save files. Is created if it does not exist.
  97. std::string m_external;
  98. // If this is set to true and the external folder is empty or does not exist, the existing save on
  99. // NAND is copied to the new folder on game boot.
  100. bool m_clone = true;
  101. };
  102. // Modifies the game RAM right before jumping into the game executable.
  103. struct Memory
  104. {
  105. // Memory address where this modification takes place.
  106. u32 m_offset = 0;
  107. // Bytes to write to that address.
  108. std::vector<u8> m_value;
  109. // Like m_value, but read the bytes from a file instead.
  110. std::string m_valuefile;
  111. // If set, the memory at that address will be checked before the value is written, and the
  112. // replacement value only written if the bytes there match this.
  113. std::vector<u8> m_original;
  114. // If true, this memory patch is an ocarina-style patch.
  115. bool m_ocarina = false;
  116. // If true, the offset is not known, and instead we should search for the m_original bytes in
  117. // memory and replace them where found. Only searches in MEM1, and only replaces the first match.
  118. bool m_search = false;
  119. // For m_search. The byte stride between search points.
  120. u32 m_align = 1;
  121. };
  122. struct Patch
  123. {
  124. // Internal name of this patch.
  125. std::string m_id;
  126. // Defines a SD card path that all other paths are relative to.
  127. // For actually loading file data Dolphin uses the loader below instead.
  128. std::string m_root;
  129. std::shared_ptr<FileDataLoader> m_file_data_loader;
  130. std::vector<File> m_file_patches;
  131. std::vector<Folder> m_folder_patches;
  132. std::vector<File> m_sys_file_patches;
  133. std::vector<Folder> m_sys_folder_patches;
  134. std::vector<Savegame> m_savegame_patches;
  135. std::vector<Memory> m_memory_patches;
  136. ~Patch();
  137. };
  138. struct Disc
  139. {
  140. // Riivolution version. Only '1' exists at time of writing.
  141. int m_version = 0;
  142. // Info about which game and revision these patches are for.
  143. GameFilter m_game_filter;
  144. // The options shown to the user in the UI.
  145. std::vector<Section> m_sections;
  146. // The actual patch instructions.
  147. std::vector<Patch> m_patches;
  148. // The path to the parsed XML file.
  149. std::string m_xml_path;
  150. // Checks whether these patches are valid for the given game.
  151. bool IsValidForGame(const std::string& game_id, std::optional<u16> revision,
  152. std::optional<u8> disc_number) const;
  153. // Transforms an abstract XML-parsed patch set into a concrete one, with only the selected
  154. // patches applied and all placeholders replaced.
  155. std::vector<Patch> GeneratePatches(const std::string& game_id) const;
  156. };
  157. // Config format that remembers which patches are enabled/disabled for the next run.
  158. // Some patches ship with pre-made config XMLs instead of baking their defaults into the actual
  159. // patch XMLs, so it makes sense to support this format directly.
  160. struct ConfigOption
  161. {
  162. // The identifier for the referenced Option.
  163. std::string m_id;
  164. // The selected Choice index.
  165. u32 m_default = 0;
  166. };
  167. struct Config
  168. {
  169. // Config version. Riivolution itself only accepts '2' here.
  170. int m_version = 2;
  171. std::vector<ConfigOption> m_options;
  172. };
  173. std::optional<Disc> ParseFile(const std::string& filename);
  174. std::optional<Disc> ParseString(std::string_view xml, std::string xml_path);
  175. std::vector<Patch> GenerateRiivolutionPatchesFromGameModDescriptor(
  176. const GameModDescriptorRiivolution& descriptor, const std::string& game_id,
  177. std::optional<u16> revision, std::optional<u8> disc_number);
  178. std::vector<Patch> GenerateRiivolutionPatchesFromConfig(const std::string root_directory,
  179. const std::string& game_id,
  180. std::optional<u16> revision,
  181. std::optional<u8> disc_number);
  182. std::optional<Config> ParseConfigFile(const std::string& filename);
  183. std::optional<Config> ParseConfigString(std::string_view xml);
  184. std::string WriteConfigString(const Config& config);
  185. bool WriteConfigFile(const std::string& filename, const Config& config);
  186. void ApplyConfigDefaults(Disc* disc, const Config& config);
  187. } // namespace DiscIO::Riivolution