From c202b55bd43544de9b9301e10e81e473ee47bd16 Mon Sep 17 00:00:00 2001 From: Jordan Woyak Date: Sun, 17 Mar 2024 01:21:22 -0500 Subject: [PATCH] DolphinQt: Allow Cheat Search to create multiple AR codes when selecting multiple lines. --- Source/Core/DolphinQt/CheatSearchWidget.cpp | 76 ++++++++++++++------- Source/Core/DolphinQt/CheatSearchWidget.h | 2 +- 2 files changed, 54 insertions(+), 24 deletions(-) diff --git a/Source/Core/DolphinQt/CheatSearchWidget.cpp b/Source/Core/DolphinQt/CheatSearchWidget.cpp index a76373238e..73bf6df95f 100644 --- a/Source/Core/DolphinQt/CheatSearchWidget.cpp +++ b/Source/Core/DolphinQt/CheatSearchWidget.cpp @@ -498,7 +498,7 @@ void CheatSearchWidget::OnAddressTableContextMenu() const QString name = QStringLiteral("mem_%1").arg(address, 8, 16, QLatin1Char('0')); emit RequestWatch(name, address); }); - menu->addAction(tr("Generate Action Replay Code"), this, &CheatSearchWidget::GenerateARCode); + menu->addAction(tr("Generate Action Replay Code(s)"), this, &CheatSearchWidget::GenerateARCodes); menu->exec(QCursor::pos()); } @@ -521,36 +521,66 @@ void CheatSearchWidget::OnDisplayHexCheckboxStateChanged() UpdateTableAllCurrentValues(UpdateSource::User); } -void CheatSearchWidget::GenerateARCode() +void CheatSearchWidget::GenerateARCodes() { if (m_address_table->selectedItems().isEmpty()) return; - auto* item = m_address_table->selectedItems()[0]; - if (!item) - return; + bool had_success = false; + bool had_error = false; + std::optional error_code; - const u32 index = item->data(ADDRESS_TABLE_RESULT_INDEX_ROLE).toUInt(); - auto result = Cheats::GenerateActionReplayCode(*m_session, index); - if (result) + for (auto* const item : m_address_table->selectedItems()) { - emit ActionReplayCodeGenerated(*result); - m_info_label_1->setText(tr("Generated AR code.")); - } - else - { - switch (result.Error()) + const u32 index = item->data(ADDRESS_TABLE_RESULT_INDEX_ROLE).toUInt(); + auto result = Cheats::GenerateActionReplayCode(*m_session, index); + if (result) { - case Cheats::GenerateActionReplayCodeErrorCode::NotVirtualMemory: - m_info_label_1->setText(tr("Can only generate AR code for values in virtual memory.")); - break; - case Cheats::GenerateActionReplayCodeErrorCode::InvalidAddress: - m_info_label_1->setText(tr("Cannot generate AR code for this address.")); - break; - default: - m_info_label_1->setText(tr("Internal error while generating AR code.")); - break; + emit ActionReplayCodeGenerated(*result); + had_success = true; } + else + { + const auto new_error_code = result.Error(); + if (!had_error) + { + error_code = new_error_code; + } + else if (error_code != new_error_code) + { + // If we have a different error code signify multiple errors with an empty optional<>. + error_code.reset(); + } + + had_error = true; + } + } + + if (had_error) + { + if (error_code.has_value()) + { + switch (*error_code) + { + case Cheats::GenerateActionReplayCodeErrorCode::NotVirtualMemory: + m_info_label_1->setText(tr("Can only generate AR code for values in virtual memory.")); + break; + case Cheats::GenerateActionReplayCodeErrorCode::InvalidAddress: + m_info_label_1->setText(tr("Cannot generate AR code for this address.")); + break; + default: + m_info_label_1->setText(tr("Internal error while generating AR code.")); + break; + } + } + else + { + m_info_label_1->setText(tr("Multiple errors while generating AR codes.")); + } + } + else if (had_success) + { + m_info_label_1->setText(tr("Generated AR code(s).")); } } diff --git a/Source/Core/DolphinQt/CheatSearchWidget.h b/Source/Core/DolphinQt/CheatSearchWidget.h index 5828c0423d..f6c4576d87 100644 --- a/Source/Core/DolphinQt/CheatSearchWidget.h +++ b/Source/Core/DolphinQt/CheatSearchWidget.h @@ -76,7 +76,7 @@ private: bool UpdateTableRows(const Core::CPUThreadGuard& guard, size_t begin_index, size_t end_index, UpdateSource source); void RecreateGUITable(); - void GenerateARCode(); + void GenerateARCodes(); int GetVisibleRowsBeginIndex() const; int GetVisibleRowsEndIndex() const;