Add the ability to work with the |this| parameter under dhooks callback (#2219)
Some checks failed
Continuous Integration / ${{ matrix.os_short }}-${{ matrix.compiler_cc }} (clang, clang++, ubuntu-latest, linux) (push) Waiting to run
Continuous Integration / ${{ matrix.os_short }}-${{ matrix.compiler_cc }} (clang-14, clang++-14, ubuntu-22.04, linux) (push) Waiting to run
Continuous Integration / ${{ matrix.os_short }}-${{ matrix.compiler_cc }} (msvc, windows-latest, win) (push) Waiting to run
hl2sdk-mock tests / mock (push) Waiting to run
SourcePawn scripting / build (ubuntu-latest, linux) (push) Has been cancelled
SourcePawn scripting / build (windows-latest, win) (push) Has been cancelled

This commit is contained in:
A1m` 2025-11-06 22:33:41 +07:00 committed by GitHub
parent 66d3f5e60e
commit 98831a8667
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 74 additions and 100 deletions

View File

@ -532,6 +532,7 @@ CDynamicHooksSourcePawn::CDynamicHooksSourcePawn(HookSetup *setup, CHook *pDetou
this->hookType = setup->hookType; this->hookType = setup->hookType;
this->m_pDetour = pDetour; this->m_pDetour = pDetour;
this->callConv = setup->callConv; this->callConv = setup->callConv;
this->thisFuncCallConv = setup->callConv;
} }
HookReturnStruct *CDynamicHooksSourcePawn::GetReturnStruct() HookReturnStruct *CDynamicHooksSourcePawn::GetReturnStruct()

View File

@ -42,6 +42,8 @@ enum SDKFuncConfSource
SDKConf_Address SDKConf_Address
}; };
using ParamVector = SourceHook::CVector<ParamInfo>;
bool GetHandleIfValidOrError(HandleType_t type, void **object, IPluginContext *pContext, cell_t param) bool GetHandleIfValidOrError(HandleType_t type, void **object, IPluginContext *pContext, cell_t param)
{ {
if(param == BAD_HANDLE) if(param == BAD_HANDLE)
@ -81,6 +83,56 @@ bool GetCallbackArgHandleIfValidOrError(HandleType_t type, HandleType_t otherTyp
return true; return true;
} }
bool GetObjectAddrOrThis(IPluginContext *pContext, const cell_t *params, void *&retAddr)
{
HookParamsStruct *paramStruct = NULL;
retAddr = NULL;
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)&paramStruct, pContext, params[1]))
{
return false;
}
if(params[2] != 0)
{
const ParamVector &paramsVec = paramStruct->dg->params;
if(params[2] < 0 || params[2] > static_cast<int>(paramsVec.size()))
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramsVec.size());
}
int index = params[2] - 1;
const ParamInfo &param = paramsVec.at(index);
if(param.type != HookParamType_ObjectPtr && param.type != HookParamType_Object)
{
return pContext->ThrowNativeError("Invalid object value type %i", param.type);
}
size_t offset = GetParamOffset(paramStruct, index);
retAddr = GetObjectAddr(param.type, param.flags, paramStruct->orgParams, offset);
return true;
}
const DHooksInfo* dgInfo = paramStruct->dg;
if(dgInfo->thisFuncCallConv != CallConv_THISCALL)
{
return pContext->ThrowNativeError("Parameter 'this' is only available in member functions");
}
if(dgInfo->thisType != ThisPointer_Address
&& dgInfo->thisType != ThisPointer_CBaseEntity
&& dgInfo->hookType != HookType_GameRules)
{
return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available");
}
retAddr = g_SHPtr->GetIfacePtr();
return true;
}
IPluginFunction *GetCallback(IPluginContext *pContext, HookSetup * setup, const cell_t *params, cell_t callback_index) IPluginFunction *GetCallback(IPluginContext *pContext, HookSetup * setup, const cell_t *params, cell_t callback_index)
{ {
IPluginFunction *ret = NULL; IPluginFunction *ret = NULL;
@ -1089,28 +1141,12 @@ cell_t Native_RemoveEntityListener(IPluginContext *pContext, const cell_t *param
//native any:DHookGetParamObjectPtrVar(Handle:hParams, num, offset, ObjectValueType:type); //native any:DHookGetParamObjectPtrVar(Handle:hParams, num, offset, ObjectValueType:type);
cell_t Native_GetParamObjectPtrVar(IPluginContext *pContext, const cell_t *params) cell_t Native_GetParamObjectPtrVar(IPluginContext *pContext, const cell_t *params)
{ {
HookParamsStruct *paramStruct; void *addr = NULL;
if(!GetObjectAddrOrThis(pContext, params, addr))
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)&paramStruct, pContext, params[1]))
{ {
return 0; return 0;
} }
if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
int index = params[2] - 1;
if(paramStruct->dg->params.at(index).type != HookParamType_ObjectPtr && paramStruct->dg->params.at(index).type != HookParamType_Object)
{
return pContext->ThrowNativeError("Invalid object value type %i", paramStruct->dg->params.at(index).type);
}
size_t offset = GetParamOffset(paramStruct, index);
void *addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset);
switch((ObjectValueType)params[4]) switch((ObjectValueType)params[4])
{ {
case ObjectValueType_Int: case ObjectValueType_Int:
@ -1160,28 +1196,12 @@ cell_t Native_GetParamObjectPtrVar(IPluginContext *pContext, const cell_t *param
//native DHookSetParamObjectPtrVar(Handle:hParams, num, offset, ObjectValueType:type, value) //native DHookSetParamObjectPtrVar(Handle:hParams, num, offset, ObjectValueType:type, value)
cell_t Native_SetParamObjectPtrVar(IPluginContext *pContext, const cell_t *params) cell_t Native_SetParamObjectPtrVar(IPluginContext *pContext, const cell_t *params)
{ {
HookParamsStruct *paramStruct; void *addr = NULL;
if(!GetObjectAddrOrThis(pContext, params, addr))
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)&paramStruct, pContext, params[1]))
{ {
return 0; return 0;
} }
if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
int index = params[2] - 1;
if(paramStruct->dg->params.at(index).type != HookParamType_ObjectPtr && paramStruct->dg->params.at(index).type != HookParamType_Object)
{
return pContext->ThrowNativeError("Invalid object value type %i", paramStruct->dg->params.at(index).type);
}
size_t offset = GetParamOffset(paramStruct, index);
void *addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset);
switch((ObjectValueType)params[4]) switch((ObjectValueType)params[4])
{ {
case ObjectValueType_Int: case ObjectValueType_Int:
@ -1245,28 +1265,12 @@ cell_t Native_SetParamObjectPtrVar(IPluginContext *pContext, const cell_t *param
//native DHookGetParamObjectPtrVarVector(Handle:hParams, num, offset, ObjectValueType:type, Float:buffer[3]); //native DHookGetParamObjectPtrVarVector(Handle:hParams, num, offset, ObjectValueType:type, Float:buffer[3]);
cell_t Native_GetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t *params) cell_t Native_GetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t *params)
{ {
HookParamsStruct *paramStruct; void *addr = NULL;
if(!GetObjectAddrOrThis(pContext, params, addr))
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)&paramStruct, pContext, params[1]))
{ {
return 0; return 0;
} }
if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
int index = params[2] - 1;
if(paramStruct->dg->params.at(index).type != HookParamType_ObjectPtr && paramStruct->dg->params.at(index).type != HookParamType_Object)
{
return pContext->ThrowNativeError("Invalid object value type %i", paramStruct->dg->params.at(index).type);
}
size_t offset = GetParamOffset(paramStruct, index);
void *addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset);
cell_t *buffer; cell_t *buffer;
pContext->LocalToPhysAddr(params[5], &buffer); pContext->LocalToPhysAddr(params[5], &buffer);
@ -1299,28 +1303,12 @@ cell_t Native_GetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t
//native DHookSetParamObjectPtrVarVector(Handle:hParams, num, offset, ObjectValueType:type, Float:value[3]); //native DHookSetParamObjectPtrVarVector(Handle:hParams, num, offset, ObjectValueType:type, Float:value[3]);
cell_t Native_SetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t *params) cell_t Native_SetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t *params)
{ {
HookParamsStruct *paramStruct; void *addr = NULL;
if(!GetObjectAddrOrThis(pContext, params, addr))
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)&paramStruct, pContext, params[1]))
{ {
return 0; return 0;
} }
if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
int index = params[2] - 1;
if(paramStruct->dg->params.at(index).type != HookParamType_ObjectPtr && paramStruct->dg->params.at(index).type != HookParamType_Object)
{
return pContext->ThrowNativeError("Invalid object value type %i", paramStruct->dg->params.at(index).type);
}
size_t offset = GetParamOffset(paramStruct, index);
void *addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset);
cell_t *buffer; cell_t *buffer;
pContext->LocalToPhysAddr(params[5], &buffer); pContext->LocalToPhysAddr(params[5], &buffer);
@ -1352,28 +1340,12 @@ cell_t Native_SetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t
//native DHookGetParamObjectPtrString(Handle:hParams, num, offset, ObjectValueType:type, String:buffer[], size) //native DHookGetParamObjectPtrString(Handle:hParams, num, offset, ObjectValueType:type, String:buffer[], size)
cell_t Native_GetParamObjectPtrString(IPluginContext *pContext, const cell_t *params) cell_t Native_GetParamObjectPtrString(IPluginContext *pContext, const cell_t *params)
{ {
HookParamsStruct *paramStruct; void *addr = NULL;
if (!GetObjectAddrOrThis(pContext, params, addr))
if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)&paramStruct, pContext, params[1]))
{ {
return 0; return 0;
} }
if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
int index = params[2] - 1;
if(paramStruct->dg->params.at(index).type != HookParamType_ObjectPtr && paramStruct->dg->params.at(index).type != HookParamType_Object)
{
return pContext->ThrowNativeError("Invalid object value type %i", paramStruct->dg->params.at(index).type);
}
size_t offset = GetParamOffset(paramStruct, index);
void *addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset);
switch((ObjectValueType)params[4]) switch((ObjectValueType)params[4])
{ {
case ObjectValueType_CharPtr: case ObjectValueType_CharPtr:

View File

@ -162,6 +162,7 @@ public:
int entity; int entity;
ThisPointerType thisType; ThisPointerType thisType;
HookType hookType; HookType hookType;
CallingConvention thisFuncCallConv;
}; };
class DHooksCallback : public SourceHook::ISHDelegate, public DHooksInfo class DHooksCallback : public SourceHook::ISHDelegate, public DHooksInfo

View File

@ -310,7 +310,7 @@ methodmap DHookParam < Handle
// Gets an object's variable value. // Gets an object's variable value.
// //
// @param num Parameter number to get, starting at 1. // @param num Parameter number to get, 0 for param "this", other parameters start from 1
// @param offset Byte offset within the object to the var to get. // @param offset Byte offset within the object to the var to get.
// @param type Type of var it is. // @param type Type of var it is.
// //
@ -320,7 +320,7 @@ methodmap DHookParam < Handle
// Gets an object's vector variable value. // Gets an object's vector variable value.
// //
// @param num Parameter number to get, starting at 1. // @param num Parameter number to get, 0 for param "this", other parameters start from 1.
// @param offset Byte offset within the object to the var to get. // @param offset Byte offset within the object to the var to get.
// @param type Type of var it is. // @param type Type of var it is.
// @param vec Buffer to store the result vector. // @param vec Buffer to store the result vector.
@ -330,7 +330,7 @@ methodmap DHookParam < Handle
// Gets an object's string variable value. // Gets an object's string variable value.
// //
// @param num Parameter number to get, starting at 1. // @param num Parameter number to get, 0 for param "this", other parameters start from 1.
// @param offset Byte offset within the object to the var to get. // @param offset Byte offset within the object to the var to get.
// @param type Type of var it is. // @param type Type of var it is.
// @param buffer Buffer to store the result string. // @param buffer Buffer to store the result string.
@ -344,7 +344,7 @@ methodmap DHookParam < Handle
// The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride // The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride
// is returned in the callback. // is returned in the callback.
// //
// @param num Parameter number to set, starting at 1. // @param num Parameter number to set, 0 for param "this", other parameters start from 1.
// @param offset Byte offset within the object to the var to set. // @param offset Byte offset within the object to the var to set.
// @param type Type of var it is. // @param type Type of var it is.
// @param value The value to set the var to. // @param value The value to set the var to.
@ -357,7 +357,7 @@ methodmap DHookParam < Handle
// The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride // The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride
// is returned in the callback. // is returned in the callback.
// //
// @param num Parameter number to set, starting at 1. // @param num Parameter number to set, 0 for param "this", other parameters start from 1.
// @param offset Byte offset within the object to the var to set. // @param offset Byte offset within the object to the var to set.
// @param type Type of var it is. // @param type Type of var it is.
// @param vec The value to set the vector var to. // @param vec The value to set the vector var to.
@ -928,7 +928,7 @@ native void DHookSetReturnString(Handle hReturn, char[] value);
* Gets an objects variable value * Gets an objects variable value
* *
* @param hParams Handle to params structure * @param hParams Handle to params structure
* @param num Param number to get. * @param num Param number to get, 0 for param "this".
* @param offset Offset within the object to the var to get. * @param offset Offset within the object to the var to get.
* @param type Type of var it is * @param type Type of var it is
* *
@ -941,7 +941,7 @@ native any DHookGetParamObjectPtrVar(Handle hParams, int num, int offset, Object
* Sets an objects variable value * Sets an objects variable value
* *
* @param hParams Handle to params structure * @param hParams Handle to params structure
* @param num Param number to set. * @param num Param number to set, 0 for param "this".
* @param offset Offset within the object to the var to set. * @param offset Offset within the object to the var to set.
* @param type Type of var it is * @param type Type of var it is
* @param value The value to set the var to. * @param value The value to set the var to.
@ -954,7 +954,7 @@ native void DHookSetParamObjectPtrVar(Handle hParams, int num, int offset, Objec
* Gets an objects vector variable value * Gets an objects vector variable value
* *
* @param hParams Handle to params structure * @param hParams Handle to params structure
* @param num Param number to get. * @param num Param number to get, 0 for param "this".
* @param offset Offset within the object to the var to get. * @param offset Offset within the object to the var to get.
* @param type Type of var it is * @param type Type of var it is
* @param buffer Buffer to store the result vector * @param buffer Buffer to store the result vector
@ -967,7 +967,7 @@ native void DHookGetParamObjectPtrVarVector(Handle hParams, int num, int offset,
* Sets an objects vector variable value * Sets an objects vector variable value
* *
* @param hParams Handle to params structure * @param hParams Handle to params structure
* @param num Param number to set. * @param num Param number to set, 0 for param "this".
* @param offset Offset within the object to the var to set. * @param offset Offset within the object to the var to set.
* @param type Type of var it is * @param type Type of var it is
* @param value The value to set the vector var to. * @param value The value to set the vector var to.
@ -980,7 +980,7 @@ native void DHookSetParamObjectPtrVarVector(Handle hParams, int num, int offset,
* Gets an objects string variable value * Gets an objects string variable value
* *
* @param hParams Handle to params structure * @param hParams Handle to params structure
* @param num Param number to get. * @param num Param number to get, 0 for param "this".
* @param offset Offset within the object to the var to get. * @param offset Offset within the object to the var to get.
* @param type Type of var it is * @param type Type of var it is
* @param buffer Buffer to store the result vector * @param buffer Buffer to store the result vector