1
0
mirror of synced 2024-11-28 09:30:51 +01:00

patterns: Move logic to draw patterns into separate class (#472)

* refactor(patterns): add visitor interface

* refactor(patterns): add public accessors

* refactor(patterns): add method to get pattern value

* refactor(pattern): make some methods public

* refactor(pattern): extract code to draw GUI

* refactor(patterns): remove GUI related code from patterns

* refactor: move common GUI function from pattern to pattern_drawer

* refactor(pattern_drawer): extract common code into methods

* refactor: rename ImGuiDrawer -> PatternDrawer

* refactor(patternr): move displayEnd into PatternDrawer

* refactor: use ArrayPattern concept to restrict argument type

Co-authored-by: Dmitry Polshakov <dmitry.polshakov@dsr-corporation.com>
This commit is contained in:
Polshakov Dmitry 2022-03-21 15:52:07 +03:00 committed by GitHub
parent 854c99bafa
commit 5dfa9cf501
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 674 additions and 433 deletions

View File

@ -0,0 +1,470 @@
#pragma once
#include <hex/pattern_language/patterns/pattern_array_dynamic.hpp>
#include <hex/pattern_language/patterns/pattern_array_static.hpp>
#include <hex/pattern_language/patterns/pattern_bitfield.hpp>
#include <hex/pattern_language/patterns/pattern_boolean.hpp>
#include <hex/pattern_language/patterns/pattern_character.hpp>
#include <hex/pattern_language/patterns/pattern_enum.hpp>
#include <hex/pattern_language/patterns/pattern_float.hpp>
#include <hex/pattern_language/patterns/pattern_padding.hpp>
#include <hex/pattern_language/patterns/pattern_pointer.hpp>
#include <hex/pattern_language/patterns/pattern_signed.hpp>
#include <hex/pattern_language/patterns/pattern_string.hpp>
#include <hex/pattern_language/patterns/pattern_struct.hpp>
#include <hex/pattern_language/patterns/pattern_union.hpp>
#include <hex/pattern_language/patterns/pattern_unsigned.hpp>
#include <hex/pattern_language/patterns/pattern_wide_character.hpp>
#include <hex/pattern_language/patterns/pattern_wide_string.hpp>
#include <nlohmann/json.hpp>
namespace {
constexpr static auto DisplayEndDefault = 50u;
constexpr static auto DisplayEndStep = 50u;
template<typename T>
concept ArrayPattern = requires(T pattern, std::function<void(int, hex::pl::Pattern&)> fn) {
{ pattern.forEachArrayEntry(fn) } -> std::same_as<void>;
};
};
namespace hex::pl {
class PatternDrawer : public PatternVisitor
{
public:
PatternDrawer()
: m_provider{nullptr}
{ }
void setProvider(prv::Provider *provider) {
m_provider = provider;
}
void visit(PatternArrayDynamic& pattern) override {
this->drawArray(pattern);
}
void visit(PatternArrayStatic& pattern) override {
this->drawArray(pattern);
}
void visit(PatternBitfieldField& pattern) override {
ImGui::TableNextRow();
ImGui::TableNextColumn();
this->drawNameColumn(pattern);
this->makeSelectable(pattern);
this->drawColorColumn(pattern);
auto byteAddr = pattern.getOffset() + pattern.getBitOffset() / 8;
auto firstBitIdx = pattern.getBitOffset() % 8;
auto lastBitIdx = firstBitIdx + (pattern.getBitSize() - 1) % 8;
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();
u64 extractedValue = pattern.getValue(m_provider);
ImGui::TextFormatted("{}", pattern.formatDisplayValue(hex::format("{0} (0x{1:X})", extractedValue, extractedValue), &pattern));
}
void visit(PatternBitfield& pattern) override {
std::vector<u8> value = pattern.getValue(m_provider);
bool open = true;
if (!pattern.isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = this->createTreeNode(pattern);
ImGui::TableNextColumn();
this->makeSelectable(pattern);
this->drawCommentTooltip(pattern);
ImGui::TableNextColumn();
this->drawOffsetColumn(pattern);
this->drawSizeColumn(pattern);
this->drawTypenameColumn(pattern, "bitfield");
std::string valueString = "{ ";
for (auto i : value)
valueString += hex::format("{0:02X} ", i);
valueString += "}";
ImGui::TextFormatted("{}", pattern.formatDisplayValue(valueString, &pattern));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
if (open) {
pattern.forEachMember([&] (auto &field) {
this->draw(field);
});
ImGui::TreePop();
}
}
void visit(PatternBoolean& pattern) override {
u8 boolean = pattern.getValue(m_provider);
if (boolean == 0)
this->createDefaultEntry(pattern, "false", false);
else if (boolean == 1)
this->createDefaultEntry(pattern, "true", true);
else
this->createDefaultEntry(pattern, "true*", true);
}
void visit(PatternCharacter& pattern) override {
char character = pattern.getValue(m_provider);
this->createDefaultEntry(pattern, hex::format("'{0}'", character), character);
}
void visit(PatternEnum& pattern) override {
u64 value = pattern.getValue(m_provider);
std::string valueString = pattern.getTypeName() + "::";
bool foundValue = false;
for (auto &[entryValueLiteral, entryName] : pattern.getEnumValues()) {
auto visitor = overloaded {
[&, name = entryName](auto &entryValue) {
if (value == entryValue) {
valueString += name;
foundValue = true;
return true;
}
return false;
},
[](const std::string &) { return false; },
[](Pattern *) { return false; },
};
bool matches = std::visit(visitor, entryValueLiteral);
if (matches)
break;
}
if (!foundValue)
valueString += "???";
ImGui::TableNextRow();
this->createLeafNode(pattern);
this->drawCommentTooltip(pattern);
ImGui::TableNextColumn();
this->makeSelectable(pattern);
ImGui::SameLine();
this->drawNameColumn(pattern);
this->drawColorColumn(pattern);
this->drawOffsetColumn(pattern);
this->drawSizeColumn(pattern);
this->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);
u32 data = *reinterpret_cast<u32 *>(&f32);
this->createDefaultEntry(pattern, hex::format("{:e} (0x{:0{}X})", f32, data, pattern.getSize() * 2), f32);
} else if (pattern.getSize() == 8) {
double f64 = pattern.getValue(m_provider);
u64 data = *reinterpret_cast<u64 *>(&f64);
this->createDefaultEntry(pattern, hex::format("{:e} (0x{:0{}X})", f64, data, pattern.getSize() * 2), f64);
}
}
void visit(PatternPadding& pattern) override {
// Do nothing
}
void visit(PatternPointer& pattern) override {
u64 data = pattern.getValue(m_provider);
bool open = true;
if (!pattern.isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = this->createTreeNode(pattern);
ImGui::TableNextColumn();
this->makeSelectable(pattern);
this->drawCommentTooltip(pattern);
ImGui::SameLine(0, 0);
this->drawColorColumn(pattern);
this->drawOffsetColumn(pattern);
this->drawSizeColumn(pattern);
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", pattern.getFormattedName());
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", pattern.formatDisplayValue(hex::format("*(0x{0:X})", data), u128(data)));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
if (open) {
pattern.getPointedAtPattern()->accept(*this);
ImGui::TreePop();
}
}
void visit(PatternSigned& pattern) override {
i128 data = pattern.getValue(m_provider);
this->createDefaultEntry(pattern, hex::format("{:d} (0x{:0{}X})", data, data, 1 * 2), data);
}
void visit(PatternString& pattern) override {
auto size = std::min<size_t>(pattern.getSize(), 0x7F);
if (size == 0)
return;
std::string displayString = pattern.getValue(m_provider, size);
this->createDefaultEntry(pattern, hex::format("\"{0}\" {1}", displayString, size > pattern.getSize() ? "(truncated)" : ""), displayString);
}
void visit(PatternStruct& pattern) override {
bool open = true;
if (!pattern.isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = this->createTreeNode(pattern);
ImGui::TableNextColumn();
this->makeSelectable(pattern);
this->drawCommentTooltip(pattern);
ImGui::TableNextColumn();
this->drawOffsetColumn(pattern);
this->drawSizeColumn(pattern);
this->drawTypenameColumn(pattern, "struct");
ImGui::TextFormatted("{}", pattern.formatDisplayValue("{ ... }", &pattern));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
if (open) {
pattern.forEachMember([&](auto &member){
this->draw(member);
});
ImGui::TreePop();
}
}
void visit(PatternUnion& pattern) override {
bool open = true;
if (!pattern.isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = this->createTreeNode(pattern);
ImGui::TableNextColumn();
this->makeSelectable(pattern);
this->drawCommentTooltip(pattern);
ImGui::TableNextColumn();
this->drawOffsetColumn(pattern);
this->drawSizeColumn(pattern);
this->drawTypenameColumn(pattern, "union");
ImGui::TextFormatted("{}", pattern.formatDisplayValue("{ ... }", &pattern));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
if (open) {
pattern.forEachMember([&](auto &member) {
this->draw(member);
});
ImGui::TreePop();
}
}
void visit(PatternUnsigned& pattern) override {
u128 data = pattern.getValue(m_provider);
this->createDefaultEntry(pattern, hex::format("{:d} (0x{:0{}X})", data, data, pattern.getSize() * 2), data);
}
void visit(PatternWideCharacter& pattern) override {
char16_t character = pattern.getValue(m_provider);
u128 literal = character;
auto str = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(character);
this->createDefaultEntry(pattern, hex::format("'{0}'", str), literal);
}
void visit(PatternWideString& pattern) override {
auto size = std::min<size_t>(pattern.getSize(), 0x100);
if (size == 0)
return;
std::string utf8String = pattern.getValue(m_provider, size);
this->createDefaultEntry(pattern, hex::format("\"{0}\" {1}", utf8String, size > pattern.getSize() ? "(truncated)" : ""), utf8String);
}
private:
void createDefaultEntry(const Pattern &pattern, const std::string &value, Token::Literal &&literal) const {
ImGui::TableNextRow();
this->createLeafNode(pattern);
ImGui::TableNextColumn();
ImGui::PushID(pattern.getOffset());
ImGui::PushID(pattern.getVariableName().c_str());
this->makeSelectable(pattern);
ImGui::PopID();
ImGui::PopID();
this->drawCommentTooltip(pattern);
ImGui::SameLine();
this->drawNameColumn(pattern);
this->drawColorColumn(pattern);
this->drawOffsetColumn(pattern);
this->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)) {
ImHexApi::HexEditor::setSelection(pattern.getOffset(), pattern.getSize());
}
}
void drawCommentTooltip(const Pattern &pattern) const {
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) && pattern.getComment().has_value()) {
ImGui::BeginTooltip();
ImGui::TextUnformatted(pattern.getComment()->c_str());
ImGui::EndTooltip();
}
}
void draw(Pattern& pattern) {
if (pattern.isHidden())
return;
pattern.accept(*this);
}
template<ArrayPattern T>
void drawArray(T& pattern) {
if (pattern.getEntryCount() == 0)
return;
bool open = true;
if (!pattern.isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = this->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);
ImGui::TableNextColumn();
this->drawOffsetColumn(pattern);
this->drawSizeColumn(pattern);
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{0}", pattern.getTypeName());
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("[");
ImGui::SameLine(0, 0);
ImGui::TextFormattedColored(ImColor(0xFF00FF00), "{0}", pattern.getEntryCount());
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("]");
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", pattern.formatDisplayValue("{ ... }", &pattern));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
auto& displayEnd = this->getDisplayEnd(pattern);
if (open) {
pattern.forEachArrayEntry([&] (u64 idx, auto &entry){
u64 lastVisible = displayEnd - 1;
if (idx < lastVisible) {
this->draw(entry);
} else if (idx == lastVisible) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::Selectable("... (Double-click to see more items)", false, ImGuiSelectableFlags_SpanAllColumns);
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
displayEnd += DisplayEndStep;
}
});
ImGui::TreePop();
} else {
displayEnd = DisplayEndDefault;
}
}
void createLeafNode(const Pattern& pattern) const {
ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf |
ImGuiTreeNodeFlags_NoTreePushOnOpen |
ImGuiTreeNodeFlags_SpanFullWidth |
ImGuiTreeNodeFlags_AllowItemOverlap);
}
bool createTreeNode(const Pattern& pattern) const {
return ImGui::TreeNodeEx(pattern.getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
}
void drawTypenameColumn(const Pattern& pattern, const std::string& pattern_name) const {
ImGui::TextFormattedColored(ImColor(0xFFD69C56), pattern_name);
ImGui::SameLine();
ImGui::TextUnformatted(pattern.getTypeName().c_str());
ImGui::TableNextColumn();
}
void drawNameColumn(const Pattern& pattern) const {
ImGui::TextUnformatted(pattern.getDisplayName().c_str());
ImGui::TableNextColumn();
}
void drawColorColumn(const Pattern& pattern) const {
ImGui::ColorButton("color", ImColor(pattern.getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
ImGui::TableNextColumn();
}
void drawOffsetColumn(const Pattern& pattern) const {
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 {
ImGui::TextFormatted("0x{0:04X}", pattern.getSize());
ImGui::TableNextColumn();
}
u64& getDisplayEnd(const Pattern& pattern) {
auto it = m_displayEnd.find(&pattern);
if (it != m_displayEnd.end()) {
return it->second;
}
auto [inserted, success] = m_displayEnd.emplace(&pattern, DisplayEndDefault);
return inserted->second;
}
private:
prv::Provider *m_provider;
std::map<const Pattern*, u64> m_displayEnd;
};
};

View File

@ -0,0 +1,44 @@
#pragma once
namespace hex::pl {
class PatternArrayDynamic;
class PatternArrayStatic;
class PatternBitfield;
class PatternBitfieldField;
class PatternBoolean;
class PatternCharacter;
class PatternEnum;
class PatternFloat;
class PatternPadding;
class PatternPointer;
class PatternSigned;
class PatternString;
class PatternStruct;
class PatternUnion;
class PatternUnsigned;
class PatternWideCharacter;
class PatternWideString;
class PatternVisitor
{
public:
virtual void visit(PatternArrayDynamic& pattern) = 0;
virtual void visit(PatternArrayStatic& pattern) = 0;
virtual void visit(PatternBitfield& pattern) = 0;
virtual void visit(PatternBitfieldField& pattern) = 0;
virtual void visit(PatternBoolean& pattern) = 0;
virtual void visit(PatternCharacter& pattern) = 0;
virtual void visit(PatternEnum& pattern) = 0;
virtual void visit(PatternFloat& pattern) = 0;
virtual void visit(PatternPadding& pattern) = 0;
virtual void visit(PatternPointer& pattern) = 0;
virtual void visit(PatternSigned& pattern) = 0;
virtual void visit(PatternString& pattern) = 0;
virtual void visit(PatternStruct& pattern) = 0;
virtual void visit(PatternUnion& pattern) = 0;
virtual void visit(PatternUnsigned& pattern) = 0;
virtual void visit(PatternWideCharacter& pattern) = 0;
virtual void visit(PatternWideString& pattern) = 0;
};
};

View File

@ -6,6 +6,7 @@
#include <hex/ui/imgui_imhex_extensions.h>
#include <hex/pattern_language/evaluator.hpp>
#include <hex/pattern_language/pattern_visitor.hpp>
#include <hex/providers/provider.hpp>
#include <string>
@ -107,7 +108,6 @@ namespace hex::pl {
[[nodiscard]] const auto &getFormatterFunction() const { return this->m_formatterFunction; }
void setFormatterFunction(const ContentRegistry::PatternLanguage::Function &function) { this->m_formatterFunction = function; }
virtual void createEntry(prv::Provider *&provider) = 0;
[[nodiscard]] virtual std::string getFormattedName() const = 0;
[[nodiscard]] virtual const Pattern *getPattern(u64 offset) const {
@ -179,12 +179,6 @@ namespace hex::pl {
return false;
}
void draw(prv::Provider *provider) {
if (isHidden()) return;
this->createEntry(provider);
}
void setHidden(bool hidden) {
this->m_hidden = hidden;
}
@ -250,42 +244,7 @@ namespace hex::pl {
this->m_cachedDisplayValue.reset();
}
protected:
void createDefaultEntry(const std::string &value, Token::Literal &&literal) const {
ImGui::TableNextRow();
ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap);
ImGui::TableNextColumn();
ImGui::PushID(this->getOffset());
ImGui::PushID(this->getVariableName().c_str());
if (ImGui::Selectable("##PatternLine", false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
ImHexApi::HexEditor::setSelection(this->getOffset(), this->getSize());
}
ImGui::PopID();
ImGui::PopID();
this->drawCommentTooltip();
ImGui::SameLine();
ImGui::TextUnformatted(this->getDisplayName().c_str());
ImGui::TableNextColumn();
ImGui::ColorButton("color", ImColor(this->getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", this->getOffset(), this->getOffset() + this->getSize() - 1);
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:04X}", this->getSize());
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", this->getTypeName().empty() ? this->getFormattedName() : this->getTypeName());
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", formatDisplayValue(value, literal));
}
void drawCommentTooltip() const {
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) && this->getComment().has_value()) {
ImGui::BeginTooltip();
ImGui::TextUnformatted(this->getComment()->c_str());
ImGui::EndTooltip();
}
}
virtual void accept(PatternVisitor &v) = 0;
protected:
std::optional<std::endian> m_endian;

View File

@ -29,63 +29,6 @@ namespace hex::pl {
entry->setColor(color);
}
void createEntry(prv::Provider *&provider) override {
if (this->m_entries.empty())
return;
bool open = true;
if (!this->isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::TableNextColumn();
if (ImGui::Selectable(("##PatternLine"s + std::to_string(u64(this))).c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
ImHexApi::HexEditor::setSelection(this->getOffset(), this->getSize());
}
this->drawCommentTooltip();
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", this->getOffset(), this->getOffset() + this->getSize() - 1);
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:04X}", this->getSize());
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{0}", this->m_entries[0]->getTypeName());
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("[");
ImGui::SameLine(0, 0);
ImGui::TextFormattedColored(ImColor(0xFF00FF00), "{0}", this->m_entries.size());
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("]");
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
if (open) {
for (u64 i = 0; i < this->m_entries.size(); i++) {
this->m_entries[i]->draw(provider);
if (i >= (this->m_displayEnd - 1)) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::Selectable("... (Double-click to see more items)", false, ImGuiSelectableFlags_SpanAllColumns);
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
this->m_displayEnd += 50;
break;
}
}
ImGui::TreePop();
} else {
this->m_displayEnd = 50;
}
}
void getHighlightedAddresses(std::map<u64, u32> &highlight) const override {
for (auto &entry : this->m_entries) {
entry->getHighlightedAddresses(highlight);
@ -96,6 +39,10 @@ namespace hex::pl {
return this->m_entries[0]->getTypeName() + "[" + std::to_string(this->m_entries.size()) + "]";
}
[[nodiscard]] std::string getTypeName() const {
return this->m_entries[0]->getTypeName();
}
void setOffset(u64 offset) override {
for (auto &entry : this->m_entries)
entry->setOffset(entry->getOffset() - this->getOffset() + offset);
@ -103,10 +50,19 @@ namespace hex::pl {
Pattern::setOffset(offset);
}
[[nodiscard]] size_t getEntryCount() const {
return this->m_entries.size();
}
[[nodiscard]] const auto &getEntries() {
return this->m_entries;
}
void forEachArrayEntry(const std::function<void(int, Pattern&)>& fn) {
for (u64 i = 0; i < this->m_entries.size(); i++)
fn(i, *this->m_entries[i]);
}
void setEntries(std::vector<std::shared_ptr<Pattern>> &&entries) {
this->m_entries = std::move(entries);
@ -151,9 +107,12 @@ namespace hex::pl {
Pattern::setEndian(endian);
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
private:
std::vector<std::shared_ptr<Pattern>> m_entries;
u64 m_displayEnd = 50;
};
}

View File

@ -19,65 +19,12 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternArrayStatic(*this));
}
void createEntry(prv::Provider *&provider) override {
if (this->getEntryCount() == 0)
return;
bool open = true;
if (!this->isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::TableNextColumn();
if (ImGui::Selectable(("##PatternLine"s + std::to_string(u64(this))).c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
ImHexApi::HexEditor::setSelection(this->getOffset(), this->getSize());
}
this->drawCommentTooltip();
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", this->getOffset(), this->getOffset() + this->getSize() - 1);
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:04X}", this->getSize());
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{0}", this->m_template->getTypeName().c_str());
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("[");
ImGui::SameLine(0, 0);
ImGui::TextFormattedColored(ImColor(0xFF00FF00), "{0}", this->m_entryCount);
ImGui::SameLine(0, 0);
ImGui::TextUnformatted("]");
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
if (open) {
auto entry = 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());
entry->draw(provider);
if (index >= (this->m_displayEnd - 1)) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::Selectable("... (Double-click to see more items)", false, ImGuiSelectableFlags_SpanAllColumns);
if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left))
this->m_displayEnd += 50;
break;
}
}
ImGui::TreePop();
} else {
this->m_displayEnd = 50;
void forEachArrayEntry(const std::function<void(int, Pattern&)>& fn) {
auto entry = std::shared_ptr(this->m_template->clone());
for (u64 index = 0; index < this->m_entryCount; index++) {
entry->setVariableName(hex::format("[{0}]", index));
entry->setOffset(this->getOffset() + index * this->m_template->getSize());
fn(index, *entry);
}
}
@ -105,6 +52,10 @@ namespace hex::pl {
return this->m_template->getTypeName() + "[" + std::to_string(this->m_entryCount) + "]";
}
[[nodiscard]] std::string getTypeName() const {
return this->m_template->getTypeName();
}
[[nodiscard]] const std::shared_ptr<Pattern> &getTemplate() const {
return this->m_template;
}
@ -155,11 +106,14 @@ namespace hex::pl {
Pattern::setEndian(endian);
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
private:
std::shared_ptr<Pattern> m_template = nullptr;
mutable std::unique_ptr<Pattern> m_highlightTemplate = nullptr;
size_t m_entryCount = 0;
u64 m_displayEnd = 50;
};
}

View File

@ -14,41 +14,16 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternBitfieldField(*this));
}
void createEntry(prv::Provider *&provider) override {
u64 getValue(prv::Provider *&provider) {
std::vector<u8> value(this->m_bitField->getSize(), 0);
provider->read(this->m_bitField->getOffset(), &value[0], value.size());
if (this->m_bitField->getEndian() != std::endian::native)
std::reverse(value.begin(), value.end());
ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted(this->getDisplayName().c_str());
ImGui::SameLine();
if (ImGui::Selectable(("##PatternLine"s + std::to_string(u64(this))).c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
ImHexApi::HexEditor::setSelection(this->getOffset(), this->getSize());
}
ImGui::TableNextColumn();
ImGui::ColorButton("color", ImColor(this->getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
ImGui::TableNextColumn();
if (this->m_bitSize == 1)
ImGui::TextFormatted("0x{0:08X} bit {1}", this->getOffset() + this->m_bitOffset / 8, (this->m_bitOffset + (this->m_bitSize - 1)) % 8);
else
ImGui::TextFormatted("0x{0:08X} bits {1} - {2}", this->getOffset() + this->m_bitOffset / 8, this->m_bitOffset % 8, this->m_bitOffset % 8 + (this->m_bitSize - 1) % 8);
ImGui::TableNextColumn();
if (this->m_bitSize == 1)
ImGui::TextFormatted("{0} bit", this->m_bitSize);
else
ImGui::TextFormatted("{0} bits", this->m_bitSize);
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "bits");
ImGui::TableNextColumn();
{
u8 numBytes = (this->m_bitSize / 8) + 1;
u8 numBytes = (this->m_bitSize / 8) + 1;
u64 extractedValue = hex::extract(this->m_bitOffset + (this->m_bitSize - 1), this->m_bitOffset, value);
ImGui::TextFormatted("{}", this->formatDisplayValue(hex::format("{0} (0x{1:X})", extractedValue, extractedValue), this));
}
return hex::extract(this->m_bitOffset + (this->m_bitSize - 1), this->m_bitOffset, value);
}
[[nodiscard]] std::string getFormattedName() const override {
@ -71,6 +46,10 @@ namespace hex::pl {
return this->m_bitOffset == otherBitfieldField.m_bitOffset && this->m_bitSize == otherBitfieldField.m_bitSize;
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
private:
u8 m_bitOffset, m_bitSize;
Pattern *m_bitField;
@ -92,52 +71,19 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternBitfield(*this));
}
void createEntry(prv::Provider *&provider) override {
std::vector<u8> getValue(prv::Provider *&provider) const {
std::vector<u8> value(this->getSize(), 0);
provider->read(this->getOffset(), &value[0], value.size());
if (this->m_endian == std::endian::little)
if (this->getEndian() == std::endian::little)
std::reverse(value.begin(), value.end());
bool open = true;
if (!this->isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::TableNextColumn();
if (ImGui::Selectable(("##PatternLine"s + std::to_string(u64(this))).c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
ImHexApi::HexEditor::setSelection(this->getOffset(), this->getSize());
}
this->drawCommentTooltip();
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", this->getOffset(), this->getOffset() + this->getSize() - 1);
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:04X}", this->getSize());
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFFD69C56), "bitfield");
ImGui::SameLine();
ImGui::TextUnformatted(Pattern::getTypeName().c_str());
ImGui::TableNextColumn();
std::string valueString = "{ ";
for (auto i : value)
valueString += hex::format("{0:02X} ", i);
valueString += "}";
ImGui::TextFormatted("{}", this->formatDisplayValue(valueString, this));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
if (open) {
for (auto &field : this->m_fields)
field->draw(provider);
ImGui::TreePop();
}
return value;
}
void forEachMember(const std::function<void(Pattern&)>& fn) {
for (auto &field : this->m_fields)
fn(*field);
}
void setOffset(u64 offset) override {
@ -186,6 +132,10 @@ namespace hex::pl {
return true;
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
private:
std::vector<std::shared_ptr<Pattern>> m_fields;
};

View File

@ -13,16 +13,10 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternBoolean(*this));
}
void createEntry(prv::Provider *&provider) override {
u8 getValue(prv::Provider *&provider) {
u8 boolean;
provider->read(this->getOffset(), &boolean, 1);
if (boolean == 0)
this->createDefaultEntry("false", false);
else if (boolean == 1)
this->createDefaultEntry("true", true);
else
this->createDefaultEntry("true*", true);
return boolean;
}
[[nodiscard]] std::string getFormattedName() const override {
@ -30,6 +24,10 @@ namespace hex::pl {
}
[[nodiscard]] bool operator==(const Pattern &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
void accept(PatternVisitor &v) override {
v.visit(*this);
}
};
}

View File

@ -13,11 +13,10 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternCharacter(*this));
}
void createEntry(prv::Provider *&provider) override {
char getValue(prv::Provider *&provider) {
char character;
provider->read(this->getOffset(), &character, 1);
this->createDefaultEntry(hex::format("'{0}'", character), character);
return character;
}
[[nodiscard]] std::string getFormattedName() const override {
@ -25,6 +24,10 @@ namespace hex::pl {
}
[[nodiscard]] bool operator==(const Pattern &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
void accept(PatternVisitor &v) override {
v.visit(*this);
}
};
}

View File

@ -14,66 +14,28 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternEnum(*this));
}
void createEntry(prv::Provider *&provider) override {
u64 getValue(prv::Provider *&provider) {
u64 value = 0;
provider->read(this->getOffset(), &value, this->getSize());
value = hex::changeEndianess(value, this->getSize(), this->getEndian());
std::string valueString = Pattern::getTypeName() + "::";
bool foundValue = false;
for (auto &[entryValueLiteral, entryName] : this->m_enumValues) {
bool matches = std::visit(overloaded {
[&, name = entryName](auto &&entryValue) {
if (value == entryValue) {
valueString += name;
foundValue = true;
return true;
}
return false;
},
[](std::string &) { return false; },
[](Pattern *) { return false; } },
entryValueLiteral);
if (matches)
break;
}
if (!foundValue)
valueString += "???";
ImGui::TableNextRow();
ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_Leaf | ImGuiTreeNodeFlags_NoTreePushOnOpen | ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_AllowItemOverlap);
this->drawCommentTooltip();
ImGui::TableNextColumn();
if (ImGui::Selectable(("##PatternLine"s + std::to_string(u64(this))).c_str(), false, ImGuiSelectableFlags_SpanAllColumns)) {
ImHexApi::HexEditor::setSelection(this->getOffset(), this->getSize());
}
ImGui::SameLine();
ImGui::TextUnformatted(this->getDisplayName().c_str());
ImGui::TableNextColumn();
ImGui::ColorButton("color", ImColor(this->getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", this->getOffset(), this->getOffset() + this->getSize() - 1);
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:04X}", this->getSize());
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFFD69C56), "enum");
ImGui::SameLine();
ImGui::TextUnformatted(Pattern::getTypeName().c_str());
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", this->formatDisplayValue(hex::format("{} (0x{:0{}X})", valueString.c_str(), value, this->getSize() * 2), this));
return hex::changeEndianess(value, this->getSize(), this->getEndian());
}
[[nodiscard]] std::string getFormattedName() const override {
return "enum " + Pattern::getTypeName();
}
[[nodiscard]] std::string getTypeName() const {
return Pattern::getTypeName();
}
void setEnumValues(const std::vector<std::pair<Token::Literal, std::string>> &enumValues) {
this->m_enumValues = enumValues;
}
const auto& getEnumValues() const {
return this->m_enumValues;
}
[[nodiscard]] bool operator==(const Pattern &other) const override {
if (!areCommonPropertiesEqual<decltype(*this)>(other))
return false;
@ -90,6 +52,10 @@ namespace hex::pl {
return true;
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
private:
std::vector<std::pair<Token::Literal, std::string>> m_enumValues;
};

View File

@ -13,19 +13,20 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternFloat(*this));
}
void createEntry(prv::Provider *&provider) override {
double getValue(prv::Provider *&provider) {
if (this->getSize() == 4) {
u32 data = 0;
provider->read(this->getOffset(), &data, 4);
data = hex::changeEndianess(data, 4, this->getEndian());
this->createDefaultEntry(hex::format("{:e} (0x{:0{}X})", *reinterpret_cast<float *>(&data), data, this->getSize() * 2), *reinterpret_cast<float *>(&data));
return *reinterpret_cast<float *>(&data);
} else if (this->getSize() == 8) {
u64 data = 0;
provider->read(this->getOffset(), &data, 8);
data = hex::changeEndianess(data, 8, this->getEndian());
this->createDefaultEntry(hex::format("{:e} (0x{:0{}X})", *reinterpret_cast<double *>(&data), data, this->getSize() * 2), *reinterpret_cast<double *>(&data));
return *reinterpret_cast<double *>(&data);
} else {
assert(false);
return std::numeric_limits<double>::quiet_NaN();
}
}
@ -41,6 +42,10 @@ namespace hex::pl {
}
[[nodiscard]] bool operator==(const Pattern &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
void accept(PatternVisitor &v) override {
v.visit(*this);
}
};
}

