From 377f244844cb0d5d6dbfb24e3016d2664c26b744 Mon Sep 17 00:00:00 2001 From: pongo1231 Date: Tue, 22 Aug 2023 17:00:05 +0000 Subject: [PATCH] ChaosMod: Run hooks in seperate thread Speeds up mod startup quite a bit --- ChaosMod/Memory/Memory.cpp | 108 ++++++++++++++++++++----------------- 1 file changed, 60 insertions(+), 48 deletions(-) diff --git a/ChaosMod/Memory/Memory.cpp b/ChaosMod/Memory/Memory.cpp index da03732b1..44935ed87 100644 --- a/ChaosMod/Memory/Memory.cpp +++ b/ChaosMod/Memory/Memory.cpp @@ -85,30 +85,36 @@ namespace Memory } LOG("Running hooks"); - for (auto registeredHook = g_pRegisteredHooks; registeredHook; registeredHook = registeredHook->GetNext()) - { - if (registeredHook->IsLateHook()) - { - continue; - } - - const auto &hookName = registeredHook->GetName(); - - if (ms_BlacklistedHookNames.contains(hookName)) - { - LOG(hookName << " hook has been blacklisted from running!"); - continue; - } - - LOG("Running " << hookName << " hook"); - - if (!registeredHook->RunHook()) - { - LOG(hookName << " hook failed!"); - } - } - - MH_EnableHook(MH_ALL_HOOKS); + std::thread( + []() + { + for (auto registeredHook = g_pRegisteredHooks; registeredHook; + registeredHook = registeredHook->GetNext()) + { + if (registeredHook->IsLateHook()) + { + continue; + } + + const auto &hookName = registeredHook->GetName(); + + if (ms_BlacklistedHookNames.contains(hookName)) + { + LOG(hookName << " hook has been blacklisted from running!"); + continue; + } + + LOG("Running " << hookName << " hook"); + + if (!registeredHook->RunHook()) + { + LOG(hookName << " hook failed!"); + } + } + + MH_EnableHook(MH_ALL_HOOKS); + }) + .detach(); } void Uninit() @@ -137,30 +143,36 @@ namespace Memory { LOG("Running late hooks"); - for (auto registeredHook = g_pRegisteredHooks; registeredHook; registeredHook = registeredHook->GetNext()) - { - if (!registeredHook->IsLateHook()) - { - continue; - } - - const auto &hookName = registeredHook->GetName(); - - if (ms_BlacklistedHookNames.contains(hookName)) - { - LOG(hookName << " hook has been blacklisted from running!"); - continue; - } - - LOG("Running " << hookName << " hook"); - - if (!registeredHook->RunHook()) - { - LOG(hookName << " hook failed!"); - } - } - - MH_EnableHook(MH_ALL_HOOKS); + std::thread( + []() + { + for (auto registeredHook = g_pRegisteredHooks; registeredHook; + registeredHook = registeredHook->GetNext()) + { + if (!registeredHook->IsLateHook()) + { + continue; + } + + const auto &hookName = registeredHook->GetName(); + + if (ms_BlacklistedHookNames.contains(hookName)) + { + LOG(hookName << " hook has been blacklisted from running!"); + continue; + } + + LOG("Running " << hookName << " hook"); + + if (!registeredHook->RunHook()) + { + LOG(hookName << " hook failed!"); + } + } + + MH_EnableHook(MH_ALL_HOOKS); + }) + .detach(); } Handle FindPattern(const std::string &pattern, const PatternScanRange &&scanRange)