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

Conversation

A1mDev
Copy link
Contributor

@A1mDev A1mDev commented Nov 11, 2024

Sometimes we have to work with this parameter which is a class but dhooks doesn't have this feature, this PR solves this problem. This code has been tested on both post and pre hooks. Below is an example code for working with the new functionality.

/*class CBaseEntity : public IServerEntity
{
	...
protected:
	EHANDLE m_pParent;  // for movement hierarchy	
	...
};*/

MRESReturn Handler_CBaseEntity__OnTakeDamage(Address pThis, Handle hReturn, Handle hParams)
{
	// If EHANDLE type or entity returns entity index.
	// Index num 0, for working with this parameter

	int iParent = DHookGetParamObjectPtrVar(hParams, 0, 384, ObjectValueType_Ehandle);
	if (iParent < 1 || !IsValidEntity(iParent)) {
		return MRES_Ignored;
	}

	char sEntityName[64];
	GetEntityClassname(iParent, sEntityName, sizeof(sEntityName));
	
	PrintToChatAll("[Handler_CBaseEntity__OnTakeDamage] iParent: %s (%d)", sEntityName, iParent);
	
	return MRES_Ignored;
}

Copy link
Member

@Kenzzer Kenzzer left a comment

Choose a reason for hiding this comment

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

Excellent work, especially given the situation that surround Address. The less we mess with memory directly in plugin, the better !
I just have a few request changes, but otherwise I think this is a good addition to dhooks.

Comment on lines +1120 to +1129
/*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'");
}*/
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);

Comment on lines 1131 to 1134
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.

Comment on lines 1216 to 1230
/*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

Comment on lines 1326 to 1342
/*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

Comment on lines 1316 to 1422
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

extensions/dhooks/natives.cpp Outdated Show resolved Hide resolved
Comment on lines 1463 to 1501
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants