1
0
mirror of synced 2025-01-18 00:56:49 +01:00

patterns: Added ability to format patterns as strings.

This adds support for printing char16 strings
This commit is contained in:
WerWolv 2021-11-04 20:32:34 +01:00
parent beea4c4147
commit 1c1396bf4b
2 changed files with 32 additions and 2 deletions

View File

@ -16,7 +16,7 @@
namespace hex::plugin::builtin { namespace hex::plugin::builtin {
std::string format(pl::Evaluator *, auto params) { std::string format(pl::Evaluator *ctx, auto params) {
auto format = pl::Token::literalToString(params[0], true); auto format = pl::Token::literalToString(params[0], true);
std::string message; std::string message;
@ -27,7 +27,7 @@ namespace hex::plugin::builtin {
std::visit(overloaded { std::visit(overloaded {
[&](pl::PatternData* value) { [&](pl::PatternData* value) {
formatArgs.push_back(hex::format("{} {} @ 0x{:X}", value->getTypeName(), value->getVariableName(), value->getOffset())); formatArgs.push_back(value->toString(ctx->getProvider()));
}, },
[&](auto &&value) { [&](auto &&value) {
formatArgs.push_back(value); formatArgs.push_back(value);

View File

@ -160,6 +160,11 @@ namespace hex::pl {
virtual void sort(ImGuiTableSortSpecs *sortSpecs, prv::Provider *provider) { } virtual void sort(ImGuiTableSortSpecs *sortSpecs, prv::Provider *provider) { }
[[nodiscard]]
virtual std::string toString(prv::Provider *provider) const {
return hex::format("{} {} @ 0x{:X}", this->getTypeName(), this->getVariableName(), this->getOffset());
}
static bool sortPatternDataTable(ImGuiTableSortSpecs *sortSpecs, prv::Provider *provider, pl::PatternData* left, pl::PatternData* right) { static bool sortPatternDataTable(ImGuiTableSortSpecs *sortSpecs, prv::Provider *provider, pl::PatternData* left, pl::PatternData* right) {
if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("name")) { if (sortSpecs->Specs->ColumnUserID == ImGui::GetID("name")) {
if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending) if (sortSpecs->Specs->SortDirection == ImGuiSortDirection_Ascending)
@ -611,6 +616,14 @@ namespace hex::pl {
return "char16"; return "char16";
} }
[[nodiscard]] std::string toString(prv::Provider *provider) const override {
char16_t character;
provider->read(this->getOffset(), &character, 2);
character = hex::changeEndianess(character, this->getEndian());
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(character);
}
[[nodiscard]] bool operator==(const PatternData &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); } [[nodiscard]] bool operator==(const PatternData &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
}; };
@ -636,6 +649,13 @@ namespace hex::pl {
return "String"; return "String";
} }
[[nodiscard]] std::string toString(prv::Provider *provider) const override {
std::string buffer(this->getSize(), 0x00);
provider->read(this->getOffset(), buffer.data(), buffer.size());
return buffer;
}
[[nodiscard]] bool operator==(const PatternData &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); } [[nodiscard]] bool operator==(const PatternData &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
}; };
@ -666,6 +686,16 @@ namespace hex::pl {
return "String16"; return "String16";
} }
[[nodiscard]] std::string toString(prv::Provider *provider) const override {
std::u16string buffer(this->getSize(), 0x00);
provider->read(this->getOffset(), buffer.data(), buffer.size());
for (auto &c : buffer)
c = hex::changeEndianess(c, 2, this->getEndian());
return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(buffer);
}
[[nodiscard]] bool operator==(const PatternData &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); } [[nodiscard]] bool operator==(const PatternData &other) const override { return areCommonPropertiesEqual<decltype(*this)>(other); }
}; };