Skip to content

Commit

Permalink
complete the natives
Browse files Browse the repository at this point in the history
  • Loading branch information
Kenzzer committed Jul 27, 2024
1 parent 3310259 commit b02445b
Show file tree
Hide file tree
Showing 11 changed files with 427 additions and 6 deletions.
5 changes: 5 additions & 0 deletions core/logic/MemoryPointer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ MemoryPointer::~MemoryPointer()
}
}

void MemoryPointer::Delete()
{
delete this;
}

void* MemoryPointer::Get()
{
return m_ptr;
Expand Down
1 change: 1 addition & 0 deletions core/logic/MemoryPointer.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class MemoryPointer : public SourceMod::IMemoryPointer

// SourceMod::IMemoryPointer
virtual ~MemoryPointer();
virtual void Delete() override;
virtual void* Get() override;
virtual cell_t GetSize() override;
protected:
Expand Down
83 changes: 82 additions & 1 deletion core/logic/smn_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class CoreNativeHelpers :
{
if (type == g_MemoryPtr)
{
delete (IMemoryPointer *) object;
((IMemoryPointer *)object)->Delete();
}
else if (type == g_FrameIter)
{
Expand Down Expand Up @@ -1068,6 +1068,84 @@ static cell_t MemoryPointer_Load(IPluginContext *pContext, const cell_t *params)
return ptr->Load(bytesSize, params[3]);
}

static cell_t MemoryPointer_StoreMemoryPointer(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IMemoryPointer *ptr;

HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
sec.pOwner = pContext->GetIdentity();

if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&ptr)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

hndl = (Handle_t)params[2];
IMemoryPointer *store;
if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&store)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

ptr->StorePtr(store->Get(), params[3], params[4] != 0);
}

static cell_t MemoryPointer_LoadMemoryPointer(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IMemoryPointer *ptr;

HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
sec.pOwner = pContext->GetIdentity();

if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&ptr)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

auto newPtr = new MemoryPointer(ptr->LoadPtr(params[2]), 0);

Handle_t newHandle = handlesys->CreateHandle(g_MemoryPtr, newPtr, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (newHandle == BAD_HANDLE)
{
delete newPtr;
return BAD_HANDLE;
}

return newHandle;
}

static cell_t MemoryPointer_Offset(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IMemoryPointer *ptr;

HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
sec.pOwner = pContext->GetIdentity();

if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&ptr)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

auto newPtr = new MemoryPointer((void*)(((intptr_t)ptr->Get()) + params[2]), 0);
Handle_t newHandle = handlesys->CreateHandle(g_MemoryPtr, newPtr, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (newHandle == BAD_HANDLE)
{
delete newPtr;
return BAD_HANDLE;
}

return newHandle;
}

static cell_t FrameIterator_Create(IPluginContext *pContext, const cell_t *params)
{
IFrameIterator *it = pContext->CreateFrameIterator();
Expand Down Expand Up @@ -1261,6 +1339,9 @@ REGISTER_NATIVES(coreNatives)
{"MemoryPointer.MemoryPointer", MemoryPointer_Create},
{"MemoryPointer.Store", MemoryPointer_Store},
{"MemoryPointer.Load", MemoryPointer_Load},
{"MemoryPointer.StoreMemoryPointer", MemoryPointer_StoreMemoryPointer},
{"MemoryPointer.LoadMemoryPointer", MemoryPointer_LoadMemoryPointer},
{"MemoryPointer.Offset", MemoryPointer_Offset},

{"FrameIterator.FrameIterator", FrameIterator_Create},
{"FrameIterator.Next", FrameIterator_Next},
Expand Down
77 changes: 77 additions & 0 deletions core/logic/smn_gameconfigs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

#include "common_logic.h"
#include <IHandleSys.h>
#include "MemoryPointer.h"
#include "GameConfigs.h"

HandleType_t g_GameConfigsType;
Expand Down Expand Up @@ -194,6 +195,78 @@ static cell_t smn_GameConfGetMemSig(IPluginContext *pCtx, const cell_t *params)
#endif
}

