amxmodx/dlls/MemHack/MemWrite.cpp
Twilight Suzuka ab5c11f876 Finally done!!! I finished what DS started:
AMXx Mods may now patch/retreive memory of several varieties directly from the game .dll, mod .dll, or can go free and patch/retreive any peice of memory.

I salute you DS.
2006-01-12 03:55:49 +00:00

79 lines
2.7 KiB
C++

#include "MemMisc.h"
/* Functions that patch different data types in memory */
template <typename Type>
int UTIL_PatchMemory(maddress baseaddress, maddress address, Type patch, char memType, size_t byteType, bool extraProtect)
{
unsigned long oldProtect = 0;
maddress realAddress = GetRealMemoryAddress(baseaddress, address, memType);
switch (memType)
{
case MEMTYPE_CODE:
if (MemoryProtect((void*)realAddress, byteType, MPROT_CODE_EDIT, &oldProtect, memType) == MP_FAIL) return MP_FAIL;
break;
case MEMTYPE_RODATA:
if (MemoryProtect((void*)realAddress, byteType, MPROT_RODATA_EDIT, &oldProtect, memType) == MP_FAIL) return MP_FAIL;
break;
}
*(Type*)realAddress = patch;
if (memType == MEMTYPE_CODE)
{
MemoryProtect((void*)realAddress, byteType, oldProtect, &oldProtect);
}
else if(extraProtect == true)
{
if(memType == MEMTYPE_RODATA) MemoryProtect((void*)realAddress, byteType, oldProtect, &oldProtect);
}
return MP_OK;
}
int UTIL_PatchMemory_Byte(maddress baseaddress, maddress address, char patch, char memType)
{
return UTIL_PatchMemory(baseaddress, address, (char)(NULL), memType, BYTE_BYTES, true);
}
int UTIL_PatchMemory_Word(maddress baseaddress, maddress address, short patch, char memType)
{
return UTIL_PatchMemory(baseaddress, address, (short)(NULL), memType, WORD_BYTES, true);
}
int UTIL_PatchMemory_Dword(maddress baseaddress, maddress address, int32_t patch, char memType)
{
return UTIL_PatchMemory(baseaddress, address, (int32_t)(NULL), memType, DWORD_BYTES, true);
}
int UTIL_PatchMemory_Qword(maddress baseaddress, maddress address, long long patch, char memType)
{
return UTIL_PatchMemory(baseaddress, address, (long long)(NULL), memType, QWORD_BYTES, false);
}
int UTIL_PatchMemory_Float(maddress baseaddress, maddress address, float patch, char memType)
{
return UTIL_PatchMemory(baseaddress, address, (float)(NULL), memType, FLOAT_BYTES, false);
}
int UTIL_PatchMemory_UnsignedByte(maddress baseaddress, maddress address, unsigned char patch, char memType)
{
return UTIL_PatchMemory(baseaddress, address, (unsigned char)(NULL), memType, BYTE_BYTES, false);
}
int UTIL_PatchMemory_UnsignedWord(maddress baseaddress, maddress address, unsigned short patch, char memType)
{
return UTIL_PatchMemory(baseaddress, address, (unsigned short)(NULL), memType, WORD_BYTES, false);
}
int UTIL_PatchMemory_UnsignedDword(maddress baseaddress, maddress address, uint32_t patch, char memType)
{
return UTIL_PatchMemory(baseaddress, address, (uint32_t)(NULL), memType, DWORD_BYTES, false);
}
int UTIL_PatchMemory_Pointer(maddress baseaddress, maddress address, maddress patch, char memType)
{
return UTIL_PatchMemory(baseaddress, address, (maddress)(NULL), memType, DWORD_BYTES, true);
}