From b491b03dfcd07e97197d68524e424b66405daba3 Mon Sep 17 00:00:00 2001 From: Skull <86374920+skkuull@users.noreply.github.com> Date: Sat, 22 Mar 2025 02:34:02 +0300 Subject: [PATCH] feat(patches): add relaunch and com err stub --- src/client/component/patches.cpp | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 src/client/component/patches.cpp diff --git a/src/client/component/patches.cpp b/src/client/component/patches.cpp new file mode 100644 index 00000000..94f3edb5 --- /dev/null +++ b/src/client/component/patches.cpp @@ -0,0 +1,79 @@ +#include +#include "loader/component_loader.hpp" + +#include "game/game.hpp" + +#include +#include + +namespace patches +{ + namespace + { + std::string GetExecutableName() + { + char path[MAX_PATH]; + if (GetModuleFileNameA(NULL, path, MAX_PATH) > 0) + { + std::string fullPath(path); + size_t pos = fullPath.find_last_of("\\/"); + return (pos == std::string::npos) ? fullPath : fullPath.substr(pos + 1); + } + return ""; + } + + utils::hook::detour relaunch_hook; + void relaunch_stub(const char* filename, const char* params) + { + if (filename == "s2_sp64_ship.exe"s || filename == "s2_mp64_ship.exe"s) + { + static char buf[MAX_PATH]; + memset(buf, 0, sizeof(buf)); + + auto exe_name = GetExecutableName(); + assert(exe_name.size() < MAX_PATH); + memcpy(buf, exe_name.data(), exe_name.size()); + + filename = buf; + + params = utils::string::va("%s -%s", params, game::environment::is_sp() ? "multiplayer" : "singleplayer"); + + relaunch_hook.invoke(filename, params); + return; + } + + relaunch_hook.invoke(filename, params); + } + + utils::hook::detour com_error_hook; + void com_error_stub(const int error, const char* msg, ...) + { + char buffer[2048]{}; + va_list ap; + + va_start(ap, msg); + vsnprintf_s(buffer, _TRUNCATE, msg, ap); + va_end(ap); + + printf("Error: %s\n", buffer); + + com_error_hook.invoke(error, "%s", buffer); + } + } + + class component final : public component_interface + { + public: + void post_load() override + { + relaunch_hook.create(SELECT_VALUE(0x1404B5170, 0x140715B10), relaunch_stub); + } + + void post_unpack() override + { + com_error_hook.create(SELECT_VALUE(0x14043CA00, 0x140073AA0), com_error_stub); + } + }; +} + +REGISTER_COMPONENT(patches::component) \ No newline at end of file