Skip to content

Commit

Permalink
ChaosMod: Do pattern scanning asynchronously in effect threads
Browse files Browse the repository at this point in the history
Rather than scanning for patterns synchronously in effect threads we do this in a separate thread and put the current thread on pause whilst this is running, allowing the game & rest of the mod to run undisturbed
  • Loading branch information
pongo1231 committed Aug 24, 2023
1 parent d3ef55b commit 16266cf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
5 changes: 5 additions & 0 deletions ChaosMod/Effects/EffectThreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,9 @@ namespace EffectThreads

return m_Threads.at(threadId)->HasOnStartExecuted();
}

bool IsThreadAnEffectThread()
{
return m_Threads.contains(GetCurrentFiber());
}
}
4 changes: 3 additions & 1 deletion ChaosMod/Effects/EffectThreads.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ namespace EffectThreads

bool DoesThreadExist(LPVOID threadId);
bool HasThreadOnStartExecuted(LPVOID threadId);

bool IsThreadAnEffectThread();
};

struct EffectThreadData
Expand Down Expand Up @@ -132,4 +134,4 @@ class EffectThread
{
return !m_IsRunning;
}
};
};
51 changes: 41 additions & 10 deletions ChaosMod/Memory/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

#include "Memory.h"

#include "Effects/EffectThreads.h"

#include "Memory/Hooks/Hook.h"

#include "Util/Script.h"
#include "Util/Text.h"

static DWORD64 ms_BaseAddr;
Expand Down Expand Up @@ -177,27 +180,55 @@ namespace Memory

Handle FindPattern(const std::string &pattern, const PatternScanRange &&scanRange)
{
DEBUG_LOG("Searching for pattern: " << pattern);

if ((scanRange.StartAddr != 0 || scanRange.EndAddr != 0) && scanRange.StartAddr >= scanRange.EndAddr)
{
LOG("startAddr is equal / bigger than endAddr???");
return Handle();
}

auto copy = pattern;
for (size_t pos = copy.find("??"); pos != std::string::npos; pos = copy.find("??", pos + 1))
auto scanPattern = [&]()
{
copy.replace(pos, 2, "?");
}
auto copy = pattern;
for (size_t pos = copy.find("??"); pos != std::string::npos; pos = copy.find("??", pos + 1))
{
copy.replace(pos, 2, "?");
}

auto thePattern = scanRange.StartAddr == 0 && scanRange.EndAddr == 0
? hook::pattern(copy)
: hook::pattern(scanRange.StartAddr, scanRange.EndAddr, copy);
if (!thePattern.size())
{
return Handle();
}

auto thePattern = scanRange.StartAddr == 0 && scanRange.EndAddr == 0
? hook::pattern(copy)
: hook::pattern(scanRange.StartAddr, scanRange.EndAddr, copy);
if (!thePattern.size())
return Handle(uintptr_t(thePattern.get_first()));
};

if (EffectThreads::IsThreadAnEffectThread())
{
return Handle();
Handle handle;
bool isDone = false;

std::thread thread(
[&]()
{
handle = scanPattern();
isDone = true;
});
thread.detach();

while (!isDone)
{
WAIT(0);
}

return handle;
}

return Handle(uintptr_t(thePattern.get_first()));
return scanPattern();
}

MH_STATUS AddHook(void *target, void *detour, void *orig)
Expand Down

0 comments on commit 16266cf

Please sign in to comment.