From 05ca4983434ae1446b810e71d4da9560d2cd1f32 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Wed, 2 Aug 2023 13:09:21 +0200 Subject: [PATCH] feat: Added Fill option to hex editor --- plugins/builtin/romfs/lang/en_US.json | 1 + .../source/content/views/view_hex_editor.cpp | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/plugins/builtin/romfs/lang/en_US.json b/plugins/builtin/romfs/lang/en_US.json index b2064bb01..0ba5c7a63 100644 --- a/plugins/builtin/romfs/lang/en_US.json +++ b/plugins/builtin/romfs/lang/en_US.json @@ -778,6 +778,7 @@ "hex.builtin.view.hex_editor.goto.offset.relative": "Relative", "hex.builtin.view.hex_editor.menu.edit.copy": "Copy", "hex.builtin.view.hex_editor.menu.edit.copy_as": "Copy as...", + "hex.builtin.view.hex_editor.menu.edit.fill": "Fill...", "hex.builtin.view.hex_editor.menu.edit.insert": "Insert...", "hex.builtin.view.hex_editor.menu.edit.jump_to": "Jump to", "hex.builtin.view.hex_editor.menu.edit.open_in_new_provider": "Open selection view...", diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index d76a548fe..bf0952da1 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -502,6 +502,53 @@ namespace hex::plugin::builtin { u64 m_size; }; + class PopupFill : public ViewHexEditor::Popup { + public: + PopupFill(u64 address, size_t size) : m_address(address), m_size(size) {} + + void draw(ViewHexEditor *editor) override { + ImGui::TextUnformatted("hex.builtin.view.hex_editor.menu.edit.fill"_lang); + + ImGui::InputHexadecimal("hex.builtin.common.address"_lang, &this->m_address); + ImGui::InputHexadecimal("hex.builtin.common.size"_lang, &this->m_size); + + ImGui::Separator(); + + ImGui::InputTextIcon("hex.builtin.common.bytes"_lang, ICON_VS_SYMBOL_NAMESPACE, this->m_input); + + View::confirmButtons("hex.builtin.common.set"_lang, "hex.builtin.common.cancel"_lang, + [&, this] { + fill(this->m_address, static_cast(this->m_size), this->m_input); + editor->closePopup(); + }, + [&] { + editor->closePopup(); + }); + } + + private: + static void fill(u64 address, size_t size, const std::string &input) { + if (!ImHexApi::Provider::isValid()) + return; + + auto bytes = crypt::decode16(input); + if (bytes.empty()) + return; + + auto provider = ImHexApi::Provider::get(); + for (u64 i = 0; i < size; i += bytes.size()) { + auto remainingSize = std::min(size - i, bytes.size()); + provider->write(provider->getBaseAddress() + address + i, bytes.data(), remainingSize); + } + } + + private: + u64 m_address; + u64 m_size; + + std::string m_input; + }; + /* Hex Editor */ ViewHexEditor::ViewHexEditor() : View("hex.builtin.view.hex_editor.name") { @@ -1187,6 +1234,15 @@ namespace hex::plugin::builtin { }, [] { return ImHexApi::HexEditor::isSelectionValid() && ImHexApi::Provider::isValid() && ImHexApi::Provider::get()->isResizable(); }); + /* Fill */ + ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.fill" }, 1810, Shortcut::None, + [this] { + auto selection = ImHexApi::HexEditor::getSelection(); + + this->openPopup(selection->getStartAddress(), selection->getSize()); + }, + [] { return ImHexApi::HexEditor::isSelectionValid() && ImHexApi::Provider::isValid() && ImHexApi::Provider::get()->isWritable(); }); + /* Jump to */ ContentRegistry::Interface::addMenuItem({ "hex.builtin.menu.edit", "hex.builtin.view.hex_editor.menu.edit.jump_to" }, 1850, Shortcut::None, [] {