1
0
mirror of synced 2024-11-26 00:20:50 +01:00
ImHex/plugins/builtin/source/ui/pattern_drawer.cpp

914 lines
38 KiB
C++
Raw Normal View History

#include <ui/pattern_drawer.hpp>
#include <pl/patterns/pattern_array_dynamic.hpp>
#include <pl/patterns/pattern_array_static.hpp>
#include <pl/patterns/pattern_bitfield.hpp>
#include <pl/patterns/pattern_boolean.hpp>
#include <pl/patterns/pattern_character.hpp>
#include <pl/patterns/pattern_enum.hpp>
#include <pl/patterns/pattern_float.hpp>
#include <pl/patterns/pattern_padding.hpp>
#include <pl/patterns/pattern_pointer.hpp>
#include <pl/patterns/pattern_signed.hpp>
#include <pl/patterns/pattern_string.hpp>
#include <pl/patterns/pattern_struct.hpp>
#include <pl/patterns/pattern_union.hpp>
#include <pl/patterns/pattern_unsigned.hpp>
#include <pl/patterns/pattern_wide_character.hpp>
#include <pl/patterns/pattern_wide_string.hpp>
#include <string>
#include <hex/api/imhex_api.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/api/localization.hpp>
2022-12-16 11:20:39 +01:00
#include <content/helpers/math_evaluator.hpp>
#include <hex/helpers/disassembler.hpp>
#include <imgui.h>
#include <implot.h>
#include <hex/ui/imgui_imhex_extensions.h>
namespace hex::plugin::builtin::ui {
namespace {
constexpr auto DisplayEndDefault = 50U;
constexpr auto DisplayEndStep = 50U;
using namespace ::std::literals::string_literals;
bool isPatternSelected(u64 address, u64 size) {
auto currSelection = ImHexApi::HexEditor::getSelection();
if (!currSelection.has_value())
return false;
return Region{ address, size }.overlaps(*currSelection);
}
template<typename T>
auto highlightWhenSelected(u64 address, u64 size, const T &callback) {
constexpr bool HasReturn = !requires(T t) { { t() } -> std::same_as<void>; };
auto selected = isPatternSelected(address, size);
if (selected)
ImGui::PushStyleColor(ImGuiCol_Text, ImGui::GetStyleColorVec4(ImGuiCol_HeaderActive));
if constexpr (HasReturn) {
auto result = callback();
if (selected)
ImGui::PopStyleColor();
return result;
} else {
callback();
if (selected)
ImGui::PopStyleColor();
}
}
template<typename T>
auto highlightWhenSelected(const pl::ptrn::Pattern& pattern, const T &callback) {
return highlightWhenSelected(pattern.getOffset(), pattern.getSize(), callback);
}
void drawTypenameColumn(const pl::ptrn::Pattern& pattern, const std::string& pattern_name) {
ImGui::TextFormattedColored(ImColor(0xFFD69C56), pattern_name);
ImGui::SameLine();
ImGui::TextUnformatted(pattern.getTypeName().c_str());
ImGui::TableNextColumn();
}
void drawNameColumn(const pl::ptrn::Pattern& pattern) {
highlightWhenSelected(pattern, [&]{ ImGui::TextUnformatted(pattern.getDisplayName().c_str()); });
ImGui::TableNextColumn();
}
void drawColorColumn(const pl::ptrn::Pattern& pattern) {
ImGui::ColorButton("color", ImColor(pattern.getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
ImGui::TableNextColumn();
}
void drawOffsetColumn(const pl::ptrn::Pattern& pattern) {
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", pattern.getOffset(), pattern.getOffset() + pattern.getSize() - (pattern.getSize() == 0 ? 0 : 1));
ImGui::TableNextColumn();
}
void drawSizeColumn(const pl::ptrn::Pattern& pattern) {
ImGui::TextFormatted("0x{0:04X}", pattern.getSize());
ImGui::TableNextColumn();
}
void drawCommentTooltip(const pl::ptrn::Pattern &pattern) {
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem)) {
if (auto comment = pattern.getComment(); !comment.empty()) {
ImGui::BeginTooltip();
ImGui::TextUnformatted(pattern.getComment().c_str());
ImGui::EndTooltip();
}
}
}
void drawVisualizer(const std::vector<pl::core::Token::Literal> &arguments, pl::ptrn::Pattern &pattern, pl::ptrn::Iteratable &iteratable, bool reset) {
2023-01-12 11:18:36 +01:00
auto visualizer = pl::core::Token::literalToString(arguments.front(), true);
if (visualizer == "line_plot") {
if (ImPlot::BeginPlot("##plot", ImVec2(400, 250), ImPlotFlags_NoChild | ImPlotFlags_CanvasOnly)) {
ImPlot::SetupAxes("X", "Y", ImPlotAxisFlags_AutoFit, ImPlotAxisFlags_AutoFit);
ImPlot::PlotLineG("##line", [](void *data, int idx) -> ImPlotPoint {
auto &iteratable = *static_cast<pl::ptrn::Iteratable *>(data);
return { static_cast<double>(idx), pl::core::Token::literalToFloatingPoint(iteratable.getEntry(idx)->getValue()) };
}, &iteratable, iteratable.getEntryCount());
ImPlot::EndPlot();
}
} else if (visualizer == "image") {
static ImGui::Texture texture;
if (reset) {
std::vector<u8> data;
data.resize(pattern.getSize());
pattern.getEvaluator()->readData(pattern.getOffset(), data.data(), data.size(), pattern.getSection());
texture = ImGui::Texture(data.data(), data.size());
}
if (texture.isValid())
ImGui::Image(texture, texture.getSize());
} else if (visualizer == "bitmap" && arguments.size() == 3) {
static ImGui::Texture texture;
if (reset) {
2023-01-12 11:18:36 +01:00
auto width = pl::core::Token::literalToUnsigned(arguments[1]);
auto height = pl::core::Token::literalToUnsigned(arguments[2]);
std::vector<u8> data;
data.resize(width * height * 4);
pattern.getEvaluator()->readData(pattern.getOffset(), data.data(), data.size(), pattern.getSection());
texture = ImGui::Texture(data.data(), data.size(), width, height);
}
if (texture.isValid())
ImGui::Image(texture, texture.getSize());
} else if (visualizer == "disassembler" && arguments.size() == 4) {
struct Disassembly {
u64 address;
std::vector<u8> bytes;
std::string instruction;
};
static std::vector<Disassembly> disassembly;
if (reset) {
auto baseAddress = pl::core::Token::literalToUnsigned(arguments[1]);
auto architecture = pl::core::Token::literalToUnsigned(arguments[2]);
auto mode = pl::core::Token::literalToUnsigned(arguments[3]);
disassembly.clear();
csh capstone;
if (cs_open(static_cast<cs_arch>(architecture), static_cast<cs_mode>(mode), &capstone) == CS_ERR_OK) {
cs_option(capstone, CS_OPT_SKIPDATA, CS_OPT_ON);
std::vector<u8> data;
data.resize(pattern.getSize());
pattern.getEvaluator()->readData(pattern.getOffset(), data.data(), data.size(), pattern.getSection());
cs_insn *instructions = nullptr;
size_t instructionCount = cs_disasm(capstone, data.data(), data.size(), baseAddress, 0, &instructions);
for (size_t i = 0; i < instructionCount; i++) {
disassembly.push_back({ instructions[i].address, { instructions[i].bytes, instructions[i].bytes + instructions[i].size }, hex::format("{} {}", instructions[i].mnemonic, instructions[i].op_str) });
}
cs_free(instructions, instructionCount);
cs_close(&capstone);
}
}
if (ImGui::BeginTable("##disassembly", 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_Resizable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_ScrollY, scaled(ImVec2(0, 300)))) {
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("hex.builtin.common.address"_lang);
ImGui::TableSetupColumn("hex.builtin.common.bytes"_lang);
ImGui::TableSetupColumn("hex.builtin.common.instruction"_lang);
ImGui::TableHeadersRow();
for (auto &entry : disassembly) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:08X}", entry.address);
ImGui::TableNextColumn();
std::string bytes;
for (auto byte : entry.bytes)
bytes += hex::format("{0:02X} ", byte);
ImGui::TextUnformatted(bytes.c_str());
ImGui::TableNextColumn();
ImGui::TextUnformatted(entry.instruction.c_str());
}
2023-01-12 11:18:36 +01:00
ImGui::EndTable();
2023-01-12 11:18:36 +01:00
}
} else {
ImGui::TextUnformatted("hex.builtin.pattern_drawer.unknown_visualizer"_lang);
}
}
}
void PatternDrawer::createLeafNode(const pl::ptrn::Pattern& pattern) {
ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf |
ImGuiTreeNodeFlags_NoTreePushOnOpen |
ImGuiTreeNodeFlags_SpanFullWidth |
ImGuiTreeNodeFlags_AllowItemOverlap);
}
bool PatternDrawer::createTreeNode(const pl::ptrn::Pattern& pattern) {
if (pattern.isSealed()) {
ImGui::Indent();
highlightWhenSelected(pattern, [&]{ ImGui::TextUnformatted(pattern.getDisplayName().c_str()); });
ImGui::Unindent();
return false;
}
else {
return highlightWhenSelected(pattern, [&]{
switch (this->m_treeStyle) {
using enum TreeStyle;
default:
case Default:
return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
case AutoExpanded:
return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_SpanFullWidth);
case Flattened:
return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth);
}
});
}
}
void PatternDrawer::makeSelectable(const pl::ptrn::Pattern &pattern) {
ImGui::PushID(static_cast<int>(pattern.getOffset()));
ImGui::PushID(pattern.getVariableName().c_str());
if (ImGui::Selectable("##PatternLine", false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
ImHexApi::HexEditor::setSelection(pattern.getOffset(), pattern.getSize());
this->m_editingPattern = nullptr;
}
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
this->m_editingPattern = &pattern;
ImGui::SameLine(0, 0);
ImGui::PopID();
ImGui::PopID();
}
void PatternDrawer::createDefaultEntry(pl::ptrn::Pattern &pattern) {
ImGui::TableNextRow();
createLeafNode(pattern);
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
ImGui::SameLine();
drawNameColumn(pattern);
drawColorColumn(pattern);
drawOffsetColumn(pattern);
drawSizeColumn(pattern);
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", pattern.getFormattedName().empty() ? pattern.getTypeName() : pattern.getFormattedName());
ImGui::TableNextColumn();
}
void PatternDrawer::closeTreeNode(bool inlined) {
if (!inlined && this->m_treeStyle != TreeStyle::Flattened)
ImGui::TreePop();
}
void PatternDrawer::visit(pl::ptrn::PatternArrayDynamic& pattern) {
drawArray(pattern, pattern, pattern.isInlined());
}
void PatternDrawer::visit(pl::ptrn::PatternArrayStatic& pattern) {
drawArray(pattern, pattern, pattern.isInlined());
}
void PatternDrawer::visit(pl::ptrn::PatternBitfieldField& pattern) {
ImGui::TableNextRow();
createLeafNode(pattern);
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
ImGui::SameLine();
drawNameColumn(pattern);
drawColorColumn(pattern);
auto byteAddr = pattern.getOffset() + pattern.getBitOffset() / 8;
auto firstBitIdx = pattern.getBitOffset() % 8;
2022-08-09 08:38:41 +02:00
auto lastBitIdx = firstBitIdx + (pattern.getBitSize() - 1);
if (firstBitIdx == lastBitIdx)
ImGui::TextFormatted("0x{0:08X} bit {1}", byteAddr, firstBitIdx);
else
ImGui::TextFormatted("0x{0:08X} bits {1} - {2}", byteAddr, firstBitIdx, lastBitIdx);
ImGui::TableNextColumn();
if (pattern.getBitSize() == 1)
ImGui::TextFormatted("{0} bit", pattern.getBitSize());
else
ImGui::TextFormatted("{0} bits", pattern.getBitSize());
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "bits");
ImGui::TableNextColumn();
ui/ux: Rewrite of the entire hex editor view to make it more flexible (#512) * ui/ux: Initial recreation of the hex editor view * ui/ux: Added back support for editing cells * ux: Make scrolling and selecting bytes feel nice again * ui/ux: Improved byte selecting, added footer * sys: Make math evaluator more generic to support integer only calculations * patterns: Moved value formatting into pattern language * ui/ux: Added Goto and Search popups, improved selection * ui: Added better tooltips for bookmarks and patterns * sys: Use worse hex search algorithm on macOS Sadly it still doesn't support `std::boyer_moore_horsepool_searcher` * ui: Added back missing events, menu items and shortcuts * fix: Bookmark highlighting being rendered off by one * fix: Various macOS build errors * fix: size_t is not u64 on macos * fix: std::fmod and std::pow not working with integer types on macos * fix: Missing semicolons * sys: Added proper integer pow function * ui: Added back support for custom encodings * fix: Editor not jumping to selection when selection gets changed * ui: Turn Hexii setting into a data visualizer * sys: Added back remaining shortcuts * sys: Remove old hex editor files * sys: Moved more legacy things away from the hex editor view, updated localization * fix: Hex editor scrolling behaving weirdly and inconsistently * sys: Cleaned up Hex editor code * sys: Added selection color setting, localized all new settings * fix: Search feature not working correctly * ui: Replace custom ImGui::Disabled function with native ImGui ones * ui: Fix bookmark tooltip rendering issues * fix: Another size_t not being 64 bit issue on MacOS
2022-05-27 20:42:07 +02:00
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
void PatternDrawer::visit(pl::ptrn::PatternBitfield& pattern) {
bool open = true;
if (!pattern.isInlined() && this->m_treeStyle != TreeStyle::Flattened) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = createTreeNode(pattern);
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
drawColorColumn(pattern);
drawOffsetColumn(pattern);
drawSizeColumn(pattern);
drawTypenameColumn(pattern, "bitfield");
ui/ux: Rewrite of the entire hex editor view to make it more flexible (#512) * ui/ux: Initial recreation of the hex editor view * ui/ux: Added back support for editing cells * ux: Make scrolling and selecting bytes feel nice again * ui/ux: Improved byte selecting, added footer * sys: Make math evaluator more generic to support integer only calculations * patterns: Moved value formatting into pattern language * ui/ux: Added Goto and Search popups, improved selection * ui: Added better tooltips for bookmarks and patterns * sys: Use worse hex search algorithm on macOS Sadly it still doesn't support `std::boyer_moore_horsepool_searcher` * ui: Added back missing events, menu items and shortcuts * fix: Bookmark highlighting being rendered off by one * fix: Various macOS build errors * fix: size_t is not u64 on macos * fix: std::fmod and std::pow not working with integer types on macos * fix: Missing semicolons * sys: Added proper integer pow function * ui: Added back support for custom encodings * fix: Editor not jumping to selection when selection gets changed * ui: Turn Hexii setting into a data visualizer * sys: Added back remaining shortcuts * sys: Remove old hex editor files * sys: Moved more legacy things away from the hex editor view, updated localization * fix: Hex editor scrolling behaving weirdly and inconsistently * sys: Cleaned up Hex editor code * sys: Added selection color setting, localized all new settings * fix: Search feature not working correctly * ui: Replace custom ImGui::Disabled function with native ImGui ones * ui: Fix bookmark tooltip rendering issues * fix: Another size_t not being 64 bit issue on MacOS
2022-05-27 20:42:07 +02:00
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
if (open) {
int id = 1;
pattern.forEachEntry(0, pattern.getEntryCount(), [&] (u64, auto *field) {
ImGui::PushID(id);
this->draw(*field);
ImGui::PopID();
id += 1;
});
closeTreeNode(pattern.isInlined());
}
}
void PatternDrawer::visit(pl::ptrn::PatternBoolean& pattern) {
createDefaultEntry(pattern);
2022-12-16 11:20:39 +01:00
if (this->m_editingPattern == &pattern) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
2022-12-16 11:20:39 +01:00
bool value = hex::get_or(pattern.getValue(), true) != 0;
if (ImGui::Checkbox(pattern.getFormattedValue().c_str(), &value)) {
pattern.setValue(value);
this->m_editingPattern = nullptr;
}
2022-12-16 11:20:39 +01:00
ImGui::PopItemWidth();
ImGui::PopStyleVar();
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
void PatternDrawer::visit(pl::ptrn::PatternCharacter& pattern) {
createDefaultEntry(pattern);
2022-12-16 11:20:39 +01:00
if (this->m_editingPattern == &pattern) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
auto value = hex::encodeByteString(pattern.getBytes());
if (ImGui::InputText("##Character", value.data(), value.size() + 1, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
if (!value.empty()) {
auto result = hex::decodeByteString(value);
if (!result.empty())
pattern.setValue(char(result[0]));
this->m_editingPattern = nullptr;
}
2022-12-16 11:20:39 +01:00
}
ImGui::PopItemWidth();
ImGui::PopStyleVar();
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
2022-12-16 11:20:39 +01:00
}
}
void PatternDrawer::visit(pl::ptrn::PatternEnum& pattern) {
ImGui::TableNextRow();
createLeafNode(pattern);
drawCommentTooltip(pattern);
ImGui::TableNextColumn();
makeSelectable(pattern);
ImGui::SameLine();
drawNameColumn(pattern);
drawColorColumn(pattern);
drawOffsetColumn(pattern);
drawSizeColumn(pattern);
drawTypenameColumn(pattern, "enum");
2022-12-16 11:20:39 +01:00
if (this->m_editingPattern == &pattern) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
2022-12-16 11:20:39 +01:00
if (ImGui::BeginCombo("##Enum", pattern.getFormattedValue().c_str())) {
auto currValue = pl::core::Token::literalToUnsigned(pattern.getValue());
for (auto &value : pattern.getEnumValues()) {
auto min = pl::core::Token::literalToUnsigned(value.min);
auto max = pl::core::Token::literalToUnsigned(value.max);
bool isSelected = min <= currValue && max >= currValue;
if (ImGui::Selectable(fmt::format("{}::{} (0x{:0{}X})", pattern.getTypeName(), value.name, min, pattern.getSize() * 2).c_str(), isSelected)) {
pattern.setValue(value.min);
this->m_editingPattern = nullptr;
}
if (isSelected)
ImGui::SetItemDefaultFocus();
2022-12-16 11:20:39 +01:00
}
ImGui::EndCombo();
2022-12-16 11:20:39 +01:00
}
ImGui::PopItemWidth();
ImGui::PopStyleVar();
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
void PatternDrawer::visit(pl::ptrn::PatternFloat& pattern) {
createDefaultEntry(pattern);
2022-12-16 11:20:39 +01:00
if (this->m_editingPattern == &pattern) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
2022-12-16 11:20:39 +01:00
auto value = pattern.toString();
if (ImGui::InputText("##Value", value, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
MathEvaluator<long double> mathEvaluator;
if (auto result = mathEvaluator.evaluate(value); result.has_value())
pattern.setValue(double(result.value()));
2022-12-16 11:20:39 +01:00
this->m_editingPattern = nullptr;
}
ImGui::PopItemWidth();
ImGui::PopStyleVar();
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
void PatternDrawer::visit(pl::ptrn::PatternPadding& pattern) {
// Do nothing
hex::unused(pattern);
}
void PatternDrawer::visit(pl::ptrn::PatternPointer& pattern) {
bool open = true;
if (!pattern.isInlined() && this->m_treeStyle != TreeStyle::Flattened) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = createTreeNode(pattern);
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
drawColorColumn(pattern);
drawOffsetColumn(pattern);
drawSizeColumn(pattern);
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", pattern.getFormattedName());
ImGui::TableNextColumn();
ui/ux: Rewrite of the entire hex editor view to make it more flexible (#512) * ui/ux: Initial recreation of the hex editor view * ui/ux: Added back support for editing cells * ux: Make scrolling and selecting bytes feel nice again * ui/ux: Improved byte selecting, added footer * sys: Make math evaluator more generic to support integer only calculations * patterns: Moved value formatting into pattern language * ui/ux: Added Goto and Search popups, improved selection * ui: Added better tooltips for bookmarks and patterns * sys: Use worse hex search algorithm on macOS Sadly it still doesn't support `std::boyer_moore_horsepool_searcher` * ui: Added back missing events, menu items and shortcuts * fix: Bookmark highlighting being rendered off by one * fix: Various macOS build errors * fix: size_t is not u64 on macos * fix: std::fmod and std::pow not working with integer types on macos * fix: Missing semicolons * sys: Added proper integer pow function * ui: Added back support for custom encodings * fix: Editor not jumping to selection when selection gets changed * ui: Turn Hexii setting into a data visualizer * sys: Added back remaining shortcuts * sys: Remove old hex editor files * sys: Moved more legacy things away from the hex editor view, updated localization * fix: Hex editor scrolling behaving weirdly and inconsistently * sys: Cleaned up Hex editor code * sys: Added selection color setting, localized all new settings * fix: Search feature not working correctly * ui: Replace custom ImGui::Disabled function with native ImGui ones * ui: Fix bookmark tooltip rendering issues * fix: Another size_t not being 64 bit issue on MacOS
2022-05-27 20:42:07 +02:00
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
if (open) {
pattern.getPointedAtPattern()->accept(*this);
closeTreeNode(pattern.isInlined());
}
}
void PatternDrawer::visit(pl::ptrn::PatternSigned& pattern) {
createDefaultEntry(pattern);
2022-12-16 11:20:39 +01:00
if (this->m_editingPattern == &pattern) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
2022-12-16 11:20:39 +01:00
auto value = pattern.getFormattedValue();
if (ImGui::InputText("##Value", value, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
MathEvaluator<i128> mathEvaluator;
2022-12-16 11:20:39 +01:00
if (auto result = mathEvaluator.evaluate(value); result.has_value())
pattern.setValue(result.value());
2022-12-16 11:20:39 +01:00
this->m_editingPattern = nullptr;
}
ImGui::PopItemWidth();
ImGui::PopStyleVar();
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
void PatternDrawer::visit(pl::ptrn::PatternString& pattern) {
2022-12-16 11:20:39 +01:00
if (pattern.getSize() > 0) {
createDefaultEntry(pattern);
if (this->m_editingPattern == &pattern) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
auto value = pattern.toString();
if (ImGui::InputText("##Value", value.data(), value.size() + 1, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
pattern.setValue(value);
this->m_editingPattern = nullptr;
}
ImGui::PopItemWidth();
ImGui::PopStyleVar();
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
2022-12-16 11:20:39 +01:00
}
void PatternDrawer::visit(pl::ptrn::PatternStruct& pattern) {
bool open = true;
if (!pattern.isInlined() && this->m_treeStyle != TreeStyle::Flattened) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = createTreeNode(pattern);
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
if (pattern.isSealed())
drawColorColumn(pattern);
else
ImGui::TableNextColumn();
drawOffsetColumn(pattern);
drawSizeColumn(pattern);
drawTypenameColumn(pattern, "struct");
if (this->m_editingPattern == &pattern) {
if (pattern.getWriteFormatterFunction().empty())
ImGui::TextFormatted("{}", pattern.getFormattedValue());
else {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
auto value = pattern.toString();
if (ImGui::InputText("##Value", value, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
pattern.setValue(value);
this->m_editingPattern = nullptr;
}
ImGui::PopItemWidth();
ImGui::PopStyleVar();
}
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
if (open) {
int id = 1;
pattern.forEachEntry(0, pattern.getEntryCount(), [&](u64, auto *member){
ImGui::PushID(id);
this->draw(*member);
ImGui::PopID();
id += 1;
});
closeTreeNode(pattern.isInlined());
}
}
void PatternDrawer::visit(pl::ptrn::PatternUnion& pattern) {
bool open = true;
if (!pattern.isInlined() && this->m_treeStyle != TreeStyle::Flattened) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = createTreeNode(pattern);
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
if (pattern.isSealed())
drawColorColumn(pattern);
else
ImGui::TableNextColumn();
drawOffsetColumn(pattern);
drawSizeColumn(pattern);
drawTypenameColumn(pattern, "union");
if (this->m_editingPattern == &pattern) {
if (pattern.getWriteFormatterFunction().empty())
ImGui::TextFormatted("{}", pattern.getFormattedValue());
else {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
auto value = pattern.toString();
if (ImGui::InputText("##Value", value, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
pattern.setValue(value);
this->m_editingPattern = nullptr;
}
ImGui::PopItemWidth();
ImGui::PopStyleVar();
}
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
if (open) {
int id = 1;
pattern.forEachEntry(0, pattern.getEntryCount(), [&](u64, auto *member) {
ImGui::PushID(id);
this->draw(*member);
ImGui::PopID();
id += 1;
});
closeTreeNode(pattern.isInlined());
}
}
void PatternDrawer::visit(pl::ptrn::PatternUnsigned& pattern) {
createDefaultEntry(pattern);
2022-12-16 11:20:39 +01:00
if (this->m_editingPattern == &pattern) {
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
auto value = pattern.toString();
if (ImGui::InputText("##Value", value, ImGuiInputTextFlags_AutoSelectAll | ImGuiInputTextFlags_EnterReturnsTrue)) {
MathEvaluator<u128> mathEvaluator;
if (auto result = mathEvaluator.evaluate(value); result.has_value())
pattern.setValue(result.value());
2022-12-16 11:20:39 +01:00
this->m_editingPattern = nullptr;
}
ImGui::PopItemWidth();
ImGui::PopStyleVar();
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
2022-12-16 11:20:39 +01:00
}
}
void PatternDrawer::visit(pl::ptrn::PatternWideCharacter& pattern) {
createDefaultEntry(pattern);
2022-12-16 11:20:39 +01:00
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
void PatternDrawer::visit(pl::ptrn::PatternWideString& pattern) {
2022-12-16 11:20:39 +01:00
if (pattern.getSize() > 0) {
createDefaultEntry(pattern);
2022-12-16 11:20:39 +01:00
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
void PatternDrawer::draw(pl::ptrn::Pattern& pattern) {
if (pattern.isHidden())
return;
pattern.accept(*this);
}
void PatternDrawer::drawArray(pl::ptrn::Pattern& pattern, pl::ptrn::Iteratable &iteratable, bool isInlined) {
if (iteratable.getEntryCount() == 0)
return;
bool open = true;
if (!isInlined && this->m_treeStyle != TreeStyle::Flattened) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = createTreeNode(pattern);
ImGui::TableNextColumn();
makeSelectable(pattern);
drawCommentTooltip(pattern);
if (pattern.isSealed())
drawColorColumn(pattern);
else
ImGui::TableNextColumn();
drawOffsetColumn(pattern);
drawSizeColumn(pattern);
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{0}", pattern.getTypeName());
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("[");
ImGui::SameLine(0, 0);
ImGui::TextFormattedColored(ImColor(0xFF00FF00), "{0}", iteratable.getEntryCount());
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("]");
ImGui::TableNextColumn();
if (const auto &arguments = pattern.getAttributeArguments("hex::visualize"); !arguments.empty()) {
static bool shouldReset = false;
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
if (ImGui::Button(pattern.getFormattedValue().c_str(), ImVec2(-1, ImGui::GetTextLineHeight()))) {
this->m_currVisualizedPattern = &pattern;
shouldReset = true;
ImGui::OpenPopup("Visualizer");
}
ImGui::PopStyleVar();
if (ImGui::BeginPopup("Visualizer")) {
if (this->m_currVisualizedPattern == &pattern) {
drawVisualizer(arguments, pattern, iteratable, shouldReset);
shouldReset = false;
}
ImGui::EndPopup();
}
} else {
ImGui::TextFormatted("{}", pattern.getFormattedValue());
}
}
if (open) {
u64 chunkCount = 0;
for (u64 i = 0; i < iteratable.getEntryCount(); i += ChunkSize) {
chunkCount++;
auto &displayEnd = this->getDisplayEnd(pattern);
if (chunkCount > displayEnd) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::Selectable(hex::format("... ({})", "hex.builtin.pattern_drawer.double_click"_lang).c_str(), false, ImGuiSelectableFlags_SpanAllColumns);
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
displayEnd += DisplayEndStep;
break;
} else {
2022-09-19 15:26:25 +02:00
auto endIndex = std::min<u64>(iteratable.getEntryCount(), i + ChunkSize);
bool chunkOpen = true;
if (iteratable.getEntryCount() > ChunkSize) {
auto startOffset = iteratable.getEntry(i)->getOffset();
auto endOffset = iteratable.getEntry(endIndex - 1)->getOffset();
auto endSize = iteratable.getEntry(endIndex - 1)->getSize();
size_t chunkSize = (endOffset - startOffset) + endSize;
ImGui::TableNextRow();
ImGui::TableNextColumn();
chunkOpen = highlightWhenSelected(startOffset, ((endOffset + endSize) - startOffset) - 1, [&]{
return ImGui::TreeNodeEx(hex::format("{0}[{1} ... {2}]", this->m_treeStyle == TreeStyle::Flattened ? pattern.getDisplayName().c_str() : "", i, endIndex - 1).c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
});
ImGui::TableNextColumn();
drawColorColumn(pattern);
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", startOffset, startOffset + chunkSize - (pattern.getSize() == 0 ? 0 : 1));
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:04X}", chunkSize);
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{0}", pattern.getTypeName());
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("[");
ImGui::SameLine(0, 0);
ImGui::TextFormattedColored(ImColor(0xFF00FF00), "{0}", endIndex - i);
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("]");
ImGui::TableNextColumn();
ImGui::TextFormatted("[ ... ]");
}
if (chunkOpen) {
int id = 1;
iteratable.forEachEntry(i, endIndex, [&](u64, auto *entry){
ImGui::PushID(id);
this->draw(*entry);
ImGui::PopID();
id += 1;
});
if (iteratable.getEntryCount() > ChunkSize)
ImGui::TreePop();
}
}
}
closeTreeNode(isInlined);
}
}
u64& PatternDrawer::getDisplayEnd(const pl::ptrn::Pattern& pattern) {
auto it = this->m_displayEnd.find(&pattern);
if (it != this->m_displayEnd.end()) {
return it->second;
}
auto [value, success] = this->m_displayEnd.emplace(&pattern, DisplayEndDefault);
return value->second;
}
static bool sortPatterns(const ImGuiTableSortSpecs* sortSpecs, const pl::ptrn::Pattern * left, const pl::ptrn::Pattern * right) {
if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("name")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left->getDisplayName() > right->getDisplayName();
else
return left->getDisplayName() < right->getDisplayName();
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("offset")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left->getOffset() > right->getOffset();
else
return left->getOffset() < right->getOffset();
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("size")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left->getSize() > right->getSize();
else
return left->getSize() < right->getSize();
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("value")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left->getValue() > right->getValue();
else
return left->getValue() < right->getValue();
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("type")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left->getTypeName() > right->getTypeName();
else
return left->getTypeName() < right->getTypeName();
} else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("color")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
return left->getColor() > right->getColor();
else
return left->getColor() < right->getColor();
}
return false;
}
static bool beginPatternTable(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, std::vector<pl::ptrn::Pattern*> &sortedPatterns, float height) {
if (ImGui::BeginTable("##Patterntable", 6, ImGuiTableFlags_Borders | ImGuiTableFlags_Resizable | ImGuiTableFlags_Sortable | ImGuiTableFlags_Hideable | ImGuiTableFlags_Reorderable | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY, ImVec2(0, height))) {
ImGui::TableSetupScrollFreeze(0, 1);
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.var_name"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("name"));
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.color"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("color"));
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.offset"_lang, ImGuiTableColumnFlags_PreferSortAscending | ImGuiTableColumnFlags_DefaultSort, 0, ImGui::GetID("offset"));
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.size"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("size"));
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.type"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("type"));
ImGui::TableSetupColumn("hex.builtin.pattern_drawer.value"_lang, ImGuiTableColumnFlags_PreferSortAscending, 0, ImGui::GetID("value"));
auto sortSpecs = ImGui::TableGetSortSpecs();
if (patterns.empty())
sortedPatterns.clear();
if (!patterns.empty() && (sortSpecs->SpecsDirty || sortedPatterns.empty())) {
sortedPatterns.clear();
std::transform(patterns.begin(), patterns.end(), std::back_inserter(sortedPatterns), [](const std::shared_ptr<pl::ptrn::Pattern> &pattern) {
return pattern.get();
});
std::sort(sortedPatterns.begin(), sortedPatterns.end(), [&sortSpecs](pl::ptrn::Pattern *left, pl::ptrn::Pattern *right) -> bool {
return sortPatterns(sortSpecs, left, right);
});
for (auto &pattern : sortedPatterns)
pattern->sort([&sortSpecs](const pl::ptrn::Pattern *left, const pl::ptrn::Pattern *right){
return sortPatterns(sortSpecs, left, right);
});
sortSpecs->SpecsDirty = false;
}
return true;
}
return false;
}
void PatternDrawer::draw(const std::vector<std::shared_ptr<pl::ptrn::Pattern>> &patterns, float height) {
if (ImGui::IsMouseClicked(ImGuiMouseButton_Left) && !ImGui::IsAnyItemHovered())
this->m_editingPattern = nullptr;
if (beginPatternTable(patterns, this->m_sortedPatterns, height)) {
ImGui::TableHeadersRow();
int id = 1;
for (auto &pattern : this->m_sortedPatterns) {
ImGui::PushID(id);
this->draw(*pattern);
2022-12-16 11:20:39 +01:00
ImGui::PopID();
id += 1;
}
ImGui::EndTable();
}
}
}