From c6e1f45dc3788f646969b2bb4a36f1e2fd52880c Mon Sep 17 00:00:00 2001 From: WerWolv Date: Sat, 28 Jan 2023 21:12:35 +0100 Subject: [PATCH] feat: Added option to copy custom encoding strings from hex view --- .../include/hex/helpers/encoding_file.hpp | 3 ++- lib/libimhex/source/helpers/encoding_file.cpp | 2 +- plugins/builtin/include/ui/hex_editor.hpp | 4 ++++ plugins/builtin/romfs/lang/en_US.json | 1 + .../source/content/views/view_hex_editor.cpp | 17 +++++++++++++++++ 5 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/encoding_file.hpp b/lib/libimhex/include/hex/helpers/encoding_file.hpp index 05ffa451a..0d260e42b 100644 --- a/lib/libimhex/include/hex/helpers/encoding_file.hpp +++ b/lib/libimhex/include/hex/helpers/encoding_file.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -21,7 +22,7 @@ namespace hex { EncodingFile() = default; EncodingFile(Type type, const std::fs::path &path); - [[nodiscard]] std::pair getEncodingFor(const std::vector &buffer) const; + [[nodiscard]] std::pair getEncodingFor(std::span buffer) const; [[nodiscard]] size_t getLongestSequence() const { return this->m_longestSequence; } [[nodiscard]] bool valid() const { return this->m_valid; } diff --git a/lib/libimhex/source/helpers/encoding_file.cpp b/lib/libimhex/source/helpers/encoding_file.cpp index 911d63a8e..1b0ab8b45 100644 --- a/lib/libimhex/source/helpers/encoding_file.cpp +++ b/lib/libimhex/source/helpers/encoding_file.cpp @@ -17,7 +17,7 @@ namespace hex { this->m_valid = true; } - std::pair EncodingFile::getEncodingFor(const std::vector &buffer) const { + std::pair EncodingFile::getEncodingFor(std::span buffer) const { for (auto riter = this->m_mapping.crbegin(); riter != this->m_mapping.crend(); ++riter) { const auto &[size, mapping] = *riter; diff --git a/plugins/builtin/include/ui/hex_editor.hpp b/plugins/builtin/include/ui/hex_editor.hpp index a33a39852..cd6b9ac4c 100644 --- a/plugins/builtin/include/ui/hex_editor.hpp +++ b/plugins/builtin/include/ui/hex_editor.hpp @@ -132,6 +132,10 @@ namespace hex::plugin::builtin::ui { this->m_characterCellPadding = characterCellPadding; } + [[nodiscard]] const std::optional& getCustomEncoding() const { + return this->m_currCustomEncoding; + } + void setCustomEncoding(EncodingFile encoding) { this->m_currCustomEncoding = std::move(encoding); } diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index 27e358458..7511ff9c0 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -655,6 +655,7 @@ "hex.builtin.view.hex_editor.copy.cpp": "C++ Array", "hex.builtin.view.hex_editor.copy.crystal": "Crystal Array", "hex.builtin.view.hex_editor.copy.csharp": "C# Array", + "hex.builtin.view.hex_editor.copy.custom_encoding": "Custom Encoding", "hex.builtin.view.hex_editor.copy.go": "Go Array", "hex.builtin.view.hex_editor.copy.hex_view": "Hex View", "hex.builtin.view.hex_editor.copy.html": "HTML", diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 7df5f670c..ca6bd0f38 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -985,6 +985,23 @@ namespace hex::plugin::builtin { if (ImGui::MenuItem("hex.builtin.view.hex_editor.copy.address"_lang)) ImGui::SetClipboardText(hex::format("0x{:08X}", selection->getStartAddress()).c_str()); + auto &customEncoding = this->m_hexEditor.getCustomEncoding(); + if (ImGui::MenuItem("hex.builtin.view.hex_editor.copy.custom_encoding"_lang, "", false, customEncoding.has_value())) { + + std::vector buffer(customEncoding->getLongestSequence(), 0x00); + std::string string; + + u64 offset = selection->getStartAddress(); + while (offset < selection->getEndAddress()) { + provider->read(offset, buffer.data(), std::min(buffer.size(), selection->size - (offset - selection->getStartAddress()))); + auto [result, size] = customEncoding->getEncodingFor(buffer); + + string += result; + offset += size; + }; + + ImGui::SetClipboardText(string.c_str()); + } ImGui::Separator(); for (const auto &[unlocalizedName, callback] : ContentRegistry::DataFormatter::getEntries()) {