From 6f112c2d16f0e31e636411d4280aedd7fec90826 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 15 May 2025 22:00:26 +0200 Subject: [PATCH] feat: Added custom encoding row to data inspector --- plugins/builtin/romfs/lang/en_US.json | 3 + .../builtin/source/content/data_inspector.cpp | 64 ++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index 8e0dd5cc5..74038cca1 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -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)", diff --git a/plugins/builtin/source/content/data_inspector.cpp b/plugins/builtin/source/content/data_inspector.cpp index 515805c53..65c94d8b3 100644 --- a/plugins/builtin/source/content/data_inspector.cpp +++ b/plugins/builtin/source/content/data_inspector.cpp @@ -14,7 +14,10 @@ #include #include +#include +#include #include +#include namespace hex::plugin::builtin { @@ -448,7 +451,7 @@ namespace hex::plugin::builtin { u32 codepoint = 0; std::memcpy(utf8Buffer, reinterpret_cast(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 &, std::endian, Style) mutable { + std::string value, copyValue; + + if (encodingFile.valid()) { + auto currSelection = ImHexApi::HexEditor::getSelection(); + + if (currSelection.has_value()) { + std::vector stringBuffer(std::min(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 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{ {"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 -} \ No newline at end of file +}