feat: Added custom encoding row to data inspector

This commit is contained in:
WerWolv
2025-05-15 22:00:26 +02:00
parent 8f222dab99
commit 6f112c2d16
2 changed files with 65 additions and 2 deletions

View File

@@ -79,6 +79,9 @@
"hex.builtin.inspector.ascii": "ASCII Character",
"hex.builtin.inspector.binary": "Binary (8 bit)",
"hex.builtin.inspector.bool": "bool",
"hex.builtin.inspector.custom_encoding": "Custom Encoding",
"hex.builtin.inspector.custom_encoding.change": "Select encoding",
"hex.builtin.inspector.custom_encoding.no_encoding": "No encoding selected",
"hex.builtin.inspector.dos_date": "DOS Date",
"hex.builtin.inspector.dos_time": "DOS Time",
"hex.builtin.inspector.double": "double (64 bit)",

View File

@@ -14,7 +14,10 @@
#include <imgui_internal.h>
#include <fonts/vscode_icons.hpp>
#include <hex/helpers/default_paths.hpp>
#include <hex/helpers/encoding_file.hpp>
#include <hex/ui/imgui_imhex_extensions.h>
#include <popups/popup_file_chooser.hpp>
namespace hex::plugin::builtin {
@@ -448,7 +451,7 @@ namespace hex::plugin::builtin {
u32 codepoint = 0;
std::memcpy(utf8Buffer, reinterpret_cast<char8_t *>(buffer.data()), 4);
u8 codepointSize = ImTextCharFromUtf8(&codepoint, utf8Buffer, utf8Buffer + 4);
u8 codepointSize = ImTextCharFromUtf8(&codepoint, utf8Buffer, nullptr);
std::memcpy(codepointString, utf8Buffer, std::min(codepointSize, u8(4)));
auto value = hex::format("'{0}' (U+0x{1:04X})",
@@ -616,6 +619,63 @@ namespace hex::plugin::builtin {
}
);
ContentRegistry::DataInspector::add("hex.builtin.inspector.custom_encoding", 1, [encodingFile = EncodingFile()](const std::vector<u8> &, std::endian, Style) mutable {
std::string value, copyValue;
if (encodingFile.valid()) {
auto currSelection = ImHexApi::HexEditor::getSelection();
if (currSelection.has_value()) {
std::vector<u8> stringBuffer(std::min<size_t>(currSelection->size, 0x1000), 0x00);
ImHexApi::Provider::get()->read(currSelection->address, stringBuffer.data(), stringBuffer.size());
u64 offset = 0;
while (offset < stringBuffer.size()) {
const auto [character, size] = encodingFile.getEncodingFor(std::span(stringBuffer).subspan(offset));
value += character;
offset += size;
}
copyValue = value;
if (value.size() > MaxStringLength) {
value.resize(MaxStringLength);
value += "...";
}
} else {
value = "";
copyValue = "";
}
} else {
value = "Invalid";
}
return [&, value, copyValue]() mutable -> std::string {
ContentRegistry::DataInspector::drawMenuItems([&] {
if (ImGui::MenuItemEx("hex.builtin.inspector.custom_encoding.change"_lang, "")) {
const auto basePaths = paths::Encodings.read();
std::vector<std::fs::path> paths;
for (const auto &basePath : basePaths) {
for (const auto &entry : std::filesystem::directory_iterator(basePath)) {
paths.push_back(entry.path());
}
}
ui::PopupFileChooser::open(basePaths, paths, std::vector<fs::ItemFilter>{ {"Thingy Table File", "tbl"} }, false, [&](const auto &path) {
encodingFile = EncodingFile(EncodingFile::Type::Thingy, path);
});
}
});
if (encodingFile.valid() && !value.empty()) {
ImGuiExt::TextFormatted("({})\"{}\"", encodingFile.getName(), value);
} else {
ImGuiExt::TextFormatted("hex.builtin.inspector.custom_encoding.no_encoding"_lang);
}
return copyValue;
};
});
#if defined(OS_WINDOWS)
ContentRegistry::DataInspector::add("hex.builtin.inspector.time32", sizeof(u32), [](auto buffer, auto endian, auto style) {
@@ -769,4 +829,4 @@ namespace hex::plugin::builtin {
}
// clang-format on
}
}