1
0
mirror of synced 2025-01-31 03:53:44 +01:00

sys: Add highlighting provider function support, move pattern highlighting code out of hex editor

This commit is contained in:
WerWolv 2022-02-16 14:57:13 +01:00
parent b9030d7e47
commit 7117592f38
6 changed files with 83 additions and 30 deletions

View File

@ -3,8 +3,9 @@
#include <hex.hpp>
#include <list>
#include <vector>
#include <optional>
#include <string>
#include <vector>
#include <hex/helpers/concepts.hpp>
#include <hex/api/task.hpp>
@ -32,21 +33,32 @@ namespace hex {
class Highlighting {
public:
Highlighting() = default;
Highlighting(Region region, color_t color, const std::string &tooltip = "");
Highlighting(Region region, color_t color, std::string tooltip = "");
[[nodiscard]] const Region &getRegion() const { return this->m_region; }
[[nodiscard]] const color_t &getColor() const { return this->m_color; }
[[nodiscard]] const std::string &getTooltip() const { return this->m_tooltip; }
private:
Region m_region;
color_t m_color;
Region m_region = {};
color_t m_color = 0x00;
std::string m_tooltip;
};
u32 addHighlight(const Region &region, color_t color, std::string tooltip = "");
namespace impl {
using HighlightingFunction = std::function<std::optional<Highlighting>(u64)>;
std::map<u32, Highlighting> &getHighlights();
std::map<u32, HighlightingFunction> &getHighlightingFunctions();
}
u32 addHighlight(const Region &region, color_t color, const std::string &tooltip = "");
void removeHighlight(u32 id);
std::map<u32, Highlighting> &getHighlights();
u32 addHighlightingProvider(const impl::HighlightingFunction &function);
void removeHighlightingProvider(u32 id);
Region getSelection();
void setSelection(const Region &region);

View File

@ -2,6 +2,7 @@
#include <hex/api/event.hpp>
#include <hex/providers/provider.hpp>
#include <utility>
#include <unistd.h>
@ -26,16 +27,29 @@ namespace hex {
namespace ImHexApi::HexEditor {
static std::map<u32, ImHexApi::HexEditor::Highlighting> s_highlights;
Highlighting::Highlighting(Region region, color_t color, const std::string &tooltip)
: m_region(region), m_color(color), m_tooltip(tooltip) {
Highlighting::Highlighting(Region region, color_t color, std::string tooltip)
: m_region(region), m_color(color), m_tooltip(std::move(tooltip)) {
}
u32 addHighlight(const Region &region, color_t color, std::string tooltip) {
auto id = s_highlights.size();
namespace impl {
s_highlights.insert({
static std::map<u32, Highlighting> s_highlights;
std::map<u32, Highlighting> &getHighlights() {
return s_highlights;
}
static std::map<u32, HighlightingFunction> s_highlightingFunctions;
std::map<u32, HighlightingFunction> &getHighlightingFunctions() {
return s_highlightingFunctions;
}
}
u32 addHighlight(const Region &region, color_t color, const std::string &tooltip) {
auto &highlights = impl::getHighlights();
auto id = highlights.size();
highlights.insert({
id, Highlighting {region, color, tooltip}
});
@ -45,13 +59,27 @@ namespace hex {
}
void removeHighlight(u32 id) {
s_highlights.erase(id);
impl::getHighlights().erase(id);
EventManager::post<EventHighlightingChanged>();
}
std::map<u32, Highlighting> &getHighlights() {
return s_highlights;
u32 addHighlightingProvider(const impl::HighlightingFunction &function) {
auto &highlightFuncs = impl::getHighlightingFunctions();
auto id = highlightFuncs.size();
highlightFuncs.insert({ id, function });
EventManager::post<EventHighlightingChanged>();
return id;
}
void removeHighlightingProvider(u32 id) {
impl::getHighlightingFunctions().erase(id);
EventManager::post<EventHighlightingChanged>();
}
Region getSelection() {

View File

@ -165,7 +165,8 @@ namespace hex::init {
bool deleteSharedData() {
ImHexApi::System::getInitArguments().clear();
ImHexApi::Tasks::getDeferredCalls().clear();
ImHexApi::HexEditor::getHighlights().clear();
ImHexApi::HexEditor::impl::getHighlights().clear();
ImHexApi::HexEditor::impl::getHighlightingFunctions().clear();
while (ImHexApi::Provider::isValid())
ImHexApi::Provider::remove(ImHexApi::Provider::get());

View File

@ -43,9 +43,9 @@ namespace hex::plugin::builtin {
std::vector<char> m_searchStringBuffer;
std::vector<char> m_searchHexBuffer;
SearchFunction m_searchFunction = nullptr;
std::vector<std::pair<u64, u64>> *m_lastSearchBuffer;
bool m_searchRequested = false;
SearchFunction m_searchFunction = nullptr;
std::vector<std::pair<u64, u64>> *m_lastSearchBuffer = nullptr;
bool m_searchRequested = false;
i64 m_lastSearchIndex = 0;
std::vector<std::pair<u64, u64>> m_lastStringSearch;
@ -53,7 +53,7 @@ namespace hex::plugin::builtin {
u64 m_gotoAddressAbsolute = 0;
i64 m_gotoAddressRelative = 0;
bool m_gotoRequested = false;
bool m_gotoRequested = false;
char m_baseAddressBuffer[0x20] = { 0 };
u64 m_resizeSize = 0;

View File

@ -1,6 +1,8 @@
#include "content/views/view_hex_editor.hpp"
#include <hex/api/imhex_api.hpp>
#include <hex/api/content_registry.hpp>
#include <hex/providers/provider.hpp>
#include <hex/helpers/crypto.hpp>
#include <hex/helpers/file.hpp>
@ -9,7 +11,6 @@
#include <hex/helpers/project_file_handler.hpp>
#include <hex/helpers/loader_script_handler.hpp>
#include <hex/pattern_language/pattern_data.hpp>
#include <content/providers/file_provider.hpp>
@ -97,7 +98,7 @@ namespace hex::plugin::builtin {
std::optional<color_t> highlightColor;
std::string highlightTooltip;
for (const auto &[id, highlight] : ImHexApi::HexEditor::getHighlights()) {
for (const auto &[id, highlight] : ImHexApi::HexEditor::impl::getHighlights()) {
auto &region = highlight.getRegion();
auto &color = highlight.getColor();
auto &tooltip = highlight.getTooltip();
@ -108,13 +109,11 @@ namespace hex::plugin::builtin {
}
}
auto patterns = provider->getPatternLanguageRuntime().getPatterns();
for (const auto &pattern : patterns) {
auto child = pattern->getPattern(blockStartOffset + i);
if (child != nullptr) {
auto color = (child->getColor() & 0x00FFFFFF) | alpha;
highlightColor = highlightColor.has_value() ? ImAlphaBlendColors(color, highlightColor.value()) : color;
break;
for (const auto &[id, function] : ImHexApi::HexEditor::impl::getHighlightingFunctions()) {
auto highlight = function(blockStartOffset + i);
if (highlight.has_value()) {
highlightColor = highlightColor.has_value() ? ImAlphaBlendColors(highlight->getColor(), highlightColor.value()) : highlight->getColor();
highlightTooltip = highlight->getTooltip();
}
}

View File

@ -222,6 +222,19 @@ namespace hex::plugin::builtin {
});
}
});
ImHexApi::HexEditor::addHighlightingProvider([](u64 address) -> std::optional<ImHexApi::HexEditor::Highlighting> {
auto patterns = ImHexApi::Provider::get()->getPatternLanguageRuntime().getPatterns();
for (const auto &pattern : patterns) {
auto child = pattern->getPattern(address);
if (child != nullptr) {
return ImHexApi::HexEditor::Highlighting(Region { address, 1 }, child->getColor(), child->getVariableName());
}
}
return std::nullopt;
});
}
ViewPatternEditor::~ViewPatternEditor() {