CheatSearchFactoryWidget.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2021 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "DolphinQt/CheatSearchFactoryWidget.h"
  4. #include <string>
  5. #include <vector>
  6. #include <QButtonGroup>
  7. #include <QCheckBox>
  8. #include <QComboBox>
  9. #include <QGroupBox>
  10. #include <QLabel>
  11. #include <QLineEdit>
  12. #include <QPushButton>
  13. #include <QRadioButton>
  14. #include <QVBoxLayout>
  15. #include "Common/StringUtil.h"
  16. #include "Core/CheatSearch.h"
  17. #include "Core/Config/MainSettings.h"
  18. #include "Core/Core.h"
  19. #include "Core/HW/Memmap.h"
  20. #include "Core/PowerPC/MMU.h"
  21. #include "Core/System.h"
  22. #include "DolphinQt/QtUtils/ModalMessageBox.h"
  23. #include "DolphinQt/QtUtils/NonDefaultQPushButton.h"
  24. #include "DolphinQt/QtUtils/WrapInScrollArea.h"
  25. CheatSearchFactoryWidget::CheatSearchFactoryWidget()
  26. {
  27. CreateWidgets();
  28. ConnectWidgets();
  29. RefreshGui();
  30. }
  31. CheatSearchFactoryWidget::~CheatSearchFactoryWidget() = default;
  32. Q_DECLARE_METATYPE(Cheats::DataType);
  33. void CheatSearchFactoryWidget::CreateWidgets()
  34. {
  35. auto* layout = new QVBoxLayout();
  36. auto* address_space_group = new QGroupBox(tr("Address Space"));
  37. auto* address_space_layout = new QVBoxLayout();
  38. address_space_group->setLayout(address_space_layout);
  39. m_standard_address_space = new QRadioButton(tr("Typical GameCube/Wii Address Space"));
  40. m_standard_address_space->setChecked(true);
  41. m_custom_address_space = new QRadioButton(tr("Custom Address Space"));
  42. QLabel* label_standard_address_space =
  43. new QLabel(tr("Sets up the search using standard MEM1 and (on Wii) MEM2 mappings in virtual "
  44. "address space. This will work for the vast majority of games."));
  45. label_standard_address_space->setWordWrap(true);
  46. auto* custom_address_space_layout = new QVBoxLayout();
  47. custom_address_space_layout->setContentsMargins(6, 6, 6, 6);
  48. auto* custom_address_space_button_group = new QButtonGroup(this);
  49. m_custom_virtual_address_space = new QRadioButton(tr("Use virtual addresses when possible"));
  50. m_custom_virtual_address_space->setChecked(true);
  51. m_custom_physical_address_space = new QRadioButton(tr("Use physical addresses"));
  52. m_custom_effective_address_space =
  53. new QRadioButton(tr("Use memory mapper configuration at time of scan"));
  54. custom_address_space_button_group->addButton(m_custom_virtual_address_space);
  55. custom_address_space_button_group->addButton(m_custom_physical_address_space);
  56. custom_address_space_button_group->addButton(m_custom_effective_address_space);
  57. custom_address_space_layout->addWidget(m_custom_virtual_address_space);
  58. custom_address_space_layout->addWidget(m_custom_physical_address_space);
  59. custom_address_space_layout->addWidget(m_custom_effective_address_space);
  60. QLabel* label_range_start = new QLabel(tr("Range Start: "));
  61. m_custom_address_start = new QLineEdit(QStringLiteral("0x80000000"));
  62. QLabel* label_range_end = new QLabel(tr("Range End: "));
  63. m_custom_address_end = new QLineEdit(QStringLiteral("0x81800000"));
  64. custom_address_space_layout->addWidget(label_range_start);
  65. custom_address_space_layout->addWidget(m_custom_address_start);
  66. custom_address_space_layout->addWidget(label_range_end);
  67. custom_address_space_layout->addWidget(m_custom_address_end);
  68. address_space_layout->addWidget(m_standard_address_space);
  69. address_space_layout->addWidget(label_standard_address_space);
  70. address_space_layout->addWidget(m_custom_address_space);
  71. address_space_layout->addLayout(custom_address_space_layout);
  72. layout->addWidget(address_space_group);
  73. auto* data_type_group = new QGroupBox(tr("Data Type"));
  74. auto* data_type_layout = new QVBoxLayout();
  75. data_type_group->setLayout(data_type_layout);
  76. m_data_type_dropdown = new QComboBox();
  77. m_data_type_dropdown->addItem(tr("8-bit Unsigned Integer"),
  78. QVariant::fromValue(Cheats::DataType::U8));
  79. m_data_type_dropdown->addItem(tr("16-bit Unsigned Integer"),
  80. QVariant::fromValue(Cheats::DataType::U16));
  81. m_data_type_dropdown->addItem(tr("32-bit Unsigned Integer"),
  82. QVariant::fromValue(Cheats::DataType::U32));
  83. m_data_type_dropdown->addItem(tr("64-bit Unsigned Integer"),
  84. QVariant::fromValue(Cheats::DataType::U64));
  85. m_data_type_dropdown->addItem(tr("8-bit Signed Integer"),
  86. QVariant::fromValue(Cheats::DataType::S8));
  87. m_data_type_dropdown->addItem(tr("16-bit Signed Integer"),
  88. QVariant::fromValue(Cheats::DataType::S16));
  89. m_data_type_dropdown->addItem(tr("32-bit Signed Integer"),
  90. QVariant::fromValue(Cheats::DataType::S32));
  91. m_data_type_dropdown->addItem(tr("64-bit Signed Integer"),
  92. QVariant::fromValue(Cheats::DataType::S64));
  93. m_data_type_dropdown->addItem(tr("32-bit Float"), QVariant::fromValue(Cheats::DataType::F32));
  94. m_data_type_dropdown->addItem(tr("64-bit Float"), QVariant::fromValue(Cheats::DataType::F64));
  95. m_data_type_dropdown->setCurrentIndex(6); // select 32bit signed int by default
  96. data_type_layout->addWidget(m_data_type_dropdown);
  97. m_data_type_aligned = new QCheckBox(tr("Aligned to data type length"));
  98. m_data_type_aligned->setChecked(true);
  99. data_type_layout->addWidget(m_data_type_aligned);
  100. layout->addWidget(data_type_group);
  101. m_new_search = new NonDefaultQPushButton(tr("New Search"));
  102. layout->addWidget(m_new_search);
  103. layout->addStretch();
  104. WrapInScrollArea(this, layout);
  105. }
  106. void CheatSearchFactoryWidget::ConnectWidgets()
  107. {
  108. connect(m_new_search, &QPushButton::clicked, this, &CheatSearchFactoryWidget::OnNewSearchClicked);
  109. connect(m_standard_address_space, &QPushButton::toggled, this,
  110. &CheatSearchFactoryWidget::OnAddressSpaceRadioChanged);
  111. connect(m_custom_address_space, &QRadioButton::toggled, this,
  112. &CheatSearchFactoryWidget::OnAddressSpaceRadioChanged);
  113. }
  114. void CheatSearchFactoryWidget::RefreshGui()
  115. {
  116. bool enable_custom = m_custom_address_space->isChecked();
  117. m_custom_virtual_address_space->setEnabled(enable_custom);
  118. m_custom_physical_address_space->setEnabled(enable_custom);
  119. m_custom_effective_address_space->setEnabled(enable_custom);
  120. m_custom_address_start->setEnabled(enable_custom);
  121. m_custom_address_end->setEnabled(enable_custom);
  122. }
  123. void CheatSearchFactoryWidget::OnAddressSpaceRadioChanged()
  124. {
  125. RefreshGui();
  126. }
  127. void CheatSearchFactoryWidget::OnNewSearchClicked()
  128. {
  129. std::vector<Cheats::MemoryRange> memory_ranges;
  130. PowerPC::RequestedAddressSpace address_space;
  131. if (m_standard_address_space->isChecked())
  132. {
  133. auto& system = Core::System::GetInstance();
  134. if (!Core::IsRunning(system))
  135. {
  136. ModalMessageBox::warning(
  137. this, tr("No game running."),
  138. tr("Please start a game before starting a search with standard memory regions."));
  139. return;
  140. }
  141. auto& memory = system.GetMemory();
  142. memory_ranges.emplace_back(0x80000000, memory.GetRamSizeReal());
  143. if (system.IsWii())
  144. memory_ranges.emplace_back(0x90000000, memory.GetExRamSizeReal());
  145. address_space = PowerPC::RequestedAddressSpace::Virtual;
  146. }
  147. else
  148. {
  149. const std::string address_start_str = m_custom_address_start->text().toStdString();
  150. const std::string address_end_str = m_custom_address_end->text().toStdString();
  151. u64 address_start;
  152. u64 address_end;
  153. if (!TryParse(address_start_str, &address_start) || !TryParse(address_end_str, &address_end))
  154. return;
  155. if (address_end <= address_start || address_end > 0x1'0000'0000)
  156. return;
  157. memory_ranges.emplace_back(static_cast<u32>(address_start), address_end - address_start);
  158. if (m_custom_virtual_address_space->isChecked())
  159. address_space = PowerPC::RequestedAddressSpace::Virtual;
  160. else if (m_custom_physical_address_space->isChecked())
  161. address_space = PowerPC::RequestedAddressSpace::Physical;
  162. else
  163. address_space = PowerPC::RequestedAddressSpace::Effective;
  164. }
  165. bool aligned = m_data_type_aligned->isChecked();
  166. auto data_type = m_data_type_dropdown->currentData().value<Cheats::DataType>();
  167. auto session = Cheats::MakeSession(std::move(memory_ranges), address_space, aligned, data_type);
  168. if (session)
  169. emit NewSessionCreated(*session);
  170. }