Advertisement
RaYRoDTV

MultiverseAPI - Memory Manager

Aug 27th, 2022
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.62 KB | None | 0 0
  1.     class MULTIVERSE_API MemoryManager {
  2.     public:
  3.  
  4.         // -- Resolves the address of a LEA instruction --
  5.         // Param 1: Address of LEA instruction
  6.         static uintptr_t ResolveLeaAddress(uintptr_t leaAddress);
  7.  
  8.         // -- Resolves the address of a CALL instruction --
  9.         // Param 1: Address of CALL instruction
  10.         static uintptr_t ResolveCallAddress(uintptr_t addressOfCall);
  11.  
  12.         // -- Resolves a relative address --
  13.         // Param 1: Address of relative instruction
  14.         static uintptr_t ResolveRelativeAddress(uintptr_t relativeAddress);
  15.  
  16.         // -- Returns the value of an Address --
  17.         // Param 1: Memory Address
  18.         template <typename T>
  19.         static T Dereference(T address) {
  20.             return *(T*)address;
  21.         }
  22.  
  23.         /// -- Dereference a multi-level pointer -
  24.         // Param 1: basePointerAddress
  25.         // Param 2: The Offsets of the Multi-level pointer
  26.         // Param 3: The level of the Multi-level pointer
  27.         static uintptr_t DerefMultiLevelPointer(uintptr_t basePointerAddress, uintptr_t offsets[], int pointerLevel);
  28.  
  29.         /// - NOP (No Operation) Bytes Internally (Useful for internal hacks) -
  30.         // Param 1: Memory Address
  31.         // Param 2: Size of bytes to NOP
  32.         static void NopInternal(char* address, unsigned int size);
  33.  
  34.         /// - NOP (No Operation) Bytes Externally (Useful for external hacks) -
  35.         // Param 1: Memory Address
  36.         // Param 2: Size of bytes to NOP
  37.         static void NopExternal(char* address, unsigned int size, HANDLE processHandle);
  38.  
  39.         /// - Clears a String in memory -
  40.         // Param 1: address - Memory Address of String
  41.         // Param 2: stringLength - (Number of Characters in String to clear)
  42.         static void ClearString(uintptr_t address, SIZE_T stringLength);
  43.  
  44.         // - Sets/Writes Bytes in Memory ( useful for patching instructions, etc... ) -
  45.         // Param 1: Memory Address - Starting Point
  46.         // Param 2: int/hex value to write (for patching)
  47.         // Param 3: Size of bytes to copy the value of Param 2 to
  48.         static void WriteBytes(uintptr_t address, int value, SIZE_T size);
  49.  
  50.         // - Sets/Writes Bytes in Memory ( useful for patching instructions, etc... ) -
  51.         // Param 1: Memory Address - Starting Point
  52.         // Param 2: bytes values to write (for patching) - eg. { 0xE2, 0xA7, 0x36, 0xC0 }
  53.         static void WriteBytes(uintptr_t address, std::vector<unsigned int> value);
  54.  
  55.         /// - Sets/Writes a string to memory -
  56.         // Param 1: Memory Address
  57.         // Param 2: String to write
  58.         static void WriteString(char* address, char* newString);
  59.  
  60.         /// - Reads a string from memory -
  61.         // Param 1: Memory Address of String
  62.         static char* ReadString(uintptr_t address);
  63.  
  64.         // -- Compares Two String Values in memory against each other --
  65.         // Param 1: Memory Address of String
  66.         // Param 2: String to search for to compare
  67.         static bool StringCompare(char* address, const char* stringToCompareAgainst);
  68.  
  69.         // -- Compares Two String Values in memory against each other --
  70.         // Param 1: Memory Address of String
  71.         // Param 2: Offset to Accumulate with address (use 0x0 for no additional offset)
  72.         // Param 3: String to search for to compare
  73.         static bool StringCompare(char* address, int offset, const char* stringToCompareAgainst);
  74.  
  75.         // -- Compares Two String Values in memory against each other --
  76.         // Param 1: Memory Address of String
  77.         // Param 2: Offset to Accumulate with address (use 0x0 for no additional offset)
  78.         // Param 3: OffsetEnum::Add = Increment Offset | OffsetEnum::Subtract = Decrement Offset
  79.         // Param 4: String to search for to compare
  80.         static bool StringCompare(char* address, int offset, OffsetEnum offsetEnum, const char* stringToCompareAgainst);
  81.  
  82.         // -- Compare Two Memory Values against each other --
  83.         // Param 1: Memory Address
  84.         // Param 2: Offset to Accumulate with address (use 0x0 for no additional offset)
  85.         // Param 3: OffsetEnum::Add = Increment Offset | OffsetEnum::Subtract = Decrement Offset
  86.         // Param 4: Value to search for
  87.         template <typename T>
  88.         static bool CompareMemoryValue(uintptr_t address, int offset, OffsetEnum offsetEnum, T value) {
  89.  
  90.             uintptr_t addressWithOffset{};
  91.  
  92.             if (offsetEnum = OffsetEnum::Add)
  93.                 addressWithOffset = (uintptr_t)address + offset;
  94.  
  95.             if (offsetEnum = OffsetEnum::Subtract)
  96.                 addressWithOffset = (uintptr_t)address - offset;
  97.  
  98.             if (*(T*)(addressWithOffset) == (T)value)
  99.                 return true;
  100.             else
  101.                 return false;
  102.         }
  103.  
  104.         // -- Alters protection flags at a memory location & writes to it --
  105.         // Param 1: Memory Address
  106.         // Param 2: size of dataType
  107.         // Param 3: Value to write
  108.         template <typename T>
  109.         void WriteMemSafe(uintptr_t address, const int blockSize, T value) {
  110.  
  111.             DWORD oldProtect{};
  112.             if (VirtualProtect((LPVOID)address, blockSize, PAGE_EXECUTE_READWRITE, &oldProtect)) {
  113.                 *(uintptr_t*)(address) = value;
  114.             }
  115.         }
  116.  
  117.         // -- Alters protection flags at a memory location & writes to it --
  118.         // Param 1: Memory Address
  119.         // Param 2: the number of bytes to jump after the address of Param 1
  120.         // Param 3: size of dataType
  121.         // Param 4: Value to write
  122.         template <typename T>
  123.         void WriteMemSafe(uintptr_t address, const int offset, const int blockSize, T value) {
  124.  
  125.             DWORD oldProtect{};
  126.             if (VirtualProtect((LPVOID)address + offset, blockSize, PAGE_EXECUTE_READWRITE, &oldProtect)) {
  127.                 *(uintptr_t*)(address)+offset = value;
  128.             }
  129.         }
  130.  
  131.         // - Sets/Writes a value to a memory address -
  132.         // Param 1: Memory Address
  133.         // Param 2: Value to set
  134.         template <typename T>
  135.         static uintptr_t SetValue(uintptr_t address, T valueToSet) {
  136.             //memory.write<T>(address, valueToSet); // External
  137.             *(T*)address = valueToSet;
  138.             return address;
  139.         }
  140.  
  141.         // - Sets/Writes a colour to a memory addresses -
  142.         // Param 1: address - memory address of Colour
  143.         // Param 2: Colour to set - eg. #B0807D49
  144.         static uintptr_t SetColour(uintptr_t address, std::string colourToSet);
  145.  
  146.         // - Sets/Writes to the memory addresses of a 'Float Array' -
  147.         // Param 1: Address - The address of the float Array
  148.         // Param 2: value to set - eg. { 1.f, 2.f, 3.f, 4.f }
  149.         static void WriteFloatArray(uintptr_t address, std::vector<float> valueToSet);
  150.  
  151.         // - Sets/Writes to the memory addresses of an 'Int Array' -
  152.         // Param 1: Address - The address of the Int Array
  153.         // Param 2: value to set - eg. { 1, 2, 3, 4 }
  154.         static void WriteIntArray(uintptr_t address, std::vector<int> valueToSet);
  155.  
  156.         // - Sets/Writes to the memory addresses of a 'Bool Array' -
  157.         // Param 1: Address - The address of the Bool Array
  158.         // Param 2: value to set - eg. { false, true, false, true }
  159.         static void WriteBoolArray(uintptr_t address, std::vector<bool> valueToSet);
  160.  
  161.         // - Gets the memory addresses of a 'Colour' in memory -
  162.         // Param 1: address - memory address of Colour
  163.         static std::string GetHexColour(uintptr_t address);
  164.  
  165.         // - Logs a hooked function's parameter, for a specified number of cycles o -
  166.         // Param 1: functionName (user defined)
  167.         // Param 2: argumentName (user defined)
  168.         // Param 3: numberOfCycles - The number of cycles to be run through the hook that calls this function (user defined)
  169.         // Param 4: argument - the argument to print
  170.         // Param 5: printInHexadecimal: Whether to print in hex or not
  171.         template <typename T>
  172.         void PrintHookArgument(const char* functionName, const char* argumentName, int numberOfCycles, T argument, bool printInHexadecimal) {
  173.  
  174.             static int cycle{};
  175.             if (cycle <= numberOfCycles && printInHexadecimal)
  176.                 std::cout << argumentName << " of cycle " << cycle << " in " << functionName << " is: " << std::hex << argument << std::endl;
  177.  
  178.             if (cycle <= numberOfCycles && !printInHexadecimal)
  179.                 std::cout << argumentName << " of cycle " << cycle << " in " << functionName << " is: " << argument << std::endl;
  180.             cycle++;
  181.         }
  182.  
  183.     };
  184.     inline MemoryManager g_MemoryManager;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement