From 76f732dc533b227eecbfa11b6baec2845151f4c7 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 7 Apr 2022 12:47:45 +0200 Subject: [PATCH] patterns: Fixed various render issues --- lib/libimhex/include/hex/helpers/logger.hpp | 10 +- .../hex/pattern_language/pattern_drawer.hpp | 129 +++++++++--------- .../hex/pattern_language/patterns/pattern.hpp | 2 +- .../patterns/pattern_array_dynamic.hpp | 4 +- .../patterns/pattern_array_static.hpp | 3 +- .../patterns/pattern_enum.hpp | 2 +- .../patterns/pattern_union.hpp | 4 - .../include/hex/pattern_language/token.hpp | 2 + 8 files changed, 79 insertions(+), 77 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/logger.hpp b/lib/libimhex/include/hex/helpers/logger.hpp index 69b0f6a56..6bd22b21b 100644 --- a/lib/libimhex/include/hex/helpers/logger.hpp +++ b/lib/libimhex/include/hex/helpers/logger.hpp @@ -41,26 +41,26 @@ namespace hex::log { [[maybe_unused]] void debug(const std::string &fmt, auto &&...args) { #if defined(DEBUG) - print(fg(fmt::color::green_yellow) | fmt::emphasis::bold, "[DEBUG]", fmt, args...); + hex::log::print(fg(fmt::color::green_yellow) | fmt::emphasis::bold, "[DEBUG]", fmt, args...); #else hex::unused(fmt, args...); #endif } [[maybe_unused]] void info(const std::string &fmt, auto &&...args) { - print(fg(fmt::color::cadet_blue) | fmt::emphasis::bold, "[INFO] ", fmt, args...); + hex::log::print(fg(fmt::color::cadet_blue) | fmt::emphasis::bold, "[INFO] ", fmt, args...); } [[maybe_unused]] void warn(const std::string &fmt, auto &&...args) { - print(fg(fmt::color::orange) | fmt::emphasis::bold, "[WARN] ", fmt, args...); + hex::log::print(fg(fmt::color::orange) | fmt::emphasis::bold, "[WARN] ", fmt, args...); } [[maybe_unused]] void error(const std::string &fmt, auto &&...args) { - print(fg(fmt::color::red) | fmt::emphasis::bold, "[ERROR]", fmt, args...); + hex::log::print(fg(fmt::color::red) | fmt::emphasis::bold, "[ERROR]", fmt, args...); } [[maybe_unused]] void fatal(const std::string &fmt, auto &&...args) { - print(fg(fmt::color::purple) | fmt::emphasis::bold, "[FATAL]", fmt, args...); + hex::log::print(fg(fmt::color::purple) | fmt::emphasis::bold, "[FATAL]", fmt, args...); } [[maybe_unused]] void redirectToFile(); diff --git a/lib/libimhex/include/hex/pattern_language/pattern_drawer.hpp b/lib/libimhex/include/hex/pattern_language/pattern_drawer.hpp index b3cfd9c74..fd33efe6f 100644 --- a/lib/libimhex/include/hex/pattern_language/pattern_drawer.hpp +++ b/lib/libimhex/include/hex/pattern_language/pattern_drawer.hpp @@ -20,8 +20,8 @@ #include namespace { - constexpr static auto DisplayEndDefault = 50u; - constexpr static auto DisplayEndStep = 50u; + constexpr auto DisplayEndDefault = 50u; + constexpr auto DisplayEndStep = 50u; template concept ArrayPattern = requires(T pattern, std::function fn) { @@ -53,9 +53,9 @@ namespace hex::pl { void visit(PatternBitfieldField& pattern) override { ImGui::TableNextRow(); ImGui::TableNextColumn(); - this->drawNameColumn(pattern); - this->makeSelectable(pattern); - this->drawColorColumn(pattern); + drawNameColumn(pattern); + makeSelectable(pattern); + drawColorColumn(pattern); auto byteAddr = pattern.getOffset() + pattern.getBitOffset() / 8; auto firstBitIdx = pattern.getBitOffset() % 8; @@ -84,14 +84,14 @@ namespace hex::pl { if (!pattern.isInlined()) { ImGui::TableNextRow(); ImGui::TableNextColumn(); - open = this->createTreeNode(pattern); + open = createTreeNode(pattern); ImGui::TableNextColumn(); - this->makeSelectable(pattern); - this->drawCommentTooltip(pattern); + makeSelectable(pattern); + drawCommentTooltip(pattern); ImGui::TableNextColumn(); - this->drawOffsetColumn(pattern); - this->drawSizeColumn(pattern); - this->drawTypenameColumn(pattern, "bitfield"); + drawOffsetColumn(pattern); + drawSizeColumn(pattern); + drawTypenameColumn(pattern, "bitfield"); std::string valueString = "{ "; for (auto i : value) @@ -159,22 +159,22 @@ namespace hex::pl { valueString += "???"; ImGui::TableNextRow(); - this->createLeafNode(pattern); - this->drawCommentTooltip(pattern); + createLeafNode(pattern); + drawCommentTooltip(pattern); ImGui::TableNextColumn(); - this->makeSelectable(pattern); + makeSelectable(pattern); ImGui::SameLine(); - this->drawNameColumn(pattern); - this->drawColorColumn(pattern); - this->drawOffsetColumn(pattern); - this->drawSizeColumn(pattern); - this->drawTypenameColumn(pattern, "enum"); + drawNameColumn(pattern); + drawColorColumn(pattern); + drawOffsetColumn(pattern); + drawSizeColumn(pattern); + drawTypenameColumn(pattern, "enum"); ImGui::TextFormatted("{}", pattern.formatDisplayValue(hex::format("{} (0x{:0{}X})", valueString.c_str(), value, pattern.getSize() * 2), &pattern)); } void visit(PatternFloat& pattern) override { if (pattern.getSize() == 4) { - float f32 = pattern.getValue(m_provider); + float f32 = static_cast(pattern.getValue(m_provider)); u32 integerResult = 0; std::memcpy(&integerResult, &f32, sizeof(float)); this->createDefaultEntry(pattern, hex::format("{:e} (0x{:0{}X})", f32, integerResult, pattern.getSize() * 2), f32); @@ -199,14 +199,14 @@ namespace hex::pl { if (!pattern.isInlined()) { ImGui::TableNextRow(); ImGui::TableNextColumn(); - open = this->createTreeNode(pattern); + open = createTreeNode(pattern); ImGui::TableNextColumn(); - this->makeSelectable(pattern); - this->drawCommentTooltip(pattern); + makeSelectable(pattern); + drawCommentTooltip(pattern); ImGui::SameLine(0, 0); - this->drawColorColumn(pattern); - this->drawOffsetColumn(pattern); - this->drawSizeColumn(pattern); + drawColorColumn(pattern); + drawOffsetColumn(pattern); + drawSizeColumn(pattern); ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", pattern.getFormattedName()); ImGui::TableNextColumn(); ImGui::TextFormatted("{}", pattern.formatDisplayValue(hex::format("*(0x{0:X})", data), u128(data))); @@ -243,14 +243,14 @@ namespace hex::pl { if (!pattern.isInlined()) { ImGui::TableNextRow(); ImGui::TableNextColumn(); - open = this->createTreeNode(pattern); + open = createTreeNode(pattern); ImGui::TableNextColumn(); - this->makeSelectable(pattern); - this->drawCommentTooltip(pattern); + makeSelectable(pattern); + drawCommentTooltip(pattern); ImGui::TableNextColumn(); - this->drawOffsetColumn(pattern); - this->drawSizeColumn(pattern); - this->drawTypenameColumn(pattern, "struct"); + drawOffsetColumn(pattern); + drawSizeColumn(pattern); + drawTypenameColumn(pattern, "struct"); ImGui::TextFormatted("{}", pattern.formatDisplayValue("{ ... }", &pattern)); } else { ImGui::SameLine(); @@ -272,14 +272,14 @@ namespace hex::pl { if (!pattern.isInlined()) { ImGui::TableNextRow(); ImGui::TableNextColumn(); - open = this->createTreeNode(pattern); + open = createTreeNode(pattern); ImGui::TableNextColumn(); - this->makeSelectable(pattern); - this->drawCommentTooltip(pattern); + makeSelectable(pattern); + drawCommentTooltip(pattern); ImGui::TableNextColumn(); - this->drawOffsetColumn(pattern); - this->drawSizeColumn(pattern); - this->drawTypenameColumn(pattern, "union"); + drawOffsetColumn(pattern); + drawSizeColumn(pattern); + drawTypenameColumn(pattern, "union"); ImGui::TextFormatted("{}", pattern.formatDisplayValue("{ ... }", &pattern)); } else { ImGui::SameLine(); @@ -321,33 +321,36 @@ namespace hex::pl { private: void createDefaultEntry(const Pattern &pattern, const std::string &value, Token::Literal &&literal) const { ImGui::TableNextRow(); - this->createLeafNode(pattern); + createLeafNode(pattern); ImGui::TableNextColumn(); - ImGui::PushID(pattern.getOffset()); - ImGui::PushID(pattern.getVariableName().c_str()); - this->makeSelectable(pattern); - ImGui::PopID(); - ImGui::PopID(); + makeSelectable(pattern); - this->drawCommentTooltip(pattern); + drawCommentTooltip(pattern); ImGui::SameLine(); - this->drawNameColumn(pattern); - this->drawColorColumn(pattern); - this->drawOffsetColumn(pattern); - this->drawSizeColumn(pattern); + drawNameColumn(pattern); + drawColorColumn(pattern); + drawOffsetColumn(pattern); + drawSizeColumn(pattern); ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", pattern.getTypeName().empty() ? pattern.getFormattedName() : pattern.getTypeName()); ImGui::TableNextColumn(); ImGui::TextFormatted("{}", pattern.formatDisplayValue(value, literal)); } - void makeSelectable(const Pattern &pattern) const { - if (ImGui::Selectable(("##PatternLine"s + std::to_string(u64(&pattern))).c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) { + static void makeSelectable(const Pattern &pattern) { + ImGui::PushID(&pattern); + ImGui::PushID(static_cast(pattern.getOffset())); + ImGui::PushID(pattern.getVariableName().c_str()); + if (ImGui::Selectable("##PatternLine", false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) { ImHexApi::HexEditor::setSelection(pattern.getOffset(), pattern.getSize()); } + ImGui::SameLine(); + ImGui::PopID(); + ImGui::PopID(); + ImGui::PopID(); } - void drawCommentTooltip(const Pattern &pattern) const { + static void drawCommentTooltip(const Pattern &pattern) { if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) && pattern.getComment().has_value()) { ImGui::BeginTooltip(); ImGui::TextUnformatted(pattern.getComment()->c_str()); @@ -371,15 +374,15 @@ namespace hex::pl { if (!pattern.isInlined()) { ImGui::TableNextRow(); ImGui::TableNextColumn(); - open = this->createTreeNode(pattern); + open = createTreeNode(pattern); ImGui::TableNextColumn(); if (ImGui::Selectable(("##PatternLine"s + std::to_string(u64(&pattern))).c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) { ImHexApi::HexEditor::setSelection(pattern.getOffset(), pattern.getSize()); } - this->drawCommentTooltip(pattern); + drawCommentTooltip(pattern); ImGui::TableNextColumn(); - this->drawOffsetColumn(pattern); - this->drawSizeColumn(pattern); + drawOffsetColumn(pattern); + drawSizeColumn(pattern); ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{0}", pattern.getTypeName()); ImGui::SameLine(0, 0); @@ -418,40 +421,40 @@ namespace hex::pl { } } - void createLeafNode(const Pattern& pattern) const { + static void createLeafNode(const Pattern& pattern) { ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap); } - bool createTreeNode(const Pattern& pattern) const { + static bool createTreeNode(const Pattern& pattern) { return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth); } - void drawTypenameColumn(const Pattern& pattern, const std::string& pattern_name) const { + static void drawTypenameColumn(const 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 Pattern& pattern) const { + static void drawNameColumn(const Pattern& pattern) { ImGui::TextUnformatted(pattern.getDisplayName().c_str()); ImGui::TableNextColumn(); } - void drawColorColumn(const Pattern& pattern) const { + static void drawColorColumn(const Pattern& pattern) { ImGui::ColorButton("color", ImColor(pattern.getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight())); ImGui::TableNextColumn(); } - void drawOffsetColumn(const Pattern& pattern) const { + static void drawOffsetColumn(const 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 Pattern& pattern) const { + static void drawSizeColumn(const Pattern& pattern) { ImGui::TextFormatted("0x{0:04X}", pattern.getSize()); ImGui::TableNextColumn(); } diff --git a/lib/libimhex/include/hex/pattern_language/patterns/pattern.hpp b/lib/libimhex/include/hex/pattern_language/patterns/pattern.hpp index 3c8aa93ba..8f1986af7 100644 --- a/lib/libimhex/include/hex/pattern_language/patterns/pattern.hpp +++ b/lib/libimhex/include/hex/pattern_language/patterns/pattern.hpp @@ -77,7 +77,7 @@ namespace hex::pl { [[nodiscard]] const std::optional &getComment() const { return this->m_comment; } void setComment(std::string comment) { this->m_comment = std::move(comment); } - [[nodiscard]] const std::string &getTypeName() const { return this->m_typeName; } + [[nodiscard]] virtual std::string getTypeName() const { return this->m_typeName; } void setTypeName(std::string name) { this->m_typeName = std::move(name); } [[nodiscard]] u32 getColor() const { return this->m_color; } diff --git a/lib/libimhex/include/hex/pattern_language/patterns/pattern_array_dynamic.hpp b/lib/libimhex/include/hex/pattern_language/patterns/pattern_array_dynamic.hpp index 6f7333262..d634b87ac 100644 --- a/lib/libimhex/include/hex/pattern_language/patterns/pattern_array_dynamic.hpp +++ b/lib/libimhex/include/hex/pattern_language/patterns/pattern_array_dynamic.hpp @@ -39,7 +39,7 @@ namespace hex::pl { return this->m_entries[0]->getTypeName() + "[" + std::to_string(this->m_entries.size()) + "]"; } - [[nodiscard]] std::string getTypeName() const { + [[nodiscard]] std::string getTypeName() const override { return this->m_entries[0]->getTypeName(); } @@ -58,7 +58,7 @@ namespace hex::pl { return this->m_entries; } - void forEachArrayEntry(const std::function& fn) { + void forEachArrayEntry(const std::function& fn) { for (u64 i = 0; i < this->m_entries.size(); i++) fn(i, *this->m_entries[i]); } diff --git a/lib/libimhex/include/hex/pattern_language/patterns/pattern_array_static.hpp b/lib/libimhex/include/hex/pattern_language/patterns/pattern_array_static.hpp index e2c040d2a..51cafd664 100644 --- a/lib/libimhex/include/hex/pattern_language/patterns/pattern_array_static.hpp +++ b/lib/libimhex/include/hex/pattern_language/patterns/pattern_array_static.hpp @@ -19,9 +19,10 @@ namespace hex::pl { return std::unique_ptr(new PatternArrayStatic(*this)); } - void forEachArrayEntry(const std::function& fn) { + void forEachArrayEntry(const std::function& fn) { auto entry = std::shared_ptr(this->m_template->clone()); for (u64 index = 0; index < this->m_entryCount; index++) { + entry->clearFormatCache(); entry->setVariableName(hex::format("[{0}]", index)); entry->setOffset(this->getOffset() + index * this->m_template->getSize()); fn(index, *entry); diff --git a/lib/libimhex/include/hex/pattern_language/patterns/pattern_enum.hpp b/lib/libimhex/include/hex/pattern_language/patterns/pattern_enum.hpp index 609a14113..3359a1cda 100644 --- a/lib/libimhex/include/hex/pattern_language/patterns/pattern_enum.hpp +++ b/lib/libimhex/include/hex/pattern_language/patterns/pattern_enum.hpp @@ -24,7 +24,7 @@ namespace hex::pl { return "enum " + Pattern::getTypeName(); } - [[nodiscard]] std::string getTypeName() const { + [[nodiscard]] std::string getTypeName() const override { return Pattern::getTypeName(); } diff --git a/lib/libimhex/include/hex/pattern_language/patterns/pattern_union.hpp b/lib/libimhex/include/hex/pattern_language/patterns/pattern_union.hpp index 963cd01d2..fadd34a8e 100644 --- a/lib/libimhex/include/hex/pattern_language/patterns/pattern_union.hpp +++ b/lib/libimhex/include/hex/pattern_language/patterns/pattern_union.hpp @@ -67,10 +67,6 @@ namespace hex::pl { return "union " + Pattern::getTypeName(); } - [[nodiscard]] std::string getTypeName() const { - return Pattern::getTypeName(); - } - [[nodiscard]] const auto &getMembers() const { return this->m_members; } diff --git a/lib/libimhex/include/hex/pattern_language/token.hpp b/lib/libimhex/include/hex/pattern_language/token.hpp index d8269bd38..405d11c66 100644 --- a/lib/libimhex/include/hex/pattern_language/token.hpp +++ b/lib/libimhex/include/hex/pattern_language/token.hpp @@ -241,6 +241,8 @@ namespace hex::pl { return "padding"; case ValueType::String: return "str"; + case ValueType::Boolean: + return "bool"; default: return "< ??? >"; }