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