Advertisement
RaYRoDTV

Multiverse API - Memory Manager (2020)

Aug 27th, 2022 (edited)
1,664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.94 KB | None | 0 0
  1. namespace Multiverse::Api::Memory::Utils
  2. {
  3.     enum class OffsetEnum { Add, Subtract };
  4.     class MULTIVERSE_API MemoryManager {
  5.  
  6.         /*
  7.         *****************************************************
  8.         *                                                   *
  9.         *        Memory Manager by RaYRoD TV (2020)         *
  10.         *       - Provides General Memory Management -      *
  11.         *                                                   *
  12.         *****************************************************
  13.         */
  14.  
  15.     public:
  16.  
  17.         // - Logs a hooked function's parameter, for a specified number of cycles o -
  18.         // Param 1: functionName (user defined)
  19.         // Param 2: argumentName (user defined)
  20.         // Param 3: numberOfCycles - The number of cycles to be run through the hook that calls this function (user defined)
  21.         // Param 4: argument - the argument to print
  22.         // Param 5: printInHexadecimal: Whether to print in hex or not
  23.         template <typename T>
  24.         static void PrintHookArgument(const char* functionName, const char* argumentName, int numberOfCycles, T argument, bool printInHexadecimal) {
  25.  
  26.             static int cycle{};
  27.             if (cycle <= numberOfCycles && printInHexadecimal)
  28.                 std::cout << argumentName << " of cycle " << cycle << " in " << functionName << " is: " << std::hex << argument << std::endl;
  29.  
  30.             if (cycle <= numberOfCycles && !printInHexadecimal)
  31.                 std::cout << argumentName << " of cycle " << cycle << " in " << functionName << " is: " << argument << std::endl;
  32.             cycle++;
  33.         }
  34.  
  35.  
  36.         // -- Resolves the address of a LEA instruction --
  37.         // Param 1: Address of LEA instruction
  38.         static uintptr_t ResolveLeaAddress(uintptr_t leaAddress);
  39.  
  40.         // -- Resolves the address of a CALL instruction --
  41.         // Param 1: Address of CALL instruction
  42.         static uintptr_t ResolveCallAddress(uintptr_t addressOfCall);
  43.  
  44.         // -- Resolves a relative address --
  45.         // Param 1: Address of relative instruction
  46.         static uintptr_t ResolveRelativeAddress(uintptr_t relativeAddress);
  47.  
  48.         // -- Returns the value of an Address --
  49.         // Param 1: Memory Address
  50.         template <typename T>
  51.         static T Dereference(T address) {
  52.             return *(T*)address;
  53.         }
  54.  
  55.         // -- Dereference a multi-level pointer -
  56.         // Param 1: basePointerAddress
  57.         // Param 2: The Offsets of the Multi-level pointer
  58.         // Param 3: The level of the Multi-level pointer
  59.         static uintptr_t DerefMultiLevelPointer(uintptr_t basePointerAddress, uintptr_t offsets[], int pointerLevel);
  60.  
  61.         // - NOP (No Operation) Bytes Internally (Useful for internal hacks) -
  62.         // Param 1: Memory Address
  63.         // Param 2: Size of bytes to NOP
  64.         static void NopInternal(char* address, unsigned int size);
  65.  
  66.         // - NOP (No Operation) Bytes Externally (Useful for external hacks) -
  67.         // Param 1: Memory Address
  68.         // Param 2: Size of bytes to NOP
  69.         static void NopExternal(char* address, unsigned int size, HANDLE processHandle);
  70.  
  71.         // -- Compare Two Memory Values against each other --
  72.         // Param 1: Memory Address
  73.         // Param 2: Offset to Accumulate with address (use 0x0 for no additional offset)
  74.         // Param 3: OffsetEnum::Add = Increment Offset | OffsetEnum::Subtract = Decrement Offset
  75.         // Param 4: Value to search for
  76.         template <typename T>
  77.         static bool CompareMemoryValue(uintptr_t address, int offset, OffsetEnum offsetEnum, T value) {
  78.  
  79.             uintptr_t addressWithOffset{};
  80.  
  81.             if (offsetEnum == OffsetEnum::Add)
  82.                 addressWithOffset = (uintptr_t)address + offset;
  83.  
  84.             if (offsetEnum == OffsetEnum::Subtract)
  85.                 addressWithOffset = (uintptr_t)address - offset;
  86.  
  87.             if (*(T*)(addressWithOffset) == (T)value)
  88.                 return true;
  89.             else
  90.                 return false;
  91.         }
  92.  
  93.         // - Clears a String in memory -
  94.         // Param 1: address - Memory Address of String
  95.         // Param 2: stringLength - (Number of Characters in String to clear)
  96.         static void ClearString(uintptr_t address, SIZE_T stringLength);
  97.  
  98.         // -- Compares Two String Values in memory against each other --
  99.         // Param 1: Memory Address of String
  100.         // Param 2: String to search for to compare
  101.         static bool StringCompare(uintptr_t address, const char* stringToCompareAgainst);
  102.  
  103.         // -- Compares Two String Values in memory against each other --
  104.         // Param 1: Memory Address of String
  105.         // Param 2: Offset to Accumulate with address (use 0x0 for no additional offset)
  106.         // Param 3: String to search for to compare
  107.         static bool StringCompare(uintptr_t address, int offset, const char* stringToCompareAgainst);
  108.  
  109.         // -- Compares Two String Values in memory against each other --
  110.         // Param 1: Memory Address of String
  111.         // Param 2: Offset to Accumulate with address (use 0x0 for no additional offset)
  112.         // Param 3: OffsetEnum::Add = Increment Offset | OffsetEnum::Subtract = Decrement Offset
  113.         // Param 4: String to search for to compare
  114.         static bool StringCompare(uintptr_t address, int offset, OffsetEnum offsetEnum, const char* stringToCompareAgainst);
  115.  
  116.         // - Gets the values of an Array & stores them in a Vector Variable -
  117.         // Param 1: MemVec_t address
  118.         template <typename T>
  119.         static Vec_t<T> DerefArrayValues(MemVec_t address) {
  120.  
  121.             Vec_t<T> result{};
  122.  
  123.             for (size_t i = 0; i < address.size(); i++)
  124.                 result.push_back(*(T*)address[i]);
  125.  
  126.                 return result;
  127.         }
  128.  
  129.         // - Gets the values of an Array & stores them in a Vector Variable -
  130.         // Param 1: MemVec_t address
  131.         // Param 2: padding size
  132.         template <typename T>
  133.         static Vec_t<T> DerefArrayValues(uintptr_t address, int paddingSize) {
  134.  
  135.             Vec_t<T> result;
  136.  
  137.             for (size_t i = 0; i < address.size(); i++) {
  138.                 result.push_back(*(T*)address[i]);
  139.                 address += paddingSize;
  140.             }
  141.             return result;
  142.  
  143.         }
  144.  
  145.         // - Dereferences the values of an 'array of Colour' & stores them in a 2D Vector Variable -
  146.         static Vec2D_t<float> DerefColourArrayValues(static MemVec_t& memVec);
  147.         static Vec2D_t<float> DerefColourArrayValues(Vec_t<MemColour_t>);
  148.  
  149.  
  150.         // - Dereferences the values of an 'array of Strings' & stores them in a Vector Variable -
  151.         static Vec_t<std::string> DerefStringArrayValues(MemVec_t memVec);
  152.  
  153.         // - Dereferences the values of a 'Vector2f' Array & stores them in a Vector Variable -
  154.         static Vec_t<Vec2f_t> DerefVector2fArrayValues(MemVec_t memVec);
  155.  
  156.         // - Dereferences the values of a 'Vector3f' Array & stores them in a Vector Variable -
  157.         static Vec_t<Vec3f_t> DerefVector3fArrayValues(MemVec_t memVec);
  158.  
  159.         // - Dereferences the values of a 'Vector4f' Array & stores them in a Vector Variable -
  160.         static Vec_t<Vec4f_t> DerefVector4fArrayValues(MemVec_t memVec);
  161.         static Vec2D_t<float> MemoryManager::DerefVector4fArrayValues(Vec_t<MemVec4f_t> variable);
  162.  
  163.  
  164.         // - Dereferences & Gets the Hex Colour value from a memory address, eg. "#B4B469FF" -
  165.         // Param 1: address - memory address of Colour
  166.         static std::string GetHexColour(uintptr_t address);
  167.  
  168.         // - Reads a string from memory -
  169.         // Param 1: Memory Address of String
  170.         static char* ReadStringCharP(uintptr_t address);
  171.  
  172.         // - Reads a string from memory -
  173.         // Param 1: Memory Address of String
  174.         static std::string ReadString(uintptr_t address);
  175.  
  176.         // -- Alters protection flags at a memory location & writes to it --
  177.         // Param 1: Memory Address
  178.         // Param 2: size of dataType
  179.         // Param 3: Value to write
  180.         template <typename T>
  181.         void WriteMemSafe(uintptr_t address, const int blockSize, T value) {
  182.  
  183.             DWORD oldProtect{};
  184.             if (VirtualProtect((LPVOID)address, blockSize, PAGE_EXECUTE_READWRITE, &oldProtect)) {
  185.                 *(uintptr_t*)(address) = value;
  186.             }
  187.         }
  188.  
  189.         // -- Alters protection flags at a memory location & writes to it --
  190.         // Param 1: Memory Address
  191.         // Param 2: the number of bytes to jump after the address of Param 1
  192.         // Param 3: size of dataType
  193.         // Param 4: Value to write
  194.         template <typename T>
  195.         void WriteMemSafe(uintptr_t address, const int offset, const int blockSize, T value) {
  196.  
  197.             DWORD oldProtect{};
  198.             if (VirtualProtect((LPVOID)address + offset, blockSize, PAGE_EXECUTE_READWRITE, &oldProtect)) {
  199.                 *(uintptr_t*)(address)+offset = value;
  200.             }
  201.         }
  202.  
  203.         // - Sets/Writes Bytes in Memory ( useful for patching instructions, etc... ) -
  204.         // Param 1: Memory Address - Starting Point
  205.         // Param 2: int/hex value to write (for patching)
  206.         // Param 3: Size of bytes to copy the value of Param 2 to
  207.         static void WriteBytes(uintptr_t address, int value, SIZE_T size);
  208.  
  209.         // - Sets/Writes Bytes in Memory ( useful for patching instructions, etc... ) -
  210.         // Param 1: Memory Address - Starting Point
  211.         // Param 2: bytes values to write (for patching) - eg. { 0xE2, 0xA7, 0x36, 0xC0 }
  212.         static void WriteBytes(uintptr_t address, vector<unsigned int> value);
  213.  
  214.         // - Sets/Writes a value to a memory address -
  215.         // Param 1: Memory Address
  216.         // Param 2: Value to set
  217.         template <typename T>
  218.         static void WriteValue(uintptr_t address, T valueToSet) {
  219.             //memory.write<T>(address, valueToSet); // External
  220.             if (address)
  221.             *(T*)address = valueToSet;
  222.         }
  223.  
  224.         // - Sets/Writes to the memory addresses of a 'Bool Array' -
  225.         // Param 1: Address - The address of the Bool Array
  226.         // Param 2: value to set - eg. { false, true, false, true }
  227.         static void WriteBoolArray(uintptr_t address, vector<bool> valueToSet);
  228.  
  229.         // - Sets/Writes to the memory addresses of an 'Int Array' -
  230.         // Param 1: Address - The address of the Int Array
  231.         // Param 2: value to set - eg. { 1, 2, 3, 4 }
  232.         static void WriteIntArray(uintptr_t address, vector<int> valueToSet);
  233.  
  234.         // - Sets/Writes to the memory addresses of a 'Float Array' -
  235.         // Param 1: Address - The address of the float Array
  236.         // Param 2: value to set - eg. { 1.f, 2.f, 3.f, 4.f }
  237.         static void WriteFloatArray(uintptr_t address, vector<float> valueToSet);
  238.  
  239.         // - Sets/Writes a string to memory -
  240.         // Param 1: Memory Address
  241.         // Param 2: String to write
  242.         static void WriteString(uintptr_t address, char* newString);
  243.  
  244.         // - Writes a 'Hex Colour' to a 'Colour' field memory address, eg. "#B4B469FF"
  245.         // Param 1: address - memory address of Colour
  246.         // Param 2: Colour to set - eg. #B0807D49
  247.         static uintptr_t WriteHexColour(uintptr_t address, std::string colourToSet);
  248.  
  249.     };
  250.     inline MemoryManager g_MemoryManager;
  251.  
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement