2023-11-10 20:47:08 +01:00
|
|
|
#include <numeric>
|
|
|
|
#include <string_view>
|
|
|
|
#include <ranges>
|
2023-07-13 14:08:23 +02:00
|
|
|
|
|
|
|
#include "hex/subcommands/subcommands.hpp"
|
|
|
|
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/event_manager.hpp>
|
2023-07-13 14:08:23 +02:00
|
|
|
#include <hex/api/plugin_manager.hpp>
|
|
|
|
#include <hex/api/imhex_api.hpp>
|
|
|
|
#include <hex/helpers/logger.hpp>
|
2023-11-10 20:47:08 +01:00
|
|
|
#include <hex/helpers/fmt.hpp>
|
2023-07-13 14:08:23 +02:00
|
|
|
|
|
|
|
namespace hex::subcommands {
|
|
|
|
|
|
|
|
std::optional<SubCommand> findSubCommand(const std::string &arg) {
|
|
|
|
for (auto &plugin : PluginManager::getPlugins()) {
|
|
|
|
for (auto &subCommand : plugin.getSubCommands()) {
|
2024-03-14 18:24:31 +01:00
|
|
|
if (hex::format("--{}", subCommand.commandLong) == arg || hex::format("-{}", subCommand.commandShort) == arg) {
|
2023-07-13 14:08:23 +02:00
|
|
|
return subCommand;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-20 11:09:57 +02:00
|
|
|
|
2023-07-13 14:08:23 +02:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void processArguments(const std::vector<std::string> &args) {
|
|
|
|
// If no arguments, do not even try to process arguments
|
|
|
|
// (important because this function will exit ImHex if an instance is already opened,
|
|
|
|
// and we don't want that if no arguments were provided)
|
2024-06-20 11:09:57 +02:00
|
|
|
if (args.empty())
|
|
|
|
return;
|
2023-07-13 14:08:23 +02:00
|
|
|
|
|
|
|
std::vector<std::pair<SubCommand, std::vector<std::string>>> subCommands;
|
|
|
|
|
|
|
|
auto argsIter = args.begin();
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Get subcommand associated with the first argument
|
2023-07-13 14:08:23 +02:00
|
|
|
std::optional<SubCommand> currentSubCommand = findSubCommand(*argsIter);
|
|
|
|
|
|
|
|
if (currentSubCommand) {
|
2023-11-10 20:47:08 +01:00
|
|
|
argsIter += 1;
|
2023-10-04 12:00:32 +02:00
|
|
|
// If it is a valid subcommand, remove it from the argument list
|
2023-07-13 14:08:23 +02:00
|
|
|
} else {
|
2023-10-04 12:00:32 +02:00
|
|
|
// If no (valid) subcommand was provided, the default one is --open
|
2023-07-13 14:08:23 +02:00
|
|
|
currentSubCommand = findSubCommand("--open");
|
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Arguments of the current subcommand
|
2023-07-13 14:08:23 +02:00
|
|
|
std::vector<std::string> currentSubCommandArgs;
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Compute all subcommands to run
|
2023-07-13 14:08:23 +02:00
|
|
|
while (argsIter != args.end()) {
|
|
|
|
const std::string &arg = *argsIter;
|
|
|
|
|
|
|
|
if (arg == "--othercmd") {
|
2023-10-04 12:00:32 +02:00
|
|
|
// Save command to run
|
2023-07-13 14:08:23 +02:00
|
|
|
if (currentSubCommand) {
|
2023-07-16 23:46:41 +02:00
|
|
|
subCommands.emplace_back(*currentSubCommand, currentSubCommandArgs);
|
2023-07-13 14:08:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
currentSubCommand = std::nullopt;
|
|
|
|
currentSubCommandArgs = { };
|
|
|
|
|
|
|
|
} else if (currentSubCommand) {
|
2023-10-04 12:00:32 +02:00
|
|
|
// Add current argument to the current command
|
2023-07-13 14:08:23 +02:00
|
|
|
currentSubCommandArgs.push_back(arg);
|
|
|
|
} else {
|
2023-10-04 12:00:32 +02:00
|
|
|
// Get next subcommand from current argument
|
2023-07-13 14:08:23 +02:00
|
|
|
currentSubCommand = findSubCommand(arg);
|
|
|
|
if (!currentSubCommand) {
|
|
|
|
log::error("No subcommand named '{}' found", arg);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 20:47:08 +01:00
|
|
|
argsIter += 1;
|
2023-07-13 14:08:23 +02:00
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Save last command to run
|
2024-06-20 11:09:57 +02:00
|
|
|
if (currentSubCommand.has_value()) {
|
2023-07-16 23:46:41 +02:00
|
|
|
subCommands.emplace_back(*currentSubCommand, currentSubCommandArgs);
|
2023-07-13 14:08:23 +02:00
|
|
|
}
|
|
|
|
|
2023-10-04 12:00:32 +02:00
|
|
|
// Run the subcommands
|
2024-06-20 11:09:57 +02:00
|
|
|
for (auto &[subcommand, args] : subCommands) {
|
|
|
|
subcommand.callback(args);
|
2023-07-13 14:08:23 +02:00
|
|
|
}
|
|
|
|
|
2024-06-20 11:09:57 +02:00
|
|
|
// Exit the process if it's not the main instance (the commands have been forwarded to another instance)
|
2023-07-13 14:08:23 +02:00
|
|
|
if (!ImHexApi::System::isMainInstance()) {
|
2024-06-20 11:09:57 +02:00
|
|
|
std::exit(0);
|
2023-07-13 14:08:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void forwardSubCommand(const std::string &cmdName, const std::vector<std::string> &args) {
|
|
|
|
log::debug("Forwarding subcommand {} (maybe to us)", cmdName);
|
|
|
|
|
2023-10-24 09:59:23 +02:00
|
|
|
std::vector<u8> data;
|
2024-06-20 11:09:57 +02:00
|
|
|
if (!args.empty()) {
|
|
|
|
for (const auto &arg: args) {
|
|
|
|
data.insert(data.end(), arg.begin(), arg.end());
|
|
|
|
data.push_back('\0');
|
|
|
|
}
|
|
|
|
|
|
|
|
data.pop_back();
|
2023-10-24 09:59:23 +02:00
|
|
|
}
|
2024-06-20 11:09:57 +02:00
|
|
|
|
2023-12-08 10:29:44 +01:00
|
|
|
SendMessageToMainInstance::post(hex::format("command/{}", cmdName), data);
|
2023-07-13 14:08:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void registerSubCommand(const std::string &cmdName, const ForwardCommandHandler &handler) {
|
|
|
|
log::debug("Registered new forward command handler: {}", cmdName);
|
|
|
|
|
2024-02-10 23:31:05 +01:00
|
|
|
ImHexApi::Messaging::registerHandler(hex::format("command/{}", cmdName), [handler](const std::vector<u8> &eventData){
|
|
|
|
std::string string(reinterpret_cast<const char *>(eventData.data()), eventData.size());
|
2023-07-13 14:08:23 +02:00
|
|
|
|
|
|
|
std::vector<std::string> args;
|
|
|
|
|
2024-03-14 18:24:31 +01:00
|
|
|
for (const auto &argument : std::views::split(string, char(0x00))) {
|
|
|
|
std::string arg(argument.data(), argument.size());
|
2023-07-13 14:08:23 +02:00
|
|
|
args.push_back(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
handler(args);
|
2024-02-10 23:31:05 +01:00
|
|
|
});
|
2023-07-13 14:08:23 +02:00
|
|
|
}
|
|
|
|
}
|