From 3c33d11d80feb2b64d2f8c396a2eff2b9efe97a4 Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Tue, 16 Mar 2021 23:49:07 -0600 Subject: [PATCH] fix function finding (restore to ithvnr) - should fix system40 and rugp2 hooks --- texthook/engine/engine.cc | 14 +++++--------- texthook/util/util.cc | 10 ++++++++++ texthook/util/util.h | 1 + 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/texthook/engine/engine.cc b/texthook/engine/engine.cc index c565b5d..baf6503 100644 --- a/texthook/engine/engine.cc +++ b/texthook/engine/engine.cc @@ -4494,7 +4494,8 @@ bool InsertRUGP1Hook() */ bool InsertRUGP2Hook() { - if (!Util::CheckFile(L"vm60.dll") /*|| !SafeFillRange(L"vm60.dll", &low, &high)*/) { + auto module = GetModuleHandleW(L"vm60.dll"); + if (!module /*|| !SafeFillRange(L"vm60.dll", &low, &high)*/) { ConsoleOutput("vnreng:rUGP2: vm60.dll does not exist"); return false; } @@ -4508,7 +4509,7 @@ bool InsertRUGP2Hook() 0x89,0x75, 0x0c // 1001e527 8975 0c mov dword ptr ss:[ebp+0xc],esi }; enum { addr_offset = 0x1001e51d - 0x1001e515 }; - ULONG addr = MemDbg::findBytes(bytes, sizeof(bytes), processStartAddress, processStopAddress); + ULONG addr = MemDbg::findBytes(bytes, sizeof(bytes), (DWORD)module, Util::QueryModuleLimits(module).second); //GROWL_DWORD(addr); if (!addr) { ConsoleOutput("vnreng:rUGP2: pattern not found"); @@ -4631,20 +4632,15 @@ static void InsertAliceHook2(DWORD addr) // jichi 5/13/2015: Looking for function entries in StoatSpriteEngine.dll bool InsertAliceHook() { - DWORD addr; - if (addr = (DWORD)GetProcAddress(GetModuleHandleW(L"SACT2.dll"), "SP_TextDraw")) { + if (auto addr = Util::FindFunction("SP_TextDraw")) { InsertAliceHook1(addr); return true; } - if (addr = (DWORD)GetProcAddress(GetModuleHandleW(L"SACTDX.dll"), "SP_TextDraw")) { - InsertAliceHook1(addr); - return true; - } //if (GetFunctionAddr("SP_SetTextSprite", &addr, &low, &high, 0) && addr) { // InsertAliceHook2(addr); // return true; //} - if (addr = (DWORD)GetProcAddress(GetModuleHandleW(L"StoatSpriteEngine.dll"), "SP_SetTextSprite")) { // Artikash 6/27/2018 not sure if this works + if (auto addr = Util::FindFunction("SP_SetTextSprite")) { // Artikash 6/27/2018 not sure if this works InsertAliceHook2(addr); return true; } diff --git a/texthook/util/util.cc b/texthook/util/util.cc index 03d5779..4c841a4 100644 --- a/texthook/util/util.cc +++ b/texthook/util/util.cc @@ -6,6 +6,7 @@ #include "util/util.h" #include "ithsys/ithsys.h" #include "main.h" +#include namespace { // unnamed @@ -345,6 +346,15 @@ std::vector SearchMemory(const void* bytes, short length, DWORD protec return ret; } + +uintptr_t FindFunction(const char* function) +{ + static HMODULE modules[300] = {}; + static auto _ = EnumProcessModules(GetCurrentProcess(), modules, sizeof(modules), DUMMY); + for (auto module : modules) if (auto addr = GetProcAddress(module, function)) return (uintptr_t)addr; + return 0; +} + } // EOF diff --git a/texthook/util/util.h b/texthook/util/util.h index 87d4275..9b4f59e 100644 --- a/texthook/util/util.h +++ b/texthook/util/util.h @@ -22,6 +22,7 @@ bool SearchResourceString(LPCWSTR str); std::pair QueryModuleLimits(HMODULE module); std::vector SearchMemory(const void* bytes, short length, DWORD protect = PAGE_EXECUTE, uintptr_t minAddr = 0, uintptr_t maxAddr = -1ULL); +uintptr_t FindFunction(const char* function); } // namespace Util