Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Multiverse::Api::Memory::Utils
- {
- enum class OffsetEnum { Add, Subtract };
- class MULTIVERSE_API MemoryManager {
- /*
- *****************************************************
- * *
- * Memory Manager by RaYRoD TV (2020) *
- * - Provides General Memory Management - *
- * *
- *****************************************************
- */
- public:
- // - Logs a hooked function's parameter, for a specified number of cycles o -
- // Param 1: functionName (user defined)
- // Param 2: argumentName (user defined)
- // Param 3: numberOfCycles - The number of cycles to be run through the hook that calls this function (user defined)
- // Param 4: argument - the argument to print
- // Param 5: printInHexadecimal: Whether to print in hex or not
- template <typename T>
- static void PrintHookArgument(const char* functionName, const char* argumentName, int numberOfCycles, T argument, bool printInHexadecimal) {
- static int cycle{};
- if (cycle <= numberOfCycles && printInHexadecimal)
- std::cout << argumentName << " of cycle " << cycle << " in " << functionName << " is: " << std::hex << argument << std::endl;
- if (cycle <= numberOfCycles && !printInHexadecimal)
- std::cout << argumentName << " of cycle " << cycle << " in " << functionName << " is: " << argument << std::endl;
- cycle++;
- }
- // -- Resolves the address of a LEA instruction --
- // Param 1: Address of LEA instruction
- static uintptr_t ResolveLeaAddress(uintptr_t leaAddress);
- // -- Resolves the address of a CALL instruction --
- // Param 1: Address of CALL instruction
- static uintptr_t ResolveCallAddress(uintptr_t addressOfCall);
- // -- Resolves a relative address --
- // Param 1: Address of relative instruction
- static uintptr_t ResolveRelativeAddress(uintptr_t relativeAddress);
- // -- Returns the value of an Address --
- // Param 1: Memory Address
- template <typename T>
- static T Dereference(T address) {
- return *(T*)address;
- }
- // -- Dereference a multi-level pointer -
- // Param 1: basePointerAddress
- // Param 2: The Offsets of the Multi-level pointer
- // Param 3: The level of the Multi-level pointer
- static uintptr_t DerefMultiLevelPointer(uintptr_t basePointerAddress, uintptr_t offsets[], int pointerLevel);
- // - NOP (No Operation) Bytes Internally (Useful for internal hacks) -
- // Param 1: Memory Address
- // Param 2: Size of bytes to NOP
- static void NopInternal(char* address, unsigned int size);
- // - NOP (No Operation) Bytes Externally (Useful for external hacks) -
- // Param 1: Memory Address
- // Param 2: Size of bytes to NOP
- static void NopExternal(char* address, unsigned int size, HANDLE processHandle);
- // -- Compare Two Memory Values against each other --
- // Param 1: Memory Address
- // Param 2: Offset to Accumulate with address (use 0x0 for no additional offset)
- // Param 3: OffsetEnum::Add = Increment Offset | OffsetEnum::Subtract = Decrement Offset
- // Param 4: Value to search for
- template <typename T>
- static bool CompareMemoryValue(uintptr_t address, int offset, OffsetEnum offsetEnum, T value) {
- uintptr_t addressWithOffset{};
- if (offsetEnum == OffsetEnum::Add)
- addressWithOffset = (uintptr_t)address + offset;
- if (offsetEnum == OffsetEnum::Subtract)
- addressWithOffset = (uintptr_t)address - offset;
- if (*(T*)(addressWithOffset) == (T)value)
- return true;
- else
- return false;
- }
- // - Clears a String in memory -
- // Param 1: address - Memory Address of String
- // Param 2: stringLength - (Number of Characters in String to clear)
- static void ClearString(uintptr_t address, SIZE_T stringLength);
- // -- Compares Two String Values in memory against each other --
- // Param 1: Memory Address of String
- // Param 2: String to search for to compare
- static bool StringCompare(uintptr_t address, const char* stringToCompareAgainst);
- // -- Compares Two String Values in memory against each other --
- // Param 1: Memory Address of String
- // Param 2: Offset to Accumulate with address (use 0x0 for no additional offset)
- // Param 3: String to search for to compare
- static bool StringCompare(uintptr_t address, int offset, const char* stringToCompareAgainst);
- // -- Compares Two String Values in memory against each other --
- // Param 1: Memory Address of String
- // Param 2: Offset to Accumulate with address (use 0x0 for no additional offset)
- // Param 3: OffsetEnum::Add = Increment Offset | OffsetEnum::Subtract = Decrement Offset
- // Param 4: String to search for to compare
- static bool StringCompare(uintptr_t address, int offset, OffsetEnum offsetEnum, const char* stringToCompareAgainst);
- // - Gets the values of an Array & stores them in a Vector Variable -
- // Param 1: MemVec_t address
- template <typename T>
- static Vec_t<T> DerefArrayValues(MemVec_t address) {
- Vec_t<T> result{};
- for (size_t i = 0; i < address.size(); i++)
- result.push_back(*(T*)address[i]);
- return result;
- }
- // - Gets the values of an Array & stores them in a Vector Variable -
- // Param 1: MemVec_t address
- // Param 2: padding size
- template <typename T>
- static Vec_t<T> DerefArrayValues(uintptr_t address, int paddingSize) {
- Vec_t<T> result;
- for (size_t i = 0; i < address.size(); i++) {
- result.push_back(*(T*)address[i]);
- address += paddingSize;
- }
- return result;
- }
- // - Dereferences the values of an 'array of Colour' & stores them in a 2D Vector Variable -
- static Vec2D_t<float> DerefColourArrayValues(static MemVec_t& memVec);
- static Vec2D_t<float> DerefColourArrayValues(Vec_t<MemColour_t>);
- // - Dereferences the values of an 'array of Strings' & stores them in a Vector Variable -
- static Vec_t<std::string> DerefStringArrayValues(MemVec_t memVec);
- // - Dereferences the values of a 'Vector2f' Array & stores them in a Vector Variable -
- static Vec_t<Vec2f_t> DerefVector2fArrayValues(MemVec_t memVec);
- // - Dereferences the values of a 'Vector3f' Array & stores them in a Vector Variable -
- static Vec_t<Vec3f_t> DerefVector3fArrayValues(MemVec_t memVec);
- // - Dereferences the values of a 'Vector4f' Array & stores them in a Vector Variable -
- static Vec_t<Vec4f_t> DerefVector4fArrayValues(MemVec_t memVec);
- static Vec2D_t<float> MemoryManager::DerefVector4fArrayValues(Vec_t<MemVec4f_t> variable);
- // - Dereferences & Gets the Hex Colour value from a memory address, eg. "#B4B469FF" -
- // Param 1: address - memory address of Colour
- static std::string GetHexColour(uintptr_t address);
- // - Reads a string from memory -
- // Param 1: Memory Address of String
- static char* ReadStringCharP(uintptr_t address);
- // - Reads a string from memory -
- // Param 1: Memory Address of String
- static std::string ReadString(uintptr_t address);
- // -- Alters protection flags at a memory location & writes to it --
- // Param 1: Memory Address
- // Param 2: size of dataType
- // Param 3: Value to write
- template <typename T>
- void WriteMemSafe(uintptr_t address, const int blockSize, T value) {
- DWORD oldProtect{};
- if (VirtualProtect((LPVOID)address, blockSize, PAGE_EXECUTE_READWRITE, &oldProtect)) {
- *(uintptr_t*)(address) = value;
- }
- }
- // -- Alters protection flags at a memory location & writes to it --
- // Param 1: Memory Address
- // Param 2: the number of bytes to jump after the address of Param 1
- // Param 3: size of dataType
- // Param 4: Value to write
- template <typename T>
- void WriteMemSafe(uintptr_t address, const int offset, const int blockSize, T value) {
- DWORD oldProtect{};
- if (VirtualProtect((LPVOID)address + offset, blockSize, PAGE_EXECUTE_READWRITE, &oldProtect)) {
- *(uintptr_t*)(address)+offset = value;
- }
- }
- // - Sets/Writes Bytes in Memory ( useful for patching instructions, etc... ) -
- // Param 1: Memory Address - Starting Point
- // Param 2: int/hex value to write (for patching)
- // Param 3: Size of bytes to copy the value of Param 2 to
- static void WriteBytes(uintptr_t address, int value, SIZE_T size);
- // - Sets/Writes Bytes in Memory ( useful for patching instructions, etc... ) -
- // Param 1: Memory Address - Starting Point
- // Param 2: bytes values to write (for patching) - eg. { 0xE2, 0xA7, 0x36, 0xC0 }
- static void WriteBytes(uintptr_t address, vector<unsigned int> value);
- // - Sets/Writes a value to a memory address -
- // Param 1: Memory Address
- // Param 2: Value to set
- template <typename T>
- static void WriteValue(uintptr_t address, T valueToSet) {
- //memory.write<T>(address, valueToSet); // External
- if (address)
- *(T*)address = valueToSet;
- }
- // - Sets/Writes to the memory addresses of a 'Bool Array' -
- // Param 1: Address - The address of the Bool Array
- // Param 2: value to set - eg. { false, true, false, true }
- static void WriteBoolArray(uintptr_t address, vector<bool> valueToSet);
- // - Sets/Writes to the memory addresses of an 'Int Array' -
- // Param 1: Address - The address of the Int Array
- // Param 2: value to set - eg. { 1, 2, 3, 4 }
- static void WriteIntArray(uintptr_t address, vector<int> valueToSet);
- // - Sets/Writes to the memory addresses of a 'Float Array' -
- // Param 1: Address - The address of the float Array
- // Param 2: value to set - eg. { 1.f, 2.f, 3.f, 4.f }
- static void WriteFloatArray(uintptr_t address, vector<float> valueToSet);
- // - Sets/Writes a string to memory -
- // Param 1: Memory Address
- // Param 2: String to write
- static void WriteString(uintptr_t address, char* newString);
- // - Writes a 'Hex Colour' to a 'Colour' field memory address, eg. "#B4B469FF"
- // Param 1: address - memory address of Colour
- // Param 2: Colour to set - eg. #B0807D49
- static uintptr_t WriteHexColour(uintptr_t address, std::string colourToSet);
- };
- inline MemoryManager g_MemoryManager;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement