diff --git a/lib/libimhex/source/subcommands/subcommands.cpp b/lib/libimhex/source/subcommands/subcommands.cpp index e089f529d..e80bae2cf 100644 --- a/lib/libimhex/source/subcommands/subcommands.cpp +++ b/lib/libimhex/source/subcommands/subcommands.cpp @@ -20,6 +20,7 @@ namespace hex::subcommands { } } } + return std::nullopt; } @@ -27,7 +28,8 @@ namespace hex::subcommands { // 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) - if (args.empty()) return; + if (args.empty()) + return; std::vector>> subCommands; @@ -76,18 +78,18 @@ namespace hex::subcommands { } // Save last command to run - if (currentSubCommand) { + if (currentSubCommand.has_value()) { subCommands.emplace_back(*currentSubCommand, currentSubCommandArgs); } // Run the subcommands - for (auto& subCommandPair : subCommands) { - subCommandPair.first.callback(subCommandPair.second); + for (auto &[subcommand, args] : subCommands) { + subcommand.callback(args); } - // Exit the process if its not the main instance (the commands have been forwarded to another instance) + // Exit the process if it's not the main instance (the commands have been forwarded to another instance) if (!ImHexApi::System::isMainInstance()) { - exit(0); + std::exit(0); } } @@ -95,12 +97,15 @@ namespace hex::subcommands { log::debug("Forwarding subcommand {} (maybe to us)", cmdName); std::vector data; - for (const auto &arg: args) { - data.insert(data.end(), arg.begin(), arg.end()); - data.push_back('\0'); + if (!args.empty()) { + for (const auto &arg: args) { + data.insert(data.end(), arg.begin(), arg.end()); + data.push_back('\0'); + } + + data.pop_back(); } - data.erase(data.end()-1); - + SendMessageToMainInstance::post(hex::format("command/{}", cmdName), data); } diff --git a/main/gui/source/messaging/common.cpp b/main/gui/source/messaging/common.cpp index 931dc64c6..4af6b0004 100644 --- a/main/gui/source/messaging/common.cpp +++ b/main/gui/source/messaging/common.cpp @@ -15,12 +15,13 @@ namespace hex::messaging { void setupEvents() { SendMessageToMainInstance::subscribe([](const std::string &eventName, const std::vector &eventData) { - log::debug("Forwarding message {} (maybe to us)", eventName); if (ImHexApi::System::isMainInstance()) { + log::debug("Executing message '{}' in current instance", eventName); EventImHexStartupFinished::subscribe([eventName, eventData]{ ImHexApi::Messaging::impl::runHandler(eventName, eventData); }); } else { + log::debug("Forwarding message '{}' to existing instance", eventName); sendToOtherInstance(eventName, eventData); } });