View File

@ -12,14 +12,15 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternPadding(*this));
}
void createEntry(prv::Provider *&provider) override {
}
[[nodiscard]] std::string getFormattedName() const override {
return "";
}
[[nodiscard]] bool operator==(const Pattern &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
void accept(PatternVisitor &v) override {
v.visit(*this);
}
};
}

View File

@ -19,42 +19,10 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternPointer(*this));
}
void createEntry(prv::Provider *&provider) override {
u64 getValue(prv::Provider *&provider) {
u64 data = 0;
provider->read(this->getOffset(), &data, this->getSize());
data = hex::changeEndianess(data, this->getSize(), this->getEndian());
bool open = true;
if (!this->isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::TableNextColumn();
if (ImGui::Selectable(("##PatternLine"s + std::to_string(u64(this))).c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
ImHexApi::HexEditor::setSelection(this->getOffset(), this->getSize());
}
this->drawCommentTooltip();
ImGui::SameLine(0, 0);
ImGui::ColorButton("color", ImColor(this->getColor()), ImGuiColorEditFlags_NoTooltip, ImVec2(ImGui::GetColumnWidth(), ImGui::GetTextLineHeight()));
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", this->getOffset(), this->getOffset() + this->getSize() - 1);
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:04X}", this->getSize());
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFF9BC64D), "{}", this->getFormattedName());
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", formatDisplayValue(hex::format("*(0x{0:X})", data), u128(data)));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
if (open) {
this->m_pointedAt->createEntry(provider);
ImGui::TreePop();
}
return hex::changeEndianess(data, this->getSize(), this->getEndian());
}
void getHighlightedAddresses(std::map<u64, u32> &highlight) const override {
@ -135,6 +103,10 @@ namespace hex::pl {
Pattern::setEndian(endian);
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
private:
std::unique_ptr<Pattern> m_pointedAt;
u64 m_pointedAtAddress = 0;

View File

@ -13,13 +13,12 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternSigned(*this));
}
void createEntry(prv::Provider *&provider) override {
i128 getValue(prv::Provider *&provider) {
i128 data = 0;
provider->read(this->getOffset(), &data, this->getSize());
data = hex::changeEndianess(data, this->getSize(), this->getEndian());
data = hex::signExtend(this->getSize() * 8, data);
this->createDefaultEntry(hex::format("{:d} (0x{:0{}X})", data, data, 1 * 2), data);
return hex::signExtend(this->getSize() * 8, data);
}
[[nodiscard]] std::string getFormattedName() const override {
@ -40,6 +39,10 @@ namespace hex::pl {
}
[[nodiscard]] bool operator==(const Pattern &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
void accept(PatternVisitor &v) override {
v.visit(*this);
}
};
}

View File

@ -13,17 +13,14 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternString(*this));
}
void createEntry(prv::Provider *&provider) override {
auto size = std::min<size_t>(this->getSize(), 0x7F);
if (size == 0)
return;
std::string getValue(prv::Provider *&provider) {
return this->getValue(provider, this->getSize());
}
std::string getValue(prv::Provider *&provider, size_t size) {
std::vector<u8> buffer(size, 0x00);
provider->read(this->getOffset(), buffer.data(), size);
auto displayString = hex::encodeByteString(buffer);
this->createDefaultEntry(hex::format("\"{0}\" {1}", displayString, size > this->getSize() ? "(truncated)" : ""), displayString);
return hex::encodeByteString(buffer);
}
[[nodiscard]] std::string getFormattedName() const override {
@ -42,6 +39,10 @@ namespace hex::pl {
}
[[nodiscard]] bool operator==(const Pattern &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
void accept(PatternVisitor &v) override {
v.visit(*this);
}
};
}

View File

@ -24,47 +24,17 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternStruct(*this));
}
void createEntry(prv::Provider *&provider) override {
bool open = true;
if (!this->isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::TableNextColumn();
if (ImGui::Selectable(("##PatternLine"s + std::to_string(u64(this))).c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
ImHexApi::HexEditor::setSelection(this->getOffset(), this->getSize());
}
this->drawCommentTooltip();
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", this->getOffset(), this->getOffset() + this->getSize() - (this->getSize() == 0 ? 0 : 1));
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:04X}", this->getSize());
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFFD69C56), "struct");
ImGui::SameLine();
ImGui::TextUnformatted(this->getTypeName().c_str());
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
if (open) {
for (auto &member : this->m_sortedMembers)
member->draw(provider);
ImGui::TreePop();
}
}
void getHighlightedAddresses(std::map<u64, u32> &highlight) const override {
for (auto &member : this->m_members) {
member->getHighlightedAddresses(highlight);
}
}
void forEachMember(const std::function<void(Pattern&)>& fn) {
for (auto &member : this->m_sortedMembers)
fn(*member);
}
void setOffset(u64 offset) override {
for (auto &member : this->m_members)
member->setOffset(member->getOffset() - this->getOffset() + offset);
@ -151,6 +121,10 @@ namespace hex::pl {
Pattern::setEndian(endian);
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
private:
std::vector<std::shared_ptr<Pattern>> m_members;
std::vector<Pattern *> m_sortedMembers;

View File

@ -24,40 +24,9 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternUnion(*this));
}
void createEntry(prv::Provider *&provider) override {
bool open = true;
if (!this->isInlined()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
open = ImGui::TreeNodeEx(this->getDisplayName().c_str(), ImGuiTreeNodeFlags_SpanFullWidth);
ImGui::TableNextColumn();
if (ImGui::Selectable(("##PatternLine"s + std::to_string(u64(this))).c_str(), false, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowItemOverlap)) {
ImHexApi::HexEditor::setSelection(this->getOffset(), this->getSize());
}
this->drawCommentTooltip();
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:08X} : 0x{1:08X}", this->getOffset(), std::max(this->getOffset() + this->getSize() - (this->getSize() == 0 ? 0 : 1), u64(0)));
ImGui::TableNextColumn();
ImGui::TextFormatted("0x{0:04X}", this->getSize());
ImGui::TableNextColumn();
ImGui::TextFormattedColored(ImColor(0xFFD69C56), "union");
ImGui::SameLine();
ImGui::TextUnformatted(Pattern::getTypeName().c_str());
ImGui::TableNextColumn();
ImGui::TextFormatted("{}", this->formatDisplayValue("{ ... }", this));
} else {
ImGui::SameLine();
ImGui::TreeNodeEx("", ImGuiTreeNodeFlags_SpanFullWidth | ImGuiTreeNodeFlags_Leaf);
}
if (open) {
for (auto &member : this->m_sortedMembers)
member->draw(provider);
ImGui::TreePop();
}
void forEachMember(const std::function<void(Pattern&)>& fn) {
for (auto &member : this->m_sortedMembers)
fn(*member);
}
void getHighlightedAddresses(std::map<u64, u32> &highlight) const override {
@ -96,7 +65,10 @@ namespace hex::pl {
[[nodiscard]] std::string getFormattedName() const override {
return "union " + Pattern::getTypeName();
;
}
[[nodiscard]] std::string getTypeName() const {
return Pattern::getTypeName();
}
[[nodiscard]] const auto &getMembers() const {
@ -152,6 +124,10 @@ namespace hex::pl {
Pattern::setEndian(endian);
}
void accept(PatternVisitor &v) override {
v.visit(*this);
}
private:
std::vector<std::shared_ptr<Pattern>> m_members;
std::vector<Pattern *> m_sortedMembers;

View File

@ -13,12 +13,10 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternUnsigned(*this));
}
void createEntry(prv::Provider *&provider) override {
u128 getValue(prv::Provider *&provider) {
u128 data = 0;
provider->read(this->getOffset(), &data, this->getSize());
data = hex::changeEndianess(data, this->getSize(), this->getEndian());
this->createDefaultEntry(hex::format("{:d} (0x{:0{}X})", data, data, this->getSize() * 2), data);
return hex::changeEndianess(data, this->getSize(), this->getEndian());
}
[[nodiscard]] std::string getFormattedName() const override {
@ -39,6 +37,10 @@ namespace hex::pl {
}
[[nodiscard]] bool operator==(const Pattern &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
void accept(PatternVisitor &v) override {
v.visit(*this);
}
};
}

View File

@ -15,13 +15,10 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternWideCharacter(*this));
}
void createEntry(prv::Provider *&provider) override {
char16_t getValue(prv::Provider *&provider) {
char16_t character;
provider->read(this->getOffset(), &character, 2);
character = hex::changeEndianess(character, this->getEndian());
u128 literal = character;
this->createDefaultEntry(hex::format("'{0}'", std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(character)), literal);
return hex::changeEndianess(character, this->getEndian());
}
[[nodiscard]] std::string getFormattedName() const override {
@ -37,6 +34,10 @@ namespace hex::pl {
}
[[nodiscard]] bool operator==(const Pattern &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
void accept(PatternVisitor &v) override {
v.visit(*this);
}
};
}

View File

@ -15,26 +15,22 @@ namespace hex::pl {
return std::unique_ptr<Pattern>(new PatternWideString(*this));
}
void createEntry(prv::Provider *&provider) override {
auto size = std::min<size_t>(this->getSize(), 0x100);
if (size == 0)
return;
std::string getValue(prv::Provider *&provider) {
return this->getValue(provider, this->getSize());
}
std::string getValue(prv::Provider *&provider, size_t size) {
std::u16string buffer(this->getSize() / sizeof(char16_t), 0x00);
provider->read(this->getOffset(), buffer.data(), size);
for (auto &c : buffer)
c = hex::changeEndianess(c, 2, this->getEndian());
buffer.erase(std::remove_if(buffer.begin(), buffer.end(), [](auto c) {
return c == 0x00;
}),
buffer.end());
auto it = std::remove_if(buffer.begin(), buffer.end(),
[](auto c) { return c == 0x00; });
buffer.erase(it, buffer.end());
auto utf8String = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(buffer);
this->createDefaultEntry(hex::format("\"{0}\" {1}", utf8String, size > this->getSize() ? "(truncated)" : ""), utf8String);
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.to_bytes(buffer);
}
[[nodiscard]] std::string getFormattedName() const override {
@ -56,6 +52,10 @@ namespace hex::pl {
}
[[nodiscard]] bool operator==(const Pattern &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
void accept(PatternVisitor &v) override {
v.visit(*this);
}
};
}

View File

@ -3,6 +3,7 @@
#include <hex.hpp>
#include <imgui.h>
#include <hex/pattern_language/pattern_drawer.hpp>
#include <hex/ui/view.hpp>
#include <vector>
@ -20,6 +21,7 @@ namespace hex::plugin::builtin {
private:
std::map<prv::Provider *, std::vector<std::shared_ptr<pl::Pattern>>> m_sortedPatterns;
hex::pl::PatternDrawer m_drawer;
};
}

View File

@ -61,8 +61,9 @@ namespace hex::plugin::builtin {
ImGui::TableHeadersRow();
if (!sortedPatterns.empty()) {
m_drawer.setProvider(provider);
for (auto &patterns : sortedPatterns)
patterns->draw(provider);
patterns->accept(m_drawer);
}
ImGui::EndTable();