diff --git a/include/helpers/project_file_handler.hpp b/include/helpers/project_file_handler.hpp index 7b3a621d3..4f0f0717f 100644 --- a/include/helpers/project_file_handler.hpp +++ b/include/helpers/project_file_handler.hpp @@ -5,7 +5,7 @@ #include #include "patches.hpp" -#include +#include namespace hex { @@ -30,8 +30,8 @@ namespace hex { [[nodiscard]] static const Patches& getPatches() { return ProjectFile::s_patches; } static void setPatches(const Patches &patches) { ProjectFile::s_hasUnsavedChanged = true; ProjectFile::s_patches = patches; } - [[nodiscard]] static const std::list& getBookmarks() { return ProjectFile::s_bookmarks; } - static void setBookmarks(const std::list &bookmarks) { ProjectFile::s_hasUnsavedChanged = true; ProjectFile::s_bookmarks = bookmarks; } + [[nodiscard]] static const std::list& getBookmarks() { return ProjectFile::s_bookmarks; } + static void setBookmarks(const std::list &bookmarks) { ProjectFile::s_hasUnsavedChanged = true; ProjectFile::s_bookmarks = bookmarks; } private: static inline std::string s_currProjectFilePath; @@ -40,7 +40,7 @@ namespace hex { static inline std::string s_filePath; static inline std::string s_pattern; static inline Patches s_patches; - static inline std::list s_bookmarks; + static inline std::list s_bookmarks; }; } \ No newline at end of file diff --git a/include/views/view_bookmarks.hpp b/include/views/view_bookmarks.hpp index 7fa63bb17..4f5b74889 100644 --- a/include/views/view_bookmarks.hpp +++ b/include/views/view_bookmarks.hpp @@ -13,14 +13,11 @@ namespace hex { class ViewBookmarks : public View { public: - explicit ViewBookmarks(std::list &bookmarks); + ViewBookmarks(); ~ViewBookmarks() override; void drawContent() override; void drawMenu() override; - - private: - std::list &m_bookmarks; }; } \ No newline at end of file diff --git a/include/views/view_hexeditor.hpp b/include/views/view_hexeditor.hpp index e44185e0e..0125c511f 100644 --- a/include/views/view_hexeditor.hpp +++ b/include/views/view_hexeditor.hpp @@ -21,7 +21,7 @@ namespace hex { class ViewHexEditor : public View { public: - ViewHexEditor(std::vector &patternData, const std::list &bookmarks); + ViewHexEditor(std::vector &patternData); ~ViewHexEditor() override; void drawContent() override; @@ -33,7 +33,6 @@ namespace hex { imgui_addons::ImGuiFileBrowser m_fileBrowser; std::vector &m_patternData; - const std::list &m_bookmarks; std::map m_highlightedBytes; diff --git a/plugins/libimhex/CMakeLists.txt b/plugins/libimhex/CMakeLists.txt index ab7e36038..dd50103da 100644 --- a/plugins/libimhex/CMakeLists.txt +++ b/plugins/libimhex/CMakeLists.txt @@ -17,6 +17,7 @@ set(CMAKE_SHARED_LIBRARY_PREFIX "") add_library(libimhex SHARED source/api/event.cpp + source/api/imhex_api.cpp source/api/content_registry.cpp source/helpers/utils.cpp source/helpers/shared_data.cpp diff --git a/plugins/libimhex/include/hex/api/content_registry.hpp b/plugins/libimhex/include/hex/api/content_registry.hpp index 485c4c124..4aa76588d 100644 --- a/plugins/libimhex/include/hex/api/content_registry.hpp +++ b/plugins/libimhex/include/hex/api/content_registry.hpp @@ -22,8 +22,7 @@ namespace hex { It allows you to add/register new content that will be picked up and used by the ImHex core or by other plugins when needed. */ - class ContentRegistry { - public: + struct ContentRegistry { ContentRegistry() = delete; /* Settings Registry. Allows adding of new entries into the ImHex preferences window. */ diff --git a/plugins/libimhex/include/hex/api/imhex_api.hpp b/plugins/libimhex/include/hex/api/imhex_api.hpp new file mode 100644 index 000000000..524b5b71a --- /dev/null +++ b/plugins/libimhex/include/hex/api/imhex_api.hpp @@ -0,0 +1,31 @@ +#pragma once + +#include +#include + +#include + +namespace hex { + + struct ImHexApi { + ImHexApi() = delete; + + struct Bookmarks { + Bookmarks() = delete; + + struct Entry { + Region region; + + std::vector name; + std::vector comment; + u32 color; + }; + + static void add(Region region, std::string_view name, std::string_view comment, u32 color = 0x00000000); + static void add(u64 addr, size_t size, std::string_view name, std::string_view comment, u32 color = 0x00000000); + + static std::list& getEntries(); + }; + }; + +} diff --git a/plugins/libimhex/include/hex/helpers/shared_data.hpp b/plugins/libimhex/include/hex/helpers/shared_data.hpp index 19c720722..f1e94a673 100644 --- a/plugins/libimhex/include/hex/helpers/shared_data.hpp +++ b/plugins/libimhex/include/hex/helpers/shared_data.hpp @@ -2,10 +2,12 @@ #include #include -#include +#include #include +#include #include +#include #include #include @@ -55,6 +57,7 @@ namespace hex { static std::vector dataInspectorEntries; static u32 patternPaletteOffset; static std::string errorPopupMessage; + static std::list bookmarkEntries; static int mainArgc; static char **mainArgv; diff --git a/plugins/libimhex/include/hex/helpers/utils.hpp b/plugins/libimhex/include/hex/helpers/utils.hpp index 455c2cce0..5e6082073 100644 --- a/plugins/libimhex/include/hex/helpers/utils.hpp +++ b/plugins/libimhex/include/hex/helpers/utils.hpp @@ -197,11 +197,4 @@ namespace hex { size_t size; }; - struct Bookmark { - Region region; - - std::vector name; - std::vector comment; - u32 color; - }; } \ No newline at end of file diff --git a/plugins/libimhex/include/hex/plugin.hpp b/plugins/libimhex/include/hex/plugin.hpp index 073ee2e56..d25952328 100644 --- a/plugins/libimhex/include/hex/plugin.hpp +++ b/plugins/libimhex/include/hex/plugin.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include diff --git a/plugins/libimhex/source/api/imhex_api.cpp b/plugins/libimhex/source/api/imhex_api.cpp new file mode 100644 index 000000000..312afe989 --- /dev/null +++ b/plugins/libimhex/source/api/imhex_api.cpp @@ -0,0 +1,31 @@ +#include + +#include +#include + +namespace hex { + + void ImHexApi::Bookmarks::add(Region region, std::string_view name, std::string_view comment, u32 color) { + Entry entry; + + entry.region = region; + + entry.name.reserve(name.length()); + entry.comment.reserve(comment.length()); + std::copy(name.begin(), name.end(), std::back_inserter(entry.name)); + std::copy(comment.begin(), comment.end(), std::back_inserter(entry.comment)); + + entry.color = color; + + EventManager::post(Events::AddBookmark, &entry); + } + + void ImHexApi::Bookmarks::add(u64 addr, size_t size, std::string_view name, std::string_view comment, u32 color) { + Bookmarks::add(Region{addr, size}, name, comment, color); + } + + std::list& ImHexApi::Bookmarks::getEntries() { + return SharedData::bookmarkEntries; + } + +} \ No newline at end of file diff --git a/plugins/libimhex/source/helpers/shared_data.cpp b/plugins/libimhex/source/helpers/shared_data.cpp index bca02e73c..f0ccaa0ac 100644 --- a/plugins/libimhex/source/helpers/shared_data.cpp +++ b/plugins/libimhex/source/helpers/shared_data.cpp @@ -16,6 +16,7 @@ namespace hex { std::vector SharedData::dataInspectorEntries; u32 SharedData::patternPaletteOffset; std::string SharedData::errorPopupMessage; + std::list SharedData::bookmarkEntries; int SharedData::mainArgc; char **SharedData::mainArgv; diff --git a/source/helpers/loader_script_handler.cpp b/source/helpers/loader_script_handler.cpp index 4fdd0a4c7..24aec6cf8 100644 --- a/source/helpers/loader_script_handler.cpp +++ b/source/helpers/loader_script_handler.cpp @@ -45,12 +45,13 @@ namespace hex { } PyObject* LoaderScript::Py_addBookmark(PyObject *self, PyObject *args) { - Bookmark bookmark; + u64 address; + size_t size; char *name = nullptr; char *comment = nullptr; - if (!PyArg_ParseTuple(args, "K|n|s|s", &bookmark.region.address, &bookmark.region.size, &name, &comment)) { + if (!PyArg_ParseTuple(args, "K|n|s|s", &address, &size, &name, &comment)) { PyErr_BadArgument(); return nullptr; } @@ -60,10 +61,7 @@ namespace hex { return nullptr; } - std::copy(name, name + std::strlen(name), std::back_inserter(bookmark.name)); - std::copy(comment, comment + std::strlen(comment), std::back_inserter(bookmark.comment)); - - View::postEvent(Events::AddBookmark, &bookmark); + ImHexApi::Bookmarks::add(address, size, name, comment); Py_RETURN_NONE; } diff --git a/source/helpers/project_file_handler.cpp b/source/helpers/project_file_handler.cpp index 6aa6afced..ae5d43469 100644 --- a/source/helpers/project_file_handler.cpp +++ b/source/helpers/project_file_handler.cpp @@ -7,11 +7,11 @@ using json = nlohmann::json; namespace hex { - void to_json(json& j, const hex::Bookmark& b) { + void to_json(json& j, const ImHexApi::Bookmarks::Entry& b) { j = json{ { "address", b.region.address }, { "size", b.region.size }, { "name", b.name.data() }, { "comment", b.comment.data() } }; } - void from_json(const json& j, hex::Bookmark& b) { + void from_json(const json& j, ImHexApi::Bookmarks::Entry& b) { std::string name, comment; j.at("address").get_to(b.region.address); @@ -38,7 +38,7 @@ namespace hex { ProjectFile::s_patches = projectFileData["patches"].get(); for (auto &element : projectFileData["bookmarks"].items()) { - ProjectFile::s_bookmarks.push_back(element.value().get()); + ProjectFile::s_bookmarks.push_back(element.value().get()); } } catch (json::exception &e) { diff --git a/source/main.cpp b/source/main.cpp index f27c2eada..a61184dba 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -29,10 +29,9 @@ int main(int argc, char **argv) { // Shared Data std::vector patternData; - std::list bookmarks; // Create views - ContentRegistry::Views::add(patternData, bookmarks); + ContentRegistry::Views::add(patternData); ContentRegistry::Views::add(patternData); ContentRegistry::Views::add(patternData); ContentRegistry::Views::add(); @@ -40,7 +39,7 @@ int main(int argc, char **argv) { ContentRegistry::Views::add(); ContentRegistry::Views::add(); ContentRegistry::Views::add(); - ContentRegistry::Views::add(bookmarks); + ContentRegistry::Views::add(); ContentRegistry::Views::add(); ContentRegistry::Views::add(); ContentRegistry::Views::add(); diff --git a/source/views/view_bookmarks.cpp b/source/views/view_bookmarks.cpp index 500e2456b..2c1bf78e3 100644 --- a/source/views/view_bookmarks.cpp +++ b/source/views/view_bookmarks.cpp @@ -7,9 +7,9 @@ namespace hex { - ViewBookmarks::ViewBookmarks(std::list &bookmarks) : View("Bookmarks"), m_bookmarks(bookmarks) { + ViewBookmarks::ViewBookmarks() : View("Bookmarks") { View::subscribeEvent(Events::AddBookmark, [this](const void *userData) { - Bookmark bookmark = *reinterpret_cast(userData); + auto bookmark = *reinterpret_cast(userData); bookmark.comment.resize(0xF'FFFF); if (bookmark.name.empty()) { @@ -25,15 +25,15 @@ namespace hex { bookmark.color = ImGui::GetColorU32(ImGuiCol_Header); - this->m_bookmarks.push_back(bookmark); + SharedData::bookmarkEntries.push_back(bookmark); ProjectFile::markDirty(); }); - View::subscribeEvent(Events::ProjectFileLoad, [this](const void*) { - this->m_bookmarks = ProjectFile::getBookmarks(); + View::subscribeEvent(Events::ProjectFileLoad, [](const void*) { + SharedData::bookmarkEntries = ProjectFile::getBookmarks(); }); - View::subscribeEvent(Events::ProjectFileStore, [this](const void*) { - ProjectFile::setBookmarks(this->m_bookmarks); + View::subscribeEvent(Events::ProjectFileStore, [](const void*) { + ProjectFile::setBookmarks(SharedData::bookmarkEntries); }); } @@ -47,15 +47,17 @@ namespace hex { if (ImGui::Begin("Bookmarks", &this->getWindowOpenState())) { if (ImGui::BeginChild("##scrolling")) { - if (this->m_bookmarks.empty()) { + auto &bookmarks = ImHexApi::Bookmarks::getEntries(); + + if (bookmarks.empty()) { ImGui::NewLine(); ImGui::Indent(30); ImGui::TextWrapped("No bookmarks created yet. Add one with Edit -> Add Bookmark"); } u32 id = 1; - std::list::const_iterator bookmarkToRemove = this->m_bookmarks.end(); - for (auto iter = this->m_bookmarks.begin(); iter != this->m_bookmarks.end(); iter++) { + auto bookmarkToRemove = bookmarks.end(); + for (auto iter = bookmarks.begin(); iter != bookmarks.end(); iter++) { auto &[region, name, comment, color] = *iter; auto headerColor = ImColor(color); @@ -97,14 +99,14 @@ namespace hex { ImGui::NewLine(); ImGui::TextUnformatted("Name"); ImGui::Separator(); - ImGui::InputText("##nameInput", name.data(), 64); + ImGui::InputText("##nameInput", std::string(name.data()).data(), 64); ImGui::SameLine(); ImGui::ColorEdit4("Color", (float*)&headerColor.Value, ImGuiColorEditFlags_NoInputs | ImGuiColorEditFlags_NoLabel | ImGuiColorEditFlags_NoAlpha); color = headerColor; ImGui::NewLine(); ImGui::TextUnformatted("Comment"); ImGui::Separator(); - ImGui::InputTextMultiline("##colorInput", comment.data(), 0xF'FFFF); + ImGui::InputTextMultiline("##colorInput", std::string(comment.data()).data(), 0xF'FFFF); ImGui::NewLine(); } @@ -113,8 +115,8 @@ namespace hex { id++; } - if (bookmarkToRemove != this->m_bookmarks.end()) { - this->m_bookmarks.erase(bookmarkToRemove); + if (bookmarkToRemove != bookmarks.end()) { + bookmarks.erase(bookmarkToRemove); ProjectFile::markDirty(); } diff --git a/source/views/view_command_palette.cpp b/source/views/view_command_palette.cpp index e7be81cc0..f95ad3fe0 100644 --- a/source/views/view_command_palette.cpp +++ b/source/views/view_command_palette.cpp @@ -31,18 +31,6 @@ namespace hex { return hex::format("#%s = ???", input.data()); }); - ContentRegistry::CommandPaletteCommands::add( - ContentRegistry::CommandPaletteCommands::Type::KeywordCommand, - "/bm", "Create Bookmark", - [](auto input) { - Bookmark bookmark; - bookmark.name.resize(64); - std::strncpy(bookmark.name.data(), input.c_str(), bookmark.name.size()); - - View::postEvent(Events::AddBookmark, &bookmark); - return ""; - }); - this->m_lastResults = this->getCommandResults(""); } diff --git a/source/views/view_hexeditor.cpp b/source/views/view_hexeditor.cpp index d25ee7b4c..4a1a79582 100644 --- a/source/views/view_hexeditor.cpp +++ b/source/views/view_hexeditor.cpp @@ -1,6 +1,7 @@ #include "views/view_hexeditor.hpp" #include +#include #include "providers/file_provider.hpp" #include @@ -15,8 +16,8 @@ namespace hex { - ViewHexEditor::ViewHexEditor(std::vector &patternData, const std::list &bookmarks) - : View("Hex Editor"), m_patternData(patternData), m_bookmarks(bookmarks) { + ViewHexEditor::ViewHexEditor(std::vector &patternData) + : View("Hex Editor"), m_patternData(patternData) { this->m_memoryEditor.ReadFn = [](const ImU8 *data, size_t off) -> ImU8 { auto provider = SharedData::currentProvider; @@ -44,7 +45,7 @@ namespace hex { std::optional currColor, prevColor; - for (const auto &[region, name, comment, color] : _this->m_bookmarks) { + for (const auto &[region, name, comment, color] : ImHexApi::Bookmarks::getEntries()) { if (off >= region.address && off < (region.address + region.size)) currColor = (color & 0x00FFFFFF) | 0x80000000; if ((off - 1) >= region.address && (off - 1) < (region.address + region.size)) @@ -74,11 +75,9 @@ namespace hex { }; this->m_memoryEditor.HoverFn = [](const ImU8 *data, size_t addr) { - ViewHexEditor *_this = (ViewHexEditor *) data; - bool tooltipShown = false; - for (const auto &[region, name, comment, color] : _this->m_bookmarks) { + for (const auto &[region, name, comment, color] : ImHexApi::Bookmarks::getEntries()) { if (addr >= region.address && addr < (region.address + region.size)) { if (!tooltipShown) { ImGui::BeginTooltip(); @@ -1041,14 +1040,13 @@ R"( if (ImGui::MenuItem("Create bookmark", nullptr, false, this->m_memoryEditor.DataPreviewAddr != -1 && this->m_memoryEditor.DataPreviewAddrEnd != -1)) { size_t start = std::min(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); size_t end = std::max(this->m_memoryEditor.DataPreviewAddr, this->m_memoryEditor.DataPreviewAddrEnd); - Bookmark bookmark = { start, end - start + 1, { }, { } }; - View::postEvent(Events::AddBookmark, &bookmark); + ImHexApi::Bookmarks::add(start, end - start + 1, { }, { }); } auto provider = SharedData::currentProvider; if (ImGui::MenuItem("Set base address", nullptr, false, provider != nullptr && provider->isReadable())) { - std::memset(this->m_baseAddressBuffer, sizeof(this->m_baseAddressBuffer), 0x00); + std::memset(this->m_baseAddressBuffer, 0x00, sizeof(this->m_baseAddressBuffer)); View::doLater([]{ ImGui::OpenPopup("Set base address"); }); } }