From 3f7a6c01e5e0ddf43099b5581f9198ed7b8c7810 Mon Sep 17 00:00:00 2001 From: rocketz Date: Sat, 16 Nov 2024 12:34:11 +0100 Subject: [PATCH] - Fixed crash when deleting patches from Assembly view - Added "are you sure?" popup when clicking "delete all" --- src/core/patchmanager.cc | 7 +++--- src/core/patchmanager.h | 2 +- src/gui/widgets/assembly.cc | 43 +++++++++++++++++++------------------ src/gui/widgets/patches.cc | 15 ++++++++++--- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/core/patchmanager.cc b/src/core/patchmanager.cc index 10edf4cf8..7f9f83c32 100644 --- a/src/core/patchmanager.cc +++ b/src/core/patchmanager.cc @@ -71,13 +71,14 @@ void PCSX::PatchManager::undoPatch(Patch& patch) { patch.active = false; } -PCSX::PatchManager::Patch::Type PCSX::PatchManager::findPatch(uint32_t address) const { +int PCSX::PatchManager::findPatch(uint32_t address) const { + int idx = 0; for (const Patch& patch : m_patches) { if (patch.addr == address) { - return patch.type; + return idx; } } - return PCSX::PatchManager::Patch::Type::None; + return -1; } void PCSX::PatchManager::deletePatch(uint32_t index) { diff --git a/src/core/patchmanager.h b/src/core/patchmanager.h index a397a8e67..0db8bbc9f 100644 --- a/src/core/patchmanager.h +++ b/src/core/patchmanager.h @@ -47,7 +47,7 @@ class PatchManager { Patch& getPatch(int index) { return m_patches[index]; } int registerPatch(uint32_t address, Patch::Type type); - PatchManager::Patch::Type findPatch(uint32_t address) const; + int findPatch(uint32_t address) const; void deletePatch(uint32_t index); void deleteAllPatches(); void deactivateAll(); diff --git a/src/gui/widgets/assembly.cc b/src/gui/widgets/assembly.cc index 59718a122..46a1fab42 100644 --- a/src/gui/widgets/assembly.cc +++ b/src/gui/widgets/assembly.cc @@ -789,28 +789,29 @@ settings, otherwise debugging features may not work.)"); } if (absAddr < 0x00800000) { PatchManager& pm = *g_emulator->m_patchManager; - PatchManager::Patch::Type patchType = pm.findPatch(dispAddr); - switch (patchType) { - case PatchManager::Patch::Type::None: - if (ImGui::MenuItem(_("Patch in Return"))) { - pm.registerPatch(dispAddr, PatchManager::Patch::Type::Return); - } - if (ImGui::MenuItem(_("Patch in NOP"))) { - pm.registerPatch(dispAddr, PatchManager::Patch::Type::NOP); - } - break; - - case PatchManager::Patch::Type::Return: - if (ImGui::MenuItem(_("Remove Return Patch"))) { - pm.undoPatch(dispAddr); - } - break; + int patchIdx = pm.findPatch(dispAddr); + if (patchIdx == -1) { + if (ImGui::MenuItem(_("Patch in Return"))) { + pm.registerPatch(dispAddr, PatchManager::Patch::Type::Return); + } + if (ImGui::MenuItem(_("Patch in NOP"))) { + pm.registerPatch(dispAddr, PatchManager::Patch::Type::NOP); + } + } else { + PatchManager::Patch& patch = pm.getPatch(patchIdx); + switch (patch.type) { + case PatchManager::Patch::Type::Return: + if (ImGui::MenuItem(_("Delete Return Patch"))) { + pm.deletePatch(patchIdx); + } + break; - case PatchManager::Patch::Type::NOP: - if (ImGui::MenuItem(_("Remove NOP Patch"))) { - pm.undoPatch(dispAddr); - } - break; + case PatchManager::Patch::Type::NOP: + if (ImGui::MenuItem(_("Delete NOP Patch"))) { + pm.deletePatch(patchIdx); + } + break; + } } if (ImGui::MenuItem(_("Assemble"))) { diff --git a/src/gui/widgets/patches.cc b/src/gui/widgets/patches.cc index b561fec86..29cbc2cf4 100644 --- a/src/gui/widgets/patches.cc +++ b/src/gui/widgets/patches.cc @@ -105,9 +105,18 @@ void PCSX::Widgets::Patches::draw(const char* title) { patchManager.deactivateAll(); } - ImGui::SameLine(); - if (ImGui::Button(_("Delete All"))) { - patchManager.deleteAllPatches(); + if (patchManager.getNumPatches() > 0) { + ImGui::SameLine(); + if (ImGui::Button(_("Delete All"))) { + ImGui::OpenPopup("delpatches_popup"); + } + if (ImGui::BeginPopup("delpatches_popup")) { + ImGui::TextUnformatted(_("Delete all Patches?")); + if (ImGui::Button(_("Delete##patches"))) { + patchManager.deleteAllPatches(); + } + ImGui::EndPopup(); + } } }