From c769e9cc32c991e0fc544a9de8e4762386df380a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 28 Sep 2022 15:01:43 +0200 Subject: [PATCH] ui: Highlight modified bytes in red --- lib/libimhex/include/hex/api/imhex_api.hpp | 2 +- lib/libimhex/source/providers/provider.cpp | 2 ++ .../include/content/views/view_patches.hpp | 2 +- .../source/content/views/view_bookmarks.cpp | 2 +- .../source/content/views/view_find.cpp | 2 +- .../source/content/views/view_hex_editor.cpp | 19 +++++++++++++++---- .../source/content/views/view_patches.cpp | 11 +++++++++-- .../content/views/view_pattern_editor.cpp | 2 +- 8 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index faac3c1f9..485624618 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -66,7 +66,7 @@ namespace hex { namespace impl { - using HighlightingFunction = std::function(u64, const u8*, size_t)>; + using HighlightingFunction = std::function(u64, const u8*, size_t, bool)>; std::map &getBackgroundHighlights(); std::map &getBackgroundHighlightingFunctions(); diff --git a/lib/libimhex/source/providers/provider.cpp b/lib/libimhex/source/providers/provider.cpp index 4ab7f8675..2ee377fbc 100644 --- a/lib/libimhex/source/providers/provider.cpp +++ b/lib/libimhex/source/providers/provider.cpp @@ -113,6 +113,8 @@ namespace hex::prv { this->writeRaw(patchAddress - this->getBaseAddress(), &patch, 1); } this->markDirty(); + + this->m_patches.emplace_back(); } diff --git a/plugins/builtin/include/content/views/view_patches.hpp b/plugins/builtin/include/content/views/view_patches.hpp index ec782dd7f..9f8bdfd79 100644 --- a/plugins/builtin/include/content/views/view_patches.hpp +++ b/plugins/builtin/include/content/views/view_patches.hpp @@ -12,7 +12,7 @@ namespace hex::plugin::builtin { class ViewPatches : public View { public: explicit ViewPatches(); - ~ViewPatches() override; + ~ViewPatches() override = default; void drawContent() override; diff --git a/plugins/builtin/source/content/views/view_bookmarks.cpp b/plugins/builtin/source/content/views/view_bookmarks.cpp index 5a9c8c162..9ac274e87 100644 --- a/plugins/builtin/source/content/views/view_bookmarks.cpp +++ b/plugins/builtin/source/content/views/view_bookmarks.cpp @@ -33,7 +33,7 @@ namespace hex::plugin::builtin { ImHexApi::Provider::markDirty(); }); - ImHexApi::HexEditor::addBackgroundHighlightingProvider([](u64 address, const u8* data, size_t size) -> std::optional { + ImHexApi::HexEditor::addBackgroundHighlightingProvider([](u64 address, const u8* data, size_t size, bool) -> std::optional { hex::unused(data); for (const auto &bookmark : ProviderExtraData::getCurrent().bookmarks) { diff --git a/plugins/builtin/source/content/views/view_find.cpp b/plugins/builtin/source/content/views/view_find.cpp index d004672af..1957b1034 100644 --- a/plugins/builtin/source/content/views/view_find.cpp +++ b/plugins/builtin/source/content/views/view_find.cpp @@ -16,7 +16,7 @@ namespace hex::plugin::builtin { ViewFind::ViewFind() : View("hex.builtin.view.find.name") { const static auto HighlightColor = [] { return (ImGui::GetCustomColorU32(ImGuiCustomCol_ToolbarPurple) & 0x00FFFFFF) | 0x70000000; }; - ImHexApi::HexEditor::addBackgroundHighlightingProvider([this](u64 address, const u8* data, size_t size) -> std::optional { + ImHexApi::HexEditor::addBackgroundHighlightingProvider([this](u64 address, const u8* data, size_t size, bool) -> std::optional { hex::unused(data, size); if (this->m_searchTask.isRunning()) diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index d37fd69ed..47c92bba4 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -460,9 +460,12 @@ namespace hex::plugin::builtin { this->registerEvents(); this->registerMenuItems(); - ImHexApi::HexEditor::addForegroundHighlightingProvider([this](u64 address, const u8 *data, size_t size) -> std::optional { + ImHexApi::HexEditor::addForegroundHighlightingProvider([this](u64 address, const u8 *data, size_t size, bool hasColor) -> std::optional { hex::unused(address); + if (hasColor) + return std::nullopt; + if (!this->m_grayOutZero) return std::nullopt; @@ -484,11 +487,15 @@ namespace hex::plugin::builtin { } static std::optional queryBackgroundColor(u64 address, const u8 *data, size_t size) { + std::optional result; for (const auto &[id, callback] : ImHexApi::HexEditor::impl::getBackgroundHighlightingFunctions()) { - if (auto color = callback(address, data, size); color.has_value()) + if (auto color = callback(address, data, size, result.has_value()); color.has_value()) return color.value(); } + if (result.has_value()) + return result; + for (const auto &[id, highlighting] : ImHexApi::HexEditor::impl::getBackgroundHighlights()) { if (highlighting.getRegion().overlaps({ address, size })) return highlighting.getColor(); @@ -498,11 +505,15 @@ namespace hex::plugin::builtin { } static std::optional queryForegroundColor(u64 address, const u8 *data, size_t size) { + std::optional result; for (const auto &[id, callback] : ImHexApi::HexEditor::impl::getForegroundHighlightingFunctions()) { - if (auto color = callback(address, data, size); color.has_value()) - return color.value(); + if (auto color = callback(address, data, size, result.has_value()); color.has_value()) + result = color; } + if (result.has_value()) + return result; + for (const auto &[id, highlighting] : ImHexApi::HexEditor::impl::getForegroundHighlights()) { if (highlighting.getRegion().overlaps({ address, size })) return highlighting.getColor(); diff --git a/plugins/builtin/source/content/views/view_patches.cpp b/plugins/builtin/source/content/views/view_patches.cpp index d45500f34..1e887f128 100644 --- a/plugins/builtin/source/content/views/view_patches.cpp +++ b/plugins/builtin/source/content/views/view_patches.cpp @@ -29,10 +29,17 @@ namespace hex::plugin::builtin { return true; } }); - } - ViewPatches::~ViewPatches() { + ImHexApi::HexEditor::addForegroundHighlightingProvider([](u64 offset, const u8* buffer, size_t, bool) -> std::optional { + if (!ImHexApi::Provider::isValid()) + return std::nullopt; + auto &patches = ImHexApi::Provider::get()->getPatches(); + if (patches.contains(offset) && patches[offset] != buffer[0]) + return ImGui::GetCustomColorU32(ImGuiCustomCol_ToolbarRed); + else + return std::nullopt; + }); } void ViewPatches::drawContent() { diff --git a/plugins/builtin/source/content/views/view_pattern_editor.cpp b/plugins/builtin/source/content/views/view_pattern_editor.cpp index 9dc5afeb6..56e39e461 100644 --- a/plugins/builtin/source/content/views/view_pattern_editor.cpp +++ b/plugins/builtin/source/content/views/view_pattern_editor.cpp @@ -250,7 +250,7 @@ namespace hex::plugin::builtin { }); - ImHexApi::HexEditor::addBackgroundHighlightingProvider([this](u64 address, const u8 *data, size_t size) -> std::optional { + ImHexApi::HexEditor::addBackgroundHighlightingProvider([this](u64 address, const u8 *data, size_t size, bool) -> std::optional { hex::unused(data, size); if (this->m_runningEvaluators != 0)