From 7e87e044139ff18c340d5c8d100a8d9132d79915 Mon Sep 17 00:00:00 2001 From: ineed bots Date: Wed, 20 Sep 2023 14:20:39 -0600 Subject: [PATCH] Add scr init callback --- src/codsrc/clientscript/cscr_vm.cpp | 9 ++++ src/component/scheduler.cpp | 71 +++++++++++++++++++++++++++++ src/component/scheduler.hpp | 5 ++ 3 files changed, 85 insertions(+) diff --git a/src/codsrc/clientscript/cscr_vm.cpp b/src/codsrc/clientscript/cscr_vm.cpp index 1ec8ec0..ff1edce 100644 --- a/src/codsrc/clientscript/cscr_vm.cpp +++ b/src/codsrc/clientscript/cscr_vm.cpp @@ -1,5 +1,6 @@ #include #include "clientscript_public.hpp" +#include #pragma warning(push) #pragma warning(disable: 4244) @@ -4701,6 +4702,10 @@ namespace codsrc // Decomp Status: Tested, Completed void Scr_InitSystem(game::scriptInstance_t inst) { + // our additions + scheduler::exec_pre_scr_init_funcs(inst); + // + assert(!game::gScrVarPub[inst].timeArrayId); //assert(!game::gScrVarPub[inst].ext_threadcount); @@ -4726,6 +4731,10 @@ namespace codsrc assert(!game::gScrVarPub[inst].freeEntList); game::g_script_error_level[inst] = -1; + + // our additions + scheduler::exec_post_scr_init_funcs(inst); + // } //Restored function diff --git a/src/component/scheduler.cpp b/src/component/scheduler.cpp index 9a574e6..27270fc 100644 --- a/src/component/scheduler.cpp +++ b/src/component/scheduler.cpp @@ -124,6 +124,45 @@ namespace scheduler com_init_hook.invoke(); on_post_init_hook(); } + + std::vector> pre_scr_init_funcs; + std::vector> post_scr_init_funcs; + + utils::hook::detour pre_scr_init_system_hook; + utils::hook::detour post_scr_init_system_hook; + + void* pre_scr_init_system_original; + void* post_scr_init_system_original; + + NAKED void pre_scr_init_system_stub() + { + __asm + { + pushad; + push eax; + call exec_pre_scr_init_funcs; + add esp, 4; + popad; + + push pre_scr_init_system_original; + ret; + } + } + + NAKED void post_scr_init_system_stub() + { + __asm + { + pushad; + push eax; + call exec_post_scr_init_funcs; + add esp, 4; + popad; + + push post_scr_init_system_original; + ret; + } + } } void schedule(const std::function& callback, const pipeline type, @@ -171,6 +210,32 @@ namespace scheduler } } + void on_pre_scr_init_system(const std::function& callback) + { + pre_scr_init_funcs.push_back(callback); + } + + void on_post_scr_init_system(const std::function& callback) + { + post_scr_init_funcs.push_back(callback); + } + + void exec_pre_scr_init_funcs(game::scriptInstance_t inst) + { + for (const auto& func : pre_scr_init_funcs) + { + func(inst); + } + } + + void exec_post_scr_init_funcs(game::scriptInstance_t inst) + { + for (const auto& func : post_scr_init_funcs) + { + func(inst); + } + } + class component final : public component_interface { public: @@ -188,6 +253,12 @@ namespace scheduler com_init_hook.create(SELECT(0x0, 0x59D710), com_init_stub); utils::hook::call(SELECT(0x0, 0x503B5D), execute_server); utils::hook::call(SELECT(0x0, 0x59DCFD), execute_main); + + // for when we dont use decomp + pre_scr_init_system_hook.create(0x699865, pre_scr_init_system_stub); + pre_scr_init_system_original = pre_scr_init_system_hook.get_original(); + post_scr_init_system_hook.create(0x699924, post_scr_init_system_stub); + post_scr_init_system_original = post_scr_init_system_hook.get_original(); } }; } diff --git a/src/component/scheduler.hpp b/src/component/scheduler.hpp index 4649c14..7dd5f41 100644 --- a/src/component/scheduler.hpp +++ b/src/component/scheduler.hpp @@ -21,4 +21,9 @@ namespace scheduler std::chrono::milliseconds delay = 0ms); void on_init(const std::function& callback); + + void on_pre_scr_init_system(const std::function& callback); + void on_post_scr_init_system(const std::function& callback); + void exec_pre_scr_init_funcs(game::scriptInstance_t inst); + void exec_post_scr_init_funcs(game::scriptInstance_t inst); }