diff --git a/extensions/dhooks/natives.cpp b/extensions/dhooks/natives.cpp index 20237fa0e..555f1ded2 100644 --- a/extensions/dhooks/natives.cpp +++ b/extensions/dhooks/natives.cpp @@ -1086,6 +1086,46 @@ cell_t Native_RemoveEntityListener(IPluginContext *pContext, const cell_t *param return pContext->ThrowNativeError("Failed to get g_pEntityListener"); } +void* GetObjectAddrOrThis(HookParamsStruct* paramStruct, IPluginContext* pContext, const cell_t* params) +{ + if (params[2] != 0) + { + const auto& paramsVec = paramStruct->dg->params; + + if (params[2] < 0 || params[2] > static_cast(paramsVec.size())) + { + return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramsVec.size()); + } + + int index = params[2] - 1; + const auto& 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); + return GetObjectAddr(param.type, param.flags, paramStruct->orgParams, offset); + } + + const auto& dgInfo = paramStruct->dg; + + if (dgInfo->thisFuncCallConv != CallConv_THISCALL) + { + return pContext->ThrowNativeError("Parameter 'this' is only available in member functions, specify the calling convention type 'thiscall'"); + } + + if (dgInfo->thisType != ThisPointer_Address + && dgInfo->thisType != ThisPointer_CBaseEntity + && !(dgInfo->thisType == ThisPointer_Ignore && dgInfo->hookType == HookType_GameRules)) + { + return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available"); + } + + return g_SHPtr->GetIfacePtr(); +} + //native any:DHookGetParamObjectPtrVar(Handle:hParams, num, offset, ObjectValueType:type); cell_t Native_GetParamObjectPtrVar(IPluginContext *pContext, const cell_t *params) { @@ -1096,39 +1136,7 @@ cell_t Native_GetParamObjectPtrVar(IPluginContext *pContext, const cell_t *param return 0; } - void *addr = NULL; - - if(params[2] != 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); - addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset); - } - else - { - if(paramStruct->dg->thisFuncCallConv != CallConv_THISCALL) - { - return pContext->ThrowNativeError("Parameter 'this' is only available in member functions, specify the calling convention type 'thiscall'"); - } - - if(paramStruct->dg->thisType != ThisPointer_Address && paramStruct->dg->thisType != ThisPointer_CBaseEntity && !(paramStruct->dg->thisType == ThisPointer_Ignore && paramStruct->dg->hookType == HookType_GameRules)) - { - return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available"); - } - - addr = g_SHPtr->GetIfacePtr(); - } + void* addr = GetObjectAddrOrThis(paramStruct, pContext, params); switch((ObjectValueType)params[4]) { @@ -1186,39 +1194,7 @@ cell_t Native_SetParamObjectPtrVar(IPluginContext *pContext, const cell_t *param return 0; } - void *addr = NULL; - - if(params[2] != 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); - addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset); - } - else - { - if(paramStruct->dg->thisFuncCallConv != CallConv_THISCALL) - { - return pContext->ThrowNativeError("Parameter 'this' is only available in member functions, specify the calling convention type 'thiscall'"); - } - - if(paramStruct->dg->thisType != ThisPointer_Address && paramStruct->dg->thisType != ThisPointer_CBaseEntity && !(paramStruct->dg->thisType == ThisPointer_Ignore && paramStruct->dg->hookType == HookType_GameRules)) - { - return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available"); - } - - addr = g_SHPtr->GetIfacePtr(); - } + void* addr = GetObjectAddrOrThis(paramStruct, pContext, params); switch((ObjectValueType)params[4]) { @@ -1290,39 +1266,7 @@ cell_t Native_GetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t return 0; } - void *addr = NULL; - - if(params[2] != 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); - addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset); - } - else - { - if(paramStruct->dg->thisFuncCallConv != CallConv_THISCALL) - { - return pContext->ThrowNativeError("Parameter 'this' is only available in member functions, specify the calling convention type 'thiscall'"); - } - - if(paramStruct->dg->thisType != ThisPointer_Address && paramStruct->dg->thisType != ThisPointer_CBaseEntity && !(paramStruct->dg->thisType == ThisPointer_Ignore && paramStruct->dg->hookType == HookType_GameRules)) - { - return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available"); - } - - addr = g_SHPtr->GetIfacePtr(); - } + void* addr = GetObjectAddrOrThis(paramStruct, pContext, params); cell_t *buffer; pContext->LocalToPhysAddr(params[5], &buffer); @@ -1363,39 +1307,7 @@ cell_t Native_SetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t return 0; } - void *addr = NULL; - - if(params[2] != 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); - addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset); - } - else - { - if(paramStruct->dg->thisFuncCallConv != CallConv_THISCALL) - { - return pContext->ThrowNativeError("Parameter 'this' is only available in member functions, specify the calling convention type 'thiscall'"); - } - - if(paramStruct->dg->thisType != ThisPointer_Address && paramStruct->dg->thisType != ThisPointer_CBaseEntity && !(paramStruct->dg->thisType == ThisPointer_Ignore && paramStruct->dg->hookType == HookType_GameRules)) - { - return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available"); - } - - addr = g_SHPtr->GetIfacePtr(); - } + void* addr = GetObjectAddrOrThis(paramStruct, pContext, params); cell_t *buffer; pContext->LocalToPhysAddr(params[5], &buffer); @@ -1435,39 +1347,7 @@ cell_t Native_GetParamObjectPtrString(IPluginContext *pContext, const cell_t *pa return 0; } - void *addr = NULL; - - if(params[2] != 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); - addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset); - } - else - { - if(paramStruct->dg->thisFuncCallConv != CallConv_THISCALL) - { - return pContext->ThrowNativeError("Parameter 'this' is only available in member functions, specify the calling convention type 'thiscall'"); - } - - if(paramStruct->dg->thisType != ThisPointer_Address && paramStruct->dg->thisType != ThisPointer_CBaseEntity && !(paramStruct->dg->thisType == ThisPointer_Ignore && paramStruct->dg->hookType == HookType_GameRules)) - { - return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available"); - } - - addr = g_SHPtr->GetIfacePtr(); - } + void* addr = GetObjectAddrOrThis(paramStruct, pContext, params); switch((ObjectValueType)params[4]) {