Move address management code to addr_utils

This commit is contained in:
Werseter 2023-11-01 18:27:28 +01:00
parent f09eb47926
commit a823b885e4
22 changed files with 150 additions and 158 deletions

View File

@ -1,6 +1,8 @@
#include "Main.hpp" #include "Main.hpp"
#include "game_inc.h" #include "game_inc.h"
#include "addr_utils.hpp"
void entry_point() void entry_point()
{ {
XUID xuid; XUID xuid;
@ -34,7 +36,7 @@ char buffer[0x5000];
BOOL WINAPI DllMain(HMODULE hModule, DWORD Reason, LPVOID lpVoid) BOOL WINAPI DllMain(HMODULE hModule, DWORD Reason, LPVOID lpVoid)
{ {
g_Addrs.ModuleBase = (uintptr_t)(GetModuleHandle(0)); initAddrUtils();
utils::hook::set<char>(0x1403061A0_g, 0xC3); // Mystery function 1 utils::hook::set<char>(0x1403061A0_g, 0xC3); // Mystery function 1
if (Reason == DLL_PROCESS_ATTACH) { if (Reason == DLL_PROCESS_ATTACH) {
AllocConsole(); AllocConsole();
@ -50,7 +52,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD Reason, LPVOID lpVoid)
va = (const char* (*)(const char*, ...))0x1413F3010_g; //j_va va = (const char* (*)(const char*, ...))0x1413F3010_g; //j_va
printf("Base Address: %p\n", base); printf("Base Address: %p\n", 0_b);
addCustomDvars(); addCustomDvars();
addCustomCmds(); addCustomCmds();
@ -66,127 +68,4 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD Reason, LPVOID lpVoid)
return TRUE; 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<PIMAGE_NT_HEADERS>(module_base + reinterpret_cast<PIMAGE_DOS_HEADER>(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<unsigned char*>(module_start); current_page < reinterpret_cast<unsigned char*>(module_end); current_page = reinterpret_cast<unsigned char*>(page_information.BaseAddress + page_information.RegionSize))
{
VirtualQuery(reinterpret_cast<LPCVOID>(current_page), reinterpret_cast<PMEMORY_BASIC_INFORMATION>(&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<unsigned char*>(page_information.BaseAddress); current_address < reinterpret_cast<unsigned char*>(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<uintptr_t>(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<PIMAGE_NT_HEADERS>(module_base + reinterpret_cast<PIMAGE_DOS_HEADER>(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<unsigned char*>(module_start); current_page < reinterpret_cast<unsigned char*>(module_end); current_page = reinterpret_cast<unsigned char*>(page_information.BaseAddress + page_information.RegionSize))
{
VirtualQuery(reinterpret_cast<LPCVOID>(current_page), reinterpret_cast<PMEMORY_BASIC_INFORMATION>(&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<unsigned char*>(page_information.BaseAddress); current_address < reinterpret_cast<unsigned char*>(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<uintptr_t>(current_address);
pattern_current += 3;
if (pattern_current[-1] == NULL)
return current_match;
}
}
}
return 0ULL;
}
menu_variables vars; 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<size_t>(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<size_t>(val));
}

View File

@ -22,18 +22,10 @@
#include "ini.h" #include "ini.h"
#include "json.hpp" #include "json.hpp"
#define base g_Addrs.ModuleBase
#pragma warning(disable:4996) #pragma warning(disable:4996)
#pragma comment(lib, "Gdi32.lib") #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 { struct menu_variables {
bool bInitiateMenu; bool bInitiateMenu;
bool bMenuOpen; bool bMenuOpen;
@ -41,17 +33,6 @@ struct menu_variables {
}; };
extern menu_variables vars; 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 struct DvarPair
{ {
const char* m_key; const char* m_key;

101
hook_lib/addr_utils.cpp Executable file
View File

@ -0,0 +1,101 @@
#include "addr_utils.hpp"
#include <cctype>
#include <string>
#include <windows.h>
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<size_t>(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<size_t>(val)); }
namespace {
DWORD get_module_size(uintptr_t module_base) {
return reinterpret_cast<PIMAGE_NT_HEADERS>(
module_base +
reinterpret_cast<PIMAGE_DOS_HEADER>(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<unsigned char*>(module_start);
current_page < reinterpret_cast<unsigned char*>(module_end);
current_page = reinterpret_cast<unsigned char*>(page_information.BaseAddress + page_information.RegionSize)) {
if (VirtualQuery(reinterpret_cast<LPCVOID>(current_page),
reinterpret_cast<PMEMORY_BASIC_INFORMATION>(&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<unsigned char*>(page_information.BaseAddress);
current_address < reinterpret_cast<unsigned char*>(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<uintptr_t>(current_address);
}
pattern_current += 3;
if (pattern_current[-1] == NULL)
{
return current_match;
}
}
}
return 0ULL;
}

16
hook_lib/addr_utils.hpp Normal file
View File

@ -0,0 +1,16 @@
#include <vadefs.h>
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);

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "cmd.h" #include "cmd.h"
void Cmd_Exec_Internal(bool isSuperUser) void Cmd_Exec_Internal(bool isSuperUser)

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "devgui.h" #include "devgui.h"
void CL_CreateDevGui_Detour(int fsMenuEntries, const char* modeCfg) void CL_CreateDevGui_Detour(int fsMenuEntries, const char* modeCfg)

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "functions.hpp" #include "functions.hpp"
void* RtlAddVectoredExceptionHandler(LONG First, PVECTORED_EXCEPTION_HANDLER Handler) { 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; cmd_function_s load_weapdef_f_VAR;
CmdArgs* cmd_args; CmdArgs* cmd_args;
Addresses g_Addrs;

View File

@ -5,11 +5,6 @@ struct gentity_s;
struct CmdArgs; struct CmdArgs;
struct Addresses {
uintptr_t ModuleBase;
uintptr_t jmp_rbx;
};
union DvarValue union DvarValue
{ {
bool enabled; bool enabled;
@ -258,5 +253,3 @@ int G_Main_GetTime();
const char* _va(const char* format, ...); const char* _va(const char* format, ...);
#pragma endregion #pragma endregion
extern Addresses g_Addrs;

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "g_cmds.h" #include "g_cmds.h"
#include "game_inc.h" #include "game_inc.h"
@ -216,7 +217,7 @@ void set_byte_f()
if (Cmd_Argc() == 3) if (Cmd_Argc() == 3)
{ {
Cmd_ArgvBuffer(1, command, 500); Cmd_ArgvBuffer(1, command, 500);
uintptr_t address = atoll(command) + base; uintptr_t address = _b(atoll(command));
Cmd_ArgvBuffer(2, command, 500); Cmd_ArgvBuffer(2, command, 500);
utils::hook::set<unsigned char>(address, atoi(command)); utils::hook::set<unsigned char>(address, atoi(command));
} }
@ -228,7 +229,7 @@ void set_short_f()
if (Cmd_Argc() == 3) if (Cmd_Argc() == 3)
{ {
Cmd_ArgvBuffer(1, command, 500); Cmd_ArgvBuffer(1, command, 500);
uintptr_t address = atoll(command) + base; uintptr_t address = _b(atoll(command));
Cmd_ArgvBuffer(2, command, 500); Cmd_ArgvBuffer(2, command, 500);
utils::hook::set<unsigned short>(address, atol(command)); utils::hook::set<unsigned short>(address, atol(command));
} }
@ -240,7 +241,7 @@ void set_int_f()
if (Cmd_Argc() == 3) if (Cmd_Argc() == 3)
{ {
Cmd_ArgvBuffer(1, command, 500); Cmd_ArgvBuffer(1, command, 500);
uintptr_t address = atoll(command) + base; uintptr_t address = _b(atoll(command));
Cmd_ArgvBuffer(2, command, 500); Cmd_ArgvBuffer(2, command, 500);
utils::hook::set<unsigned int>(address, _atoi64(command)); utils::hook::set<unsigned int>(address, _atoi64(command));
} }
@ -252,7 +253,7 @@ void set_float_f()
if (Cmd_Argc() == 3) if (Cmd_Argc() == 3)
{ {
Cmd_ArgvBuffer(1, command, 500); Cmd_ArgvBuffer(1, command, 500);
uintptr_t address = atoll(command) + base; uintptr_t address = _b(atoll(command));
Cmd_ArgvBuffer(2, command, 500); Cmd_ArgvBuffer(2, command, 500);
utils::hook::set<float>(address, strToFloat(command)); utils::hook::set<float>(address, strToFloat(command));
} }
@ -264,7 +265,7 @@ void set_pointer_f()
if (Cmd_Argc() == 3) if (Cmd_Argc() == 3)
{ {
Cmd_ArgvBuffer(1, command, 500); Cmd_ArgvBuffer(1, command, 500);
uintptr_t address = atoll(command) + base; uintptr_t address = _b(atoll(command));
Cmd_ArgvBuffer(2, command, 500); Cmd_ArgvBuffer(2, command, 500);
utils::hook::set<unsigned __int64>(address, _atoi64(command)); utils::hook::set<unsigned __int64>(address, _atoi64(command));
} }

View File

@ -82,6 +82,7 @@
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<TargetName>discord_game_sdk</TargetName> <TargetName>discord_game_sdk</TargetName>
<LibraryPath>$(SolutionDir)\lib;$(LibraryPath)</LibraryPath> <LibraryPath>$(SolutionDir)\lib;$(LibraryPath)</LibraryPath>
<IncludePath>$(SolutionDir)\hook_lib;$(IncludePath)</IncludePath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental> <LinkIncremental>false</LinkIncremental>
@ -149,6 +150,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="addr_utils.cpp" />
<ClCompile Include="cmd.cpp" /> <ClCompile Include="cmd.cpp" />
<ClCompile Include="common\exception\minidump.cpp" /> <ClCompile Include="common\exception\minidump.cpp" />
<ClCompile Include="common\utils\binary_resource.cpp" /> <ClCompile Include="common\utils\binary_resource.cpp" />
@ -189,6 +191,7 @@
<ClCompile Include="zones.cpp" /> <ClCompile Include="zones.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="addr_utils.hpp" />
<ClInclude Include="assets.h" /> <ClInclude Include="assets.h" />
<ClInclude Include="cmd.h" /> <ClInclude Include="cmd.h" />
<ClInclude Include="common\exception\minidump.hpp" /> <ClInclude Include="common\exception\minidump.hpp" />

View File

@ -67,6 +67,9 @@
<ClCompile Include="structs.cpp"> <ClCompile Include="structs.cpp">
<Filter>hook_lib\game</Filter> <Filter>hook_lib\game</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="addr_utils.cpp">
<Filter>hook_lib\game</Filter>
</ClCompile>
<ClCompile Include="assets.cpp"> <ClCompile Include="assets.cpp">
<Filter>hook_lib\game</Filter> <Filter>hook_lib\game</Filter>
</ClCompile> </ClCompile>
@ -192,6 +195,9 @@
<ClInclude Include="structs.h"> <ClInclude Include="structs.h">
<Filter>hook_lib\game</Filter> <Filter>hook_lib\game</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="addr_utils.hpp">
<Filter>hook_lib\game</Filter>
</ClInclude>
<ClInclude Include="assets.h"> <ClInclude Include="assets.h">
<Filter>hook_lib\game</Filter> <Filter>hook_lib\game</Filter>
</ClInclude> </ClInclude>

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "input.h" #include "input.h"
void CL_Keys_Event_Detour(int localClientNum, int key, bool down, unsigned int time, int virtualKey, int controllerIndex) void CL_Keys_Event_Detour(int localClientNum, int key, bool down, unsigned int time, int virtualKey, int controllerIndex)

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "inventory.h" #include "inventory.h"
#include "game_inc.h" #include "game_inc.h"

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "omnvars.h" #include "omnvars.h"
int BG_Omnvar_GetType(OmnvarDef* ovDef) int BG_Omnvar_GetType(OmnvarDef* ovDef)

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "patch.h" #include "patch.h"
#include "game_inc.h" #include "game_inc.h"

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "screen.h" #include "screen.h"
void CG_DrawWaterMark() void CG_DrawWaterMark()

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "script.h" #include "script.h"
#include "game_inc.h" #include "game_inc.h"

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "structs.h" #include "structs.h"

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "sv_main.h" #include "sv_main.h"
#include "game_inc.h" #include "game_inc.h"

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "transients.h" #include "transients.h"
#include "game_inc.h" #include "game_inc.h"

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "weapons.h" #include "weapons.h"
#include "game_inc.h" #include "game_inc.h"

View File

@ -1,3 +1,4 @@
#include "addr_utils.hpp"
#include "zones.h" #include "zones.h"
#include "game_inc.h" #include "game_inc.h"