Debian Codegraph @

Source/Core/DolphinQt/Settings/GameCubePane.cpp

raw symbols SCIP
1 // Copyright 2018 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "DolphinQt/Settings/GameCubePane.h"
5
6 #include <QCheckBox>
7 #include <QComboBox>
8 #include <QFileDialog>
9 #include <QFileInfo>
10 #include <QFormLayout>
11 #include <QGridLayout>
12 #include <QGroupBox>
13 #include <QHBoxLayout>
14 #include <QInputDialog>
15 #include <QLabel>
16 #include <QLineEdit>
17 #include <QPushButton>
18 #include <QVBoxLayout>
19
20 #include <array>
21 #include <utility>
22
23 #include "Common/Assert.h"
24 #include "Common/CommonPaths.h"
25 #include "Common/Config/Config.h"
26 #include "Common/FileUtil.h"
27 #include "Common/MsgHandler.h"
28
29 #include "Core/Config/MainSettings.h"
30 #include "Core/ConfigManager.h"
31 #include "Core/Core.h"
32 #include "Core/HW/EXI/EXI.h"
33 #include "Core/HW/GCMemcard/GCMemcard.h"
34 #include "DolphinQt/Config/SettingsWindow.h"
35 #ifdef HAS_LIBMGBA
36 #include "Core/NetPlayServer.h"
37 #endif
38 #include "Core/System.h"
39
40 #include "DolphinQt/Config/ConfigControls/ConfigBool.h"
41 #include "DolphinQt/Config/ConfigControls/ConfigChoice.h"
42 #ifdef HAS_LIBMGBA
43 #include "DolphinQt/Config/ConfigControls/ConfigText.h"
44 #include "DolphinQt/Config/ConfigControls/ConfigUserPath.h"
45 #endif
46 #include "DolphinQt/Config/Mapping/MappingWindow.h"
47 #include "DolphinQt/GCMemcardManager.h"
48 #include "DolphinQt/QtUtils/DolphinFileDialog.h"
49 #include "DolphinQt/QtUtils/ModalMessageBox.h"
50 #include "DolphinQt/QtUtils/NonDefaultQPushButton.h"
51 #include "DolphinQt/QtUtils/SignalBlocking.h"
52 #include "DolphinQt/Settings.h"
53 #include "DolphinQt/Settings/BroadbandAdapterSettingsDialog.h"
54 #include "DolphinQt/Settings/TriforcePane.h"
55
56 constexpr std::initializer_list<ExpansionInterface::Slot> GUI_SLOTS = {
57 ExpansionInterface::Slot::A, ExpansionInterface::Slot::B, ExpansionInterface::Slot::SP1};
58
59 GameCubePane::GameCubePane(MainWindow* main_window)
60 {
61 CreateWidgets();
62 LoadSettings();
63 ConnectWidgets();
64
65 connect(this, &GameCubePane::ShowTriforceWindow, main_window, &MainWindow::ShowTriforceWindow);
66 }
67
68 void GameCubePane::CreateWidgets()
69 {
70 using ExpansionInterface::EXIDeviceType;
71
72 QVBoxLayout* layout = new QVBoxLayout(this);
73
74 // IPL Settings
75 QGroupBox* ipl_box = new QGroupBox(tr("IPL Settings"), this);
76 QVBoxLayout* ipl_box_layout = new QVBoxLayout(ipl_box);
77 ipl_box->setLayout(ipl_box_layout);
78
79 m_skip_main_menu = new ConfigBool(tr("Skip Main Menu"), Config::MAIN_SKIP_IPL);
80 ipl_box_layout->addWidget(m_skip_main_menu);
81
82 QFormLayout* ipl_language_layout = new QFormLayout;
83 ipl_language_layout->setFormAlignment(Qt::AlignLeft | Qt::AlignTop);
84 ipl_language_layout->setFieldGrowthPolicy(QFormLayout::AllNonFixedFieldsGrow);
85 ipl_box_layout->addLayout(ipl_language_layout);
86
87 const QStringList language_list{tr("English"), tr("German"), tr("French"),
88 tr("Spanish"), tr("Italian"), tr("Dutch")};
89
90 m_language_combo = new ConfigChoice(language_list, Config::MAIN_GC_LANGUAGE);
91 ipl_language_layout->addRow(tr("System Language:"), m_language_combo);
92
93 // Device Settings
94 QGroupBox* device_box = new QGroupBox(tr("Device Settings"), this);
95 QGridLayout* device_layout = new QGridLayout(device_box);
96 device_box->setLayout(device_layout);
97
98 for (ExpansionInterface::Slot slot : GUI_SLOTS)
99 {
100 m_slot_combos[slot] = new QComboBox(device_box);
101 m_slot_combos[slot]->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
102 m_slot_buttons[slot] = new NonDefaultQPushButton(tr("..."), device_box);
103 m_slot_buttons[slot]->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
104 }
105
106 for (ExpansionInterface::Slot slot : ExpansionInterface::MEMCARD_SLOTS)
107 {
108 m_memcard_path_layouts[slot] = new QHBoxLayout();
109 m_memcard_path_labels[slot] = new QLabel(tr("Memory Card Path:"));
110 m_memcard_paths[slot] = new QLineEdit();
111 m_memcard_path_layouts[slot]->addWidget(m_memcard_path_labels[slot]);
112 m_memcard_path_layouts[slot]->addWidget(m_memcard_paths[slot]);
113
114 m_agp_path_layouts[slot] = new QHBoxLayout();
115 m_agp_path_labels[slot] = new QLabel(tr("GBA Cartridge Path:"));
116 m_agp_paths[slot] = new QLineEdit();
117 m_agp_path_layouts[slot]->addWidget(m_agp_path_labels[slot]);
118 m_agp_path_layouts[slot]->addWidget(m_agp_paths[slot]);
119
120 m_gci_path_layouts[slot] = new QVBoxLayout();
121 m_gci_path_labels[slot] = new QLabel(tr("GCI Folder Path:"));
122 m_gci_override_labels[slot] =
123 new QLabel(tr("Warning: A GCI folder override path is currently configured for this slot. "
124 "Adjusting the GCI path here will have no effect."));
125 m_gci_override_labels[slot]->setHidden(true);
126 m_gci_override_labels[slot]->setWordWrap(true);
127 m_gci_paths[slot] = new QLineEdit();
128 auto* hlayout = new QHBoxLayout();
129 hlayout->addWidget(m_gci_path_labels[slot]);
130 hlayout->addWidget(m_gci_paths[slot]);
131 m_gci_path_layouts[slot]->addWidget(m_gci_override_labels[slot]);
132 m_gci_path_layouts[slot]->addLayout(hlayout);
133 }
134
135 // Add slot devices
136 for (const auto device : {EXIDeviceType::None, EXIDeviceType::Dummy, EXIDeviceType::MemoryCard,
137 EXIDeviceType::MemoryCardFolder, EXIDeviceType::Gecko,
138 EXIDeviceType::AGP, EXIDeviceType::Microphone})
139 {
140 const QString name = tr(fmt::format("{:n}", device).c_str());
141 const int value = static_cast<int>(device);
142 m_slot_combos[ExpansionInterface::Slot::A]->addItem(name, value);
143 m_slot_combos[ExpansionInterface::Slot::B]->addItem(name, value);
144 }
145
146 // Add SP1 devices
147 for (const auto device : {
148 EXIDeviceType::None,
149 EXIDeviceType::Dummy,
150 EXIDeviceType::Ethernet,
151 EXIDeviceType::EthernetXLink,
152 EXIDeviceType::EthernetTapServer,
153 EXIDeviceType::EthernetBuiltIn,
154 #ifdef HAVE_CPPIPC
155 EXIDeviceType::EthernetIPC,
156 #endif
157 EXIDeviceType::ModemTapServer,
158 EXIDeviceType::Baseboard,
159 })
160 {
161 m_slot_combos[ExpansionInterface::Slot::SP1]->addItem(tr(fmt::format("{:n}", device).c_str()),
162 static_cast<int>(device));
163 }
164
165 {
166 int row = 0;
167 device_layout->addWidget(new QLabel(tr("Slot A:")), row, 0);
168 device_layout->addWidget(m_slot_combos[ExpansionInterface::Slot::A], row, 1);
169 device_layout->addWidget(m_slot_buttons[ExpansionInterface::Slot::A], row, 2);
170
171 ++row;
172 device_layout->addLayout(m_memcard_path_layouts[ExpansionInterface::Slot::A], row, 0, 1, 3);
173
174 ++row;
175 device_layout->addLayout(m_agp_path_layouts[ExpansionInterface::Slot::A], row, 0, 1, 3);
176
177 ++row;
178 device_layout->addLayout(m_gci_path_layouts[ExpansionInterface::Slot::A], row, 0, 1, 3);
179
180 ++row;
181 device_layout->addWidget(new QLabel(tr("Slot B:")), row, 0);
182 device_layout->addWidget(m_slot_combos[ExpansionInterface::Slot::B], row, 1);
183 device_layout->addWidget(m_slot_buttons[ExpansionInterface::Slot::B], row, 2);
184
185 ++row;
186 device_layout->addLayout(m_memcard_path_layouts[ExpansionInterface::Slot::B], row, 0, 1, 3);
187
188 ++row;
189 device_layout->addLayout(m_agp_path_layouts[ExpansionInterface::Slot::B], row, 0, 1, 3);
190
191 ++row;
192 device_layout->addLayout(m_gci_path_layouts[ExpansionInterface::Slot::B], row, 0, 1, 3);
193
194 ++row;
195 device_layout->addWidget(new QLabel(tr("SP1:")), row, 0);
196 device_layout->addWidget(m_slot_combos[ExpansionInterface::Slot::SP1], row, 1);
197 device_layout->addWidget(m_slot_buttons[ExpansionInterface::Slot::SP1], row, 2);
198 }
199
200 #ifdef HAS_LIBMGBA
201 // GBA Settings
202 auto* gba_box = new QGroupBox(tr("GBA Settings"), this);
203 auto* gba_layout = new QGridLayout(gba_box);
204 gba_box->setLayout(gba_layout);
205 int gba_row = 0;
206
207 m_gba_bios_edit = new ConfigUserPath(F_GBABIOS_IDX, Config::MAIN_GBA_BIOS_PATH);
208 m_gba_browse_bios = new NonDefaultQPushButton(QStringLiteral("..."));
209 gba_layout->addWidget(new QLabel(tr("BIOS:")), gba_row, 0);
210 gba_layout->addWidget(m_gba_bios_edit, gba_row, 1);
211 gba_layout->addWidget(m_gba_browse_bios, gba_row, 2);
212 gba_row++;
213
214 for (size_t i = 0; i < m_gba_rom_edits.size(); ++i)
215 {
216 m_gba_rom_edits[i] = new ConfigText(Config::MAIN_GBA_ROM_PATHS[i]);
217 m_gba_browse_roms[i] = new NonDefaultQPushButton(QStringLiteral("..."));
218 auto* const label =
219 new QLabel((i == Config::GBPLAYER_GBA_INDEX) ? tr("Game Boy Player ROM:") :
220 tr("Port %1 ROM:").arg(i + 1));
221 gba_layout->addWidget(label, gba_row, 0);
222 gba_layout->addWidget(m_gba_rom_edits[i], gba_row, 1);
223 gba_layout->addWidget(m_gba_browse_roms[i], gba_row, 2);
224 gba_row++;
225 }
226
227 m_gba_save_rom_path =
228 new ConfigBool(tr("Save in Same Directory as the ROM"), Config::MAIN_GBA_SAVES_IN_ROM_PATH);
229 gba_layout->addWidget(m_gba_save_rom_path, gba_row, 0, 1, -1);
230 gba_row++;
231
232 m_gba_saves_edit = new ConfigUserPath(D_GBASAVES_IDX, Config::MAIN_GBA_SAVES_PATH);
233 m_gba_browse_saves = new NonDefaultQPushButton(QStringLiteral("..."));
234 gba_layout->addWidget(new QLabel(tr("Saves:")), gba_row, 0);
235 gba_layout->addWidget(m_gba_saves_edit, gba_row, 1);
236 gba_layout->addWidget(m_gba_browse_saves, gba_row, 2);
237 gba_row++;
238 #endif
239
240 layout->addWidget(ipl_box);
241 layout->addWidget(device_box);
242 #ifdef HAS_LIBMGBA
243 layout->addWidget(gba_box);
244 #endif
245
246 layout->addStretch();
247
248 setLayout(layout);
249 }
250
251 void GameCubePane::ConnectWidgets()
252 {
253 // Device Settings
254 for (ExpansionInterface::Slot slot : GUI_SLOTS)
255 {
256 connect(m_slot_combos[slot], &QComboBox::currentIndexChanged, this,
257 [this, slot] { UpdateButton(slot); });
258 connect(m_slot_combos[slot], &QComboBox::currentIndexChanged, this,
259 &GameCubePane::SaveSettings);
260 connect(m_slot_buttons[slot], &QPushButton::clicked, [this, slot] { OnConfigPressed(slot); });
261 }
262
263 for (ExpansionInterface::Slot slot : ExpansionInterface::MEMCARD_SLOTS)
264 {
265 connect(m_memcard_paths[slot], &QLineEdit::editingFinished, [this, slot] {
266 // revert path change on failure
267 if (!SetMemcard(slot, m_memcard_paths[slot]->text()))
268 LoadSettings();
269 });
270 connect(m_agp_paths[slot], &QLineEdit::editingFinished,
271 [this, slot] { SetAGPRom(slot, m_agp_paths[slot]->text()); });
272 connect(m_gci_paths[slot], &QLineEdit::editingFinished, [this, slot] {
273 // revert path change on failure
274 if (!SetGCIFolder(slot, m_gci_paths[slot]->text()))
275 LoadSettings();
276 });
277 }
278
279 #ifdef HAS_LIBMGBA
280 // GBA Settings
281 connect(m_gba_browse_bios, &QPushButton::clicked, this, &GameCubePane::BrowseGBABios);
282 #if QT_VERSION >= QT_VERSION_CHECK(6, 7, 0)
283 connect(m_gba_save_rom_path, &QCheckBox::checkStateChanged, this,
284 &GameCubePane::SaveRomPathChanged);
285 #else
286 connect(m_gba_save_rom_path, &QCheckBox::stateChanged, this, &GameCubePane::SaveRomPathChanged);
287 #endif
288 connect(m_gba_browse_saves, &QPushButton::clicked, this, &GameCubePane::BrowseGBASaves);
289 for (size_t i = 0; i < m_gba_browse_roms.size(); ++i)
290 {
291 connect(m_gba_browse_roms[i], &QPushButton::clicked, this, [this, i] { BrowseGBARom(i); });
292 }
293 #endif
294
295 // Emulation State
296 connect(&Settings::Instance(), &Settings::EmulationStateChanged, this,
297 &GameCubePane::OnEmulationStateChanged);
298 OnEmulationStateChanged();
299 }
300
301 void GameCubePane::OnEmulationStateChanged()
302 {
303 #ifdef HAS_LIBMGBA
304 bool gba_enabled = !NetPlay::IsNetPlayRunning();
305 m_gba_bios_edit->setEnabled(gba_enabled);
306 m_gba_browse_bios->setEnabled(gba_enabled);
307 m_gba_save_rom_path->setEnabled(gba_enabled);
308 m_gba_saves_edit->setEnabled(gba_enabled);
309 m_gba_browse_saves->setEnabled(gba_enabled);
310 for (size_t i = 0; i < m_gba_browse_roms.size(); ++i)
311 {
312 m_gba_rom_edits[i]->setEnabled(gba_enabled);
313 m_gba_browse_roms[i]->setEnabled(gba_enabled);
314 }
315 #endif
316 }
317
318 void GameCubePane::UpdateButton(ExpansionInterface::Slot slot)
319 {
320 const auto device =
321 static_cast<ExpansionInterface::EXIDeviceType>(m_slot_combos[slot]->currentData().toInt());
322 bool has_config = false;
323
324 switch (slot)
325 {
326 case ExpansionInterface::Slot::A:
327 case ExpansionInterface::Slot::B:
328 {
329 has_config = (device == ExpansionInterface::EXIDeviceType::MemoryCard ||
330 device == ExpansionInterface::EXIDeviceType::MemoryCardFolder ||
331 device == ExpansionInterface::EXIDeviceType::AGP ||
332 device == ExpansionInterface::EXIDeviceType::Microphone);
333 const bool hide_memory_card = device != ExpansionInterface::EXIDeviceType::MemoryCard ||
334 Config::IsDefaultMemcardPathConfigured(slot);
335 const bool hide_gci_path = device != ExpansionInterface::EXIDeviceType::MemoryCardFolder ||
336 Config::IsDefaultGCIFolderPathConfigured(slot);
337 m_memcard_path_labels[slot]->setHidden(hide_memory_card);
338 m_memcard_paths[slot]->setHidden(hide_memory_card);
339 m_agp_path_labels[slot]->setHidden(device != ExpansionInterface::EXIDeviceType::AGP);
340 m_agp_paths[slot]->setHidden(device != ExpansionInterface::EXIDeviceType::AGP);
341 m_gci_path_labels[slot]->setHidden(hide_gci_path);
342 m_gci_paths[slot]->setHidden(hide_gci_path);
343
344 // In the years before we introduced the GCI folder configuration paths it has become somewhat
345 // popular to use the GCI override path instead. Check if this is the case and display a warning
346 // if it is set, so users aren't confused why configuring the normal GCI path doesn't do
347 // anything.
348 m_gci_override_labels[slot]->setHidden(
349 device != ExpansionInterface::EXIDeviceType::MemoryCardFolder ||
350 Config::Get(Config::GetInfoForGCIPathOverride(slot)).empty());
351
352 break;
353 }
354 case ExpansionInterface::Slot::SP1:
355 has_config = device == ExpansionInterface::EXIDeviceType::Ethernet ||
356 device == ExpansionInterface::EXIDeviceType::EthernetXLink ||
357 device == ExpansionInterface::EXIDeviceType::EthernetTapServer ||
358 device == ExpansionInterface::EXIDeviceType::EthernetBuiltIn ||
359 device == ExpansionInterface::EXIDeviceType::ModemTapServer ||
360 device == ExpansionInterface::EXIDeviceType::Baseboard;
361 break;
362 case ExpansionInterface::Slot::SP2:
363 has_config = false;
364 break;
365 }
366
367 m_slot_buttons[slot]->setEnabled(has_config);
368 }
369
370 void GameCubePane::OnConfigPressed(ExpansionInterface::Slot slot)
371 {
372 const ExpansionInterface::EXIDeviceType device =
373 static_cast<ExpansionInterface::EXIDeviceType>(m_slot_combos[slot]->currentData().toInt());
374
375 switch (device)
376 {
377 case ExpansionInterface::EXIDeviceType::MemoryCard:
378 BrowseMemcard(slot);
379 return;
380 case ExpansionInterface::EXIDeviceType::MemoryCardFolder:
381 BrowseGCIFolder(slot);
382 return;
383 case ExpansionInterface::EXIDeviceType::AGP:
384 BrowseAGPRom(slot);
385 return;
386 case ExpansionInterface::EXIDeviceType::Microphone:
387 {
388 // TODO: convert MappingWindow to use Slot?
389 MappingWindow dialog(this, MappingWindow::Type::MAPPING_GC_MICROPHONE, static_cast<int>(slot));
390 dialog.exec();
391 return;
392 }
393 case ExpansionInterface::EXIDeviceType::Ethernet:
394 {
395 BroadbandAdapterSettingsDialog dialog(this, BroadbandAdapterSettingsDialog::Type::Ethernet);
396 dialog.exec();
397 return;
398 }
399 case ExpansionInterface::EXIDeviceType::EthernetXLink:
400 {
401 BroadbandAdapterSettingsDialog dialog(this, BroadbandAdapterSettingsDialog::Type::XLinkKai);
402 dialog.exec();
403 return;
404 }
405 case ExpansionInterface::EXIDeviceType::EthernetTapServer:
406 {
407 BroadbandAdapterSettingsDialog dialog(this, BroadbandAdapterSettingsDialog::Type::TapServer);
408 dialog.exec();
409 return;
410 }
411 case ExpansionInterface::EXIDeviceType::ModemTapServer:
412 {
413 BroadbandAdapterSettingsDialog dialog(this,
414 BroadbandAdapterSettingsDialog::Type::ModemTapServer);
415 dialog.exec();
416 return;
417 }
418 case ExpansionInterface::EXIDeviceType::EthernetBuiltIn:
419 {
420 BroadbandAdapterSettingsDialog dialog(this, BroadbandAdapterSettingsDialog::Type::BuiltIn);
421 dialog.exec();
422 return;
423 }
424 case ExpansionInterface::EXIDeviceType::Baseboard:
425 {
426 ShowTriforceWindow();
427 return;
428 }
429 default:
430 PanicAlertFmt("Unknown settings pressed for {}", device);
431 return;
432 }
433 }
434
435 void GameCubePane::BrowseMemcard(ExpansionInterface::Slot slot)
436 {
437 ASSERT(ExpansionInterface::IsMemcardSlot(slot));
438
439 const QString filename = DolphinFileDialog::getSaveFileName(
440 this, tr("Choose a File to Open or Create"),
441 QString::fromStdString(File::GetUserPath(D_GCUSER_IDX)),
442 tr("GameCube Memory Cards (*.raw *.gcp)"), nullptr, QFileDialog::DontConfirmOverwrite);
443
444 if (!filename.isEmpty())
445 SetMemcard(slot, filename);
446 }
447
448 bool GameCubePane::SetMemcard(ExpansionInterface::Slot slot, const QString& filename)
449 {
450 if (filename.isEmpty())
451 {
452 ModalMessageBox::critical(this, tr("Error"), tr("Cannot set memory card to an empty path."));
453 return false;
454 }
455
456 const std::string raw_path =
457 WithUnifiedPathSeparators(QFileInfo(filename).absoluteFilePath().toStdString());
458
459 // Figure out if the user selected a card that has a valid region specifier in the filename.
460 const std::string jp_path = Config::GetMemcardPath(raw_path, slot, DiscIO::Region::NTSC_J);
461 const std::string us_path = Config::GetMemcardPath(raw_path, slot, DiscIO::Region::NTSC_U);
462 const std::string eu_path = Config::GetMemcardPath(raw_path, slot, DiscIO::Region::PAL);
463 const std::string dv_path = Config::GetMemcardPath(raw_path, slot, DiscIO::Region::DEV);
464 const bool raw_path_valid =
465 raw_path == jp_path || raw_path == us_path || raw_path == eu_path || raw_path == dv_path;
466
467 if (!raw_path_valid)
468 {
469 // TODO: We could try to autodetect the card region here and offer automatic renaming.
470 ModalMessageBox::critical(
471 this, tr("Error"),
472 tr("The filename %1 does not conform to Dolphin's region code format "
473 "for memory cards. Please rename this file to either %2, %3, %4, or "
474 "%5, matching the region of the save files that are on it.")
475 .arg(QString::fromStdString(PathToFileName(raw_path)))
476 .arg(QString::fromStdString(PathToFileName(us_path)))
477 .arg(QString::fromStdString(PathToFileName(eu_path)))
478 .arg(QString::fromStdString(PathToFileName(jp_path)))
479 .arg(QString::fromStdString(PathToFileName(dv_path))));
480 return false;
481 }
482
483 // Memcard validity checks
484 for (const std::string& path : {jp_path, us_path, eu_path})
485 {
486 if (File::Exists(path))
487 {
488 auto [error_code, mc] = Memcard::GCMemcard::Open(path);
489
490 if (error_code.HasCriticalErrors() || !mc || !mc->IsValid())
491 {
492 ModalMessageBox::critical(
493 this, tr("Error"),
494 tr("The file\n%1\nis either corrupted or not a GameCube memory card file.\n%2")
495 .arg(QString::fromStdString(path))
496 .arg(GCMemcardManager::GetErrorMessagesForErrorCode(error_code)));
497 return false;
498 }
499 }
500 }
501
502 // Check if the other slot has the same memory card configured and refuse to use this card if so.
503 // The EU path is compared here, but it doesn't actually matter which one we compare since they
504 // follow a known pattern, so if the EU path matches the other match too and vice-versa.
505 for (ExpansionInterface::Slot other_slot : ExpansionInterface::MEMCARD_SLOTS)
506 {
507 if (other_slot == slot)
508 continue;
509
510 const std::string other_eu_path = Config::GetMemcardPath(other_slot, DiscIO::Region::PAL);
511 if (eu_path == other_eu_path)
512 {
513 ModalMessageBox::critical(
514 this, tr("Error"),
515 tr("The same file can't be used in multiple slots; it is already used by %1.")
516 .arg(QString::fromStdString(fmt::to_string(other_slot))));
517 return false;
518 }
519 }
520
521 const std::string old_eu_path = Config::GetMemcardPath(slot, DiscIO::Region::PAL);
522 Config::SetBase(Config::GetInfoForMemcardPath(slot), raw_path);
523
524 auto& system = Core::System::GetInstance();
525 if (Core::IsRunning(system))
526 {
527 // If emulation is running and the new card is different from the old one, notify the system to
528 // eject the old and insert the new card.
529 // TODO: This should probably done by a config change callback instead.
530 if (eu_path != old_eu_path)
531 {
532 // ChangeDevice unplugs the device for 1 second, which means that games should notice that
533 // the path has changed and thus the memory card contents have changed
534 system.GetExpansionInterface().ChangeDevice(slot,
535 ExpansionInterface::EXIDeviceType::MemoryCard);
536 }
537 }
538
539 LoadSettings();
540 return true;
541 }
542
543 void GameCubePane::BrowseGCIFolder(ExpansionInterface::Slot slot)
544 {
545 ASSERT(ExpansionInterface::IsMemcardSlot(slot));
546
547 const QString path = DolphinFileDialog::getExistingDirectory(
548 this, tr("Choose GCI Base Folder"), QString::fromStdString(File::GetUserPath(D_GCUSER_IDX)));
549
550 if (!path.isEmpty())
551 SetGCIFolder(slot, path);
552 }
553
554 bool GameCubePane::SetGCIFolder(ExpansionInterface::Slot slot, const QString& path)
555 {
556 if (path.isEmpty())
557 {
558 ModalMessageBox::critical(this, tr("Error"), tr("Cannot set GCI folder to an empty path."));
559 return false;
560 }
561
562 std::string raw_path =
563 WithUnifiedPathSeparators(QFileInfo(path).absoluteFilePath().toStdString());
564 while (raw_path.ends_with('/'))
565 raw_path.pop_back();
566
567 // The user might be attempting to reset this path to its default, check for this.
568 const std::string default_jp_path = Config::GetGCIFolderPath("", slot, DiscIO::Region::NTSC_J);
569 const std::string default_us_path = Config::GetGCIFolderPath("", slot, DiscIO::Region::NTSC_U);
570 const std::string default_eu_path = Config::GetGCIFolderPath("", slot, DiscIO::Region::PAL);
571 const std::string default_dv_path = Config::GetGCIFolderPath("", slot, DiscIO::Region::DEV);
572 const bool is_default_path = raw_path == default_jp_path || raw_path == default_us_path ||
573 raw_path == default_eu_path || raw_path == default_dv_path;
574
575 bool path_changed;
576 if (is_default_path)
577 {
578 // Reset to default.
579 // Note that this does not need to check if the same card is in the other slot, because that's
580 // impossible given our constraints for folder names.
581 raw_path = "";
582 path_changed = !Config::IsDefaultGCIFolderPathConfigured(slot);
583 }
584 else
585 {
586 // Figure out if the user selected a folder that ends in a valid region specifier.
587 const std::string jp_path = Config::GetGCIFolderPath(raw_path, slot, DiscIO::Region::NTSC_J);
588 const std::string us_path = Config::GetGCIFolderPath(raw_path, slot, DiscIO::Region::NTSC_U);
589 const std::string eu_path = Config::GetGCIFolderPath(raw_path, slot, DiscIO::Region::PAL);
590 const std::string dv_path = Config::GetGCIFolderPath(raw_path, slot, DiscIO::Region::DEV);
591 const bool raw_path_valid =
592 raw_path == jp_path || raw_path == us_path || raw_path == eu_path || raw_path == dv_path;
593
594 if (!raw_path_valid)
595 {
596 // TODO: We could try to autodetect the card region here and offer automatic renaming.
597 ModalMessageBox::critical(
598 this, tr("Error"),
599 tr("The folder %1 does not conform to Dolphin's region code format "
600 "for GCI folders. Please rename this folder to either %2, %3, %4, or "
601 "%5, matching the region of the save files that are in it.")
602 .arg(QString::fromStdString(PathToFileName(raw_path)))
603 .arg(QString::fromStdString(PathToFileName(us_path)))
604 .arg(QString::fromStdString(PathToFileName(eu_path)))
605 .arg(QString::fromStdString(PathToFileName(jp_path)))
606 .arg(QString::fromStdString(PathToFileName(dv_path))));
607 return false;
608 }
609
610 // Check if the other slot has the same folder configured and refuse to use this folder if so.
611 // The EU path is compared here, but it doesn't actually matter which one we compare since they
612 // follow a known pattern, so if the EU path matches the other match too and vice-versa.
613 for (ExpansionInterface::Slot other_slot : ExpansionInterface::MEMCARD_SLOTS)
614 {
615 if (other_slot == slot)
616 continue;
617
618 const std::string other_eu_path = Config::GetGCIFolderPath(other_slot, DiscIO::Region::PAL);
619 if (eu_path == other_eu_path)
620 {
621 ModalMessageBox::critical(
622 this, tr("Error"),
623 tr("The same folder can't be used in multiple slots; it is already used by %1.")
624 .arg(QString::fromStdString(fmt::to_string(other_slot))));
625 return false;
626 }
627 }
628
629 path_changed = eu_path != Config::GetGCIFolderPath(slot, DiscIO::Region::PAL);
630 }
631
632 Config::SetBase(Config::GetInfoForGCIPath(slot), raw_path);
633
634 auto& system = Core::System::GetInstance();
635 if (Core::IsRunning(system))
636 {
637 // If emulation is running and the new card is different from the old one, notify the system to
638 // eject the old and insert the new card.
639 // TODO: This should probably be done by a config change callback instead.
640 if (path_changed)
641 {
642 // ChangeDevice unplugs the device for 1 second, which means that games should notice that
643 // the path has changed and thus the memory card contents have changed
644 system.GetExpansionInterface().ChangeDevice(
645 slot, ExpansionInterface::EXIDeviceType::MemoryCardFolder);
646 }
647 }
648
649 LoadSettings();
650 return true;
651 }
652
653 void GameCubePane::BrowseAGPRom(ExpansionInterface::Slot slot)
654 {
655 ASSERT(ExpansionInterface::IsMemcardSlot(slot));
656
657 QString filename = DolphinFileDialog::getSaveFileName(
658 this, tr("Choose a File to Open"), QString::fromStdString(File::GetUserPath(D_GCUSER_IDX)),
659 tr("Game Boy Advance Carts (*.gba)"), nullptr, QFileDialog::DontConfirmOverwrite);
660
661 if (!filename.isEmpty())
662 SetAGPRom(slot, filename);
663 }
664
665 void GameCubePane::SetAGPRom(ExpansionInterface::Slot slot, const QString& filename)
666 {
667 QString path_abs = filename.isEmpty() ? QString() : QFileInfo(filename).absoluteFilePath();
668
669 QString path_old =
670 QFileInfo(QString::fromStdString(Config::Get(Config::GetInfoForAGPCartPath(slot))))
671 .absoluteFilePath();
672
673 Config::SetBase(Config::GetInfoForAGPCartPath(slot), path_abs.toStdString());
674
675 auto& system = Core::System::GetInstance();
676 if (Core::IsRunning(system) && path_abs != path_old)
677 {
678 // ChangeDevice unplugs the device for 1 second. For an actual AGP, you can remove the
679 // cartridge without unplugging it, and it's not clear if the AGP software actually notices
680 // that it's been unplugged or the cartridge has changed, but this was done for memcards so
681 // we might as well do it for the AGP too.
682 system.GetExpansionInterface().ChangeDevice(slot, ExpansionInterface::EXIDeviceType::AGP);
683 }
684
685 LoadSettings();
686 }
687
688 #ifdef HAS_LIBMGBA
689
690 void GameCubePane::BrowseGBABios()
691 {
692 QString file = QDir::toNativeSeparators(DolphinFileDialog::getOpenFileName(
693 this, tr("Select GBA BIOS"), QString::fromStdString(Config::Get(Config::MAIN_GBA_BIOS_PATH)),
694 tr("All Files (*)")));
695 if (!file.isEmpty())
696 m_gba_bios_edit->SetTextAndUpdate(file);
697 }
698
699 void GameCubePane::BrowseGBARom(size_t index)
700 {
701 QString file = QString::fromStdString(GetOpenGBARom({}));
702 if (!file.isEmpty())
703 m_gba_rom_edits[index]->SetTextAndUpdate(file);
704 }
705
706 void GameCubePane::SaveRomPathChanged()
707 {
708 m_gba_saves_edit->setEnabled(!m_gba_save_rom_path->isChecked());
709 m_gba_browse_saves->setEnabled(!m_gba_save_rom_path->isChecked());
710 }
711
712 void GameCubePane::BrowseGBASaves()
713 {
714 QString dir = QDir::toNativeSeparators(DolphinFileDialog::getExistingDirectory(
715 this, tr("Select GBA Saves Path"),
716 QString::fromStdString(Config::Get(Config::MAIN_GBA_SAVES_PATH))));
717 if (!dir.isEmpty())
718 {
719 m_gba_saves_edit->setText(dir);
720 SaveSettings();
721 }
722 }
723
724 #endif // HAS_LIBMGBA
725
726 void GameCubePane::LoadSettings()
727 {
728 bool have_menu = false;
729
730 for (const std::string dir : {USA_DIR, JAP_DIR, EUR_DIR})
731 {
732 const auto path = DIR_SEP + dir + DIR_SEP GC_IPL;
733 if (File::Exists(File::GetUserPath(D_GCUSER_IDX) + path) ||
734 File::Exists(File::GetSysDirectory() + GC_SYS_DIR + path))
735 {
736 have_menu = true;
737 break;
738 }
739 }
740
741 m_skip_main_menu->setEnabled(have_menu || !m_skip_main_menu->isChecked());
742 m_skip_main_menu->SetDescription(have_menu ? QString{} : tr("Put IPL ROMs in User/GC/<region>."));
743
744 // Device Settings
745 for (ExpansionInterface::Slot slot : GUI_SLOTS)
746 {
747 const ExpansionInterface::EXIDeviceType exi_device =
748 Config::Get(Config::GetInfoForEXIDevice(slot));
749 SignalBlocking(m_slot_combos[slot])
750 ->setCurrentIndex(m_slot_combos[slot]->findData(static_cast<int>(exi_device)));
751 UpdateButton(slot);
752 }
753
754 for (ExpansionInterface::Slot slot : ExpansionInterface::MEMCARD_SLOTS)
755 {
756 SignalBlocking(m_memcard_paths[slot])
757 ->setText(QString::fromStdString(Config::GetMemcardPath(slot, std::nullopt)));
758 SignalBlocking(m_agp_paths[slot])
759 ->setText(QString::fromStdString(Config::Get(Config::GetInfoForAGPCartPath(slot))));
760 SignalBlocking(m_gci_paths[slot])
761 ->setText(QString::fromStdString(Config::GetGCIFolderPath(slot, std::nullopt)));
762 }
763 }
764
765 void GameCubePane::SaveSettings()
766 {
767 Config::ConfigChangeCallbackGuard config_guard;
768
769 auto& system = Core::System::GetInstance();
770 // Device Settings
771 for (ExpansionInterface::Slot slot : GUI_SLOTS)
772 {
773 const auto dev =
774 static_cast<ExpansionInterface::EXIDeviceType>(m_slot_combos[slot]->currentData().toInt());
775 const ExpansionInterface::EXIDeviceType current_exi_device =
776 Config::Get(Config::GetInfoForEXIDevice(slot));
777
778 if (Core::IsRunning(system) && current_exi_device != dev)
779 {
780 system.GetExpansionInterface().ChangeDevice(slot, dev);
781 }
782
783 Config::SetBaseOrCurrent(Config::GetInfoForEXIDevice(slot), dev);
784 }
785
786 #ifdef HAS_LIBMGBA
787 // GBA Settings
788 if (!NetPlay::IsNetPlayRunning())
789 {
790 auto server = Settings::Instance().GetNetPlayServer();
791 if (server)
792 server->SetGBAConfig(server->GetGBAConfig(), true);
793 }
794 #endif
795
796 LoadSettings();
797 }
798
799 std::string GameCubePane::GetOpenGBARom(std::string_view title)
800 {
801 QString caption = tr("Select GBA ROM");
802 if (!title.empty())
803 caption += QStringLiteral(": %1").arg(QString::fromStdString(std::string(title)));
804 return QDir::toNativeSeparators(
805 DolphinFileDialog::getOpenFileName(
806 nullptr, caption, QString(),
807 tr("Game Boy Advance ROMs (*.gba *.gbc *.gb *.7z *.zip *.agb *.mb *.rom *.bin);;"
808 "All Files (*)")))
809 .toStdString();
810 }