sm-ext-dhooks2/vhook.h

274 lines
6.3 KiB
C
Raw Normal View History

2013-08-16 05:53:38 +02:00
#ifndef _INCLUDE_VHOOK_H_
#define _INCLUDE_VHOOK_H_
#include "extension.h"
#include <sourcehook.h>
2018-01-21 13:48:50 +01:00
#include <sh_vector.h>
#include <sourcehook_pibuilder.h>
#include <registers.h>
enum CallingConvention
{
CallConv_CDECL,
CallConv_THISCALL,
CallConv_STDCALL,
CallConv_FASTCALL,
};
2013-08-19 17:07:25 +02:00
enum MRESReturn
{
MRES_ChangedHandled = -2, // Use changed values and return MRES_Handled
MRES_ChangedOverride, // Use changed values and return MRES_Override
MRES_Ignored, // plugin didn't take any action
MRES_Handled, // plugin did something, but real function should still be called
MRES_Override, // call real function, but use my return value
MRES_Supercede // skip real function; use my return value
};
2013-08-16 05:53:38 +02:00
enum ObjectValueType
{
ObjectValueType_Int = 0,
ObjectValueType_Bool,
ObjectValueType_Ehandle,
ObjectValueType_Float,
ObjectValueType_CBaseEntityPtr,
ObjectValueType_IntPtr,
ObjectValueType_BoolPtr,
ObjectValueType_EhandlePtr,
ObjectValueType_FloatPtr,
ObjectValueType_Vector,
ObjectValueType_VectorPtr,
ObjectValueType_CharPtr,
ObjectValueType_String
};
2013-08-16 05:53:38 +02:00
enum HookParamType
{
HookParamType_Unknown,
HookParamType_Int,
HookParamType_Bool,
HookParamType_Float,
HookParamType_String,
HookParamType_StringPtr,
HookParamType_CharPtr,
HookParamType_VectorPtr,
HookParamType_CBaseEntity,
HookParamType_ObjectPtr,
2013-08-21 02:18:50 +02:00
HookParamType_Edict,
HookParamType_Object
2013-08-16 05:53:38 +02:00
};
2013-08-19 17:07:25 +02:00
2013-08-16 05:53:38 +02:00
enum ReturnType
{
ReturnType_Unknown,
ReturnType_Void,
ReturnType_Int,
ReturnType_Bool,
ReturnType_Float,
ReturnType_String,
ReturnType_StringPtr,
ReturnType_CharPtr,
ReturnType_Vector,
ReturnType_VectorPtr,
ReturnType_CBaseEntity,
ReturnType_Edict
};
2013-08-19 17:07:25 +02:00
enum ThisPointerType
{
ThisPointer_Ignore,
ThisPointer_CBaseEntity,
ThisPointer_Address
};
enum HookType
{
HookType_Entity,
HookType_GameRules,
HookType_Raw
};
struct ParamInfo
2013-08-16 05:53:38 +02:00
{
HookParamType type;
size_t size;
unsigned int flags;
2013-08-19 17:07:25 +02:00
SourceHook::PassInfo::PassType pass_type;
Register_t custom_register;
2013-08-19 17:07:25 +02:00
};
#ifdef WIN32
#define OBJECT_OFFSET sizeof(void *)
#else
#define OBJECT_OFFSET (sizeof(void *)*2)
#endif
2013-08-20 04:33:44 +02:00
class HookReturnStruct
2013-08-19 17:07:25 +02:00
{
2013-08-20 04:33:44 +02:00
public:
~HookReturnStruct();
2013-08-20 04:33:44 +02:00
public:
2013-08-19 17:07:25 +02:00
ReturnType type;
bool isChanged;
void *orgResult;
void *newResult;
2013-08-16 05:53:38 +02:00
};
class DHooksInfo
{
public:
SourceHook::CVector<ParamInfo> params;
2013-08-16 05:53:38 +02:00
int offset;
2013-08-19 17:07:25 +02:00
unsigned int returnFlag;
ReturnType returnType;
bool post;
IPluginFunction *plugin_callback;
int entity;
ThisPointerType thisType;
HookType hookType;
2013-08-16 05:53:38 +02:00
};
class DHooksCallback : public SourceHook::ISHDelegate, public DHooksInfo
{
public:
virtual bool IsEqual(ISHDelegate *pOtherDeleg){return false;};
2013-08-19 17:07:25 +02:00
virtual void DeleteThis()
{
*(void ***)this = this->oldvtable;
g_pSM->GetScriptingEngine()->FreePageMemory(this->newvtable[2]);
delete this->newvtable;
delete this;
};
virtual void Call() {};
public:
void **newvtable;
void **oldvtable;
2013-08-16 05:53:38 +02:00
};
2013-08-19 17:07:25 +02:00
#ifdef WIN32
2013-08-19 17:07:25 +02:00
void *Callback(DHooksCallback *dg, void **stack, size_t *argsizep);
float Callback_float(DHooksCallback *dg, void **stack, size_t *argsizep);
2016-06-26 19:00:27 +02:00
SDKVector *Callback_vector(DHooksCallback *dg, void **stack, size_t *argsizep);
2013-08-19 17:07:25 +02:00
#else
void *Callback(DHooksCallback *dg, void **stack);
float Callback_float(DHooksCallback *dg, void **stack);
2016-06-26 19:00:27 +02:00
SDKVector *Callback_vector(DHooksCallback *dg, void **stack);
2013-08-29 21:18:43 +02:00
string_t *Callback_stringt(DHooksCallback *dg, void **stack);
2013-08-19 17:07:25 +02:00
#endif
2013-08-19 17:07:25 +02:00
bool SetupHookManager(ISmmAPI *ismm);
void CleanupHooks(IPluginContext *pContext = NULL);
2013-08-19 17:07:25 +02:00
size_t GetParamTypeSize(HookParamType type);
SourceHook::PassInfo::PassType GetParamTypePassType(HookParamType type);
void *GenerateThunk(ReturnType type);
2013-08-19 17:07:25 +02:00
static DHooksCallback *MakeHandler(ReturnType type)
{
DHooksCallback *dg = new DHooksCallback();
dg->returnType = type;
dg->oldvtable = *(void ***)dg;
dg->newvtable = new void *[3];
dg->newvtable[0] = dg->oldvtable[0];
dg->newvtable[1] = dg->oldvtable[1];
dg->newvtable[2] = GenerateThunk(type);
*(void ***)dg = dg->newvtable;
return dg;
}
class HookParamsStruct
2013-08-16 05:53:38 +02:00
{
public:
2013-08-19 17:07:25 +02:00
HookParamsStruct()
{
this->orgParams = NULL;
this->newParams = NULL;
this->dg = NULL;
2013-08-20 04:33:44 +02:00
this->isChanged = NULL;
2013-08-19 17:07:25 +02:00
}
~HookParamsStruct();
2013-08-19 17:07:25 +02:00
public:
void **orgParams;
void **newParams;
2013-08-20 04:33:44 +02:00
bool *isChanged;
DHooksInfo *dg;
2013-08-19 17:07:25 +02:00
};
class HookSetup
{
public:
HookSetup(ReturnType returnType, unsigned int returnFlag, HookType hookType, ThisPointerType thisType, int offset, IPluginFunction *callback)
{
this->returnType = returnType;
this->returnFlag = returnFlag;
this->hookType = hookType;
this->callConv = CallConv_THISCALL;
2013-08-19 17:07:25 +02:00
this->thisType = thisType;
this->offset = offset;
this->funcAddr = nullptr;
2013-08-19 17:07:25 +02:00
this->callback = callback;
2013-08-16 05:53:38 +02:00
};
HookSetup(ReturnType returnType, unsigned int returnFlag, CallingConvention callConv, ThisPointerType thisType, void *funcAddr)
{
this->returnType = returnType;
this->returnFlag = returnFlag;
this->hookType = HookType_Raw;
this->callConv = callConv;
this->thisType = thisType;
this->offset = -1;
this->funcAddr = funcAddr;
this->callback = nullptr;
};
2013-08-19 17:07:25 +02:00
~HookSetup(){};
bool IsVirtual()
{
return this->offset != -1;
}
2013-08-19 17:07:25 +02:00
public:
unsigned int returnFlag;
ReturnType returnType;
HookType hookType;
CallingConvention callConv;
2013-08-19 17:07:25 +02:00
ThisPointerType thisType;
SourceHook::CVector<ParamInfo> params;
2013-08-19 17:07:25 +02:00
int offset;
void *funcAddr;
2013-08-19 17:07:25 +02:00
IPluginFunction *callback;
};
class DHooksManager
{
public:
DHooksManager(HookSetup *setup, void *iface, IPluginFunction *remove_callback, IPluginFunction *plugincb, bool post);
2013-08-16 05:53:38 +02:00
~DHooksManager()
{
2013-08-19 17:07:25 +02:00
if(this->hookid)
2013-08-16 05:53:38 +02:00
{
2013-08-19 17:07:25 +02:00
g_SHPtr->RemoveHookByID(this->hookid);
if(this->remove_callback)
{
this->remove_callback->PushCell(this->hookid);
this->remove_callback->Execute(NULL);
}
if(this->pManager)
{
g_pHookManager->ReleaseHookMan(this->pManager);
}
2013-08-16 05:53:38 +02:00
}
}
2013-08-19 17:07:25 +02:00
public:
intptr_t addr;
2013-08-16 05:53:38 +02:00
int hookid;
2013-08-19 17:07:25 +02:00
DHooksCallback *callback;
IPluginFunction *remove_callback;
SourceHook::HookManagerPubFunc pManager;
2013-08-16 05:53:38 +02:00
};
2013-08-20 04:33:44 +02:00
size_t GetStackArgsSize(DHooksCallback *dg);
cell_t GetThisPtr(void *iface, ThisPointerType type);
2013-08-20 04:33:44 +02:00
2013-08-16 05:53:38 +02:00
extern IBinTools *g_pBinTools;
2013-08-19 17:07:25 +02:00
extern HandleType_t g_HookParamsHandle;
extern HandleType_t g_HookReturnHandle;
2013-08-20 04:33:44 +02:00
#endif