Skip to content

Commit

Permalink
remove some natives
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenzzer committed Feb 8, 2024
1 parent ca9e1ed commit 9799883
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 265 deletions.
202 changes: 0 additions & 202 deletions core/logic/smn_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,203 +1024,6 @@ static cell_t StoreAddressToAddress(IPluginContext *pContext, const cell_t *para
return 0;
}

template<typename T, typename U>
static inline void AddressMemcpy(T* dest, U* src, cell_t len)
{
for (; len; dest++, src++, len--)
{
*dest = *src;
}
}

static cell_t LoadBytesFromAddress(IPluginContext *pContext, const cell_t *params)
{
cell_t offset = params[4];

#ifdef PLATFORM_X86
int8_t *addr = reinterpret_cast<int8_t*>(params[1]) + offset;
#else
int8_t *addr = reinterpret_cast<int8_t*>(pseudoAddr.FromPseudoAddress(params[1])) + offset;
#endif

if (addr == NULL)
{
return pContext->ThrowNativeError("Address cannot be null");
}
else if (reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
{
return pContext->ThrowNativeError("Invalid address 0x%x is pointing to reserved memory.", addr);
}

cell_t* chunks = nullptr;
pContext->LocalToPhysAddr(params[2], &chunks);
cell_t len = params[3];
if (len <= 0)
{
return pContext->ThrowNativeError("Chunks array length must be positive!");
}

AddressMemcpy<cell_t, int8_t>(chunks, addr, len);
return 0;
}

static cell_t StoreBytesToAddress(IPluginContext *pContext, const cell_t *params)
{
cell_t offset = params[5];

#ifdef PLATFORM_X86
int8_t *addr = reinterpret_cast<int8_t*>(params[1]) + offset;
#else
int8_t *addr = reinterpret_cast<int8_t*>(pseudoAddr.FromPseudoAddress(params[1])) + offset;
#endif

if (addr == NULL)
{
return pContext->ThrowNativeError("Address cannot be null");
}
else if (reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
{
return pContext->ThrowNativeError("Invalid address 0x%x is pointing to reserved memory.", addr);
}

cell_t* chunks = nullptr;
pContext->LocalToPhysAddr(params[2], &chunks);
cell_t len = params[3];
if (len <= 0)
{
return pContext->ThrowNativeError("Chunks array length must be positive!");
}

if (params[4])
{
SourceHook::SetMemAccess(addr, len, SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
}

AddressMemcpy<int8_t, cell_t>(addr, chunks, len);
return 0;
}

static cell_t LoadSpanFromAddress(IPluginContext *pContext, const cell_t *params)
{
cell_t offset = params[5];

#ifdef PLATFORM_X86
void *addr = reinterpret_cast<void*>(reinterpret_cast<int8_t*>(params[1]) + offset);
#else
void *addr = reinterpret_cast<void*>(reinterpret_cast<int8_t*>(pseudoAddr.FromPseudoAddress(params[1])) + offset);
#endif

if (addr == NULL)
{
return pContext->ThrowNativeError("Address cannot be null");
}
else if (reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
{
return pContext->ThrowNativeError("Invalid address 0x%x is pointing to reserved memory.", addr);
}

cell_t* chunks = nullptr;
pContext->LocalToPhysAddr(params[2], &chunks);
cell_t len = params[3];
if (len <= 0)
{
return pContext->ThrowNativeError("Chunks array length must be positive!");
}

NumberType size = static_cast<NumberType>(params[4]);

switch(size)
{
case NumberType_Int8:
AddressMemcpy<cell_t, int8_t>(chunks, reinterpret_cast<int8_t*>(addr), len);
break;
case NumberType_Int16:
AddressMemcpy<cell_t, int16_t>(chunks, reinterpret_cast<int16_t*>(addr), len);
break;
case NumberType_Int32:
AddressMemcpy<cell_t, int32_t>(chunks, reinterpret_cast<int32_t*>(addr), len);
break;
default:
return pContext->ThrowNativeError("Invalid number types %d", size);
}
return 0;
}

static cell_t StoreSpanToAddress(IPluginContext *pContext, const cell_t *params)
{
cell_t offset = params[6];

#ifdef PLATFORM_X86
void *addr = reinterpret_cast<void*>(reinterpret_cast<int8_t*>(params[1]) + offset);
#else
void *addr = reinterpret_cast<void*>(reinterpret_cast<int8_t*>(pseudoAddr.FromPseudoAddress(params[1])) + offset);
#endif

if (addr == NULL)
{
return pContext->ThrowNativeError("Address cannot be null");
}
else if (reinterpret_cast<uintptr_t>(addr) < VALID_MINIMUM_MEMORY_ADDRESS)
{
return pContext->ThrowNativeError("Invalid address 0x%x is pointing to reserved memory.", addr);
}

cell_t* chunks = nullptr;
pContext->LocalToPhysAddr(params[2], &chunks);
cell_t len = params[3];
if (len <= 0)
{
return pContext->ThrowNativeError("Chunks array length must be positive!");
}

NumberType size = static_cast<NumberType>(params[4]);

switch(size)
{
case NumberType_Int8:
if (params[5])
{
SourceHook::SetMemAccess(addr, len * sizeof(int8_t), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
}
AddressMemcpy<int8_t, cell_t>(reinterpret_cast<int8_t*>(addr), chunks, len);
break;
case NumberType_Int16:
if (params[5])
{
SourceHook::SetMemAccess(addr, len * sizeof(int16_t), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
}
AddressMemcpy<int16_t, cell_t>(reinterpret_cast<int16_t*>(addr), chunks, len);
break;
case NumberType_Int32:
if (params[5])
{
SourceHook::SetMemAccess(addr, len * sizeof(int32_t), SH_MEM_READ|SH_MEM_WRITE|SH_MEM_EXEC);
}
AddressMemcpy<int32_t, cell_t>(reinterpret_cast<int32_t*>(addr), chunks, len);
break;
default:
return pContext->ThrowNativeError("Invalid number types %d", size);
}

return 0;
}

static cell_t OffsetAddress(IPluginContext *pContext, const cell_t *params)
{
#ifdef PLATFORM_X86
int8_t *addr = reinterpret_cast<int8_t*>(params[1]);
#else
int8_t *addr = reinterpret_cast<int8_t*>(pseudoAddr.FromPseudoAddress(params[1]));
#endif

cell_t offset = params[2];
#ifdef PLATFORM_X86
return reinterpret_cast<cell_t>(addr + offset);
#else
return pseudoAddr.ToPseudoAddress(addr + offset);
#endif
}

static cell_t IsNullVector(IPluginContext *pContext, const cell_t *params)
{
cell_t *pNullVec = pContext->GetNullRef(SP_NULL_VECTOR);
Expand Down Expand Up @@ -1430,11 +1233,6 @@ REGISTER_NATIVES(coreNatives)
{"StoreToAddress", StoreToAddress},
{"LoadAddressFromAddress", LoadAddressFromAddress},
{"StoreAddressToAddress", StoreAddressToAddress},
{"LoadBytesFromAddress ", LoadBytesFromAddress},
{"StoreBytesToAddress", StoreBytesToAddress},
{"LoadSpanFromAddress ", LoadSpanFromAddress},
{"StoreSpanToAddress", StoreSpanToAddress},
{"OffsetAddress", OffsetAddress},
{"IsNullVector", IsNullVector},
{"IsNullString", IsNullString},
{"LogStackTrace", LogStackTrace},
Expand Down
67 changes: 4 additions & 63 deletions plugins/include/sourcemod.inc
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ enum Address
* @param addr Address to a memory location.
* @param size How many bytes should be read.
* If loading a floating-point value, use NumberType_Int32.
* @param offset Amount of bytes the addr parameter needs to be offsetted by.
* @param offset Amount of bytes the base address needs to be offsetted by before loading.
* @return The value that is stored at that address.
* @error Address is null or pointing to reserved memory.
*/
Expand All @@ -751,7 +751,7 @@ native any LoadFromAddress(Address addr, NumberType size, int offset = 0);
* If storing a floating-point value, use NumberType_Int32.
* @param updateMemAccess If true, SourceMod will set read / write / exec permissions
* on the memory page being written to.
* @param offset Amount of bytes the addr parameter needs to be offsetted by.
* @param offset Amount of bytes the base address needs to be offsetted by before storing.
* @error Address is null or pointing to reserved memory.
*/
native void StoreToAddress(Address addr, any data, NumberType size, bool updateMemAccess = true, int offset = 0);
Expand All @@ -760,8 +760,8 @@ native void StoreToAddress(Address addr, any data, NumberType size, bool updateM
* Load an address value from a memory address.
*
* @param addr Address to a memory location.
* @param offset Amount of bytes the base address needs to be offsetted by before loading.
* @return The address value that is stored at that address.
* @param offset Amount of bytes the addr parameter needs to be offsetted by.
* @error Address is null or pointing to reserved memory.
*/
native Address LoadAddressFromAddress(Address addr, int offset = 0);
Expand All @@ -773,70 +773,11 @@ native Address LoadAddressFromAddress(Address addr, int offset = 0);
* @param data Address value to store at the address.
* @param updateMemAccess If true, SourceMod will set read / write / exec permissions
* on the memory page being written to.
* @param offset Amount of bytes the addr parameter needs to be offsetted by.
* @param offset Amount of bytes the base address needs to be offsetted by before storing.
* @error Address is null or pointing to reserved memory.
*/
native void StoreAddressToAddress(Address addr, Address data, bool updateMemAccess = true, int offset = 0);

/**
* Load up to (len) bytes from a memory address.
*
* @param addr Address to a memory location.
* @param bytes Array bytes to load.
* @param len How many bytes should be read.
* @param offset Amount of bytes the addr parameter needs to be offsetted by.
* @error Address is null or pointing to reserved memory. Bytes array length is null or negative.
*/
native void LoadBytesFromAddress(Address addr, char[] bytes, int len, int offset = 0);

/**
* Store up to (len) bytes to a memory address.
*
* @param addr Address to a memory location.
* @param bytes Array of bytes to store.
* @param len How many bytes should be written.
* @param updateMemAccess If true, SourceMod will set read / write / exec permissions
* on the memory page being written to.
* @param offset Amount of bytes the addr parameter needs to be offsetted by.
* @error Address is null or pointing to reserved memory. Bytes array length is null or negative.
*/
native void StoreBytesToAddress(Address addr, char[] bytes, int len, bool updateMemAccess = true, int offset = 0);

/**
* Load a sized span from a memory address.
*
* @param addr Address to a memory location.
* @param span Array of NumberType sized values that constitute the span.
* @param len Size of the span.
* @param size Size of each element.
* @param offset Amount of bytes the addr parameter needs to be offsetted by.
* @error Address is null or pointing to reserved memory. Span length is null or negative.
*/
native void LoadSpanFromAddress(Address addr, any[] span, int len, NumberType size, int offset = 0);

/**
* Store a given sized span to the given address.
*
* @param addr Address to a memory location.
* @param span Array of NumberType sized values that constitute the span.
* @param len Size of the span.
* @param size Size of each element.
* @param updateMemAccess If true, SourceMod will set read / write / exec permissions
* on the memory page being written to.
* @param offset Amount of bytes the addr parameter needs to be offsetted by.
* @error Address is null or pointing to reserved memory. Span length is null or negative.
*/
native void StoreSpanToAddress(Address addr, any[] span, int len, NumberType size, bool updateMemAccess = true, int offset = 0);

/**
* Offsets the given memory address.
*
* @param address Address to a memory location.
* @param offset How many bytes we should offset the address parameter by.
* @return The offsetted memory address.
*/
native Address OffsetAddress(Address addr, int offset);

methodmap FrameIterator < Handle {
// Creates a stack frame iterator to build your own stack traces.
// @return New handle to a FrameIterator.
Expand Down

0 comments on commit 9799883

Please sign in to comment.