Debian Codegraph @

Source/Core/DiscIO/RiivolutionParser.h

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