From a823b885e417494c6630f6fcb18c94143a6f53a8 Mon Sep 17 00:00:00 2001 From: Werseter Date: Wed, 1 Nov 2023 18:27:28 +0100 Subject: [PATCH] Move address management code to addr_utils --- hook_lib/Main.cpp | 129 +----------------------------- hook_lib/Main.hpp | 19 ----- hook_lib/addr_utils.cpp | 101 +++++++++++++++++++++++ hook_lib/addr_utils.hpp | 16 ++++ hook_lib/cmd.cpp | 1 + hook_lib/devgui.cpp | 1 + hook_lib/functions.cpp | 3 +- hook_lib/functions.hpp | 7 -- hook_lib/g_cmds.cpp | 11 +-- hook_lib/hook_lib.vcxproj | 3 + hook_lib/hook_lib.vcxproj.filters | 6 ++ hook_lib/input.cpp | 1 + hook_lib/inventory.cpp | 1 + hook_lib/omnvars.cpp | 1 + hook_lib/patch.cpp | 1 + hook_lib/screen.cpp | 1 + hook_lib/script.cpp | 1 + hook_lib/structs.cpp | 1 + hook_lib/sv_main.cpp | 1 + hook_lib/transients.cpp | 1 + hook_lib/weapons.cpp | 1 + hook_lib/zones.cpp | 1 + 22 files changed, 150 insertions(+), 158 deletions(-) create mode 100755 hook_lib/addr_utils.cpp create mode 100644 hook_lib/addr_utils.hpp diff --git a/hook_lib/Main.cpp b/hook_lib/Main.cpp index b7bfd87..91b4c5d 100644 --- a/hook_lib/Main.cpp +++ b/hook_lib/Main.cpp @@ -1,6 +1,8 @@ #include "Main.hpp" #include "game_inc.h" +#include "addr_utils.hpp" + void entry_point() { XUID xuid; @@ -34,7 +36,7 @@ char buffer[0x5000]; BOOL WINAPI DllMain(HMODULE hModule, DWORD Reason, LPVOID lpVoid) { - g_Addrs.ModuleBase = (uintptr_t)(GetModuleHandle(0)); + initAddrUtils(); utils::hook::set(0x1403061A0_g, 0xC3); // Mystery function 1 if (Reason == DLL_PROCESS_ATTACH) { AllocConsole(); @@ -50,7 +52,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD Reason, LPVOID lpVoid) va = (const char* (*)(const char*, ...))0x1413F3010_g; //j_va - printf("Base Address: %p\n", base); + printf("Base Address: %p\n", 0_b); addCustomDvars(); addCustomCmds(); @@ -66,127 +68,4 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD Reason, LPVOID lpVoid) return TRUE; } -uintptr_t find_pattern(const char* module_name, const char* pattern) { - const auto get_module_size = [=](uintptr_t module_base) - { - return reinterpret_cast(module_base + reinterpret_cast(module_base)->e_lfanew)->OptionalHeader.SizeOfImage; - }; - const auto module_start = (uintptr_t)GetModuleHandle(module_name); - if (module_start != 0ULL) - { - const auto module_end = module_start + get_module_size(module_start); - - const char* pattern_current = pattern; - uintptr_t current_match = NULL; - - MEMORY_BASIC_INFORMATION64 page_information = {}; - for (auto current_page = reinterpret_cast(module_start); current_page < reinterpret_cast(module_end); current_page = reinterpret_cast(page_information.BaseAddress + page_information.RegionSize)) - { - VirtualQuery(reinterpret_cast(current_page), reinterpret_cast(&page_information), sizeof(MEMORY_BASIC_INFORMATION)); - if (page_information.Protect == PAGE_NOACCESS) - continue; - - if (page_information.State != MEM_COMMIT) - continue; - - if (page_information.Protect & PAGE_GUARD) - continue; - - for (auto current_address = reinterpret_cast(page_information.BaseAddress); current_address < reinterpret_cast(page_information.BaseAddress + page_information.RegionSize - 0x8); current_address++) - { - if (*current_address != GET_BYTE(pattern_current) && *pattern_current != '\?') { - current_match = 0ULL; - pattern_current = pattern; - continue; - } - - if (!current_match) - current_match = reinterpret_cast(current_address); - - pattern_current += 3; - if (pattern_current[-1] == NULL) - return current_match; - } - } - } - - return 0ULL; -} - -uintptr_t find_pattern(uintptr_t start, const char* module_name, const char* pattern) { - const auto get_module_size = [=](uintptr_t module_base) - { - return reinterpret_cast(module_base + reinterpret_cast(module_base)->e_lfanew)->OptionalHeader.SizeOfImage; - }; - const auto module_start = start; - if (module_start != 0ULL) - { - const auto module_end = module_start + get_module_size(module_start); - - const char* pattern_current = pattern; - uintptr_t current_match = NULL; - - MEMORY_BASIC_INFORMATION64 page_information = {}; - for (auto current_page = reinterpret_cast(module_start); current_page < reinterpret_cast(module_end); current_page = reinterpret_cast(page_information.BaseAddress + page_information.RegionSize)) - { - VirtualQuery(reinterpret_cast(current_page), reinterpret_cast(&page_information), sizeof(MEMORY_BASIC_INFORMATION)); - if (page_information.Protect == PAGE_NOACCESS) - continue; - - if (page_information.State != MEM_COMMIT) - continue; - - if (page_information.Protect & PAGE_GUARD) - continue; - - for (auto current_address = reinterpret_cast(page_information.BaseAddress); current_address < reinterpret_cast(page_information.BaseAddress + page_information.RegionSize - 0x8); current_address++) - { - if (*current_address != GET_BYTE(pattern_current) && *pattern_current != '\?') { - current_match = 0ULL; - pattern_current = pattern; - continue; - } - - if (!current_match) - current_match = reinterpret_cast(current_address); - - pattern_current += 3; - if (pattern_current[-1] == NULL) - return current_match; - } - } - } - - return 0ULL; -} menu_variables vars; - -size_t operator"" _b(const size_t val) -{ - return base + val; -} - -size_t reverse_b(const size_t val) -{ - return val - base; -} - -size_t reverse_b(const void* val) -{ - return reverse_b(reinterpret_cast(val)); -} - -size_t operator"" _g(const size_t val) -{ - return base + (val - 0x140000000); -} - -size_t reverse_g(const size_t val) -{ - return (val - base) + 0x140000000; -} - -size_t reverse_g(const void* val) -{ - return reverse_g(reinterpret_cast(val)); -} \ No newline at end of file diff --git a/hook_lib/Main.hpp b/hook_lib/Main.hpp index 2277c7d..daa026c 100644 --- a/hook_lib/Main.hpp +++ b/hook_lib/Main.hpp @@ -22,18 +22,10 @@ #include "ini.h" #include "json.hpp" -#define base g_Addrs.ModuleBase - #pragma warning(disable:4996) #pragma comment(lib, "Gdi32.lib") - - -#define INRANGE(x, a, b) (x >= a && x <= b) -#define GET_BITS( x ) (INRANGE((x&(~0x20)),'A','F') ? ((x&(~0x20)) - 'A' + 0xa) : (INRANGE(x,'0','9') ? x - '0' : 0)) -#define GET_BYTE( x ) (GET_BITS(x[0]) << 4 | GET_BITS(x[1])) - struct menu_variables { bool bInitiateMenu; bool bMenuOpen; @@ -41,17 +33,6 @@ struct menu_variables { }; extern menu_variables vars; -uintptr_t find_pattern(const char* module_name, const char* pattern); -uintptr_t find_pattern(uintptr_t start, const char* module_name, const char* pattern); - -size_t operator"" _b(size_t val); -size_t reverse_b(size_t val); -size_t reverse_b(const void* val); - -size_t operator"" _g(size_t val); -size_t reverse_g(size_t val); -size_t reverse_g(const void* val); - struct DvarPair { const char* m_key; diff --git a/hook_lib/addr_utils.cpp b/hook_lib/addr_utils.cpp new file mode 100755 index 0000000..c8d82c1 --- /dev/null +++ b/hook_lib/addr_utils.cpp @@ -0,0 +1,101 @@ +#include "addr_utils.hpp" + +#include +#include +#include + +struct Addresses { + uintptr_t ModuleBase; + uintptr_t jmp_rbx; +}; + +Addresses g_Addrs{}; + +void initAddrUtils() { + g_Addrs.ModuleBase = (uintptr_t)(GetModuleHandle(0)); +} + +size_t _b(const size_t val) { return g_Addrs.ModuleBase + val; } + +size_t operator"" _b(const size_t val) { return _b(val); } + +size_t reverse_b(const size_t val) { return val - g_Addrs.ModuleBase; } + +size_t reverse_b(const void* val) { return reverse_b(reinterpret_cast(val)); } + +size_t _g(const size_t val) { return g_Addrs.ModuleBase + (val - 0x140000000); } + +size_t operator"" _g(const size_t val) { return _g(val); } + +size_t reverse_g(const size_t val) { return (val - g_Addrs.ModuleBase) + 0x140000000; } + +size_t reverse_g(const void* val) { return reverse_g(reinterpret_cast(val)); } + +namespace { + DWORD get_module_size(uintptr_t module_base) { + return reinterpret_cast( + module_base + + reinterpret_cast(module_base)->e_lfanew) + ->OptionalHeader.SizeOfImage; + }; + inline byte hex_nibble_to_byte(const char* hexNibble) { + if (not (std::isxdigit(hexNibble[0]) and std::isxdigit(hexNibble[1]))) { + return 0; + } + return std::stoi(std::string(hexNibble, hexNibble + 1), nullptr, 16); + } +} + +uintptr_t findPattern(const char* pattern, const char* module_name) { + return findPattern(pattern, (uintptr_t)GetModuleHandle(module_name)); +} + +uintptr_t findPattern(const char* pattern, uintptr_t module_start) { + if (module_start == 0ULL) { + return 0ULL; + } + + const auto module_end = module_start + get_module_size(module_start); + + const char* pattern_current{ pattern }; + uintptr_t current_match{ 0ULL }; + + MEMORY_BASIC_INFORMATION64 page_information{}; + for (auto current_page = reinterpret_cast(module_start); + current_page < reinterpret_cast(module_end); + current_page = reinterpret_cast(page_information.BaseAddress + page_information.RegionSize)) { + if (VirtualQuery(reinterpret_cast(current_page), + reinterpret_cast(&page_information), + sizeof(MEMORY_BASIC_INFORMATION)) == 0) { + break; + } + + if (page_information.Protect == PAGE_NOACCESS or page_information.State != MEM_COMMIT or + (page_information.Protect & PAGE_GUARD)) { + continue; + } + + for (auto current_address = reinterpret_cast(page_information.BaseAddress); + current_address < reinterpret_cast(page_information.BaseAddress + page_information.RegionSize - 0x8); + current_address++) { + if (*pattern_current != '\?' and *current_address != hex_nibble_to_byte(pattern_current)) { + current_match = 0ULL; + pattern_current = pattern; + continue; + } + + if (not current_match) + { + current_match = reinterpret_cast(current_address); + } + + pattern_current += 3; + if (pattern_current[-1] == NULL) + { + return current_match; + } + } + } + + return 0ULL; +} diff --git a/hook_lib/addr_utils.hpp b/hook_lib/addr_utils.hpp new file mode 100644 index 0000000..dc44e6b --- /dev/null +++ b/hook_lib/addr_utils.hpp @@ -0,0 +1,16 @@ +#include + +void initAddrUtils(); + +size_t _b(size_t val); +size_t operator"" _b(size_t val); +size_t reverse_b(size_t val); +size_t reverse_b(const void* val); + +size_t _g(size_t val); +size_t operator"" _g(size_t val); +size_t reverse_g(size_t val); +size_t reverse_g(const void* val); + +uintptr_t findPattern(const char* pattern, const char* module_name); +uintptr_t findPattern(const char* pattern, uintptr_t module_start); diff --git a/hook_lib/cmd.cpp b/hook_lib/cmd.cpp index 38dcde2..da7a96d 100644 --- a/hook_lib/cmd.cpp +++ b/hook_lib/cmd.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "cmd.h" void Cmd_Exec_Internal(bool isSuperUser) diff --git a/hook_lib/devgui.cpp b/hook_lib/devgui.cpp index 281ff50..c1b7d2a 100644 --- a/hook_lib/devgui.cpp +++ b/hook_lib/devgui.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "devgui.h" void CL_CreateDevGui_Detour(int fsMenuEntries, const char* modeCfg) diff --git a/hook_lib/functions.cpp b/hook_lib/functions.cpp index 560d59b..21689ef 100644 --- a/hook_lib/functions.cpp +++ b/hook_lib/functions.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "functions.hpp" void* RtlAddVectoredExceptionHandler(LONG First, PVECTORED_EXCEPTION_HANDLER Handler) { @@ -542,5 +543,3 @@ cmd_function_s dump_weapdefs_f_VAR; cmd_function_s load_weapdef_f_VAR; CmdArgs* cmd_args; - -Addresses g_Addrs; \ No newline at end of file diff --git a/hook_lib/functions.hpp b/hook_lib/functions.hpp index 23c9242..9a096af 100644 --- a/hook_lib/functions.hpp +++ b/hook_lib/functions.hpp @@ -5,11 +5,6 @@ struct gentity_s; struct CmdArgs; -struct Addresses { - uintptr_t ModuleBase; - uintptr_t jmp_rbx; -}; - union DvarValue { bool enabled; @@ -258,5 +253,3 @@ int G_Main_GetTime(); const char* _va(const char* format, ...); #pragma endregion - -extern Addresses g_Addrs; \ No newline at end of file diff --git a/hook_lib/g_cmds.cpp b/hook_lib/g_cmds.cpp index 1e760c3..0bdce1c 100644 --- a/hook_lib/g_cmds.cpp +++ b/hook_lib/g_cmds.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "g_cmds.h" #include "game_inc.h" @@ -216,7 +217,7 @@ void set_byte_f() if (Cmd_Argc() == 3) { Cmd_ArgvBuffer(1, command, 500); - uintptr_t address = atoll(command) + base; + uintptr_t address = _b(atoll(command)); Cmd_ArgvBuffer(2, command, 500); utils::hook::set(address, atoi(command)); } @@ -228,7 +229,7 @@ void set_short_f() if (Cmd_Argc() == 3) { Cmd_ArgvBuffer(1, command, 500); - uintptr_t address = atoll(command) + base; + uintptr_t address = _b(atoll(command)); Cmd_ArgvBuffer(2, command, 500); utils::hook::set(address, atol(command)); } @@ -240,7 +241,7 @@ void set_int_f() if (Cmd_Argc() == 3) { Cmd_ArgvBuffer(1, command, 500); - uintptr_t address = atoll(command) + base; + uintptr_t address = _b(atoll(command)); Cmd_ArgvBuffer(2, command, 500); utils::hook::set(address, _atoi64(command)); } @@ -252,7 +253,7 @@ void set_float_f() if (Cmd_Argc() == 3) { Cmd_ArgvBuffer(1, command, 500); - uintptr_t address = atoll(command) + base; + uintptr_t address = _b(atoll(command)); Cmd_ArgvBuffer(2, command, 500); utils::hook::set(address, strToFloat(command)); } @@ -264,7 +265,7 @@ void set_pointer_f() if (Cmd_Argc() == 3) { Cmd_ArgvBuffer(1, command, 500); - uintptr_t address = atoll(command) + base; + uintptr_t address = _b(atoll(command)); Cmd_ArgvBuffer(2, command, 500); utils::hook::set(address, _atoi64(command)); } diff --git a/hook_lib/hook_lib.vcxproj b/hook_lib/hook_lib.vcxproj index d528ef9..a67e5d8 100644 --- a/hook_lib/hook_lib.vcxproj +++ b/hook_lib/hook_lib.vcxproj @@ -82,6 +82,7 @@ true discord_game_sdk $(SolutionDir)\lib;$(LibraryPath) + $(SolutionDir)\hook_lib;$(IncludePath) false @@ -149,6 +150,7 @@ + @@ -189,6 +191,7 @@ + diff --git a/hook_lib/hook_lib.vcxproj.filters b/hook_lib/hook_lib.vcxproj.filters index 30aaeb0..8db2e37 100644 --- a/hook_lib/hook_lib.vcxproj.filters +++ b/hook_lib/hook_lib.vcxproj.filters @@ -67,6 +67,9 @@ hook_lib\game + + hook_lib\game + hook_lib\game @@ -192,6 +195,9 @@ hook_lib\game + + hook_lib\game + hook_lib\game diff --git a/hook_lib/input.cpp b/hook_lib/input.cpp index c4989dc..f36fd5d 100644 --- a/hook_lib/input.cpp +++ b/hook_lib/input.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "input.h" void CL_Keys_Event_Detour(int localClientNum, int key, bool down, unsigned int time, int virtualKey, int controllerIndex) diff --git a/hook_lib/inventory.cpp b/hook_lib/inventory.cpp index bd65575..8fe4327 100644 --- a/hook_lib/inventory.cpp +++ b/hook_lib/inventory.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "inventory.h" #include "game_inc.h" diff --git a/hook_lib/omnvars.cpp b/hook_lib/omnvars.cpp index 3bedf72..c9b27f7 100644 --- a/hook_lib/omnvars.cpp +++ b/hook_lib/omnvars.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "omnvars.h" int BG_Omnvar_GetType(OmnvarDef* ovDef) diff --git a/hook_lib/patch.cpp b/hook_lib/patch.cpp index ea6cc96..33d0cc6 100644 --- a/hook_lib/patch.cpp +++ b/hook_lib/patch.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "patch.h" #include "game_inc.h" diff --git a/hook_lib/screen.cpp b/hook_lib/screen.cpp index 37ed3f2..1ac26d2 100644 --- a/hook_lib/screen.cpp +++ b/hook_lib/screen.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "screen.h" void CG_DrawWaterMark() diff --git a/hook_lib/script.cpp b/hook_lib/script.cpp index fbe7923..afd649c 100644 --- a/hook_lib/script.cpp +++ b/hook_lib/script.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "script.h" #include "game_inc.h" diff --git a/hook_lib/structs.cpp b/hook_lib/structs.cpp index a268558..b8a02ab 100644 --- a/hook_lib/structs.cpp +++ b/hook_lib/structs.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "structs.h" diff --git a/hook_lib/sv_main.cpp b/hook_lib/sv_main.cpp index 6f4c962..1e8738d 100644 --- a/hook_lib/sv_main.cpp +++ b/hook_lib/sv_main.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "sv_main.h" #include "game_inc.h" diff --git a/hook_lib/transients.cpp b/hook_lib/transients.cpp index 8de18bc..a438426 100644 --- a/hook_lib/transients.cpp +++ b/hook_lib/transients.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "transients.h" #include "game_inc.h" diff --git a/hook_lib/weapons.cpp b/hook_lib/weapons.cpp index eaec60b..7e635e0 100644 --- a/hook_lib/weapons.cpp +++ b/hook_lib/weapons.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "weapons.h" #include "game_inc.h" diff --git a/hook_lib/zones.cpp b/hook_lib/zones.cpp index 54fc281..e5568fe 100644 --- a/hook_lib/zones.cpp +++ b/hook_lib/zones.cpp @@ -1,3 +1,4 @@ +#include "addr_utils.hpp" #include "zones.h" #include "game_inc.h"