diff --git a/lib/libimhex/include/hex/api/events/requests_interaction.hpp b/lib/libimhex/include/hex/api/events/requests_interaction.hpp index 53fa504b9..a214e4e68 100644 --- a/lib/libimhex/include/hex/api/events/requests_interaction.hpp +++ b/lib/libimhex/include/hex/api/events/requests_interaction.hpp @@ -17,7 +17,7 @@ namespace hex { * * @param region the region that should be selected */ - EVENT_DEF(RequestHexEditorSelectionChange, Region); + EVENT_DEF(RequestHexEditorSelectionChange, ImHexApi::HexEditor::ProviderRegion); /** * @brief Requests the Pattern editor to move selection diff --git a/plugins/builtin/include/content/command_line_interface.hpp b/plugins/builtin/include/content/command_line_interface.hpp index 579bb8e4e..f2468307e 100644 --- a/plugins/builtin/include/content/command_line_interface.hpp +++ b/plugins/builtin/include/content/command_line_interface.hpp @@ -14,6 +14,8 @@ namespace hex::plugin::builtin { void handleOpenCommand(const std::vector &args); void handleNewCommand(const std::vector &args); + void handleSelectCommand(const std::vector &args); + void handlePatternCommand(const std::vector &args); void handleCalcCommand(const std::vector &args); void handleHashCommand(const std::vector &args); void handleEncodeCommand(const std::vector &args); @@ -23,6 +25,7 @@ namespace hex::plugin::builtin { void handleHexdumpCommand(const std::vector &args); void handleDemangleCommand(const std::vector &args); void handleSettingsResetCommand(const std::vector &args); + void handleDebugModeCommand(const std::vector &args); void registerCommandForwarders(); diff --git a/plugins/builtin/source/content/command_line_interface.cpp b/plugins/builtin/source/content/command_line_interface.cpp index 0302a865b..62218f59a 100644 --- a/plugins/builtin/source/content/command_line_interface.cpp +++ b/plugins/builtin/source/content/command_line_interface.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include @@ -121,9 +122,31 @@ namespace hex::plugin::builtin { hex::subcommands::forwardSubCommand("new", {}); } + void handleSelectCommand(const std::vector &args) { + if (args.size() == 1) + hex::subcommands::forwardSubCommand("select", { args[0] }); + else if (args.size() == 2) + hex::subcommands::forwardSubCommand("select", { args[0], args[1] }); + else { + hex::log::println("Usage: imhex --select []"); + std::exit(EXIT_FAILURE); + } + } + + void handlePatternCommand(const std::vector &args) { + if (args.size() == 1) + hex::subcommands::forwardSubCommand("pattern", { args[0] }); + else { + hex::log::println("Usage: imhex --pattern "); + hex::log::println("Usage: imhex --pattern "); + std::exit(EXIT_FAILURE); + } + } + void handleCalcCommand(const std::vector &args) { if (args.empty()) { hex::log::println("No expression provided!"); + hex::log::println("Usage: imhex --calc "); hex::log::println("Example: imhex --calc \"5 * 7\""); std::exit(EXIT_FAILURE); } @@ -384,6 +407,10 @@ namespace hex::plugin::builtin { } } + void handleDebugModeCommand(const std::vector &) { + hex::dbg::setDebugModeEnabled(true); + } + void registerCommandForwarders() { hex::subcommands::registerSubCommand("open", [](const std::vector &args){ @@ -398,6 +425,39 @@ namespace hex::plugin::builtin { hex::subcommands::registerSubCommand("new", [](const std::vector &){ RequestOpenWindow::post("Create File"); }); + + hex::subcommands::registerSubCommand("select", [](const std::vector &args){ + try { + if (args.size() == 1) + ImHexApi::HexEditor::setSelection(std::stoull(args[0]), 1); + else if (args.size() == 2) { + const auto start = std::stoull(args[0]); + const auto size = (std::stoull(args[1]) - start) + 1; + ImHexApi::HexEditor::setSelection(start, size); + } else { + log::error("Invalid number of arguments for select command!"); + } + } catch (const std::exception &e) { + log::error("Failed to set requested selection region! {}", e.what()); + } + }); + + hex::subcommands::registerSubCommand("pattern", [](const std::vector &args){ + std::string patternSourceCode; + if (std::fs::exists(args[0])) { + wolv::io::File file(args[0], wolv::io::File::Mode::Read); + if (!file.isValid()) { + patternSourceCode = args[0]; + } else { + patternSourceCode = file.readString(); + } + } else { + patternSourceCode = args[0]; + } + + RequestSetPatternLanguageCode::post(patternSourceCode); + RequestRunPatternCode::post(); + }); } } \ No newline at end of file diff --git a/plugins/builtin/source/content/events.cpp b/plugins/builtin/source/content/events.cpp index 2bd7b154f..464aa6f51 100644 --- a/plugins/builtin/source/content/events.cpp +++ b/plugins/builtin/source/content/events.cpp @@ -50,6 +50,8 @@ namespace hex::plugin::builtin { EventProviderOpened::post(fileProvider); AchievementManager::unlockAchievement("hex.builtin.achievement.starting_out", "hex.builtin.achievement.starting_out.open_file.name"); } + + ImHexApi::Provider::setCurrentProvider(provider); } glfwRequestWindowAttention(ImHexApi::System::getMainWindowHandle()); diff --git a/plugins/builtin/source/content/views/view_hex_editor.cpp b/plugins/builtin/source/content/views/view_hex_editor.cpp index 3d836cac6..4e50889f4 100644 --- a/plugins/builtin/source/content/views/view_hex_editor.cpp +++ b/plugins/builtin/source/content/views/view_hex_editor.cpp @@ -1041,8 +1041,8 @@ namespace hex::plugin::builtin { } void ViewHexEditor::registerEvents() { - RequestHexEditorSelectionChange::subscribe(this, [this](Region region) { - auto provider = ImHexApi::Provider::get(); + RequestHexEditorSelectionChange::subscribe(this, [this](ImHexApi::HexEditor::ProviderRegion region) { + auto provider = region.getProvider(); if (region == Region::Invalid() || provider == nullptr) { m_selectionStart->reset(); @@ -1058,6 +1058,7 @@ namespace hex::plugin::builtin { if (region.size != 0) { provider->setCurrentPage(page.value()); + m_hexEditor.setProvider(provider); this->setSelection(region); this->jumpIfOffScreen(); } diff --git a/plugins/builtin/source/plugin_builtin.cpp b/plugins/builtin/source/plugin_builtin.cpp index 065127728..516dbbc3c 100644 --- a/plugins/builtin/source/plugin_builtin.cpp +++ b/plugins/builtin/source/plugin_builtin.cpp @@ -71,6 +71,8 @@ IMHEX_PLUGIN_SUBCOMMANDS() { { "open", "o", "Open files passed as argument. [default]", hex::plugin::builtin::handleOpenCommand }, { "new", "n", "Create a new empty file", hex::plugin::builtin::handleNewCommand }, + { "select", "", "Select a range of bytes in the Hex Editor", hex::plugin::builtin::handleSelectCommand }, + { "pattern", "", "Sets the loaded pattern", hex::plugin::builtin::handlePatternCommand }, { "calc", "", "Evaluate a mathematical expression", hex::plugin::builtin::handleCalcCommand }, { "hash", "", "Calculate the hash of a file", hex::plugin::builtin::handleHashCommand }, { "encode", "", "Encode a string", hex::plugin::builtin::handleEncodeCommand }, @@ -80,6 +82,7 @@ IMHEX_PLUGIN_SUBCOMMANDS() { { "hexdump", "", "Generate a hex dump of the provided file", hex::plugin::builtin::handleHexdumpCommand }, { "demangle", "", "Demangle a mangled symbol", hex::plugin::builtin::handleDemangleCommand }, { "reset-settings", "", "Resets all settings back to default", hex::plugin::builtin::handleSettingsResetCommand }, + { "debug-mode", "", "Enables debugging features", hex::plugin::builtin::handleDebugModeCommand, } }; IMHEX_PLUGIN_SETUP("Built-in", "WerWolv", "Default ImHex functionality") { diff --git a/plugins/ui/include/ui/hex_editor.hpp b/plugins/ui/include/ui/hex_editor.hpp index 01136e146..662c79e84 100644 --- a/plugins/ui/include/ui/hex_editor.hpp +++ b/plugins/ui/include/ui/hex_editor.hpp @@ -144,7 +144,7 @@ namespace hex::ui { if (!m_selectionStart.has_value()) m_selectionStart = start; if (!m_selectionEnd.has_value()) m_selectionEnd = end; - if (auto bytesPerCell = m_currDataVisualizer->getBytesPerCell(); bytesPerCell > 1) { + if (auto bytesPerCell = m_currDataVisualizer == nullptr ? 1 : m_currDataVisualizer->getBytesPerCell(); bytesPerCell > 1) { if (end > start) { start = alignDown(start, bytesPerCell); end = alignDown(end, bytesPerCell) + (bytesPerCell - 1);