From 07aabe8efa63df4595a41b74303f696c1ff73fec Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sun, 4 Jun 2023 10:42:11 +0200 Subject: [PATCH] impr: Improved types used in provider functions --- lib/libimhex/include/hex/providers/provider.hpp | 14 ++++++++++++-- .../include/content/providers/disk_provider.hpp | 2 +- .../include/content/providers/file_provider.hpp | 4 ++-- .../include/content/providers/gdb_provider.hpp | 2 +- .../content/providers/intel_hex_provider.hpp | 2 +- .../content/providers/memory_file_provider.hpp | 2 +- .../content/providers/motorola_srec_provider.hpp | 1 + .../include/content/providers/null_provider.hpp | 2 +- .../include/content/providers/view_provider.hpp | 2 +- .../source/content/providers/disk_provider.cpp | 2 +- .../source/content/providers/file_provider.cpp | 6 +++--- .../source/content/providers/gdb_provider.cpp | 2 +- .../content/providers/intel_hex_provider.cpp | 9 +++++++++ .../content/providers/motorola_srec_provider.cpp | 10 ++++++++++ plugins/builtin/source/content/ui_items.cpp | 6 +++--- .../builtin/source/content/views/view_tools.cpp | 1 - .../content/providers/process_memory_provider.hpp | 2 +- tests/common/include/hex/test/test_provider.hpp | 2 +- 18 files changed, 50 insertions(+), 21 deletions(-) diff --git a/lib/libimhex/include/hex/providers/provider.hpp b/lib/libimhex/include/hex/providers/provider.hpp index 70fd6e472..2ebf581bb 100644 --- a/lib/libimhex/include/hex/providers/provider.hpp +++ b/lib/libimhex/include/hex/providers/provider.hpp @@ -21,6 +21,16 @@ namespace hex::prv { */ class Provider { public: + struct Description { + std::string name; + std::string value; + }; + + struct MenuEntry { + std::string name; + std::function callback; + }; + constexpr static size_t MaxPageSize = 0x1000'0000; Provider(); @@ -96,7 +106,7 @@ namespace hex::prv { [[nodiscard]] virtual std::optional getPageOfAddress(u64 address) const; [[nodiscard]] virtual std::string getName() const = 0; - [[nodiscard]] virtual std::vector> getDataDescription() const = 0; + [[nodiscard]] virtual std::vector getDataDescription() const = 0; [[nodiscard]] virtual std::variant queryInformation(const std::string &category, const std::string &argument); /** @@ -121,7 +131,7 @@ namespace hex::prv { [[nodiscard]] virtual bool hasFilePicker() const; virtual bool handleFilePicker(); - virtual std::vector>> getMenuEntries() { + virtual std::vector getMenuEntries() { return { }; }; diff --git a/plugins/builtin/include/content/providers/disk_provider.hpp b/plugins/builtin/include/content/providers/disk_provider.hpp index b57861a4e..cd0c2acb5 100644 --- a/plugins/builtin/include/content/providers/disk_provider.hpp +++ b/plugins/builtin/include/content/providers/disk_provider.hpp @@ -34,7 +34,7 @@ namespace hex::plugin::builtin { void close() override; [[nodiscard]] std::string getName() const override; - [[nodiscard]] std::vector> getDataDescription() const override; + [[nodiscard]] std::vector getDataDescription() const override; [[nodiscard]] bool hasLoadInterface() const override { return true; } bool drawLoadInterface() override; diff --git a/plugins/builtin/include/content/providers/file_provider.hpp b/plugins/builtin/include/content/providers/file_provider.hpp index 7afd5bf8b..973edbed0 100644 --- a/plugins/builtin/include/content/providers/file_provider.hpp +++ b/plugins/builtin/include/content/providers/file_provider.hpp @@ -36,13 +36,13 @@ namespace hex::plugin::builtin { void saveAs(const std::fs::path &path) override; [[nodiscard]] std::string getName() const override; - [[nodiscard]] std::vector> getDataDescription() const override; + [[nodiscard]] std::vector getDataDescription() const override; std::variant queryInformation(const std::string &category, const std::string &argument) override; [[nodiscard]] bool hasFilePicker() const override { return true; } [[nodiscard]] bool handleFilePicker() override; - std::vector>> getMenuEntries() override; + std::vector getMenuEntries() override; void setPath(const std::fs::path &path); diff --git a/plugins/builtin/include/content/providers/gdb_provider.hpp b/plugins/builtin/include/content/providers/gdb_provider.hpp index 30322be66..9a0958338 100644 --- a/plugins/builtin/include/content/providers/gdb_provider.hpp +++ b/plugins/builtin/include/content/providers/gdb_provider.hpp @@ -32,7 +32,7 @@ namespace hex::plugin::builtin { void save() override; [[nodiscard]] std::string getName() const override; - [[nodiscard]] std::vector> getDataDescription() const override; + [[nodiscard]] std::vector getDataDescription() const override; [[nodiscard]] bool open() override; void close() override; diff --git a/plugins/builtin/include/content/providers/intel_hex_provider.hpp b/plugins/builtin/include/content/providers/intel_hex_provider.hpp index 71d72069c..afb98e639 100644 --- a/plugins/builtin/include/content/providers/intel_hex_provider.hpp +++ b/plugins/builtin/include/content/providers/intel_hex_provider.hpp @@ -27,7 +27,7 @@ namespace hex::plugin::builtin { void close() override; [[nodiscard]] std::string getName() const override; - [[nodiscard]] std::vector> getDataDescription() const override { return { }; } + [[nodiscard]] std::vector getDataDescription() const override; void loadSettings(const nlohmann::json &settings) override; [[nodiscard]] nlohmann::json storeSettings(nlohmann::json settings) const override; diff --git a/plugins/builtin/include/content/providers/memory_file_provider.hpp b/plugins/builtin/include/content/providers/memory_file_provider.hpp index 1ca6c1d3a..82bbd3537 100644 --- a/plugins/builtin/include/content/providers/memory_file_provider.hpp +++ b/plugins/builtin/include/content/providers/memory_file_provider.hpp @@ -30,7 +30,7 @@ namespace hex::plugin::builtin { void save() override; [[nodiscard]] std::string getName() const override { return LangEntry("hex.builtin.provider.mem_file.unsaved"); } - [[nodiscard]] std::vector> getDataDescription() const override { return { }; } + [[nodiscard]] std::vector getDataDescription() const override { return { }; } [[nodiscard]] std::string getTypeName() const override { return "hex.builtin.provider.mem_file"; diff --git a/plugins/builtin/include/content/providers/motorola_srec_provider.hpp b/plugins/builtin/include/content/providers/motorola_srec_provider.hpp index 7bfa443ef..8ffcab6dd 100644 --- a/plugins/builtin/include/content/providers/motorola_srec_provider.hpp +++ b/plugins/builtin/include/content/providers/motorola_srec_provider.hpp @@ -13,6 +13,7 @@ namespace hex::plugin::builtin { void close() override; [[nodiscard]] std::string getName() const override; + std::vector getDataDescription() const override; [[nodiscard]] std::string getTypeName() const override { return "hex.builtin.provider.motorola_srec"; diff --git a/plugins/builtin/include/content/providers/null_provider.hpp b/plugins/builtin/include/content/providers/null_provider.hpp index 81ce5ac45..9658fa71e 100644 --- a/plugins/builtin/include/content/providers/null_provider.hpp +++ b/plugins/builtin/include/content/providers/null_provider.hpp @@ -23,7 +23,7 @@ namespace hex::plugin::builtin { [[nodiscard]] size_t getActualSize() const override { return 0x00; } [[nodiscard]] std::string getName() const override { return "None"; } - [[nodiscard]] std::vector> getDataDescription() const override { return { }; } + [[nodiscard]] std::vector getDataDescription() const override { return { }; } void loadSettings(const nlohmann::json &settings) override { hex::unused(settings); } [[nodiscard]] nlohmann::json storeSettings(nlohmann::json settings) const override { return settings; } diff --git a/plugins/builtin/include/content/providers/view_provider.hpp b/plugins/builtin/include/content/providers/view_provider.hpp index 4c1dd640d..5866f8de9 100644 --- a/plugins/builtin/include/content/providers/view_provider.hpp +++ b/plugins/builtin/include/content/providers/view_provider.hpp @@ -88,7 +88,7 @@ namespace hex::plugin::builtin { else return hex::format("{} View", this->m_provider->getName()); } - [[nodiscard]] std::vector> getDataDescription() const override { + [[nodiscard]] std::vector getDataDescription() const override { if (this->m_provider == nullptr) return { }; diff --git a/plugins/builtin/source/content/providers/disk_provider.cpp b/plugins/builtin/source/content/providers/disk_provider.cpp index 221cc0a01..40a2bd9a3 100644 --- a/plugins/builtin/source/content/providers/disk_provider.cpp +++ b/plugins/builtin/source/content/providers/disk_provider.cpp @@ -270,7 +270,7 @@ namespace hex::plugin::builtin { return wolv::util::toUTF8String(this->m_path); } - std::vector> DiskProvider::getDataDescription() const { + std::vector DiskProvider::getDataDescription() const { return { { "hex.builtin.provider.disk.selected_disk"_lang, wolv::util::toUTF8String(this->m_path) }, { "hex.builtin.provider.disk.disk_size"_lang, hex::toByteString(this->m_diskSize) }, diff --git a/plugins/builtin/source/content/providers/file_provider.cpp b/plugins/builtin/source/content/providers/file_provider.cpp index f4bbfb947..93eb0cf34 100644 --- a/plugins/builtin/source/content/providers/file_provider.cpp +++ b/plugins/builtin/source/content/providers/file_provider.cpp @@ -144,8 +144,8 @@ namespace hex::plugin::builtin { return wolv::util::toUTF8String(this->m_path.filename()); } - std::vector> FileProvider::getDataDescription() const { - std::vector> result; + std::vector FileProvider::getDataDescription() const { + std::vector result; result.emplace_back("hex.builtin.provider.file.path"_lang, wolv::util::toUTF8String(this->m_path)); result.emplace_back("hex.builtin.provider.file.size"_lang, hex::toByteString(this->getActualSize())); @@ -184,7 +184,7 @@ namespace hex::plugin::builtin { }); } - std::vector>> FileProvider::getMenuEntries(){ + std::vector FileProvider::getMenuEntries(){ return { {"hex.builtin.provider.file.menu.open_folder"_lang, [path = this->m_path] { fs::openFolderWithSelectionExternal(path); diff --git a/plugins/builtin/source/content/providers/gdb_provider.cpp b/plugins/builtin/source/content/providers/gdb_provider.cpp index be6a440c3..abcc3895a 100644 --- a/plugins/builtin/source/content/providers/gdb_provider.cpp +++ b/plugins/builtin/source/content/providers/gdb_provider.cpp @@ -238,7 +238,7 @@ namespace hex::plugin::builtin { return hex::format("hex.builtin.provider.gdb.name"_lang, address, port); } - std::vector> GDBProvider::getDataDescription() const { + std::vector GDBProvider::getDataDescription() const { return { {"hex.builtin.provider.gdb.server"_lang, hex::format("{}:{}", this->m_ipAddress, this->m_port)}, }; diff --git a/plugins/builtin/source/content/providers/intel_hex_provider.cpp b/plugins/builtin/source/content/providers/intel_hex_provider.cpp index 99c430542..83e09a6c5 100644 --- a/plugins/builtin/source/content/providers/intel_hex_provider.cpp +++ b/plugins/builtin/source/content/providers/intel_hex_provider.cpp @@ -224,6 +224,15 @@ namespace hex::plugin::builtin { return hex::format("hex.builtin.provider.intel_hex.name"_lang, wolv::util::toUTF8String(this->m_sourceFilePath.filename())); } + [[nodiscard]] std::vector IntelHexProvider::getDataDescription() const { + std::vector result; + + result.emplace_back("hex.builtin.provider.file.path"_lang, wolv::util::toUTF8String(this->m_sourceFilePath)); + result.emplace_back("hex.builtin.provider.file.size"_lang, hex::toByteString(this->getActualSize())); + + return result; + } + bool IntelHexProvider::handleFilePicker() { auto picked = fs::openFileBrowser(fs::DialogMode::Open, { { "Intel Hex File", "hex" }, diff --git a/plugins/builtin/source/content/providers/motorola_srec_provider.cpp b/plugins/builtin/source/content/providers/motorola_srec_provider.cpp index 3b8ff5f7f..cc3ad9b86 100644 --- a/plugins/builtin/source/content/providers/motorola_srec_provider.cpp +++ b/plugins/builtin/source/content/providers/motorola_srec_provider.cpp @@ -202,6 +202,16 @@ namespace hex::plugin::builtin { return hex::format("hex.builtin.provider.motorola_srec.name"_lang, wolv::util::toUTF8String(this->m_sourceFilePath.filename())); } + + [[nodiscard]] std::vector MotorolaSRECProvider::getDataDescription() const { + std::vector result; + + result.emplace_back("hex.builtin.provider.file.path"_lang, wolv::util::toUTF8String(this->m_sourceFilePath)); + result.emplace_back("hex.builtin.provider.file.size"_lang, hex::toByteString(this->getActualSize())); + + return result; + } + bool MotorolaSRECProvider::handleFilePicker() { auto picked = fs::openFileBrowser(fs::DialogMode::Open, { { "Motorola SREC File", "s19" }, diff --git a/plugins/builtin/source/content/ui_items.cpp b/plugins/builtin/source/content/ui_items.cpp index 2f7f60915..d4c7ee92a 100644 --- a/plugins/builtin/source/content/ui_items.cpp +++ b/plugins/builtin/source/content/ui_items.cpp @@ -286,9 +286,9 @@ namespace hex::plugin::builtin { } if (ImGui::BeginPopup(popupID.c_str())) { - for (auto p : tabProvider->getMenuEntries()) { - if (ImGui::MenuItem(p.first.c_str())) { - p.second(); + for (const auto &menuEntry : tabProvider->getMenuEntries()) { + if (ImGui::MenuItem(menuEntry.name.c_str())) { + menuEntry.callback(); } } ImGui::EndPopup(); diff --git a/plugins/builtin/source/content/views/view_tools.cpp b/plugins/builtin/source/content/views/view_tools.cpp index d8c4ae180..b8a8395b8 100644 --- a/plugins/builtin/source/content/views/view_tools.cpp +++ b/plugins/builtin/source/content/views/view_tools.cpp @@ -50,7 +50,6 @@ namespace hex::plugin::builtin { // Attach the newly created window to the cursor, so it gets dragged around GImGui->MovingWindow = ImGui::GetCurrentWindow(); GImGui->ActiveId = GImGui->MovingWindow->MoveId; - ImGui::DockContextQueueUndockWindow(GImGui, GImGui->MovingWindow); } } ImGui::End(); diff --git a/plugins/windows/include/content/providers/process_memory_provider.hpp b/plugins/windows/include/content/providers/process_memory_provider.hpp index 95ac85c63..33791cf1f 100644 --- a/plugins/windows/include/content/providers/process_memory_provider.hpp +++ b/plugins/windows/include/content/providers/process_memory_provider.hpp @@ -38,7 +38,7 @@ namespace hex::plugin::windows { void save() override {} [[nodiscard]] std::string getName() const override { return hex::format("hex.windows.provider.process_memory.name"_lang, this->m_selectedProcess != nullptr ? this->m_selectedProcess->name : ""); } - [[nodiscard]] std::vector> getDataDescription() const override { + [[nodiscard]] std::vector getDataDescription() const override { return { { "hex.windows.provider.process_memory.process_name"_lang, this->m_selectedProcess->name }, { "hex.windows.provider.process_memory.process_id"_lang, std::to_string(this->m_selectedProcess->id) } diff --git a/tests/common/include/hex/test/test_provider.hpp b/tests/common/include/hex/test/test_provider.hpp index da2687437..b1cf01aee 100644 --- a/tests/common/include/hex/test/test_provider.hpp +++ b/tests/common/include/hex/test/test_provider.hpp @@ -27,7 +27,7 @@ namespace hex::test { return ""; } - [[nodiscard]] std::vector> getDataDescription() const override { + [[nodiscard]] std::vector getDataDescription() const override { return {}; }