From 89a96c6d2552fc29c9538a3126a64bb68b1ed66f Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 7 Feb 2023 14:18:59 +0100 Subject: [PATCH] sys: Improve pattern visualizer API --- lib/external/pattern_language | 2 +- .../include/hex/api/content_registry.hpp | 5 +- .../builtin/source/content/pl_visualizers.cpp | 84 +++++++------------ plugins/builtin/source/ui/pattern_drawer.cpp | 2 +- 4 files changed, 33 insertions(+), 60 deletions(-) diff --git a/lib/external/pattern_language b/lib/external/pattern_language index 42fbcf258..565df6232 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit 42fbcf258505d44628143f9dafd44f0c586f1d74 +Subproject commit 565df62329f586842211b41788f9fec6dfa99150 diff --git a/lib/libimhex/include/hex/api/content_registry.hpp b/lib/libimhex/include/hex/api/content_registry.hpp index 38a1ca885..84dad16e8 100644 --- a/lib/libimhex/include/hex/api/content_registry.hpp +++ b/lib/libimhex/include/hex/api/content_registry.hpp @@ -9,9 +9,10 @@ #include #include -#include +#include #include #include +#include #include #include @@ -117,7 +118,7 @@ namespace hex { /* Pattern Language Function Registry. Allows adding of new functions that may be used inside the pattern language */ namespace PatternLanguage { - using VisualizerFunctionCallback = std::function &)>; + using VisualizerFunctionCallback = std::function)>; namespace impl { diff --git a/plugins/builtin/source/content/pl_visualizers.cpp b/plugins/builtin/source/content/pl_visualizers.cpp index cf7905267..b47c05f7e 100644 --- a/plugins/builtin/source/content/pl_visualizers.cpp +++ b/plugins/builtin/source/content/pl_visualizers.cpp @@ -26,38 +26,12 @@ namespace hex::plugin::builtin { template std::vector patternToArray(pl::ptrn::Pattern *pattern){ + const auto bytes = pattern->getBytes(); + std::vector result; - result.resize(pattern->getChildren().size()); - - if (dynamic_cast(pattern) != nullptr && pattern->getSize() == 0) - return result; - - if (auto iteratable = dynamic_cast(pattern); iteratable != nullptr) { - iteratable->forEachEntry(0, iteratable->getEntryCount(), [&](u64, pl::ptrn::Pattern *entry) { - const auto children = entry->getChildren(); - for (const auto &[offset, child] : children) { - auto startOffset = child->getOffset(); - - child->setOffset(offset); - ON_SCOPE_EXIT { child->setOffset(startOffset); }; - - T value; - if constexpr (std::floating_point) - value = child->getValue().toFloatingPoint(); - else if constexpr (std::signed_integral) - value = child->getValue().toSigned(); - else if constexpr (std::unsigned_integral) - value = child->getValue().toUnsigned(); - else - static_assert(hex::always_false::value, "Invalid type"); - - result.push_back(value); - } - }); - } else { - result.resize(pattern->getSize() / sizeof(float)); - pattern->getEvaluator()->readData(pattern->getOffset(), result.data(), result.size() * sizeof(float), pattern->getSection()); - } + result.resize(bytes.size() / sizeof(T)); + for (size_t i = 0; i < result.size(); i++) + std::memcpy(&result[i], &bytes[i * sizeof(T)], sizeof(T)); return result; } @@ -80,7 +54,7 @@ namespace hex::plugin::builtin { namespace { - void drawLinePlotVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, const std::vector &arguments) { + void drawLinePlotVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, std::span arguments) { static std::vector values; auto dataPattern = arguments[0].toPattern(); @@ -99,7 +73,7 @@ namespace hex::plugin::builtin { } } - void drawScatterPlotVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, const std::vector &arguments) { + void drawScatterPlotVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, std::span arguments) { static std::vector xValues, yValues; auto xPattern = arguments[0].toPattern(); @@ -121,14 +95,12 @@ namespace hex::plugin::builtin { } } - void drawImageVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, const std::vector &arguments) { + void drawImageVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, std::span arguments) { static ImGui::Texture texture; if (shouldReset) { - auto pattern = arguments[1].toPattern(); + auto pattern = arguments[0].toPattern(); - std::vector data; - data.resize(pattern->getSize()); - pattern->getEvaluator()->readData(pattern->getOffset(), data.data(), data.size(), pattern->getSection()); + auto data = pattern->getBytes(); texture = ImGui::Texture(data.data(), data.size()); } @@ -136,14 +108,14 @@ namespace hex::plugin::builtin { ImGui::Image(texture, texture.getSize()); } - void drawBitmapVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, const std::vector &arguments) { + void drawBitmapVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, std::span arguments) { static ImGui::Texture texture; if (shouldReset) { - auto pattern = arguments[1].toPattern(); - auto width = arguments[2].toUnsigned(); - auto height = arguments[3].toUnsigned(); + auto pattern = arguments[0].toPattern(); + auto width = arguments[1].toUnsigned(); + auto height = arguments[2].toUnsigned(); - auto data = patternToArray(pattern); + auto data = pattern->getBytes(); texture = ImGui::Texture(data.data(), data.size(), width, height); } @@ -151,7 +123,7 @@ namespace hex::plugin::builtin { ImGui::Image(texture, texture.getSize()); } - void drawDisassemblyVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, const std::vector &arguments) { + void drawDisassemblyVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, std::span arguments) { struct Disassembly { u64 address; std::vector bytes; @@ -160,10 +132,10 @@ namespace hex::plugin::builtin { static std::vector disassembly; if (shouldReset) { - auto pattern = arguments[1].toPattern(); - auto baseAddress = arguments[2].toUnsigned(); - auto architecture = arguments[3].toUnsigned(); - auto mode = arguments[4].toUnsigned(); + auto pattern = arguments[0].toPattern(); + auto baseAddress = arguments[1].toUnsigned(); + auto architecture = arguments[2].toUnsigned(); + auto mode = arguments[3].toUnsigned(); disassembly.clear(); @@ -171,7 +143,7 @@ namespace hex::plugin::builtin { if (cs_open(static_cast(architecture), static_cast(mode), &capstone) == CS_ERR_OK) { cs_option(capstone, CS_OPT_SKIPDATA, CS_OPT_ON); - auto data = patternToArray(pattern); + auto data = pattern->getBytes(); cs_insn *instructions = nullptr; size_t instructionCount = cs_disasm(capstone, data.data(), data.size(), baseAddress, 0, &instructions); @@ -207,9 +179,9 @@ namespace hex::plugin::builtin { } } - void draw3DVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, const std::vector &arguments) { - auto verticesPattern = arguments[1].toPattern(); - auto indicesPattern = arguments[2].toPattern(); + void draw3DVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, std::span arguments) { + auto verticesPattern = arguments[0].toPattern(); + auto indicesPattern = arguments[1].toPattern(); static ImGui::Texture texture; static float scaling = 0.5F; @@ -329,10 +301,10 @@ namespace hex::plugin::builtin { ImGui::Image(texture, texture.getSize(), ImVec2(0, 1), ImVec2(1, 0)); } - void drawSoundVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, const std::vector &arguments) { - auto wavePattern = arguments[1].toPattern(); - auto channels = arguments[2].toUnsigned(); - auto sampleRate = arguments[3].toUnsigned(); + void drawSoundVisualizer(pl::ptrn::Pattern &, pl::ptrn::Iteratable &, bool shouldReset, std::span arguments) { + auto wavePattern = arguments[0].toPattern(); + auto channels = arguments[1].toUnsigned(); + auto sampleRate = arguments[2].toUnsigned(); static std::vector waveData, sampledData; static ma_device audioDevice; diff --git a/plugins/builtin/source/ui/pattern_drawer.cpp b/plugins/builtin/source/ui/pattern_drawer.cpp index 8f7f4d96a..813e2f39e 100644 --- a/plugins/builtin/source/ui/pattern_drawer.cpp +++ b/plugins/builtin/source/ui/pattern_drawer.cpp @@ -134,7 +134,7 @@ namespace hex::plugin::builtin::ui { ImGui::TextUnformatted("hex.builtin.pattern_drawer.visualizer.invalid_parameter_count"_lang); } else { try { - visualizer.callback(pattern, iteratable, reset, arguments); + visualizer.callback(pattern, iteratable, reset, { arguments.begin() + 1, arguments.end() }); } catch (std::exception &e) { this->m_lastVisualizerError = e.what(); }