Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dhooks] Add the ability to work with the this parameter, which is an object #2219

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 171 additions & 45 deletions extensions/dhooks/natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,20 +1096,45 @@ cell_t Native_GetParamObjectPtrVar(IPluginContext *pContext, const cell_t *param
return 0;
}

if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
void *addr = NULL;

if(params[2] != 0)
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
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;
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);
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
{
/*HookSetup* setup;
if(!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return pContext->ThrowNativeError("Cannot determine calling convention type, parameter 'this' is only available in member functions");
}

size_t offset = GetParamOffset(paramStruct, index);
void *addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset);
if(setup->callConv != CallConv_THISCALL)
{
return pContext->ThrowNativeError("Parameter 'this' is only available in member functions, specify the calling convention type 'thiscall'");
}*/
Comment on lines +1120 to +1129
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That check is fine, you should enable it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code doesn't work for some reason

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah we need to pass Handle as the first parameter but we don't so this code doesn't work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to get handle in existing code, I'll have to save it somewhere, or use such code to work with the "this" parameter:
int iParent = DHookGetParamObjectPtrVar(hParams, 0, 384, ObjectValueType_Ehandle, g_hDetour);


if(paramStruct->dg->thisType != ThisPointer_Address)
{
return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That check should be removed, thispointer type is only relevant for the dhooks callbacks. Plus it would be nice being able to manipulate directly CBaseEntity or CGamerRules, which uses thispointer_cbaseentity and thispointer_ignore respectively.


addr = g_SHPtr->GetIfacePtr();
}

switch((ObjectValueType)params[4])
{
Expand Down Expand Up @@ -1167,20 +1192,45 @@ cell_t Native_SetParamObjectPtrVar(IPluginContext *pContext, const cell_t *param
return 0;
}

if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
void *addr = NULL;

if(params[2] != 0)
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
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;
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);
}

if(paramStruct->dg->params.at(index).type != HookParamType_ObjectPtr && paramStruct->dg->params.at(index).type != HookParamType_Object)
size_t offset = GetParamOffset(paramStruct, index);
addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset);
}
else
{
return pContext->ThrowNativeError("Invalid object value type %i", paramStruct->dg->params.at(index).type);
/*HookSetup* setup;
if(!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return pContext->ThrowNativeError("Cannot determine calling convention type, parameter 'this' is only available in member functions");
}

if(setup->callConv != 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)
{
return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


addr = g_SHPtr->GetIfacePtr();
}

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])
{
Expand Down Expand Up @@ -1252,20 +1302,45 @@ cell_t Native_GetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t
return 0;
}

if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
void *addr = NULL;

if(params[2] != 0)
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
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;
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);
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
{
/*HookSetup* setup;
if(!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return pContext->ThrowNativeError("Cannot determine calling convention type, parameter 'this' is only available in member functions");
}

size_t offset = GetParamOffset(paramStruct, index);
void *addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset);
if(setup->callConv != 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)
{
return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available");
}

addr = g_SHPtr->GetIfacePtr();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

}

cell_t *buffer;
pContext->LocalToPhysAddr(params[5], &buffer);
Expand Down Expand Up @@ -1305,21 +1380,46 @@ cell_t Native_SetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t
{
return 0;
}

void *addr = NULL;

if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
if(params[2] != 0)
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
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;
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);
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
{
/*HookSetup* setup;
if(!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return pContext->ThrowNativeError("Cannot determine calling convention type, parameter 'this' is only available in member functions");
}

size_t offset = GetParamOffset(paramStruct, index);
void *addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset);
if(setup->callConv != 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)
{
return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available");
}

addr = g_SHPtr->GetIfacePtr();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


cell_t *buffer;
pContext->LocalToPhysAddr(params[5], &buffer);
Expand All @@ -1346,6 +1446,7 @@ cell_t Native_SetParamObjectPtrVarVector(IPluginContext *pContext, const cell_t
vec->z = sp_ctof(buffer[2]);
return 1;
}

A1mDev marked this conversation as resolved.
Show resolved Hide resolved
return pContext->ThrowNativeError("Invalid Object value type (not a type of vector)");
}

Expand All @@ -1359,20 +1460,45 @@ cell_t Native_GetParamObjectPtrString(IPluginContext *pContext, const cell_t *pa
return 0;
}

if(params[2] <= 0 || params[2] > (int)paramStruct->dg->params.size())
void *addr = NULL;

if(params[2] != 0)
{
return pContext->ThrowNativeError("Invalid param number %i max params is %i", params[2], paramStruct->dg->params.size());
}
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;
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);
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
{
/*HookSetup* setup;
if(!GetHandleIfValidOrError(g_HookSetupHandle, (void **)&setup, pContext, params[1]))
{
return pContext->ThrowNativeError("Cannot determine calling convention type, parameter 'this' is only available in member functions");
}

size_t offset = GetParamOffset(paramStruct, index);
void *addr = GetObjectAddr(paramStruct->dg->params.at(index).type, paramStruct->dg->params.at(index).flags, paramStruct->orgParams, offset);
if(setup->callConv != 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)
{
return pContext->ThrowNativeError("Parameter 'this' is not specified as an address, it is not available");
}

addr = g_SHPtr->GetIfacePtr();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto


switch((ObjectValueType)params[4])
{
Expand Down
20 changes: 10 additions & 10 deletions plugins/include/dhooks.inc
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ methodmap DHookParam < Handle

// 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 type Type of var it is.
//
Expand All @@ -320,7 +320,7 @@ methodmap DHookParam < Handle

// 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 type Type of var it is.
// @param vec Buffer to store the result vector.
Expand All @@ -330,7 +330,7 @@ methodmap DHookParam < Handle

// 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 type Type of var it is.
// @param buffer Buffer to store the result string.
Expand All @@ -344,7 +344,7 @@ methodmap DHookParam < Handle
// The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride
// 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 type Type of var it is.
// @param value The value to set the var to.
Expand All @@ -357,7 +357,7 @@ methodmap DHookParam < Handle
// The changes are only applied when MRES_ChangedHandled or MRES_ChangedOverride
// 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 type Type of var it is.
// @param vec The value to set the vector var to.
Expand Down Expand Up @@ -928,7 +928,7 @@ native void DHookSetReturnString(Handle hReturn, char[] value);
* Gets an objects variable value
*
* @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 type Type of var it is
*
Expand All @@ -941,7 +941,7 @@ native any DHookGetParamObjectPtrVar(Handle hParams, int num, int offset, Object
* Sets an objects variable value
*
* @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 type Type of var it is
* @param value The value to set the var to.
Expand All @@ -954,7 +954,7 @@ native void DHookSetParamObjectPtrVar(Handle hParams, int num, int offset, Objec
* Gets an objects vector variable value
*
* @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 type Type of var it is
* @param buffer Buffer to store the result vector
Expand All @@ -967,7 +967,7 @@ native void DHookGetParamObjectPtrVarVector(Handle hParams, int num, int offset,
* Sets an objects vector variable value
*
* @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 type Type of var it is
* @param value The value to set the vector var to.
Expand All @@ -980,7 +980,7 @@ native void DHookSetParamObjectPtrVarVector(Handle hParams, int num, int offset,
* Gets an objects string variable value
*
* @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 type Type of var it is
* @param buffer Buffer to store the result vector
Expand Down