CheatSearchWidget: New feature, writing a value to all selected addresses

This commit is contained in:
Martino Fontana 2025-05-26 15:10:40 +02:00
parent 1dff6f27d1
commit 20f1bbf7c6
4 changed files with 60 additions and 5 deletions

View File

@ -505,6 +505,23 @@ bool Cheats::CheatSearchSession<T>::WasFirstSearchDone() const
return m_first_search_done;
}
template <typename T>
bool Cheats::CheatSearchSession<T>::WriteValue(const Core::CPUThreadGuard& guard,
std::span<u32> addresses) const
{
if (!m_value)
return false;
T value = m_value.value();
bool result = true;
for (auto address : addresses)
{
if (!PowerPC::MMU::HostTryWrite<T>(guard, value, address, m_address_space).has_value())
result = false;
}
return result;
}
template <typename T>
std::unique_ptr<Cheats::CheatSearchSessionBase> Cheats::CheatSearchSession<T>::Clone() const
{

View File

@ -165,6 +165,8 @@ public:
virtual SearchResultValueState GetResultValueState(size_t index) const = 0;
virtual bool WasFirstSearchDone() const = 0;
virtual bool WriteValue(const Core::CPUThreadGuard&, std::span<u32>) const = 0;
// Create a complete copy of this search session.
virtual std::unique_ptr<CheatSearchSessionBase> Clone() const = 0;
@ -212,6 +214,8 @@ public:
SearchResultValueState GetResultValueState(size_t index) const override;
bool WasFirstSearchDone() const override;
bool WriteValue(const Core::CPUThreadGuard&, std::span<u32>) const override;
std::unique_ptr<CheatSearchSessionBase> Clone() const override;
std::unique_ptr<CheatSearchSessionBase> ClonePartial(size_t begin_index,
size_t end_index) const override;

View File

@ -14,6 +14,7 @@
#include <QComboBox>
#include <QCursor>
#include <QHBoxLayout>
#include <QInputDialog>
#include <QLabel>
#include <QLineEdit>
#include <QMenu>
@ -493,13 +494,14 @@ void CheatSearchWidget::OnAddressTableContextMenu()
if (m_address_table->selectedItems().isEmpty())
return;
auto* item = m_address_table->selectedItems()[0];
const u32 address = item->data(ADDRESS_TABLE_ADDRESS_ROLE).toUInt();
QMenu* menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose, true);
menu->addAction(tr("Show in memory"), [this, address] { emit ShowMemory(address); });
menu->addAction(tr("Show in memory"), this, [this] {
auto* item = m_address_table->selectedItems()[0];
const u32 address = item->data(ADDRESS_TABLE_ADDRESS_ROLE).toUInt();
emit ShowMemory(address);
});
menu->addAction(tr("Add to watch"), this, [this] {
for (auto* const item : m_address_table->selectedItems())
{
@ -509,6 +511,7 @@ void CheatSearchWidget::OnAddressTableContextMenu()
}
});
menu->addAction(tr("Generate Action Replay Code(s)"), this, &CheatSearchWidget::GenerateARCodes);
menu->addAction(tr("Write value"), this, &CheatSearchWidget::WriteValue);
menu->exec(QCursor::pos());
}
@ -594,10 +597,40 @@ void CheatSearchWidget::GenerateARCodes()
}
}
void CheatSearchWidget::WriteValue()
{
if (m_address_table->selectedItems().isEmpty())
return;
bool ok{};
QString text = QInputDialog::getText(this, tr("Write value"), tr("Value:"), QLineEdit::Normal,
QString{}, &ok);
if (ok && m_session->SetValueFromString(text.toStdString(),
m_parse_values_as_hex_checkbox->isChecked()))
{
auto items = m_address_table->selectedItems();
std::vector<u32> addresses(items.size());
std::transform(items.begin(), items.end(), addresses.begin(), [](QTableWidgetItem* item) {
return item->data(ADDRESS_TABLE_ADDRESS_ROLE).toUInt();
});
Core::CPUThreadGuard guard{m_system};
if (!m_session->WriteValue(guard, std::span<u32>(addresses)))
{
m_info_label_1->setText(tr("There was an error writing (some) values."));
}
}
else
{
m_info_label_1->setText(tr("Invalid value."));
}
}
void CheatSearchWidget::RefreshCurrentValueTableItem(
QTableWidgetItem* const current_value_table_item)
{
const auto address = current_value_table_item->data(ADDRESS_TABLE_ADDRESS_ROLE).toUInt();
const u32 address = current_value_table_item->data(ADDRESS_TABLE_ADDRESS_ROLE).toUInt();
const auto curr_val_iter = m_address_table_current_values.find(address);
if (curr_val_iter != m_address_table_current_values.end())
current_value_table_item->setText(QString::fromStdString(curr_val_iter->second));

View File

@ -77,6 +77,7 @@ private:
UpdateSource source);
void RecreateGUITable();
void GenerateARCodes();
void WriteValue();
int GetVisibleRowsBeginIndex() const;
int GetVisibleRowsEndIndex() const;