Skip to content

Commit

Permalink
ChaosMod: Run hooks in seperate thread
Browse files Browse the repository at this point in the history
Speeds up mod startup quite a bit
  • Loading branch information
pongo1231 committed Aug 22, 2023
1 parent 6985396 commit 377f244
Showing 1 changed file with 60 additions and 48 deletions.
108 changes: 60 additions & 48 deletions ChaosMod/Memory/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 377f244

Please sign in to comment.