extern HandleType_t g_MemoryPtr;

static cell_t smn_GameConfGetAddressEx(IPluginContext *pCtx, const cell_t *params)
{
Handle_t hndl = static_cast<Handle_t>(params[1]);
HandleError herr;
HandleSecurity sec;
IGameConfig *gc;

sec.pOwner = NULL;
sec.pIdentity = g_pCoreIdent;

if ((herr=handlesys->ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
!= HandleError_None)
{
return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
}

char *key;
void* val;
pCtx->LocalToString(params[2], &key);

if (!gc->GetAddress(key, &val) || val == nullptr)
return BAD_HANDLE;

auto newPtr = new MemoryPointer(val, 0);
Handle_t newHandle = handlesys->CreateHandle(g_MemoryPtr, newPtr, pCtx->GetIdentity(), g_pCoreIdent, NULL);
if (newHandle == BAD_HANDLE)
{
delete newPtr;
return BAD_HANDLE;
}

return newHandle;
}

static cell_t smn_GameConfGetMemSigEx(IPluginContext *pCtx, const cell_t *params)
{
Handle_t hndl = static_cast<Handle_t>(params[1]);
HandleError herr;
HandleSecurity sec;
IGameConfig *gc;

sec.pOwner = NULL;
sec.pIdentity = g_pCoreIdent;

if ((herr=handlesys->ReadHandle(hndl, g_GameConfigsType, &sec, (void **)&gc))
!= HandleError_None)
{
return pCtx->ThrowNativeError("Invalid game config handle %x (error %d)", hndl, herr);
}

char *key;
void *val;
pCtx->LocalToString(params[2], &key);

if (!gc->GetMemSig(key, &val) || val == nullptr)
{
return BAD_HANDLE;
}

auto newPtr = new MemoryPointer(val, 0);
Handle_t newHandle = handlesys->CreateHandle(g_MemoryPtr, newPtr, pCtx->GetIdentity(), g_pCoreIdent, NULL);
if (newHandle == BAD_HANDLE)
{
delete newPtr;
return BAD_HANDLE;
}

return newHandle;
}

static GameConfigsNatives s_GameConfigsNatives;

REGISTER_NATIVES(gameconfignatives)
Expand All @@ -207,6 +280,10 @@ REGISTER_NATIVES(gameconfignatives)
{"GameData.GameData", smn_LoadGameConfigFile},
{"GameData.GetOffset", smn_GameConfGetOffset},
{"GameData.GetKeyValue", smn_GameConfGetKeyValue},
{"GameData.GetAddressEx", smn_GameConfGetAddressEx},
{"GameData.GetMemSigEx", smn_GameConfGetMemSigEx},

// Deprecated
{"GameData.GetAddress", smn_GameConfGetAddress},
{"GameData.GetMemSig", smn_GameConfGetMemSig},
{NULL, NULL}
Expand Down
123 changes: 123 additions & 0 deletions core/smn_entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "PlayerManager.h"
#include "HalfLife2.h"
#include <IGameConfigs.h>
#include <IMemoryPointer.h>
#include "sm_stringutil.h"
#include "logic_bridge.h"

Expand Down Expand Up @@ -76,6 +77,44 @@
// Not defined in the sdk as we have no clue what it is
#define FL_EP2V_UNKNOWN (1 << 2)

HandleType_t g_MemoryPtr = 0;

class SimpleMemoryPointer : IMemoryPointer
{
public:
SimpleMemoryPointer(void* ptr) : m_ptr(ptr)
{
}

virtual void Delete()
{
delete this;
}

virtual cell_t GetSize() override
{
return 0;
}

virtual void* Get() override
{
return m_ptr;
}
protected:
void* m_ptr;
};

class EntitiesHelpers :
public SMGlobalClass
{
public:
virtual void OnSourceModAllInitialized_Post()
{
// This should never fail
handlesys->FindHandleType("MemoryPointer", &g_MemoryPtr);
}
} s_ConsoleHelpers;

enum PropType
{
Prop_Send = 0,
Expand Down Expand Up @@ -2779,6 +2818,87 @@ static cell_t GetEntityAddress(IPluginContext *pContext, const cell_t *params)
#endif
}

static cell_t GetEntityMemoryPointer(IPluginContext *pContext, const cell_t *params)
{
CBaseEntity * pEntity = GetEntity(params[1]);
if (!pEntity)
{
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[1]), params[1]);
}


