GameCubePane.cpp 31 KB

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