Skip to content

Commit

Permalink
LoadAndDecryptHwDLL hook for doing stuff in NGHL and WON.
Browse files Browse the repository at this point in the history
  • Loading branch information
YaLTeR committed Jan 26, 2015
1 parent e6ca255 commit 2fb289d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
24 changes: 24 additions & 0 deletions BunnymodXT/modules/HwDLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void HwDLL::Hook(const std::wstring& moduleName, void* moduleHandle, void* modul

if (needToIntercept)
MemUtils::Intercept(moduleName, {
{ reinterpret_cast<void**>(&ORIG_LoadAndDecryptHwDLL), reinterpret_cast<void*>(HOOKED_LoadAndDecryptHwDLL) },
{ reinterpret_cast<void**>(&ORIG_Cbuf_Execute), reinterpret_cast<void*>(HOOKED_Cbuf_Execute) },
{ reinterpret_cast<void**>(&ORIG_SeedRandomNumberGenerator), reinterpret_cast<void*>(HOOKED_SeedRandomNumberGenerator) },
{ reinterpret_cast<void**>(&ORIG_time), reinterpret_cast<void*>(HOOKED_time) },
Expand All @@ -42,6 +43,7 @@ void HwDLL::Unhook()
{
if (m_Intercepted)
MemUtils::RemoveInterception(m_Name, {
{ reinterpret_cast<void**>(&ORIG_LoadAndDecryptHwDLL), reinterpret_cast<void*>(HOOKED_LoadAndDecryptHwDLL) },
{ reinterpret_cast<void**>(&ORIG_Cbuf_Execute), reinterpret_cast<void*>(HOOKED_Cbuf_Execute) },
{ reinterpret_cast<void**>(&ORIG_SeedRandomNumberGenerator), reinterpret_cast<void*>(HOOKED_SeedRandomNumberGenerator) },
{ reinterpret_cast<void**>(&ORIG_time), reinterpret_cast<void*>(HOOKED_time) },
Expand All @@ -59,6 +61,7 @@ void HwDLL::Unhook()

void HwDLL::Clear()
{
ORIG_LoadAndDecryptHwDLL = nullptr;
ORIG_Cbuf_Execute = nullptr;
ORIG_SeedRandomNumberGenerator = nullptr;
ORIG_time = nullptr;
Expand Down Expand Up @@ -180,6 +183,12 @@ void HwDLL::FindStuff()
//DEF_FUTURE(RandomFloat)
//DEF_FUTURE(RandomLong)
DEF_FUTURE(Host_Changelevel2_f)
bool oldEngine = (m_Name.find(L"hl.exe") != std::wstring::npos);
std::future<MemUtils::ptnvec_size> fLoadAndDecryptHwDLL;
if (oldEngine) {
// In WON after the engine DLL has been loaded once for some reason there are multiple identical LoadAndDecrypt functions in the memory, we need the first one always.
fLoadAndDecryptHwDLL = std::async(MemUtils::FindFirstSequence, m_Base, m_Length, Patterns::ptnsLoadAndDecryptHwDLL, reinterpret_cast<void**>(&ORIG_LoadAndDecryptHwDLL));
}
#undef DEF_FUTURE

auto fCbuf_Execute = MemUtils::FindPatternOnly(reinterpret_cast<void**>(&ORIG_Cbuf_Execute), m_Base, m_Length, Patterns::ptnsCbuf_Execute,
Expand Down Expand Up @@ -324,6 +333,14 @@ void HwDLL::FindStuff()
ORIG_Cmd_AddMallocCommand = nullptr;
ORIG_Cbuf_Execute = nullptr;
}

if (oldEngine) {
n = fLoadAndDecryptHwDLL.get();
if (ORIG_LoadAndDecryptHwDLL)
EngineDevMsg("[hw dll] Found LoadAndDecryptHwDLL at %p (using the %s pattern).\n", ORIG_LoadAndDecryptHwDLL, Patterns::ptnsLoadAndDecryptHwDLL[n].build.c_str());
else
EngineDevWarning("[hw dll] Could not find LoadAndDecryptHwDLL.\n");
}
}

if (ORIG_Cbuf_Execute && !ORIG_time)
Expand Down Expand Up @@ -757,3 +774,10 @@ HOOK_DEF_0(HwDLL, void, __cdecl, Host_Changelevel2_f)

return ORIG_Host_Changelevel2_f();
}

HOOK_DEF_3(HwDLL, void, __cdecl, LoadAndDecryptHwDLL, int, a, void*, b, void*, c)
{
ORIG_LoadAndDecryptHwDLL(a, b, c);
EngineDevMsg("[hw dll] LoadAndDecryptHwDLL has been called. Rehooking.\n");
Hooks::HookModule(L"hl.exe");
}
1 change: 1 addition & 0 deletions BunnymodXT/modules/HwDLL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class HwDLL : public IHookableNameFilterOrdered
{
HOOK_DECL(void, __cdecl, LoadAndDecryptHwDLL, int a, void* b, void* c)
HOOK_DECL(void, __cdecl, Cbuf_Execute)
HOOK_DECL(void, __cdecl, SeedRandomNumberGenerator)
HOOK_DECL(time_t, __cdecl, time, time_t *Time)
Expand Down
19 changes: 19 additions & 0 deletions BunnymodXT/patterns.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
namespace Patterns
{
// Engine patterns.
const MemUtils::ptnvec ptnsLoadAndDecryptHwDLL =
{
{
"HL-NGHL",
{
0x8B, 0x0D, '?', '?', '?', '?', 0x53, 0x56, 0x33, 0xDB, 0x8B, 0x01, 0x57, 0x53, 0x68, '?', '?', '?', '?', 0xFF, 0x74, 0x24, 0x18, 0xFF, 0x50, 0x28, 0x8B, 0x0D, '?', '?', '?', '?', 0x8B, 0xF0, 0x6A, 0x02, 0x53, 0x8B, 0x01, 0x56, 0xFF, 0x50, 0x30, 0x8B, 0x0D
},
"xx????xxxxxxxxx????xxxxxxxxx????xxxxxxxxxxxxx"
},

{
"HL-WON",
{
0x8B, 0x44, 0x24, 0x04, 0x53, 0x56, 0x57, 0x68, '?', '?', '?', '?', 0x50, 0xE8, '?', '?', '?', '?', 0x8B, 0xF0, 0x6A, 0x02, 0x6A, 0x00, 0x56, 0xE8, '?', '?', '?', '?', 0x56, 0xE8, '?', '?', '?', '?', 0x56, 0x8B, 0xF8, 0xE8
},
"xxxxxxxx????xx????xxxxxxxx????xx????xxxx"
}
};

const MemUtils::ptnvec ptnsCbuf_Execute =
{
{
Expand Down
2 changes: 1 addition & 1 deletion SPTLib
Submodule SPTLib updated 2 files
+20 −0 MemUtils.cpp
+1 −0 MemUtils.hpp

0 comments on commit 2fb289d

Please sign in to comment.