auto newPtr = new SimpleMemoryPointer(pEntity);
Handle_t newHandle = handlesys->CreateHandle(g_MemoryPtr, newPtr, pContext->GetIdentity(), g_pCoreIdent, NULL);
if (newHandle == BAD_HANDLE)
{
delete newPtr;
return BAD_HANDLE;
}
return newHandle;
}

static cell_t MemoryPointer_StoreEntityToHandle(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IMemoryPointer *ptr;

HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
sec.pOwner = pContext->GetIdentity();

if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&ptr)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

CBaseHandle &entityHandle = *reinterpret_cast<CBaseHandle*>(((intptr_t)ptr->Get()) + params[3]);

if ((unsigned)params[2] == INVALID_EHANDLE_INDEX)
{
entityHandle.Set(NULL);
}
else
{
CBaseEntity *pOther = GetEntity(params[2]);

if (!pOther)
{
return pContext->ThrowNativeError("Entity %d (%d) is invalid", g_HL2.ReferenceToIndex(params[2]), params[2]);
}

IHandleEntity *pHandleEnt = (IHandleEntity *)pOther;
entityHandle.Set(pHandleEnt);
}
return 0;
}

static cell_t MemoryPointer_LoadEntityFromHandle(IPluginContext *pContext, const cell_t *params)
{
Handle_t hndl = (Handle_t)params[1];
HandleError err;
IMemoryPointer *ptr;

HandleSecurity sec;
sec.pIdentity = g_pCoreIdent;
sec.pOwner = pContext->GetIdentity();

if ((err=handlesys->ReadHandle(hndl, g_MemoryPtr, &sec, (void **)&ptr)) != HandleError_None)
{
return pContext->ThrowNativeError("Could not read Handle %x (error %d)", hndl, err);
}

CBaseHandle &entityHandle = *reinterpret_cast<CBaseHandle*>(((intptr_t)ptr->Get()) + params[2]);
CBaseEntity *pHandleEntity = g_HL2.ReferenceToEntity(entityHandle.GetEntryIndex());

if (!pHandleEntity || entityHandle != reinterpret_cast<IHandleEntity *>(pHandleEntity)->GetRefEHandle())
{
return -1;
}

return g_HL2.EntityToBCompatRef(pHandleEntity);
}

REGISTER_NATIVES(entityNatives)
{
{"ChangeEdictState", ChangeEdictState},
Expand Down Expand Up @@ -2823,8 +2943,11 @@ REGISTER_NATIVES(entityNatives)
{"SetEntPropString", SetEntPropString},
{"SetEntPropVector", SetEntPropVector},
{"GetEntityAddress", GetEntityAddress},
{"GetEntityMemoryPointer", GetEntityMemoryPointer},
{"FindDataMapInfo", FindDataMapInfo},
{"LoadEntityFromHandleAddress", LoadEntityFromHandleAddress},
{"StoreEntityToHandleAddress", StoreEntityToHandleAddress},
{"MemoryPointer.StoreEntityToHandle", MemoryPointer_StoreEntityToHandle},
{"MemoryPointer.LoadEntityFromHandle", MemoryPointer_LoadEntityFromHandle},
{NULL, NULL}
};
Loading

0 comments on commit b02445b

Please sign in to comment.