From cd55a5026ec78befc8691dc9f65b89d99b8ac6ad Mon Sep 17 00:00:00 2001 From: Drifter Date: Sat, 27 Jan 2018 10:08:44 -0500 Subject: [PATCH] Add support for setting callback when hooking instead of on create only. --- natives.cpp | 55 ++++++++++++++++++++------ sourcemod/scripting/dhooks-test.sp | 8 ++-- sourcemod/scripting/include/dhooks.inc | 8 ++-- vhook.cpp | 4 +- vhook.h | 2 +- 5 files changed, 55 insertions(+), 22 deletions(-) diff --git a/natives.cpp b/natives.cpp index cd718d9..03b472f 100644 --- a/natives.cpp +++ b/natives.cpp @@ -18,14 +18,26 @@ bool GetHandleIfValidOrError(HandleType_t type, void **object, IPluginContext *p return true; } +IPluginFunction *GetCallback(IPluginContext *pContext, HookSetup * setup, const cell_t *params, unsigned int callback_index) +{ + IPluginFunction *ret = NULL; + + if (params[0] >= callback_index) + { + ret = pContext->GetFunctionById(params[callback_index]); + } + + if (!ret && setup->callback) + { + ret = setup->callback; + } + + return ret; +} + //native Handle:DHookCreate(offset, HookType:hooktype, ReturnType:returntype, ThisPointerType:thistype, DHookCallback:callback); cell_t Native_CreateHook(IPluginContext *pContext, const cell_t *params) { - if(!pContext->GetFunctionById(params[5])) - { - return pContext->ThrowNativeError("Failed to retrieve function by id"); - } - HookSetup *setup = new HookSetup((ReturnType)params[3], PASSFLAG_BYVAL, (HookType)params[2], (ThisPointerType)params[4], params[1], pContext->GetFunctionById(params[5])); Handle_t hndl = handlesys->CreateHandle(g_HookSetupHandle, setup, pContext->GetIdentity(), myself->GetIdentity(), NULL); @@ -80,7 +92,7 @@ cell_t Native_AddParam(IPluginContext *pContext, const cell_t *params) return 1; } -// native DHookEntity(Handle:setup, bool:post, entity, DHookRemovalCB:removalcb); +// native DHookEntity(Handle:setup, bool:post, entity, DHookRemovalCB:removalcb, DHookCallback:callback); cell_t Native_HookEntity(IPluginContext *pContext, const cell_t *params) { HookSetup *setup; @@ -111,7 +123,14 @@ cell_t Native_HookEntity(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Invalid entity passed %i", params[2]); } - DHooksManager *manager = new DHooksManager(setup, pEnt, pContext->GetFunctionById(params[4]), post); + IPluginFunction *cb = GetCallback(pContext, setup, params, 5); + + if (!cb) + { + pContext->ThrowNativeError("Failed to hook entity %i, no callback provided", params[2]); + } + + DHooksManager *manager = new DHooksManager(setup, pEnt, pContext->GetFunctionById(params[4]), cb, post); if(!manager->hookid) { @@ -123,7 +142,7 @@ cell_t Native_HookEntity(IPluginContext *pContext, const cell_t *params) return manager->hookid; } -// native DHookGamerules(Handle:setup, bool:post, DHookRemovalCB:removalcb); +// native DHookGamerules(Handle:setup, bool:post, DHookRemovalCB:removalcb, DHookCallback:callback); cell_t Native_HookGamerules(IPluginContext *pContext, const cell_t *params) { HookSetup *setup; @@ -156,7 +175,14 @@ cell_t Native_HookGamerules(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Could not get gamerules pointer"); } - DHooksManager *manager = new DHooksManager(setup, rules, pContext->GetFunctionById(params[3]), post); + IPluginFunction *cb = GetCallback(pContext, setup, params, 4); + + if (!cb) + { + pContext->ThrowNativeError("Failed to hook gamerules, no callback provided"); + } + + DHooksManager *manager = new DHooksManager(setup, rules, pContext->GetFunctionById(params[3]), cb, post); if(!manager->hookid) { @@ -168,7 +194,7 @@ cell_t Native_HookGamerules(IPluginContext *pContext, const cell_t *params) return manager->hookid; } -// DHookRaw(Handle:setup, bool:post, Address:addr, DHookRemovalCB:removalcb); +// DHookRaw(Handle:setup, bool:post, Address:addr, DHookRemovalCB:removalcb, DHookCallback:callback); cell_t Native_HookRaw(IPluginContext *pContext, const cell_t *params) { HookSetup *setup; @@ -200,7 +226,14 @@ cell_t Native_HookRaw(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Invalid address passed"); } - DHooksManager *manager = new DHooksManager(setup, iface, pContext->GetFunctionById(params[4]), post); + IPluginFunction *cb = GetCallback(pContext, setup, params, 5); + + if (!cb) + { + pContext->ThrowNativeError("Failed to hook address, no callback provided"); + } + + DHooksManager *manager = new DHooksManager(setup, iface, pContext->GetFunctionById(params[4]), cb, post); if(!manager->hookid) { diff --git a/sourcemod/scripting/dhooks-test.sp b/sourcemod/scripting/dhooks-test.sp index da7c434..2c26da4 100644 --- a/sourcemod/scripting/dhooks-test.sp +++ b/sourcemod/scripting/dhooks-test.sp @@ -54,7 +54,7 @@ public OnPluginStart() hGetModelName = DHookCreate(offset, HookType_Entity, ReturnType_String, ThisPointer_CBaseEntity, GetModelName); offset = GameConfGetOffset(temp, "GetMaxs"); - hGetMaxs = DHookCreate(offset, HookType_Entity, ReturnType_Vector, ThisPointer_Ignore, GetMaxsPost); + hGetMaxs = DHookCreate(offset, HookType_Entity, ReturnType_Vector, ThisPointer_Ignore); offset = GameConfGetOffset(temp, "CanUse"); hHookCanUse = DHookCreate(offset, HookType_Entity, ReturnType_Bool, ThisPointer_CBaseEntity, CanUsePost); @@ -78,7 +78,7 @@ public OnPluginStart() DHookAddParam(hAcceptInput, HookParamType_Int); offset = GameConfGetOffset(temp, "GetMaxPlayerSpeed"); - hGetSpeed = DHookCreate(offset, HookType_Entity, ReturnType_Float, ThisPointer_CBaseEntity, GetMaxPlayerSpeedPost); + hGetSpeed = DHookCreate(offset, HookType_Entity, ReturnType_Float, ThisPointer_CBaseEntity); offset = GameConfGetOffset(temp, "GiveAmmo"); hGiveAmmo = DHookCreate(offset, HookType_Entity, ReturnType_Int, ThisPointer_CBaseEntity, GiveAmmo); @@ -161,11 +161,11 @@ public OnClientPutInServer(client) { DHookEntity(hSetModel, false, client, RemovalCB); DHookEntity(hHookCanUse, true, client, RemovalCB); - DHookEntity(hGetSpeed, true, client, RemovalCB); + DHookEntity(hGetSpeed, true, client, RemovalCB, GetMaxPlayerSpeedPost); DHookEntity(hGiveAmmo, false, client); DHookEntity(hGetModelName, true, client); DHookEntity(hTakeDamage, false, client); - DHookEntity(hGetMaxs, true, client, RemovalCB); + DHookEntity(hGetMaxs, true, client, _ , GetMaxsPost); DHookEntity(hBloodColor, true, client); } diff --git a/sourcemod/scripting/include/dhooks.inc b/sourcemod/scripting/include/dhooks.inc index 6bf8211..381e55f 100644 --- a/sourcemod/scripting/include/dhooks.inc +++ b/sourcemod/scripting/include/dhooks.inc @@ -174,7 +174,7 @@ native bool DHookRemoveEntityListener(ListenType type, ListenCB callback); * * @return Returns setup handle for the hook or INVALID_HANDLE. */ -native Handle DHookCreate(int offset, HookType hooktype, ReturnType returntype, ThisPointerType thistype, DHookCallback callback); +native Handle DHookCreate(int offset, HookType hooktype, ReturnType returntype, ThisPointerType thistype, DHookCallback callback=INVALID_FUNCTION); /* Adds param to a hook setup * @@ -199,7 +199,7 @@ native void DHookAddParam(Handle setup, HookParamType type, int size=-1, DHookPa * @error Invalid setup handle, invalid entity or invalid hook type. * @return -1 on fail a hookid on success */ -native int DHookEntity(Handle setup, bool post, int entity, DHookRemovalCB removalcb=INVALID_FUNCTION); +native int DHookEntity(Handle setup, bool post, int entity, DHookRemovalCB removalcb=INVALID_FUNCTION, DHookCallback callback=INVALID_FUNCTION); /* Hook gamerules * @@ -210,7 +210,7 @@ native int DHookEntity(Handle setup, bool post, int entity, DHookRemovalCB remov * @error Invalid setup handle, failing to get gamerules pointer or invalid hook type. * @return -1 on fail a hookid on success */ -native int DHookGamerules(Handle setup, bool post, DHookRemovalCB removalcb=INVALID_FUNCTION); +native int DHookGamerules(Handle setup, bool post, DHookRemovalCB removalcb=INVALID_FUNCTION, DHookCallback callback=INVALID_FUNCTION); /* Hook a raw pointer * @@ -222,7 +222,7 @@ native int DHookGamerules(Handle setup, bool post, DHookRemovalCB removalcb=INVA * @error Invalid setup handle, invalid address or invalid hook type. * @return -1 on fail a hookid on success */ -native int DHookRaw(Handle setup, bool post, Address addr, DHookRemovalCB removalcb=INVALID_FUNCTION); +native int DHookRaw(Handle setup, bool post, Address addr, DHookRemovalCB removalcb=INVALID_FUNCTION, DHookCallback callback=INVALID_FUNCTION); /* Remove hook by hook id * diff --git a/vhook.cpp b/vhook.cpp index fdcdaf0..008b1f2 100644 --- a/vhook.cpp +++ b/vhook.cpp @@ -14,13 +14,13 @@ using namespace SourceHook; #define OBJECT_OFFSET (sizeof(void *)*2) #endif -DHooksManager::DHooksManager(HookSetup *setup, void *iface, IPluginFunction *remove_callback, bool post) +DHooksManager::DHooksManager(HookSetup *setup, void *iface, IPluginFunction *remove_callback, IPluginFunction *plugincb, bool post) { this->callback = MakeHandler(setup->returnType); this->hookid = 0; this->remove_callback = remove_callback; this->callback->offset = setup->offset; - this->callback->plugin_callback = setup->callback; + this->callback->plugin_callback = plugincb; this->callback->returnFlag = setup->returnFlag; this->callback->thisType = setup->thisType; this->callback->post = post; diff --git a/vhook.h b/vhook.h index c4f581c..4d61265 100644 --- a/vhook.h +++ b/vhook.h @@ -309,7 +309,7 @@ public: class DHooksManager { public: - DHooksManager(HookSetup *setup, void *iface, IPluginFunction *remove_callback, bool post); + DHooksManager(HookSetup *setup, void *iface, IPluginFunction *remove_callback, IPluginFunction *plugincb, bool post); ~DHooksManager() { if(this->hookid)