View Single Post
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 10-17-2020 , 06:28   Re: DHooks (Dynamic Hooks - Dev Preview)
Reply With Quote #798

I've put up a new version detours15 which adds a methodmap API for your pleasure. Call arguments are saved before calling the function now by default too, so it's save to access the parameters in a post-hook.

There is a new DynamicHook methodmap for virtual hooks replacing the DHookCreate family and a new DynamicDetour methodmap for the DHookCreateDetour stuff. Return value and parameter handles pushed to the callbacks have new shiny methodmaps too. The old natives haven't been touched and work just as before.

PHP Code:
// Legacy API
Handle hHook DHookCreate(offsetHookType_EntityReturnType_IntThisPointer_CBaseEntity);
DHookAddParam(hHookHookParamType_CharPtr);
DHookEntity(hHooktrueclient, .callback=HookHandler);

public 
MRESReturn HookHandler(int pThisHandle hReturnHandle hParams)
{
    
char sParam[256];
    
DHookGetParamString(hParams1sParamsizeof(sParam));
    
PrintToServer("HookHandler(%d, \"%s\") = %d"pThissParamDHookGetReturn(hReturn));
    return 
MRES_Handled;
}

 
// Methodmap API
DynamicHook hHook = new DynamicHook(offsetHookType_EntityReturnType_IntThisPointer_CBaseEntity);
hHook.AddParam(HookParamType_CharPtr);
hHook.HookEntity(Hook_PostclientHookHandler);

public 
MRESReturn HookHandler(int pThisDHookReturn hReturnDHookParam hParams)
{
    
char sParam[256];
    
hParams.GetString(1sParamsizeof(sParam));
    
PrintToServer("HookHandler(%d, \"%s\") = %d"pThissParamhReturn.Value);
    return 
MRES_Handled;
 } 
The magic bool post parameter during hooking has been replaced with a more readable enum in the methodmap to tell if you want a pre- or post-hook.
PHP Code:
enum HookMode
{
    
Hook_Pre,                   // Callback will be executed BEFORE the original function.
    
Hook_Post                   // Callback will be executed AFTER the original function.
}; 

Methodmap definition:
Spoiler
__________________

Last edited by Peace-Maker; 10-17-2020 at 06:29.
Peace-Maker is offline