Add support for setting callback when hooking instead of on create only.
This commit is contained in:
parent
b14f62435c
commit
cd55a5026e
55
natives.cpp
55
natives.cpp
@ -18,14 +18,26 @@ bool GetHandleIfValidOrError(HandleType_t type, void **object, IPluginContext *p
|
|||||||
return true;
|
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);
|
//native Handle:DHookCreate(offset, HookType:hooktype, ReturnType:returntype, ThisPointerType:thistype, DHookCallback:callback);
|
||||||
cell_t Native_CreateHook(IPluginContext *pContext, const cell_t *params)
|
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]));
|
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);
|
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;
|
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)
|
cell_t Native_HookEntity(IPluginContext *pContext, const cell_t *params)
|
||||||
{
|
{
|
||||||
HookSetup *setup;
|
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]);
|
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)
|
if(!manager->hookid)
|
||||||
{
|
{
|
||||||
@ -123,7 +142,7 @@ cell_t Native_HookEntity(IPluginContext *pContext, const cell_t *params)
|
|||||||
|
|
||||||
return manager->hookid;
|
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)
|
cell_t Native_HookGamerules(IPluginContext *pContext, const cell_t *params)
|
||||||
{
|
{
|
||||||
HookSetup *setup;
|
HookSetup *setup;
|
||||||
@ -156,7 +175,14 @@ cell_t Native_HookGamerules(IPluginContext *pContext, const cell_t *params)
|
|||||||
return pContext->ThrowNativeError("Could not get gamerules pointer");
|
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)
|
if(!manager->hookid)
|
||||||
{
|
{
|
||||||
@ -168,7 +194,7 @@ cell_t Native_HookGamerules(IPluginContext *pContext, const cell_t *params)
|
|||||||
|
|
||||||
return manager->hookid;
|
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)
|
cell_t Native_HookRaw(IPluginContext *pContext, const cell_t *params)
|
||||||
{
|
{
|
||||||
HookSetup *setup;
|
HookSetup *setup;
|
||||||
@ -200,7 +226,14 @@ cell_t Native_HookRaw(IPluginContext *pContext, const cell_t *params)
|
|||||||
return pContext->ThrowNativeError("Invalid address passed");
|
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)
|
if(!manager->hookid)
|
||||||
{
|
{
|
||||||
|
@ -54,7 +54,7 @@ public OnPluginStart()
|
|||||||
hGetModelName = DHookCreate(offset, HookType_Entity, ReturnType_String, ThisPointer_CBaseEntity, GetModelName);
|
hGetModelName = DHookCreate(offset, HookType_Entity, ReturnType_String, ThisPointer_CBaseEntity, GetModelName);
|
||||||
|
|
||||||
offset = GameConfGetOffset(temp, "GetMaxs");
|
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");
|
offset = GameConfGetOffset(temp, "CanUse");
|
||||||
hHookCanUse = DHookCreate(offset, HookType_Entity, ReturnType_Bool, ThisPointer_CBaseEntity, CanUsePost);
|
hHookCanUse = DHookCreate(offset, HookType_Entity, ReturnType_Bool, ThisPointer_CBaseEntity, CanUsePost);
|
||||||
@ -78,7 +78,7 @@ public OnPluginStart()
|
|||||||
DHookAddParam(hAcceptInput, HookParamType_Int);
|
DHookAddParam(hAcceptInput, HookParamType_Int);
|
||||||
|
|
||||||
offset = GameConfGetOffset(temp, "GetMaxPlayerSpeed");
|
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");
|
offset = GameConfGetOffset(temp, "GiveAmmo");
|
||||||
hGiveAmmo = DHookCreate(offset, HookType_Entity, ReturnType_Int, ThisPointer_CBaseEntity, GiveAmmo);
|
hGiveAmmo = DHookCreate(offset, HookType_Entity, ReturnType_Int, ThisPointer_CBaseEntity, GiveAmmo);
|
||||||
@ -161,11 +161,11 @@ public OnClientPutInServer(client)
|
|||||||
{
|
{
|
||||||
DHookEntity(hSetModel, false, client, RemovalCB);
|
DHookEntity(hSetModel, false, client, RemovalCB);
|
||||||
DHookEntity(hHookCanUse, true, client, RemovalCB);
|
DHookEntity(hHookCanUse, true, client, RemovalCB);
|
||||||
DHookEntity(hGetSpeed, true, client, RemovalCB);
|
DHookEntity(hGetSpeed, true, client, RemovalCB, GetMaxPlayerSpeedPost);
|
||||||
DHookEntity(hGiveAmmo, false, client);
|
DHookEntity(hGiveAmmo, false, client);
|
||||||
DHookEntity(hGetModelName, true, client);
|
DHookEntity(hGetModelName, true, client);
|
||||||
DHookEntity(hTakeDamage, false, client);
|
DHookEntity(hTakeDamage, false, client);
|
||||||
DHookEntity(hGetMaxs, true, client, RemovalCB);
|
DHookEntity(hGetMaxs, true, client, _ , GetMaxsPost);
|
||||||
DHookEntity(hBloodColor, true, client);
|
DHookEntity(hBloodColor, true, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +174,7 @@ native bool DHookRemoveEntityListener(ListenType type, ListenCB callback);
|
|||||||
*
|
*
|
||||||
* @return Returns setup handle for the hook or INVALID_HANDLE.
|
* @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
|
/* 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.
|
* @error Invalid setup handle, invalid entity or invalid hook type.
|
||||||
* @return -1 on fail a hookid on success
|
* @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
|
/* 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.
|
* @error Invalid setup handle, failing to get gamerules pointer or invalid hook type.
|
||||||
* @return -1 on fail a hookid on success
|
* @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
|
/* 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.
|
* @error Invalid setup handle, invalid address or invalid hook type.
|
||||||
* @return -1 on fail a hookid on success
|
* @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
|
/* Remove hook by hook id
|
||||||
*
|
*
|
||||||
|
@ -14,13 +14,13 @@ using namespace SourceHook;
|
|||||||
#define OBJECT_OFFSET (sizeof(void *)*2)
|
#define OBJECT_OFFSET (sizeof(void *)*2)
|
||||||
#endif
|
#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->callback = MakeHandler(setup->returnType);
|
||||||
this->hookid = 0;
|
this->hookid = 0;
|
||||||
this->remove_callback = remove_callback;
|
this->remove_callback = remove_callback;
|
||||||
this->callback->offset = setup->offset;
|
this->callback->offset = setup->offset;
|
||||||
this->callback->plugin_callback = setup->callback;
|
this->callback->plugin_callback = plugincb;
|
||||||
this->callback->returnFlag = setup->returnFlag;
|
this->callback->returnFlag = setup->returnFlag;
|
||||||
this->callback->thisType = setup->thisType;
|
this->callback->thisType = setup->thisType;
|
||||||
this->callback->post = post;
|
this->callback->post = post;
|
||||||
|
2
vhook.h
2
vhook.h
@ -309,7 +309,7 @@ public:
|
|||||||
class DHooksManager
|
class DHooksManager
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
DHooksManager(HookSetup *setup, void *iface, IPluginFunction *remove_callback, bool post);
|
DHooksManager(HookSetup *setup, void *iface, IPluginFunction *remove_callback, IPluginFunction *plugincb, bool post);
|
||||||
~DHooksManager()
|
~DHooksManager()
|
||||||
{
|
{
|
||||||
if(this->hookid)
|
if(this->hookid)
|
||||||
|
Loading…
Reference in New Issue
Block a user