From 599d5da5dabe9ecb4755353ed5f6f62bc0d411e4 Mon Sep 17 00:00:00 2001 From: Ken Murdock Date: Fri, 26 Apr 2024 03:25:03 +0100 Subject: [PATCH] Allow memory editors to export directly to a file --- src/gui/gui.cc | 22 +++++++++++++++++++ .../imgui_memory_editor.cpp | 12 ++++++++-- .../imgui_memory_editor/imgui_memory_editor.h | 3 ++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/gui/gui.cc b/src/gui/gui.cc index 696f9b11..48434346 100644 --- a/src/gui/gui.cc +++ b/src/gui/gui.cc @@ -733,6 +733,28 @@ void PCSX::GUI::init(std::function applyArguments) { g_emulator->m_gpu->partialUpdateVRAM(x, y, 1, 1, &newPixel); }; + auto exportFn = [](ImU8* data, size_t len, size_t base_addr, std::string postfixName) { + std::filesystem::path writeFilepath = + g_system->getPersistentDir() / (s_gui->getSaveStatePrefix(true) + "mem_" + postfixName + ".bin"); + IO f(new PosixFile(writeFilepath.string(), FileOps::TRUNCATE)); + if (!f->failed()) { + f->write(data, len); + f->close(); + g_system->log(LogClass::UI, "Memory exported to: %s\n", writeFilepath.string().c_str()); + } else { + g_system->log(LogClass::UI, "Failed to export memory to: %s\n", writeFilepath.string().c_str()); + } + }; +#define EXPORT_FUNC(name) [=](ImU8* data, size_t len, size_t base_addr) { exportFn(data, len, base_addr, name); } + for (auto& editor : m_mainMemEditors) { + editor.editor.ExportFn = EXPORT_FUNC("wram"); + } + m_parallelPortEditor.editor.ExportFn = EXPORT_FUNC("parallel"); + m_scratchPadEditor.editor.ExportFn = EXPORT_FUNC("scratch"); + m_hwrEditor.editor.ExportFn = EXPORT_FUNC("hwr"); + m_biosEditor.editor.ExportFn = EXPORT_FUNC("bios"); + m_vramEditor.editor.ExportFn = EXPORT_FUNC("vram"); + m_offscreenShaderEditor.init(); m_outputShaderEditor.init(); diff --git a/third_party/imgui_memory_editor/imgui_memory_editor.cpp b/third_party/imgui_memory_editor/imgui_memory_editor.cpp index eaceec13..4092d56b 100644 --- a/third_party/imgui_memory_editor/imgui_memory_editor.cpp +++ b/third_party/imgui_memory_editor/imgui_memory_editor.cpp @@ -361,9 +361,9 @@ void MemoryEditor::DrawContents(void* mem_data_void, size_t mem_size) } } -void MemoryEditor::DrawOptionsLine(const Sizes& s, void* mem_data, size_t mem_size) +void MemoryEditor::DrawOptionsLine(const Sizes& s, void* mem_data_void, size_t mem_size) { - IM_UNUSED(mem_data); + ImU8* mem_data = (ImU8*)mem_data_void; ImGuiStyle& style = ImGui::GetStyle(); const char* format_range = OptUpperCaseHex ? "Range %0*" _PRISizeT "X..%0*" _PRISizeT "X" : "Range %0*" _PRISizeT "x..%0*" _PRISizeT "x"; @@ -423,6 +423,14 @@ void MemoryEditor::DrawOptionsLine(const Sizes& s, void* mem_data, size_t mem_si GotoAddr = 0; OffsetAddr = 0; } + + // Export memory via a function, if available + if (ExportFn) { + ImGui::SameLine(); + if (ImGui::Button("Export Memory")) { + ExportFn(mem_data, mem_size, BaseAddr); + } + } } void MemoryEditor::DrawPreviewLine(const Sizes& s, void* mem_data_void, size_t mem_size) diff --git a/third_party/imgui_memory_editor/imgui_memory_editor.h b/third_party/imgui_memory_editor/imgui_memory_editor.h index af0ea80f..4078bd9d 100644 --- a/third_party/imgui_memory_editor/imgui_memory_editor.h +++ b/third_party/imgui_memory_editor/imgui_memory_editor.h @@ -85,6 +85,7 @@ struct MemoryEditor bool (*HighlightFn)(const ImU8* data, size_t off);//= 0 // optional handler to return Highlight property (to support non-contiguous highlighting). std::function PushMonoFont = nullptr; size_t& OffsetAddr; // referenced from PCSX-Redux Settings + std::function ExportFn = nullptr; // optional handler to export all memory data. private: // [Internal State] @@ -129,7 +130,7 @@ public: void DrawWindow(const char* title, void* mem_data, size_t mem_size); // Memory Editor contents only void DrawContents(void* mem_data_void, size_t mem_size); - void DrawOptionsLine(const Sizes& s, void* mem_data, size_t mem_size); + void DrawOptionsLine(const Sizes& s, void* mem_data_void, size_t mem_size); void DrawPreviewLine(const Sizes& s, void* mem_data_void, size_t mem_size); // Utilities for Data Preview const char* DataTypeGetDesc(ImGuiDataType data_type) const;