1
0
mirror of synced 2025-01-30 11:37:32 +01:00

feat: Added new --select, --pattern and --debug-mode subcommands

This commit is contained in:
WerWolv 2025-01-29 18:32:54 +01:00
parent e74e4e92a0
commit aee7a09b6c
7 changed files with 73 additions and 4 deletions

View File

@ -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

View File

@ -14,6 +14,8 @@ namespace hex::plugin::builtin {
void handleOpenCommand(const std::vector<std::string> &args);
void handleNewCommand(const std::vector<std::string> &args);
void handleSelectCommand(const std::vector<std::string> &args);
void handlePatternCommand(const std::vector<std::string> &args);
void handleCalcCommand(const std::vector<std::string> &args);
void handleHashCommand(const std::vector<std::string> &args);
void handleEncodeCommand(const std::vector<std::string> &args);
@ -23,6 +25,7 @@ namespace hex::plugin::builtin {
void handleHexdumpCommand(const std::vector<std::string> &args);
void handleDemangleCommand(const std::vector<std::string> &args);
void handleSettingsResetCommand(const std::vector<std::string> &args);
void handleDebugModeCommand(const std::vector<std::string> &args);
void registerCommandForwarders();

View File

@ -15,6 +15,7 @@
#include <hex/helpers/literals.hpp>
#include <hex/helpers/utils.hpp>
#include <hex/helpers/default_paths.hpp>
#include <hex/helpers/debugging.hpp>
#include <hex/subcommands/subcommands.hpp>
@ -121,9 +122,31 @@ namespace hex::plugin::builtin {
hex::subcommands::forwardSubCommand("new", {});
}
void handleSelectCommand(const std::vector<std::string> &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 <start> [<end>]");
std::exit(EXIT_FAILURE);
}
}
void handlePatternCommand(const std::vector<std::string> &args) {
if (args.size() == 1)
hex::subcommands::forwardSubCommand("pattern", { args[0] });
else {
hex::log::println("Usage: imhex --pattern <pattern source code>");
hex::log::println("Usage: imhex --pattern <pattern file path>");
std::exit(EXIT_FAILURE);
}
}
void handleCalcCommand(const std::vector<std::string> &args) {
if (args.empty()) {
hex::log::println("No expression provided!");
hex::log::println("Usage: imhex --calc <math expression>");
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<std::string> &) {
hex::dbg::setDebugModeEnabled(true);
}
void registerCommandForwarders() {
hex::subcommands::registerSubCommand("open", [](const std::vector<std::string> &args){
@ -398,6 +425,39 @@ namespace hex::plugin::builtin {
hex::subcommands::registerSubCommand("new", [](const std::vector<std::string> &){
RequestOpenWindow::post("Create File");
});
hex::subcommands::registerSubCommand("select", [](const std::vector<std::string> &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<std::string> &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();
});
}
}

View File

@ -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());

View File

@ -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();
}

View File

@ -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") {

View File

@ -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);