r5sdk/r5dev/public/tier0/memaddr.h

133 lines
3.0 KiB
C
Raw Normal View History

2021-07-20 23:28:54 +02:00
#pragma once
class CMemory
2021-07-20 23:28:54 +02:00
{
public:
enum class Direction : int
{
DOWN = 0,
UP,
2021-07-20 23:28:54 +02:00
};
CMemory(void) = default;
2022-12-05 00:57:48 +01:00
CMemory(const uintptr_t ptr) : ptr(ptr) {}
CMemory(const void* ptr) : ptr(uintptr_t(ptr)) {}
2021-07-20 23:28:54 +02:00
inline operator uintptr_t(void) const
2021-07-20 23:28:54 +02:00
{
return ptr;
}
inline operator void*(void) const
2021-07-20 23:28:54 +02:00
{
return reinterpret_cast<void*>(ptr);
}
inline operator bool(void) const
2021-07-20 23:28:54 +02:00
{
return ptr != NULL;
}
inline bool operator!= (const CMemory& addr) const
2021-07-20 23:28:54 +02:00
{
return ptr != addr.ptr;
}
inline bool operator== (const CMemory& addr) const
2021-07-20 23:28:54 +02:00
{
return ptr == addr.ptr;
}
inline bool operator== (const uintptr_t& addr) const
2021-07-20 23:28:54 +02:00
{
return ptr == addr;
}
inline uintptr_t GetPtr(void) const
2021-07-20 23:28:54 +02:00
{
return ptr;
2021-07-20 23:28:54 +02:00
}
template<class T> inline T GetValue(void) const
{
return *reinterpret_cast<T*>(ptr);
}
template<class T> inline T GetVirtualFunctionIndex(void) const
2021-07-20 23:28:54 +02:00
{
return *reinterpret_cast<T*>(ptr) / 8; // Its divided by 8 in x64.
2021-07-20 23:28:54 +02:00
}
template<typename T> inline T CCast(void) const
{
return (T)ptr;
2021-07-20 23:28:54 +02:00
}
template<typename T> inline T RCast(void) const
{
return reinterpret_cast<T>(ptr);
Merge indev into master. (#44) * Added separate function to resolve relative addresses in address.h * Added new patterns to the print function. * Updated IsFlagSet hooks. * Cleaned up code to properly mask off Dev and Cheat flags. * Added separate define from _DEBUG so you can define it in release builds for people without C++ Debug Restributeables. * Removed un-used define in hooks.h * Fixed potential crashes in r5net and added debug prints. * Potential crashes were when in certain post functions the returned status wasn't 200. * Changed map-select drop down menu, now it displays 'Map Name + Season' instead of file-name. (#41) * Host Server shows normal map names * Changed a few stuff... * redid some stuff that isn't crucial * Update CCompanion.cpp * Update CCompanion.cpp * Updated mapname displaying. * Moved "ServerMap" as a static object into CCompanion::HostServerSection(). Co-authored-by: IcePixelx <41352111+PixieCore@users.noreply.github.com> * prevent squirrel compiler errors from killing game process (#43) * Read description for all changes. * Added ability to register custom ConVar. * Added 2 custom ConVars to open the CGameConsole and CCompanion Windows. * Changed ResolveRelativeAddress. * Added Config System for the Gui. * Added ImGui::Hotkey. * Added the ability to change 2 hotkeys for opening the window for CGameConsole and CCompanion. * Changed pattern for Squirrel_CompilerError to use a String. * Added IMemAlloc::AllocWrapper to patterns.h * Changes in description. * Added icon to launcher.exe * Launcher.exe gets remnamed to Run R5 Reloaded.exe in launcher release compilation configuration. * Extended argument buffer for starting the game in launcher.exe. * Added exception printing if in the custom ConVars an invalid value gets passed. * Wrong return. * Added shortcut with launch params. * Fixed prints. Co-authored-by: Marcii0 <58266292+Marcii0@users.noreply.github.com> Co-authored-by: BobTheBob <32057864+BobTheBob9@users.noreply.github.com>
2021-08-19 15:26:44 +02:00
}
inline CMemory Offset(ptrdiff_t offset) const
Merge indev into master. (#44) * Added separate function to resolve relative addresses in address.h * Added new patterns to the print function. * Updated IsFlagSet hooks. * Cleaned up code to properly mask off Dev and Cheat flags. * Added separate define from _DEBUG so you can define it in release builds for people without C++ Debug Restributeables. * Removed un-used define in hooks.h * Fixed potential crashes in r5net and added debug prints. * Potential crashes were when in certain post functions the returned status wasn't 200. * Changed map-select drop down menu, now it displays 'Map Name + Season' instead of file-name. (#41) * Host Server shows normal map names * Changed a few stuff... * redid some stuff that isn't crucial * Update CCompanion.cpp * Update CCompanion.cpp * Updated mapname displaying. * Moved "ServerMap" as a static object into CCompanion::HostServerSection(). Co-authored-by: IcePixelx <41352111+PixieCore@users.noreply.github.com> * prevent squirrel compiler errors from killing game process (#43) * Read description for all changes. * Added ability to register custom ConVar. * Added 2 custom ConVars to open the CGameConsole and CCompanion Windows. * Changed ResolveRelativeAddress. * Added Config System for the Gui. * Added ImGui::Hotkey. * Added the ability to change 2 hotkeys for opening the window for CGameConsole and CCompanion. * Changed pattern for Squirrel_CompilerError to use a String. * Added IMemAlloc::AllocWrapper to patterns.h * Changes in description. * Added icon to launcher.exe * Launcher.exe gets remnamed to Run R5 Reloaded.exe in launcher release compilation configuration. * Extended argument buffer for starting the game in launcher.exe. * Added exception printing if in the custom ConVars an invalid value gets passed. * Wrong return. * Added shortcut with launch params. * Fixed prints. Co-authored-by: Marcii0 <58266292+Marcii0@users.noreply.github.com> Co-authored-by: BobTheBob <32057864+BobTheBob9@users.noreply.github.com>
2021-08-19 15:26:44 +02:00
{
return CMemory(ptr + offset);
Merge indev into master. (#44) * Added separate function to resolve relative addresses in address.h * Added new patterns to the print function. * Updated IsFlagSet hooks. * Cleaned up code to properly mask off Dev and Cheat flags. * Added separate define from _DEBUG so you can define it in release builds for people without C++ Debug Restributeables. * Removed un-used define in hooks.h * Fixed potential crashes in r5net and added debug prints. * Potential crashes were when in certain post functions the returned status wasn't 200. * Changed map-select drop down menu, now it displays 'Map Name + Season' instead of file-name. (#41) * Host Server shows normal map names * Changed a few stuff... * redid some stuff that isn't crucial * Update CCompanion.cpp * Update CCompanion.cpp * Updated mapname displaying. * Moved "ServerMap" as a static object into CCompanion::HostServerSection(). Co-authored-by: IcePixelx <41352111+PixieCore@users.noreply.github.com> * prevent squirrel compiler errors from killing game process (#43) * Read description for all changes. * Added ability to register custom ConVar. * Added 2 custom ConVars to open the CGameConsole and CCompanion Windows. * Changed ResolveRelativeAddress. * Added Config System for the Gui. * Added ImGui::Hotkey. * Added the ability to change 2 hotkeys for opening the window for CGameConsole and CCompanion. * Changed pattern for Squirrel_CompilerError to use a String. * Added IMemAlloc::AllocWrapper to patterns.h * Changes in description. * Added icon to launcher.exe * Launcher.exe gets remnamed to Run R5 Reloaded.exe in launcher release compilation configuration. * Extended argument buffer for starting the game in launcher.exe. * Added exception printing if in the custom ConVars an invalid value gets passed. * Wrong return. * Added shortcut with launch params. * Fixed prints. Co-authored-by: Marcii0 <58266292+Marcii0@users.noreply.github.com> Co-authored-by: BobTheBob <32057864+BobTheBob9@users.noreply.github.com>
2021-08-19 15:26:44 +02:00
}
inline CMemory OffsetSelf(ptrdiff_t offset)
Merge indev into master. (#44) * Added separate function to resolve relative addresses in address.h * Added new patterns to the print function. * Updated IsFlagSet hooks. * Cleaned up code to properly mask off Dev and Cheat flags. * Added separate define from _DEBUG so you can define it in release builds for people without C++ Debug Restributeables. * Removed un-used define in hooks.h * Fixed potential crashes in r5net and added debug prints. * Potential crashes were when in certain post functions the returned status wasn't 200. * Changed map-select drop down menu, now it displays 'Map Name + Season' instead of file-name. (#41) * Host Server shows normal map names * Changed a few stuff... * redid some stuff that isn't crucial * Update CCompanion.cpp * Update CCompanion.cpp * Updated mapname displaying. * Moved "ServerMap" as a static object into CCompanion::HostServerSection(). Co-authored-by: IcePixelx <41352111+PixieCore@users.noreply.github.com> * prevent squirrel compiler errors from killing game process (#43) * Read description for all changes. * Added ability to register custom ConVar. * Added 2 custom ConVars to open the CGameConsole and CCompanion Windows. * Changed ResolveRelativeAddress. * Added Config System for the Gui. * Added ImGui::Hotkey. * Added the ability to change 2 hotkeys for opening the window for CGameConsole and CCompanion. * Changed pattern for Squirrel_CompilerError to use a String. * Added IMemAlloc::AllocWrapper to patterns.h * Changes in description. * Added icon to launcher.exe * Launcher.exe gets remnamed to Run R5 Reloaded.exe in launcher release compilation configuration. * Extended argument buffer for starting the game in launcher.exe. * Added exception printing if in the custom ConVars an invalid value gets passed. * Wrong return. * Added shortcut with launch params. * Fixed prints. Co-authored-by: Marcii0 <58266292+Marcii0@users.noreply.github.com> Co-authored-by: BobTheBob <32057864+BobTheBob9@users.noreply.github.com>
2021-08-19 15:26:44 +02:00
{
ptr += offset;
Merge indev into master. (#44) * Added separate function to resolve relative addresses in address.h * Added new patterns to the print function. * Updated IsFlagSet hooks. * Cleaned up code to properly mask off Dev and Cheat flags. * Added separate define from _DEBUG so you can define it in release builds for people without C++ Debug Restributeables. * Removed un-used define in hooks.h * Fixed potential crashes in r5net and added debug prints. * Potential crashes were when in certain post functions the returned status wasn't 200. * Changed map-select drop down menu, now it displays 'Map Name + Season' instead of file-name. (#41) * Host Server shows normal map names * Changed a few stuff... * redid some stuff that isn't crucial * Update CCompanion.cpp * Update CCompanion.cpp * Updated mapname displaying. * Moved "ServerMap" as a static object into CCompanion::HostServerSection(). Co-authored-by: IcePixelx <41352111+PixieCore@users.noreply.github.com> * prevent squirrel compiler errors from killing game process (#43) * Read description for all changes. * Added ability to register custom ConVar. * Added 2 custom ConVars to open the CGameConsole and CCompanion Windows. * Changed ResolveRelativeAddress. * Added Config System for the Gui. * Added ImGui::Hotkey. * Added the ability to change 2 hotkeys for opening the window for CGameConsole and CCompanion. * Changed pattern for Squirrel_CompilerError to use a String. * Added IMemAlloc::AllocWrapper to patterns.h * Changes in description. * Added icon to launcher.exe * Launcher.exe gets remnamed to Run R5 Reloaded.exe in launcher release compilation configuration. * Extended argument buffer for starting the game in launcher.exe. * Added exception printing if in the custom ConVars an invalid value gets passed. * Wrong return. * Added shortcut with launch params. * Fixed prints. Co-authored-by: Marcii0 <58266292+Marcii0@users.noreply.github.com> Co-authored-by: BobTheBob <32057864+BobTheBob9@users.noreply.github.com>
2021-08-19 15:26:44 +02:00
return *this;
}
inline CMemory Deref(int deref = 1) const
{
uintptr_t reference = ptr;
2021-07-20 23:28:54 +02:00
while (deref--)
{
if (reference)
reference = *reinterpret_cast<uintptr_t*>(reference);
2021-07-20 23:28:54 +02:00
}
return CMemory(reference);
2021-07-20 23:28:54 +02:00
}
inline CMemory DerefSelf(int deref = 1)
{
while (deref--)
{
if (ptr)
ptr = *reinterpret_cast<uintptr_t*>(ptr);
}
return *this;
2021-07-20 23:28:54 +02:00
}
inline CMemory WalkVTable(ptrdiff_t vfuncIndex)
{
uintptr_t reference = ptr + (8 * vfuncIndex);
return CMemory(reference);
}
inline CMemory WalkVTableSelf(ptrdiff_t vfuncIndex)
{
ptr += (8 * vfuncIndex);
return *this;
}
bool CheckOpCodes(const vector<uint8_t> vOpcodeArray) const;
void Patch(const vector<uint8_t> vOpcodeArray) const;
void PatchString(const string& svString) const;
CMemory FindPattern(const string& svPattern, const Direction searchDirect = Direction::DOWN, const int opCodesToScan = 512, const ptrdiff_t occurrence = 1) const;
CMemory FindPatternSelf(const string& svPattern, const Direction searchDirect = Direction::DOWN, const int opCodesToScan = 512, const ptrdiff_t occurrence = 1);
CMemory FollowNearCall(const ptrdiff_t opcodeOffset = 0x1, const ptrdiff_t nextInstructionOffset = 0x5) const;
CMemory FollowNearCallSelf(const ptrdiff_t opcodeOffset = 0x1, const ptrdiff_t nextInstructionOffset = 0x5);
CMemory ResolveRelativeAddress(const ptrdiff_t registerOffset = 0x0, const ptrdiff_t nextInstructionOffset = 0x4) const;
CMemory ResolveRelativeAddressSelf(const ptrdiff_t registerOffset = 0x0, const ptrdiff_t nextInstructionOffset = 0x4);
vector<CMemory> FindAllCallReferences(const uintptr_t sectionBase, const size_t sectionSize);
static void HookVirtualMethod(const uintptr_t virtualTable, const void* pHookMethod, const ptrdiff_t methodIndex, void** ppOriginalMethod);
2021-07-20 23:28:54 +02:00
private:
uintptr_t ptr = 0;
};