Skip to content

Commit

Permalink
peace-maker#23 DHookGetParamAddress + DHookParam.GetAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
Natanel-Shitrit committed Jul 25, 2021
1 parent 98bb4e5 commit 4041d29
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
29 changes: 29 additions & 0 deletions natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,33 @@ cell_t Native_IsNullParam(IPluginContext *pContext, const cell_t *params)
return pContext->ThrowNativeError("Param is not a pointer!");
}

//native Address:DHookGetParamAddress(Handle:hParams, num);
cell_t Native_GetParamAddress(IPluginContext *pContext, const cell_t *params)
{
HookParamsStruct *paramStruct;

if(!GetCallbackArgHandleIfValidOrError(g_HookParamsHandle, g_HookReturnHandle, (void **)&paramStruct, pContext, params[1]))
{
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;

HookParamType type = paramStruct->dg->params.at(index).type;
if(type != HookParamType_StringPtr && type != HookParamType_CharPtr && type != HookParamType_VectorPtr && type != HookParamType_CBaseEntity && type != HookParamType_ObjectPtr && type != HookParamType_Edict && type != HookParamType_Unknown)
{
return pContext->ThrowNativeError("Param is not a pointer!");
}

size_t offset = GetParamOffset(paramStruct, index);
return *(cell_t *)((intptr_t)paramStruct->orgParams + offset);
}

sp_nativeinfo_t g_Natives[] =
{
{"DHookCreate", Native_CreateHook},
Expand Down Expand Up @@ -1444,6 +1471,7 @@ sp_nativeinfo_t g_Natives[] =
{"DHookSetParamObjectPtrVarVector", Native_SetParamObjectPtrVarVector},
{"DHookGetParamObjectPtrString", Native_GetParamObjectPtrString},
{"DHookIsNullParam", Native_IsNullParam},
{"DHookGetParamAddress", Native_GetParamAddress},

// Methodmap API
{"DHookSetup.AddParam", Native_AddParam},
Expand Down Expand Up @@ -1473,6 +1501,7 @@ sp_nativeinfo_t g_Natives[] =
{"DHookParam.SetObjectVar", Native_SetParamObjectPtrVar},
{"DHookParam.SetObjectVarVector", Native_SetParamObjectPtrVarVector},
{"DHookParam.IsNull", Native_IsNullParam},
{"DHookParam.GetAddress", Native_GetParamAddress},

{"DHookReturn.Value.get", Native_GetReturn},
{"DHookReturn.Value.set", Native_SetReturn},
Expand Down
21 changes: 21 additions & 0 deletions sourcemod_files/scripting/include/dhooks.inc
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,15 @@ methodmap DHookParam < Handle
// @return True if null, false otherwise.
// @error Non-pointer parameter.
public native bool IsNull(int num);

// Get param address (Use only for ptr param types)
//
// @param hParams Handle to params structure
// @param num Param number to get. (Example if the function has 2 params and you need the value of the first param num would be 1. 0 Will return the number of params stored)
//
// @error Invalid handle. Invalid param number. Invalid param type.
// @return address of the parameter.
public native Address GetAddress(int num);
};


Expand Down Expand Up @@ -930,6 +939,16 @@ native void DHookGetParamObjectPtrString(Handle hParams, int num, int offset, Ob
*/
native bool DHookIsNullParam(Handle hParams, int num);

/* Get param address (Use only for ptr param types)
*
* @param hParams Handle to params structure
* @param num Param number to get. (Example if the function has 2 params and you need the value of the first param num would be 1. 0 Will return the number of params stored)
*
* @error Invalid handle. Invalid param number. Invalid param type.
* @return address of the parameter.
*/
native Address DHookGetParamAddress(Handle hParams, int num);

public Extension __ext_dhooks =
{
name = "dhooks",
Expand Down Expand Up @@ -980,6 +999,7 @@ public __ext_dhooks_SetNTVOptional()
MarkNativeAsOptional("DHookSetParamObjectPtrVarVector");
MarkNativeAsOptional("DHookIsNullParam");
MarkNativeAsOptional("DHookGetParamObjectPtrString");
MarkNativeAsOptional("DHookGetParamAddress");

MarkNativeAsOptional("DHookParam.IsNull");
MarkNativeAsOptional("DHookParam.Get");
Expand All @@ -993,6 +1013,7 @@ public __ext_dhooks_SetNTVOptional()
MarkNativeAsOptional("DHookParam.GetObjectVarString");
MarkNativeAsOptional("DHookParam.SetObjectVar");
MarkNativeAsOptional("DHookParam.SetObjectVarVector");
MarkNativeAsOptional("DHookParam.GetAddress");
MarkNativeAsOptional("DHookReturn.Value.get");
MarkNativeAsOptional("DHookReturn.Value.set");
MarkNativeAsOptional("DHookReturn.GetVector");
Expand Down

0 comments on commit 4041d29

Please sign in to comment.