From 5fa264ea1841c0767eb9babfc650b597e41080ac Mon Sep 17 00:00:00 2001 From: Zaggy1024 Date: Sun, 26 Mar 2023 04:01:37 -0500 Subject: [PATCH] patterns: Update pattern_language and implement support for new bitfield features (#992) This requires https://github.com/WerWolv/PatternLanguage/pull/34 to be merged first, and then this can be amended to update the submodule and merged to add support for the new features. --- lib/external/pattern_language | 2 +- plugins/builtin/include/ui/pattern_drawer.hpp | 1 + plugins/builtin/source/ui/pattern_drawer.cpp | 50 ++++++++++++------- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/lib/external/pattern_language b/lib/external/pattern_language index a2fbf46b5..ea717d9c8 160000 --- a/lib/external/pattern_language +++ b/lib/external/pattern_language @@ -1 +1 @@ -Subproject commit a2fbf46b5132f5c4a061527f4caf12d11ac0eebf +Subproject commit ea717d9c835e0f1364e8c8befed674becef5222f diff --git a/plugins/builtin/include/ui/pattern_drawer.hpp b/plugins/builtin/include/ui/pattern_drawer.hpp index beafcd7e9..8b3dfa779 100644 --- a/plugins/builtin/include/ui/pattern_drawer.hpp +++ b/plugins/builtin/include/ui/pattern_drawer.hpp @@ -29,6 +29,7 @@ namespace hex::plugin::builtin::ui { void visit(pl::ptrn::PatternArrayDynamic& pattern) override; void visit(pl::ptrn::PatternArrayStatic& pattern) override; void visit(pl::ptrn::PatternBitfieldField& pattern) override; + void visit(pl::ptrn::PatternBitfieldArray& pattern) override; void visit(pl::ptrn::PatternBitfield& pattern) override; void visit(pl::ptrn::PatternBoolean& pattern) override; void visit(pl::ptrn::PatternCharacter& pattern) override; diff --git a/plugins/builtin/source/ui/pattern_drawer.cpp b/plugins/builtin/source/ui/pattern_drawer.cpp index 454dd8e4d..c25c26d72 100644 --- a/plugins/builtin/source/ui/pattern_drawer.cpp +++ b/plugins/builtin/source/ui/pattern_drawer.cpp @@ -92,13 +92,34 @@ namespace hex::plugin::builtin::ui { ImGui::TableNextColumn(); } + inline void drawOffsetColumnForBitfieldMember(const pl::ptrn::PatternBitfieldMember &pattern) { + auto bitOffset = pattern.getBitOffsetForDisplay(); + if (pattern.getBitSize() <= 1) + ImGui::TextFormatted("bit {0}", bitOffset); + else + ImGui::TextFormatted("bits {0} - {1}", bitOffset, bitOffset + pattern.getBitSize() - 1); + } + 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)); + if (auto *bitfieldMember = dynamic_cast(&pattern); bitfieldMember != nullptr && bitfieldMember->getParentBitfield() != nullptr) + drawOffsetColumnForBitfieldMember(*bitfieldMember); + else + ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", pattern.getOffset(), pattern.getOffset() + pattern.getSize() - (pattern.getSize() == 0 ? 0 : 1)); ImGui::TableNextColumn(); } + inline void drawSizeColumnForBitfieldMember(const pl::ptrn::PatternBitfieldMember &pattern) { + if (pattern.getBitSize() == 1) + ImGui::TextFormatted("1 bit"); + else + ImGui::TextFormatted("{0} bits", pattern.getBitSize()); + } + void drawSizeColumn(const pl::ptrn::Pattern& pattern) { - ImGui::TextFormatted("0x{0:04X}", pattern.getSize()); + if (auto *bitfieldMember = dynamic_cast(&pattern); bitfieldMember != nullptr && bitfieldMember->getParentBitfield() != nullptr) + drawSizeColumnForBitfieldMember(*bitfieldMember); + else + ImGui::TextFormatted("0x{0:04X}", pattern.getSize()); ImGui::TableNextColumn(); } @@ -261,18 +282,9 @@ namespace hex::plugin::builtin::ui { ImGui::SameLine(); drawNameColumn(pattern); drawColorColumn(pattern); - - auto firstBitIdx = pattern.getBitOffset(); - auto lastBitIdx = firstBitIdx + (pattern.getBitSize() - 1); - if (firstBitIdx == lastBitIdx) - ImGui::TextFormatted("bit {0}", firstBitIdx); - else - ImGui::TextFormatted("bits {0} - {1}", firstBitIdx, lastBitIdx); + drawOffsetColumnForBitfieldMember(pattern); ImGui::TableNextColumn(); - if (pattern.getBitSize() == 1) - ImGui::TextFormatted("{0} bit", pattern.getBitSize()); - else - ImGui::TextFormatted("{0} bits", pattern.getBitSize()); + drawSizeColumnForBitfieldMember(pattern); ImGui::TableNextColumn(); ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "bits"); ImGui::TableNextColumn(); @@ -280,6 +292,10 @@ namespace hex::plugin::builtin::ui { ImGui::TextFormatted("{}", pattern.getFormattedValue()); } + void PatternDrawer::visit(pl::ptrn::PatternBitfieldArray& pattern) { + drawArray(pattern, pattern, pattern.isInlined()); + } + void PatternDrawer::visit(pl::ptrn::PatternBitfield& pattern) { bool open = true; if (!pattern.isInlined() && this->m_treeStyle != TreeStyle::Flattened) { @@ -770,14 +786,14 @@ namespace hex::plugin::builtin::ui { return left->getDisplayName() > right->getDisplayName(); } else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("offset")) { if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending) - return left->getOffset() < right->getOffset(); + return left->getOffsetForSorting() < right->getOffsetForSorting(); else - return left->getOffset() > right->getOffset(); + return left->getOffsetForSorting() > right->getOffsetForSorting(); } else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("size")) { if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending) - return left->getSize() < right->getSize(); + return left->getSizeForSorting() < right->getSizeForSorting(); else - return left->getSize() > right->getSize(); + return left->getSizeForSorting() > right->getSizeForSorting(); } else if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("value")) { if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending) return left->getValue() < right->getValue();