Skip to content

Commit

Permalink
Return array type info with FindSendPropInfo (#1548)
Browse files Browse the repository at this point in the history
  • Loading branch information
asherkin committed Aug 1, 2021
1 parent c691729 commit 296deb9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
17 changes: 11 additions & 6 deletions core/HalfLife2.cpp
Expand Up @@ -320,15 +320,20 @@ bool UTIL_FindInSendTable(SendTable *pTable,
sm_sendprop_info_t *info,
unsigned int offset)
{
const char *pname;
int props = pTable->GetNumProps();
SendProp *prop;

for (int i=0; i<props; i++)
for (int i = 0; i < props; i++)
{
prop = pTable->GetProp(i);
pname = prop->GetName();
SendProp *prop = pTable->GetProp(i);

// Skip InsideArray props (SendPropArray / SendPropArray2),
// we'll find them later by their containing array.
if (prop->IsInsideArray()) {
continue;
}

const char *pname = prop->GetName();
SendTable *pInnerTable = prop->GetDataTable();

if (pname && strcmp(name, pname) == 0)
{
// get true offset of CUtlVector
Expand Down
52 changes: 47 additions & 5 deletions core/smn_entities.cpp
Expand Up @@ -825,7 +825,7 @@ static cell_t FindSendPropInfo(IPluginContext *pContext, const cell_t *params)
{
char *cls, *prop;
sm_sendprop_info_t info;
cell_t *pType, *pBits, *pLocal;
cell_t *pType, *pBits, *pLocal, *pArraySize;

pContext->LocalToString(params[1], &cls);
pContext->LocalToString(params[2], &prop);
Expand All @@ -839,7 +839,45 @@ static cell_t FindSendPropInfo(IPluginContext *pContext, const cell_t *params)
pContext->LocalToPhysAddr(params[4], &pBits);
pContext->LocalToPhysAddr(params[5], &pLocal);

switch (info.prop->GetType())
if (params[0] >= 6) {
pContext->LocalToPhysAddr(params[6], &pArraySize);
*pArraySize = 0;
}

SendProp *pProp = info.prop;
unsigned int actual_offset = info.actual_offset;

// SendPropArray / SendPropArray2
if (pProp->GetType() == DPT_Array && pProp->GetArrayProp())
{
if (pArraySize) {
// This'll only work for SendPropArray
*pArraySize = pProp->GetNumElements();
}

// Use the type / bits / local offset of the real data prop
pProp = pProp->GetArrayProp();

// This is sane as the DPT_Array prop's local offset is always 0
actual_offset += pProp->GetOffset();
}

// Get the local offset now before we might dive into another table
*pLocal = pProp->GetOffset();

// SendPropArray3
SendTable *pTable = pProp->GetDataTable();
if (pProp->GetType() == DPT_DataTable && pTable && pTable->GetNumProps() > 0)
{
if (pArraySize) {
*pArraySize = pTable->GetNumProps();
}

// Use the type / bits of the first data prop
pProp = pTable->GetProp(0);
}

switch (pProp->GetType())
{
case DPT_Int:
{
Expand Down Expand Up @@ -868,10 +906,9 @@ static cell_t FindSendPropInfo(IPluginContext *pContext, const cell_t *params)
}
}

*pBits = info.prop->m_nBits;
*pLocal = info.prop->GetOffset();
*pBits = pProp->m_nBits;

return info.actual_offset;
return actual_offset;
}

static void GuessDataPropTypes(typedescription_t *td, cell_t * pSize, cell_t * pType)
Expand Down Expand Up @@ -1297,6 +1334,11 @@ static cell_t GetEntPropArraySize(IPluginContext *pContext, const cell_t *params
((class_name) ? class_name : ""));
}

if (info.prop->GetType() == DPT_Array)
{
return info.prop->GetNumElements();
}

if (info.prop->GetType() != DPT_DataTable)
{
return 0;
Expand Down
6 changes: 4 additions & 2 deletions plugins/include/entity.inc
Expand Up @@ -428,6 +428,7 @@ native int FindSendPropOffs(const char[] cls, const char[] prop);
* for strings.
* @param local_offset Optional parameter to store the local offset, as
* FindSendPropOffs() would return.
* @param array_size Optional parameter to store array size, 0 if not an array.
* @return On success, returns an absolutely computed offset.
* If no offset is available, 0 is returned.
* If the property is not found, -1 is returned.
Expand All @@ -436,7 +437,8 @@ native int FindSendPropInfo(const char[] cls,
const char[] prop,
PropFieldType &type=view_as<PropFieldType>(0),
int &num_bits=0,
int &local_offset=0);
int &local_offset=0,
int &array_size=0);

/**
* Given an entity, finds a datamap property offset.
Expand Down Expand Up @@ -700,7 +702,7 @@ native int SetEntPropString(int entity, PropType type, const char[] prop, const
* @param entity Entity/edict index.
* @param type Property type.
* @param prop Property name.
* @return Size of array (in elements) or 1 if property is not an array.
* @return Size of array (in elements) or 0 if property is not an array.
* @error Invalid entity or property not found.
*/
native int GetEntPropArraySize(int entity, PropType type, const char[] prop);
Expand Down

0 comments on commit 296deb9

Please sign in to comment.