From 32165afe41c4cf4d3b261ee7a9c69c369253cc13 Mon Sep 17 00:00:00 2001 From: BobTheBob <32057864+BobTheBob9@users.noreply.github.com> Date: Thu, 23 Feb 2023 00:19:48 +0000 Subject: Add `FUNCTION_AT` and `VAR_AT` macros (#416) * add FUNCTION_AT and VAR_AT macros * fix build issues oops * fix formatting * add PREDEFINED_VAR_AT * change func name * fixup formatting * update to use DEFINED_VAR_AT --- NorthstarDLL/core/convar/concommand.cpp | 2 - NorthstarDLL/core/hooks.cpp | 39 +++++++++++++++ NorthstarDLL/core/hooks.h | 86 ++++++++++++++++++++------------- 3 files changed, 92 insertions(+), 35 deletions(-) (limited to 'NorthstarDLL/core') diff --git a/NorthstarDLL/core/convar/concommand.cpp b/NorthstarDLL/core/convar/concommand.cpp index 88bd8825..67c867f8 100644 --- a/NorthstarDLL/core/convar/concommand.cpp +++ b/NorthstarDLL/core/convar/concommand.cpp @@ -4,8 +4,6 @@ #include -bool (*CCommand__Tokenize)(CCommand& self, const char* pCommandString, R2::cmd_source_t commandSource); - //----------------------------------------------------------------------------- // Purpose: Returns true if this is a command // Output : bool diff --git a/NorthstarDLL/core/hooks.cpp b/NorthstarDLL/core/hooks.cpp index 34b48d1d..7c70b0a5 100644 --- a/NorthstarDLL/core/hooks.cpp +++ b/NorthstarDLL/core/hooks.cpp @@ -65,6 +65,9 @@ __dllLoadCallback::__dllLoadCallback( void __fileAutohook::Dispatch() { + for (__autovar* var : vars) + var->Dispatch(); + for (__autohook* hook : hooks) hook->Dispatch(); } @@ -114,6 +117,42 @@ bool ManualHook::Dispatch(LPVOID addr, LPVOID* orig) return false; } +uintptr_t ParseDLLOffsetString(const char* pAddrString) +{ + // in the format server.dll + 0xDEADBEEF + int iDllNameEnd = 0; + for (; !isspace(pAddrString[iDllNameEnd]) && pAddrString[iDllNameEnd] != '+'; iDllNameEnd++) + ; + + char* pModuleName = new char[iDllNameEnd + 1]; + memcpy(pModuleName, pAddrString, iDllNameEnd); + pModuleName[iDllNameEnd] = '\0'; + + // get the module address + const HMODULE pModuleAddr = GetModuleHandleA(pModuleName); + + if (!pModuleAddr) + return 0; + + // get the offset string + uintptr_t iOffset = 0; + + int iOffsetBegin = iDllNameEnd; + int iOffsetEnd = strlen(pAddrString); + + // seek until we hit the start of the number offset + for (; !(pAddrString[iOffsetBegin] >= '0' && pAddrString[iOffsetBegin] <= '9') && pAddrString[iOffsetBegin]; iOffsetBegin++) + ; + + bool bIsHex = pAddrString[iOffsetBegin] == '0' && (pAddrString[iOffsetBegin + 1] == 'X' || pAddrString[iOffsetBegin + 1] == 'x'); + if (bIsHex) + iOffset = std::stoi(pAddrString + iOffsetBegin + 2, 0, 16); + else + iOffset = std::stoi(pAddrString + iOffsetBegin); + + return ((uintptr_t)pModuleAddr + iOffset); +} + // dll load callback stuff // this allows for code to register callbacks to be run as soon as a dll is loaded, mainly to allow for patches to be made on dll load struct DllLoadCallback diff --git a/NorthstarDLL/core/hooks.h b/NorthstarDLL/core/hooks.h index f47791fb..0144712f 100644 --- a/NorthstarDLL/core/hooks.h +++ b/NorthstarDLL/core/hooks.h @@ -63,16 +63,20 @@ class __dllLoadCallback // new macro hook stuff class __autohook; +class __autovar; class __fileAutohook { public: std::vector<__autohook*> hooks; + std::vector<__autovar*> vars; void Dispatch(); void DispatchForModule(const char* pModuleName); }; +uintptr_t ParseDLLOffsetString(const char* pAddrString); + // initialise autohooks for this file #define AUTOHOOK_INIT() \ namespace \ @@ -187,39 +191,7 @@ class __autohook case OFFSET_STRING: { - // in the format server.dll + 0xDEADBEEF - int iDllNameEnd = 0; - for (; !isspace(pAddrString[iDllNameEnd]) && pAddrString[iDllNameEnd] != '+'; iDllNameEnd++) - ; - - char* pModuleName = new char[iDllNameEnd + 1]; - memcpy(pModuleName, pAddrString, iDllNameEnd); - pModuleName[iDllNameEnd] = '\0'; - - // get the module address - const HMODULE pModuleAddr = GetModuleHandleA(pModuleName); - - if (!pModuleAddr) - break; - - // get the offset string - uintptr_t iOffset = 0; - - int iOffsetBegin = iDllNameEnd; - int iOffsetEnd = strlen(pAddrString); - - // seek until we hit the start of the number offset - for (; !(pAddrString[iOffsetBegin] >= '0' && pAddrString[iOffsetBegin] <= '9') && pAddrString[iOffsetBegin]; iOffsetBegin++) - ; - - bool bIsHex = - pAddrString[iOffsetBegin] == '0' && (pAddrString[iOffsetBegin + 1] == 'X' || pAddrString[iOffsetBegin + 1] == 'x'); - if (bIsHex) - iOffset = std::stoi(pAddrString + iOffsetBegin + 2, 0, 16); - else - iOffset = std::stoi(pAddrString + iOffsetBegin); - - targetAddr = (LPVOID)((uintptr_t)pModuleAddr + iOffset); + targetAddr = (LPVOID)ParseDLLOffsetString(pAddrString); break; } @@ -309,3 +281,51 @@ class ManualHook void MakeHook(LPVOID pTarget, LPVOID pDetour, void* ppOriginal, const char* pFuncName = ""); #define MAKEHOOK(pTarget, pDetour, ppOriginal) MakeHook(pTarget, pDetour, ppOriginal, __STR(pDetour)) + +class __autovar +{ + public: + char* m_pAddrString; + void** m_pTarget; + + public: + __autovar(__fileAutohook* pAutohook, const char* pAddrString, void** pTarget) + { + m_pTarget = pTarget; + + const int iAddrStrlen = strlen(pAddrString) + 1; + m_pAddrString = new char[iAddrStrlen]; + memcpy(m_pAddrString, pAddrString, iAddrStrlen); + + pAutohook->vars.push_back(this); + } + + void Dispatch() + { + *m_pTarget = (void*)ParseDLLOffsetString(m_pAddrString); + } +}; + +// VAR_AT(engine.dll+0x404, ConVar*, Cvar_host_timescale) +#define VAR_AT(addrString, type, name) \ + type name; \ + namespace \ + { \ + __autovar CONCAT2(__autovar, __LINE__)(&__FILEAUTOHOOK, __STR(addrString), (void**)&name); \ + } + +// FUNCTION_AT(engine.dll + 0xDEADBEEF, void, __fastcall, SomeFunc, (void* a1)) +#define FUNCTION_AT(addrString, type, callingConvention, name, args) \ + type(*callingConvention name) args; \ + namespace \ + { \ + __autovar CONCAT2(__autovar, __LINE__)(&__FILEAUTOHOOK, __STR(addrString), (void**)&name); \ + } + +// int* g_pSomeInt; +// DEFINED_VAR_AT(engine.dll + 0x5005, g_pSomeInt) +#define DEFINED_VAR_AT(addrString, name) \ + namespace \ + { \ + __autovar CONCAT2(__autovar, __LINE__)(&__FILEAUTOHOOK, __STR(addrString), (void**)&name); \ + } -- cgit v1.2.3