Debian Codegraph @

Source/Core/DolphinQt/RiivolutionBootWidget.cpp

raw symbols SCIP
1 // Copyright 2021 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/RiivolutionBootWidget.h"
5
6 #include <unordered_map>
7
8 #include <fmt/format.h>
9
10 #include <QComboBox>
11 #include <QDialogButtonBox>
12 #include <QDir>
13 #include <QFileDialog>
14 #include <QFileInfo>
15 #include <QGridLayout>
16 #include <QGroupBox>
17 #include <QLabel>
18 #include <QLineEdit>
19 #include <QMetaType>
20 #include <QPushButton>
21 #include <QScrollArea>
22 #include <QVBoxLayout>
23
24 #include "Common/FileSearch.h"
25 #include "Common/FileUtil.h"
26 #include "DiscIO/GameModDescriptor.h"
27 #include "DiscIO/RiivolutionParser.h"
28 #include "DiscIO/RiivolutionPatcher.h"
29 #include "DolphinQt/Config/HardcoreWarningWidget.h"
30 #include "DolphinQt/QtUtils/ModalMessageBox.h"
31
32 struct GuiRiivolutionPatchIndex
33 {
34 size_t m_disc_index;
35 size_t m_section_index;
36 size_t m_option_index;
37 size_t m_choice_index;
38 };
39
40 Q_DECLARE_METATYPE(GuiRiivolutionPatchIndex);
41
42 RiivolutionBootWidget::RiivolutionBootWidget(std::string game_id, std::optional<u16> revision,
43 std::optional<u8> disc, std::string base_game_path,
44 QWidget* parent)
45 : QDialog(parent), m_game_id(std::move(game_id)), m_revision(revision), m_disc_number(disc),
46 m_base_game_path(std::move(base_game_path))
47 {
48 setWindowTitle(tr("Start with Riivolution Patches"));
49
50 CreateWidgets();
51 ConnectWidgets();
52 LoadMatchingXMLs();
53
54 resize(QSize(400, 600));
55 }
56
57 RiivolutionBootWidget::~RiivolutionBootWidget() = default;
58
59 void RiivolutionBootWidget::CreateWidgets()
60 {
61 #ifdef USE_RETRO_ACHIEVEMENTS
62 m_hc_warning = new HardcoreWarningWidget(this);
63 #endif // USE_RETRO_ACHIEVEMENTS
64 auto* open_xml_button = new QPushButton(tr("Open Riivolution XML..."));
65 auto* boot_game_button = new QPushButton(tr("Start"));
66 boot_game_button->setDefault(true);
67 auto* save_preset_button = new QPushButton(tr("Save as Preset..."));
68 auto* group_box = new QGroupBox();
69 auto* scroll_area = new QScrollArea();
70
71 auto* stretch_helper = new QVBoxLayout();
72 m_patch_section_layout = new QVBoxLayout();
73 stretch_helper->addLayout(m_patch_section_layout);
74 stretch_helper->addStretch();
75 group_box->setLayout(stretch_helper);
76 scroll_area->setWidget(group_box);
77 scroll_area->setWidgetResizable(true);
78
79 auto* button_layout = new QHBoxLayout();
80 button_layout->addStretch();
81 button_layout->addWidget(open_xml_button, 0, Qt::AlignRight);
82 button_layout->addWidget(save_preset_button, 0, Qt::AlignRight);
83 button_layout->addWidget(boot_game_button, 0, Qt::AlignRight);
84
85 auto* layout = new QVBoxLayout();
86 #ifdef USE_RETRO_ACHIEVEMENTS
87 layout->addWidget(m_hc_warning);
88 #endif // USE_RETRO_ACHIEVEMENTS
89 layout->addWidget(scroll_area);
90 layout->addLayout(button_layout);
91 setLayout(layout);
92
93 connect(open_xml_button, &QPushButton::clicked, this, &RiivolutionBootWidget::OpenXML);
94 connect(boot_game_button, &QPushButton::clicked, this, &RiivolutionBootWidget::BootGame);
95 connect(save_preset_button, &QPushButton::clicked, this, &RiivolutionBootWidget::SaveAsPreset);
96 }
97
98 void RiivolutionBootWidget::ConnectWidgets()
99 {
100 #ifdef USE_RETRO_ACHIEVEMENTS
101 connect(m_hc_warning, &HardcoreWarningWidget::OpenAchievementSettings, this,
102 &RiivolutionBootWidget::OpenAchievementSettings);
103 connect(m_hc_warning, &HardcoreWarningWidget::OpenAchievementSettings, this,
104 &RiivolutionBootWidget::reject);
105 #endif // USE_RETRO_ACHIEVEMENTS
106 }
107
108 void RiivolutionBootWidget::LoadMatchingXMLs()
109 {
110 const std::string& riivolution_dir = File::GetUserPath(D_RIIVOLUTION_IDX);
111 const auto config = LoadConfigXML(riivolution_dir);
112 for (const std::string& path : Common::DoFileSearch(riivolution_dir + "riivolution", ".xml"))
113 {
114 auto parsed = DiscIO::Riivolution::ParseFile(path);
115 if (!parsed || !parsed->IsValidForGame(m_game_id, m_revision, m_disc_number))
116 continue;
117 if (config)
118 DiscIO::Riivolution::ApplyConfigDefaults(&*parsed, *config);
119 MakeGUIForParsedFile(path, riivolution_dir, *parsed);
120 }
121 }
122
123 static std::string FindRoot(const std::string& path)
124 {
125 // Try to set the virtual SD root to directory one up from current.
126 // This mimics where the XML would be on a real SD card.
127 QDir dir = QFileInfo(QString::fromStdString(path)).dir();
128 if (dir.cdUp())
129 return dir.absolutePath().toStdString();
130 return File::GetUserPath(D_RIIVOLUTION_IDX);
131 }
132
133 void RiivolutionBootWidget::OpenXML()
134 {
135 const std::string& riivolution_dir = File::GetUserPath(D_RIIVOLUTION_IDX);
136 QStringList paths = QFileDialog::getOpenFileNames(
137 this, tr("Select Riivolution XML file"), QString::fromStdString(riivolution_dir),
138 QStringLiteral("%1 (*.xml);;%2 (*)").arg(tr("Riivolution XML files")).arg(tr("All Files")));
139 if (paths.isEmpty())
140 return;
141
142 for (const QString& path : paths)
143 {
144 std::string p = path.toStdString();
145 auto parsed = DiscIO::Riivolution::ParseFile(p);
146 if (!parsed)
147 {
148 ModalMessageBox::warning(
149 this, tr("Failed loading XML."),
150 tr("Did not recognize %1 as a valid Riivolution XML file.").arg(path));
151 continue;
152 }
153
154 if (!parsed->IsValidForGame(m_game_id, m_revision, m_disc_number))
155 {
156 ModalMessageBox::warning(
157 this, tr("Invalid game."),
158 tr("The patches in %1 are not for the selected game or game revision.").arg(path));
159 continue;
160 }
161
162 auto root = FindRoot(p);
163 const auto config = LoadConfigXML(root);
164 if (config)
165 DiscIO::Riivolution::ApplyConfigDefaults(&*parsed, *config);
166 MakeGUIForParsedFile(p, std::move(root), *parsed);
167 }
168 }
169
170 void RiivolutionBootWidget::MakeGUIForParsedFile(std::string path, std::string root,
171 DiscIO::Riivolution::Disc input_disc)
172 {
173 const size_t disc_index = m_discs.size();
174 const auto& disc =
175 m_discs.emplace_back(DiscWithRoot{std::move(input_disc), std::move(root), std::move(path)});
176
177 auto* disc_box = new QGroupBox(QFileInfo(QString::fromStdString(disc.path)).fileName());
178 auto* disc_layout = new QVBoxLayout();
179 disc_box->setLayout(disc_layout);
180
181 auto* xml_root_line_edit = new QLineEdit(QString::fromStdString(disc.root));
182 xml_root_line_edit->setReadOnly(true);
183 auto* xml_root_layout = new QHBoxLayout();
184 auto* xml_root_open = new QPushButton(tr("..."));
185 xml_root_layout->addWidget(new QLabel(tr("SD Root:")), 0);
186 xml_root_layout->addWidget(xml_root_line_edit, 0);
187 xml_root_layout->addWidget(xml_root_open, 0);
188 disc_layout->addLayout(xml_root_layout);
189 connect(xml_root_open, &QPushButton::clicked, this, [this, xml_root_line_edit, disc_index] {
190 QString dir = QDir::toNativeSeparators(QFileDialog::getExistingDirectory(
191 this, tr("Select the Virtual SD Card Root"), xml_root_line_edit->text()));
192 if (!dir.isEmpty())
193 {
194 xml_root_line_edit->setText(dir);
195 m_discs[disc_index].root = dir.toStdString();
196 }
197 });
198
199 for (size_t section_index = 0; section_index < disc.disc.m_sections.size(); ++section_index)
200 {
201 const auto& section = disc.disc.m_sections[section_index];
202 auto* group_box = new QGroupBox(QString::fromStdString(section.m_name));
203 auto* grid_layout = new QGridLayout();
204 group_box->setLayout(grid_layout);
205
206 int row = 0;
207 for (size_t option_index = 0; option_index < section.m_options.size(); ++option_index)
208 {
209 const auto& option = section.m_options[option_index];
210 auto* label = new QLabel(QString::fromStdString(option.m_name));
211 auto* selection = new QComboBox();
212 const GuiRiivolutionPatchIndex gui_disabled_index{disc_index, section_index, option_index, 0};
213 selection->addItem(tr("Disabled"), QVariant::fromValue(gui_disabled_index));
214 for (size_t choice_index = 0; choice_index < option.m_choices.size(); ++choice_index)
215 {
216 const auto& choice = option.m_choices[choice_index];
217 const GuiRiivolutionPatchIndex gui_index{disc_index, section_index, option_index,
218 choice_index + 1};
219 selection->addItem(QString::fromStdString(choice.m_name), QVariant::fromValue(gui_index));
220 }
221 if (option.m_selected_choice <= option.m_choices.size())
222 selection->setCurrentIndex(static_cast<int>(option.m_selected_choice));
223
224 connect(selection, &QComboBox::currentIndexChanged, this, [this, selection](int idx) {
225 const auto gui_index = selection->currentData().value<GuiRiivolutionPatchIndex>();
226 auto& selected_disc = m_discs[gui_index.m_disc_index].disc;
227 auto& selected_section = selected_disc.m_sections[gui_index.m_section_index];
228 auto& selected_option = selected_section.m_options[gui_index.m_option_index];
229 selected_option.m_selected_choice = static_cast<u32>(gui_index.m_choice_index);
230 });
231
232 grid_layout->addWidget(label, row, 0, 1, 1);
233 grid_layout->addWidget(selection, row, 1, 1, 1);
234 ++row;
235 }
236
237 disc_layout->addWidget(group_box);
238 }
239
240 m_patch_section_layout->addWidget(disc_box);
241 }
242
243 std::optional<DiscIO::Riivolution::Config>
244 RiivolutionBootWidget::LoadConfigXML(const std::string& root_directory)
245 {
246 // The way Riivolution stores settings only makes sense for standard game IDs.
247 if (!(m_game_id.size() == 4 || m_game_id.size() == 6))
248 return std::nullopt;
249
250 return DiscIO::Riivolution::ParseConfigFile(
251 fmt::format("{}/riivolution/config/{}.xml", root_directory, m_game_id.substr(0, 4)));
252 }
253
254 void RiivolutionBootWidget::SaveConfigXMLs()
255 {
256 if (!(m_game_id.size() == 4 || m_game_id.size() == 6))
257 return;
258
259 std::unordered_map<std::string, DiscIO::Riivolution::Config> map;
260 for (const auto& disc : m_discs)
261 {
262 auto config = map.try_emplace(disc.root);
263 auto& config_options = config.first->second.m_options;
264 for (const auto& section : disc.disc.m_sections)
265 {
266 for (const auto& option : section.m_options)
267 {
268 std::string id = option.m_id.empty() ? (section.m_name + option.m_name) : option.m_id;
269 config_options.emplace_back(
270 DiscIO::Riivolution::ConfigOption{std::move(id), option.m_selected_choice});
271 }
272 }
273 }
274
275 for (const auto& config : map)
276 {
277 DiscIO::Riivolution::WriteConfigFile(
278 fmt::format("{}/riivolution/config/{}.xml", config.first, m_game_id.substr(0, 4)),
279 config.second);
280 }
281 }
282
283 void RiivolutionBootWidget::BootGame()
284 {
285 SaveConfigXMLs();
286
287 m_patches.clear();
288 for (const auto& disc : m_discs)
289 {
290 auto patches = disc.disc.GeneratePatches(m_game_id);
291
292 // set the file loader for each patch
293 for (auto& patch : patches)
294 {
295 patch.m_file_data_loader = std::make_shared<DiscIO::Riivolution::FileDataLoaderHostFS>(
296 disc.root, disc.disc.m_xml_path, patch.m_root);
297 }
298
299 m_patches.insert(m_patches.end(), patches.begin(), patches.end());
300 }
301
302 m_should_boot = true;
303 close();
304 }
305
306 void RiivolutionBootWidget::SaveAsPreset()
307 {
308 DiscIO::GameModDescriptor descriptor;
309 descriptor.base_file = m_base_game_path;
310
311 DiscIO::GameModDescriptorRiivolution riivolution_descriptor;
312 for (const auto& disc : m_discs)
313 {
314 // filter out XMLs that don't actually contribute to the preset
315 auto patches = disc.disc.GeneratePatches(m_game_id);
316 if (patches.empty())
317 continue;
318
319 auto& descriptor_patch = riivolution_descriptor.patches.emplace_back();
320 descriptor_patch.xml = disc.path;
321 descriptor_patch.root = disc.root;
322 for (const auto& section : disc.disc.m_sections)
323 {
324 for (const auto& option : section.m_options)
325 {
326 auto& descriptor_option = descriptor_patch.options.emplace_back();
327 descriptor_option.section_name = section.m_name;
328 if (!option.m_id.empty())
329 descriptor_option.option_id = option.m_id;
330 else
331 descriptor_option.option_name = option.m_name;
332 descriptor_option.choice = option.m_selected_choice;
333 }
334 }
335 }
336
337 if (!riivolution_descriptor.patches.empty())
338 descriptor.riivolution = std::move(riivolution_descriptor);
339
340 QDir dir = QFileInfo(QString::fromStdString(m_base_game_path)).dir();
341 QString target_path = QFileDialog::getSaveFileName(this, tr("Save Preset"), dir.absolutePath(),
342 QStringLiteral("%1 (*.json);;%2 (*)")
343 .arg(tr("Dolphin Game Mod Preset"))
344 .arg(tr("All Files")));
345 if (target_path.isEmpty())
346 return;
347
348 descriptor.display_name = QFileInfo(target_path).fileName().toStdString();
349 auto dot = descriptor.display_name.rfind('.');
350 if (dot != std::string::npos)
351 descriptor.display_name = descriptor.display_name.substr(0, dot);
352 DiscIO::WriteGameModDescriptorFile(target_path.toStdString(), descriptor, true);
353 }