diff --git a/ZItemKnockback/gamedata/ZItemKnockback.games.txt b/ZItemKnockback/gamedata/ZItemKnockback.games.txt new file mode 100644 index 00000000..4755211d --- /dev/null +++ b/ZItemKnockback/gamedata/ZItemKnockback.games.txt @@ -0,0 +1,72 @@ +"Games" +{ + "cstrike" + { + "Signatures" + { + "CCSPlayer::FireBullet" + { + "library" "server" + "linux" "@_ZN9CCSPlayer10FireBulletE6VectorRK6QAnglefiiifP11CBaseEntitybff" + } + } + + "Functions" + { + "CCSPlayer__FireBullet" + { + "signature" "CCSPlayer::FireBullet" + "callconv" "thiscall" + "return" "int" + "this" "entity" + "arguments" + { + "vecSrc" + { + "type" "vectorptr" + } + "shootAngles" + { + "type" "objectptr" + } + "vecSpread" + { + "type" "float" + } + "iPenetration" + { + "type" "int" + } + "iBulletType" + { + "type" "int" + } + "iDamage" + { + "type" "int" + } + "flRangeModifier" + { + "type" "float" + } + "pevAttacker" + { + "type" "cbaseentity" + } + "bDoEffects" + { + "type" "bool" + } + "x" + { + "type" "float" + } + "y" + { + "type" "float" + } + } + } + } + } +} diff --git a/ZItemKnockback/scripting/ZItemKnockback.sp b/ZItemKnockback/scripting/ZItemKnockback.sp index 6b90d354..5130085e 100644 --- a/ZItemKnockback/scripting/ZItemKnockback.sp +++ b/ZItemKnockback/scripting/ZItemKnockback.sp @@ -5,7 +5,7 @@ #include #include #include - +#include public Plugin myinfo = { @@ -16,8 +16,30 @@ public Plugin myinfo = url = "" }; +Handle hFireBulletDetour; + +int g_LastAttacker = 0; +int g_LastVictim = 0; + public void OnPluginStart() { + Handle hGameData = LoadGameConfigFile("ZItemKnockback.games"); + if(!hGameData) + SetFailState("Failed to load ZItemKnockback gamedata."); + + hFireBulletDetour = DHookCreateFromConf(hGameData, "CCSPlayer__FireBullet"); + if(!hFireBulletDetour) + SetFailState("Failed to setup detour for CCSPlayer__FireBullet"); + delete hGameData; + + if(!DHookEnableDetour(hFireBulletDetour, false, Detour_OnFireBullet)) + SetFailState("Failed to detour CCSPlayer__FireBullet."); + + for(int client = 1; client <= MaxClients; client++) + { + if(IsClientInGame(client)) + OnClientPutInServer(client); + } } public void OnClientPutInServer(int client) @@ -39,13 +61,10 @@ public Action OnWeaponEquip(int client, int entity) if(!HammerID) return; - char sTargetname[64]; - GetEntPropString(entity, Prop_Data, "m_iName", sTargetname, sizeof(sTargetname)); - - CheckWeaponChildren(client, entity); + CheckChildren(client, entity); } -void CheckWeaponChildren(int client, int parent) +void CheckChildren(int client, int parent) { int entity = INVALID_ENT_REFERENCE; while((entity = FindEntityByClassname(entity, "*")) != INVALID_ENT_REFERENCE) @@ -56,30 +75,64 @@ void CheckWeaponChildren(int client, int parent) char sClassname[64]; GetEntityClassname(entity, sClassname, sizeof(sClassname)); + char sTargetname[64]; + GetEntPropString(entity, Prop_Data, "m_iName", sTargetname, sizeof(sTargetname)); + + PrintToChatAll("%d child: \"%s\" %d (%s)", client, sClassname, entity, sTargetname); + if(!strncmp(sClassname, "func_physbox", 12, false) || !strcmp(sClassname, "func_breakable", false)) { - SDKHook(entity, SDKHook_OnTakeDamagePost, OnTakeDamagePost); + SDKHook(entity, SDKHook_OnTakeDamage, OnTakeDamage); + PrintToChatAll("Hooking \"%s\" %d (%s) OnTakeDamage", sClassname, entity, sTargetname); } - else if(!strcmp(sClassname, "trigger_push", false)) { AcceptEntityInput(entity, "Kill"); + PrintToChatAll("Killing \"%s\" %d (%s)", sClassname, entity, sTargetname); + } + else + { + CheckChildren(client, entity); } } } -public void OnTakeDamagePost(int victim, int attacker, int inflictor, float damage, int damagetype, int weapon, const float damageForce[3], const float damagePosition[3]) +public MRESReturn Detour_OnFireBullet(int pThis, Handle hParams) { - if(attacker <= 0 || attacker > MAXPLAYERS) - return; - - int iKnife = GetEntPropEnt(victim, Prop_Data, "m_pParent"); - if(iKnife <= 0) - return; - - int client = GetEntPropEnt(iKnife, Prop_Data, "m_hOwnerEntity"); - if(client <= 0 || client > MAXPLAYERS) - return; - - SDKHooks_TakeDamage(client, inflictor, attacker, damage, damagetype, weapon, damageForce, damagePosition); + PrintToServer("OnFireBullet called on entity %d!", pThis); + g_LastAttacker = 0; + g_LastVictim = 0; + return MRES_Handled; +} + +public Action OnTakeDamage(int victim, int &attacker, int &inflictor, float &damage, int &damagetype, int &weapon, float damageForce[3], float damagePosition[3]) +{ + PrintToChatAll("OnTakeDamage(%d, %d, %d, %f, %d, %d)", victim, attacker, inflictor, damage, damagetype, weapon); + if(attacker <= 0 || attacker > MAXPLAYERS) + return Plugin_Continue; + + int client = victim; + while((client = GetEntPropEnt(client, Prop_Data, "m_pParent")) != INVALID_ENT_REFERENCE) + { + if(client >= 1 && client <= MAXPLAYERS) + break; + } + + PrintToChatAll("\tclient = %d", client); + if(client <= 0) + return Plugin_Handled; + + // TODO: Fix for knife + if(g_LastAttacker == attacker && g_LastVictim == client) + return Plugin_Handled; + + g_LastAttacker = attacker; + g_LastVictim = client; + + victim = client; + + PrintToChatAll("\tvictim = %d", victim); + SDKHooks_TakeDamage(victim, inflictor, attacker, damage, damagetype, weapon, damageForce, damagePosition); + + return Plugin_Handled; } diff --git a/includes/dhooks.inc b/includes/dhooks.inc index 1224c130..9f596057 100644 --- a/includes/dhooks.inc +++ b/includes/dhooks.inc @@ -1,485 +1,603 @@ -#if defined _dhooks_included -#endinput -#endif -#define _dhooks_included - -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 -}; - -enum ListenType -{ - ListenType_Created, - ListenType_Deleted -}; - -enum ReturnType -{ - ReturnType_Unknown, - ReturnType_Void, - ReturnType_Int, - ReturnType_Bool, - ReturnType_Float, - ReturnType_String, //Note this is a string_t - ReturnType_StringPtr, //Note this is a string_t * - ReturnType_CharPtr, - ReturnType_Vector, - ReturnType_VectorPtr, - ReturnType_CBaseEntity, - ReturnType_Edict -}; - -enum HookParamType -{ - HookParamType_Unknown, - HookParamType_Int, - HookParamType_Bool, - HookParamType_Float, - HookParamType_String, //Note this is a string_t - HookParamType_StringPtr, //Note this is a string_t * - HookParamType_CharPtr, - HookParamType_VectorPtr, - HookParamType_CBaseEntity, - HookParamType_ObjectPtr, - HookParamType_Edict, - HookParamType_Object -}; - -enum ThisPointerType -{ - ThisPointer_Ignore, - ThisPointer_CBaseEntity, - ThisPointer_Address -}; - -enum HookType -{ - HookType_Entity, - HookType_GameRules, - HookType_Raw -}; - -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 -}; - -enum DHookPassFlag -{ - DHookPass_ByVal = (1<<0), /**< Passing by value */ - DHookPass_ByRef = (1<<1), /**< Passing by reference */ - DHookPass_ODTOR = (1<<2), /**< Object has a destructor */ - DHookPass_OCTOR = (1<<3), /**< Object has a constructor */ - DHookPass_OASSIGNOP = (1<<4), /**< Object has an assignment operator */ -}; - -typeset ListenCB -{ - //Deleted - function void (int entity); - - //Created - function void (int entity, const char[] classname); -}; - -typeset DHookRemovalCB -{ - function void (int hookid); -}; -typeset DHookCallback -{ - //Function Example: void Ham::Test() with this pointer ignore - function MRESReturn (); - - //Function Example: void Ham::Test() with this pointer passed - function MRESReturn (int pThis); - - //Function Example: void Ham::Test(int cake) with this pointer ignore - function MRESReturn (Handle hParams); - - //Function Example: void Ham::Test(int cake) with this pointer passed - function MRESReturn (int pThis, Handle hParams); - - //Function Example: int Ham::Test() with this pointer ignore - function MRESReturn (Handle hReturn); - - //Function Example: int Ham::Test() with this pointer passed - function MRESReturn (int pThis, Handle hReturn); - - //Function Example: int Ham::Test(int cake) with this pointer ignore - function MRESReturn (Handle hReturn, Handle hParams); - - //Function Example: int Ham::Test(int cake) with this pointer passed - function MRESReturn (int pThis, Handle hReturn, Handle hParams); - - //Address NOW - - //Function Example: void Ham::Test() with this pointer passed - function MRESReturn (Address pThis); - - //Function Example: void Ham::Test(int cake) with this pointer passed - function MRESReturn (Address pThis, Handle hParams); - - //Function Example: int Ham::Test() with this pointer passed - function MRESReturn (Address pThis, Handle hReturn); - - //Function Example: int Ham::Test(int cake) with this pointer passed - function MRESReturn (Address pThis, Handle hReturn, Handle hParams); - -}; - -/* Adds an entity listener hook - * - * @param type Type of listener to add - * @param callback Callback to use - * - * @noreturn -*/ -native void DHookAddEntityListener(ListenType type, ListenCB callback); - -/* Removes an entity listener hook - * - * @param type Type of listener to remove - * @param callback Callback this listener was using - * - * @return True if one was removed false otherwise. -*/ -native bool DHookRemoveEntityListener(ListenType type, ListenCB callback); - -/* Creates a hook - * - * @param offset vtable offset for function to hook - * @param hooktype Type of hook - * @param returntype Type type of return - * @param thistype Type of this pointer or ignore (ignore can be used if not needed) - * @param callback Callback function - * - * @return Returns setup handle for the hook or INVALID_HANDLE. -*/ -native Handle DHookCreate(int offset, HookType hooktype, ReturnType returntype, ThisPointerType thistype, DHookCallback callback); - -/* Adds param to a hook setup - * - * @param setup Setup handle to add the param to. - * @param type Param type - * @param size Used for Objects (not Object ptr) to define the size of the object. - * @param flag Used to change the pass type. - * - * @error Invalid setup handle or too many params added (request upping the max in thread) - * @noreturn -*/ -native void DHookAddParam(Handle setup, HookParamType type, int size=-1, DHookPassFlag flag=DHookPass_ByVal); -//native DHookAddParam(Handle:setup, HookParamType:type); - -/* Hook entity - * - * @param setup Setup handle to use to add the hook. - * @param post True to make the hook a post hook. (If you need to change the retunr value or need the return value use a post hook! If you need to change params and return use a pre and post hook!) - * @param entity Entity index to hook on. - * @param removalcb Callback for when the hook is removed (Entity hooks are auto-removed on entity destroyed and will call this callback) - * - * @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); - -/* Hook gamerules - * - * @param setup Setup handle to use to add the hook. - * @param post True to make the hook a post hook. (If you need to change the retunr value or need the return value use a post hook! If you need to change params and return use a pre and post hook!) - * @param removalcb Callback for when the hook is removed (Game rules hooks are auto-removed on map end and will call this callback) - * - * @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); - -/* Hook a raw pointer - * - * @param setup Setup handle to use to add the hook. - * @param post True to make the hook a post hook. (If you need to change the retunr value or need the return value use a post hook! If you need to change params and return use a pre and post hook!) - * @param addr This pointer address. - * @param removalcb Callback for when the hook is removed (Entity hooks are auto-removed on entity destroyed and will call this callback) - * - * @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); - -/* Remove hook by hook id - * - * @param hookid Hook id to remove - * - * @return true on success false otherwise - * @note This will not fire the removal callback! -*/ -native bool DHookRemoveHookID(int hookid); - -/* Get param value (Only use for: int, entity, bool or float param types) - * - * @param hParams Handle to params structure - * @param num Param number to get. (Example if the function has 2 params and you need the value of the first param num would be 1. 0 Will return the number of params stored) - * - * @error Invalid handle. Invalid param number. Invalid param type. - * @return value if num greater than 0. If 0 returns paramcount. -*/ -native any DHookGetParam(Handle hParams, int num); - -/* Get vector param value - * - * @param hParams Handle to params structure - * @param num Param number to get. (Example if the function has 2 params and you need the value of the first param num would be 1.) - * @param vec Vector buffer to store result. - * - * @error Invalid handle. Invalid param number. Invalid param type. - * @noreturn -*/ -native void DHookGetParamVector(Handle hParams, int num, float vec[3]); - -/* Get string param value - * - * @param hParams Handle to params structure - * @param num Param number to get. (Example if the function has 2 params and you need the value of the first param num would be 1.) - * @param buffer String buffer to store result - * @param size Buffer size - * - * @error Invalid handle. Invalid param number. Invalid param type. - * @noreturn -*/ -native void DHookGetParamString(Handle hParams, int num, char[] buffer, int size); - -/* Set param value (Only use for: int, entity, bool or float param types) - * - * @param hParams Handle to params structure - * @params num Param number to set (Example if the function has 2 params and you need to set the value of the first param num would be 1.) - * @param value Value to set it as (only pass int, bool, float or entity index) - * - * @error Invalid handle. Invalid param number. Invalid param type. - * @noreturn -*/ -native void DHookSetParam(Handle hParams, int num, any value); - -/* Set vector param value - * - * @param hParams Handle to params structure - * @params num Param number to set (Example if the function has 2 params and you need to set the value of the first param num would be 1.) - * @param vec Value to set vector as. - * - * @error Invalid handle. Invalid param number. Invalid param type. - * @noreturn -*/ -native void DHookSetParamVector(Handle hParams, int num, float vec[3]); - -/* Set string param value - * - * @param hParams Handle to params structure - * @params num Param number to set (Example if the function has 2 params and you need to set the value of the first param num would be 1.) - * @param value Value to set string as. - * - * @error Invalid handle. Invalid param number. Invalid param type. - * @noreturn -*/ -native void DHookSetParamString(Handle hParams, int num, char[] value); - -/* Get return value (Only use for: int, entity, bool or float return types) - * - * @param hReturn Handle to return structure - * - * @error Invalid Handle, invalid type. - * @return Returns default value if prehook returns actual value if post hook. -*/ -native any DHookGetReturn(Handle hReturn); - -/* Get return vector value - * - * @param hReturn Handle to return structure - * @param vec Vector buffer to store result in. (In pre hooks will be default value (0.0,0.0,0.0)) - * - * @error Invalid Handle, invalid type. - * @noreturn -*/ -native void DHookGetReturnVector(Handle hReturn, float vec[3]); - -/* Get return string value - * - * @param hReturn Handle to return structure - * @param buffer String buffer to store result in. (In pre hooks will be default value "") - * @param size String buffer size - * - * @error Invalid Handle, invalid type. - * @noreturn -*/ -native void DHookGetReturnString(Handle hReturn, char[] buffer, int size); - -/* Set return value (Only use for: int, entity, bool or float return types) - * - * @param hReturn Handle to return structure - * @param value Value to set return as - * - * @error Invalid Handle, invalid type. - * @noreturn -*/ -native void DHookSetReturn(Handle hReturn, any value); - -/* Set return vector value - * - * @param hReturn Handle to return structure - * @param vec Value to set return vector as - * - * @error Invalid Handle, invalid type. - * @noreturn -*/ -native void DHookSetReturnVector(Handle hReturn, float vec[3]); - -/* Set return string value - * - * @param hReturn Handle to return structure - * @param value Value to set return string as - * - * @error Invalid Handle, invalid type. - * @noreturn -*/ -native void DHookSetReturnString(Handle hReturn, char[] value); - -//WE SHOULD WRAP THESE AROUND STOCKS FOR NON PTR AS WE SUPPORT BOTH WITH THESE NATIVE'S - -/* Gets an objects variable value - * - * @param hParams Handle to params structure - * @param num Param number to get. - * @param offset Offset within the object to the var to get. - * @param type Type of var it is - * - * @error Invalid handle. Invalid param number. Invalid param type. Invalid Object type. - * @return Value of the objects var. If EHANDLE type or entity returns entity index. -*/ -native any DHookGetParamObjectPtrVar(Handle hParams, int num, int offset, ObjectValueType type); - -/* Sets an objects variable value - * - * @param hParams Handle to params structure - * @param num Param number to set. - * @param offset Offset within the object to the var to set. - * @param type Type of var it is - * @param value The value to set the var to. - * - * @error Invalid handle. Invalid param number. Invalid param type. Invalid Object type. - * @noreturn -*/ -native void DHookSetParamObjectPtrVar(Handle hParams, int num, int offset, ObjectValueType type, any value); - -/* Gets an objects vector variable value - * - * @param hParams Handle to params structure - * @param num Param number to get. - * @param offset Offset within the object to the var to get. - * @param type Type of var it is - * @param buffer Buffer to store the result vector - * - * @error Invalid handle. Invalid param number. Invalid param type. Invalid Object type. - * @noreturn -*/ -native void DHookGetParamObjectPtrVarVector(Handle hParams, int num, int offset, ObjectValueType type, float buffer[3]); - -/* Sets an objects vector variable value - * - * @param hParams Handle to params structure - * @param num Param number to set. - * @param offset Offset within the object to the var to set. - * @param type Type of var it is - * @param value The value to set the vector var to. - * - * @error Invalid handle. Invalid param number. Invalid param type. Invalid Object type. - * @noreturn -*/ -native void DHookSetParamObjectPtrVarVector(Handle hParams, int num, int offset, ObjectValueType type, float value[3]); - -/* Gets an objects string variable value - * - * @param hParams Handle to params structure - * @param num Param number to get. - * @param offset Offset within the object to the var to get. - * @param type Type of var it is - * @param buffer Buffer to store the result vector - * @param size Size of the buffer - * - * @error Invalid handle. Invalid param number. Invalid param type. Invalid Object type. - * @noreturn -*/ -native void DHookGetParamObjectPtrString(Handle hParams, int num, int offset, ObjectValueType type, char[] buffer, int size); - -/* Checks if a pointer param is null - * - * @param hParams Handle to params structure - * @param num Param number to check. - * - * @error Non pointer param - * @return True if null false otherwise. -*/ -native bool DHookIsNullParam(Handle hParams, int num); - -public Extension __ext_dhooks = -{ - name = "dhooks", - file = "dhooks.ext", -#if defined AUTOLOAD_EXTENSIONS - autoload = 1, -#else - autoload = 0, -#endif -#if defined REQUIRE_EXTENSIONS - required = 1, -#else - required = 0, -#endif -}; - -#if !defined REQUIRE_EXTENSIONS -public __ext_dhooks_SetNTVOptional() -{ - MarkNativeAsOptional("DHookAddEntityListener"); - MarkNativeAsOptional("DHookRemoveEntityListener"); - MarkNativeAsOptional("DHookCreate"); - MarkNativeAsOptional("DHookAddParam"); - MarkNativeAsOptional("DHookEntity"); - MarkNativeAsOptional("DHookGamerules"); - MarkNativeAsOptional("DHookRaw"); - MarkNativeAsOptional("DHookRemoveHookID"); - MarkNativeAsOptional("DHookGetParam"); - MarkNativeAsOptional("DHookGetParamVector"); - MarkNativeAsOptional("DHookGetParamString"); - MarkNativeAsOptional("DHookSetParam"); - MarkNativeAsOptional("DHookSetParamVector"); - MarkNativeAsOptional("DHookSetParamString"); - MarkNativeAsOptional("DHookGetReturn"); - MarkNativeAsOptional("DHookGetReturnVector"); - MarkNativeAsOptional("DHookGetReturnString"); - MarkNativeAsOptional("DHookSetReturn"); - MarkNativeAsOptional("DHookSetReturnVector"); - MarkNativeAsOptional("DHookSetReturnString"); - MarkNativeAsOptional("DHookGetParamObjectPtrVar"); - MarkNativeAsOptional("DHookSetParamObjectPtrVar"); - MarkNativeAsOptional("DHookGetParamObjectPtrVarVector"); - MarkNativeAsOptional("DHookSetParamObjectPtrVarVector"); - MarkNativeAsOptional("DHookIsNullParam"); - MarkNativeAsOptional("DHookGetParamObjectPtrString"); -} -#endif +#if defined _dhooks_included +#endinput +#endif +#define _dhooks_included + +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 +}; + +enum ListenType +{ + ListenType_Created, + ListenType_Deleted +}; + +enum ReturnType +{ + ReturnType_Unknown, + ReturnType_Void, + ReturnType_Int, + ReturnType_Bool, + ReturnType_Float, + ReturnType_String, //Note this is a string_t + ReturnType_StringPtr, //Note this is a string_t * + ReturnType_CharPtr, + ReturnType_Vector, + ReturnType_VectorPtr, + ReturnType_CBaseEntity, + ReturnType_Edict +}; + +enum HookParamType +{ + HookParamType_Unknown, + HookParamType_Int, + HookParamType_Bool, + HookParamType_Float, + HookParamType_String, //Note this is a string_t + HookParamType_StringPtr, //Note this is a string_t * + HookParamType_CharPtr, + HookParamType_VectorPtr, + HookParamType_CBaseEntity, + HookParamType_ObjectPtr, + HookParamType_Edict, + HookParamType_Object +}; + +enum ThisPointerType +{ + ThisPointer_Ignore, + ThisPointer_CBaseEntity, + ThisPointer_Address +}; + +enum HookType +{ + HookType_Entity, + HookType_GameRules, + HookType_Raw +}; + +enum CallingConvention +{ + CallConv_CDECL, + CallConv_THISCALL, + CallConv_STDCALL, + CallConv_FASTCALL, +}; + +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 +}; + +enum DHookPassFlag +{ + DHookPass_ByVal = (1<<0), /**< Passing by value */ + DHookPass_ByRef = (1<<1), /**< Passing by reference */ + DHookPass_ODTOR = (1<<2), /**< Object has a destructor */ + DHookPass_OCTOR = (1<<3), /**< Object has a constructor */ + DHookPass_OASSIGNOP = (1<<4), /**< Object has an assignment operator */ +}; + +enum DHookRegister +{ + // Don't change the register and use the default for the calling convention. + DHookRegister_Default, + + // 8-bit general purpose registers + DHookRegister_AL, + DHookRegister_CL, + DHookRegister_DL, + DHookRegister_BL, + DHookRegister_AH, + DHookRegister_CH, + DHookRegister_DH, + DHookRegister_BH, + + // 32-bit general purpose registers + DHookRegister_EAX, + DHookRegister_ECX, + DHookRegister_EDX, + DHookRegister_EBX, + DHookRegister_ESP, + DHookRegister_EBP, + DHookRegister_ESI, + DHookRegister_EDI, + + // 128-bit XMM registers + DHookRegister_XMM0, + DHookRegister_XMM1, + DHookRegister_XMM2, + DHookRegister_XMM3, + DHookRegister_XMM4, + DHookRegister_XMM5, + DHookRegister_XMM6, + DHookRegister_XMM7, + + // 80-bit FPU registers + DHookRegister_ST0 +}; + +typeset ListenCB +{ + //Deleted + function void (int entity); + + //Created + function void (int entity, const char[] classname); +}; + +typeset DHookRemovalCB +{ + function void (int hookid); +}; +typeset DHookCallback +{ + //Function Example: void Ham::Test() with this pointer ignore + function MRESReturn (); + + //Function Example: void Ham::Test() with this pointer passed + function MRESReturn (int pThis); + + //Function Example: void Ham::Test(int cake) with this pointer ignore + function MRESReturn (Handle hParams); + + //Function Example: void Ham::Test(int cake) with this pointer passed + function MRESReturn (int pThis, Handle hParams); + + //Function Example: int Ham::Test() with this pointer ignore + function MRESReturn (Handle hReturn); + + //Function Example: int Ham::Test() with this pointer passed + function MRESReturn (int pThis, Handle hReturn); + + //Function Example: int Ham::Test(int cake) with this pointer ignore + function MRESReturn (Handle hReturn, Handle hParams); + + //Function Example: int Ham::Test(int cake) with this pointer passed + function MRESReturn (int pThis, Handle hReturn, Handle hParams); + + //Address NOW + + //Function Example: void Ham::Test() with this pointer passed + function MRESReturn (Address pThis); + + //Function Example: void Ham::Test(int cake) with this pointer passed + function MRESReturn (Address pThis, Handle hParams); + + //Function Example: int Ham::Test() with this pointer passed + function MRESReturn (Address pThis, Handle hReturn); + + //Function Example: int Ham::Test(int cake) with this pointer passed + function MRESReturn (Address pThis, Handle hReturn, Handle hParams); + +}; + +/* Adds an entity listener hook + * + * @param type Type of listener to add + * @param callback Callback to use + * + * @noreturn +*/ +native void DHookAddEntityListener(ListenType type, ListenCB callback); + +/* Removes an entity listener hook + * + * @param type Type of listener to remove + * @param callback Callback this listener was using + * + * @return True if one was removed false otherwise. +*/ +native bool DHookRemoveEntityListener(ListenType type, ListenCB callback); + +/* Creates a hook + * + * @param offset vtable offset for function to hook + * @param hooktype Type of hook + * @param returntype Type type of return + * @param thistype Type of this pointer or ignore (ignore can be used if not needed) + * @param callback Optional callback function, if not set here must be set when hooking. + * + * @return Returns setup handle for the hook. + * @error Failed to create hook setup handle or invalid callback function. +*/ +native Handle DHookCreate(int offset, HookType hooktype, ReturnType returntype, ThisPointerType thistype, DHookCallback callback=INVALID_FUNCTION); + +/** + * Creates a detour + * + * @param funcaddr The address of the function to detour. + * Can be Address_Null if you want to load the address from gamedata using DHookSetFromConf. + * @param callConv Calling convention of the function. + * @param returnType Type of the return value. + * @param thisType Type of this pointer or ignore (ignore can be used if not needed) + * + * @return Setup handle for the detour. + * @error Failed to create detour setup handle. + */ +native Handle DHookCreateDetour(Address funcaddr, CallingConvention callConv, ReturnType returntype, ThisPointerType thisType); + +/** + * Setup a detour or hook for a function as described in a "Functions" section in gamedata. + * + * @param gameconf GameConfig handle + * @param name Name of the function in the gamedata to load. + * + * @return Setup handle for the detour or INVALID_HANDLE if offset/signature/address wasn't found. + * @error Failed to create detour setup handle, invalid gamedata handle, invalid callback function or failed to find function in gamedata. + */ +native Handle DHookCreateFromConf(Handle gameconf, const char[] name); + +/** + * Load details for a vhook or detour from a gamedata file. + * + * @param setup Hook setup handle to set the offset or address on. + * @param gameconf GameConfig handle + * @param source Whether to look in Offsets or Signatures. + * @param name Name of the property to find. + * + * @return True on success, false if nothing was found. + * @error Invalid setup or gamedata handle. + */ +native bool DHookSetFromConf(Handle setup, Handle gameconf, SDKFuncConfSource source, const char[] name); + +/** + * Enable the detour of the function described in the hook setup handle. + * + * @param setup Hook setup handle + * @param post True to make the hook a post hook. (If you need to change the retunr value or need the return value use a post hook! If you need to change params and return use a pre and post hook!) + * @param callback Callback function + * + * @return True if detour was enabled, false otherwise. + * @error Hook handle is not setup for a detour. + */ +native bool DHookEnableDetour(Handle setup, bool post, DHookCallback callback); + +/** + * Disable the detour of the function described in the hook setup handle. + * + * @param setup Hook setup handle + * @param post True to disable a post hook. + * @param callback Callback function + * + * @return True if detour was disabled, false otherwise. + * @error Hook handle is not setup for a detour or function is not detoured. + */ +native bool DHookDisableDetour(Handle setup, bool post, DHookCallback callback); + +/* Adds param to a hook setup + * + * @param setup Setup handle to add the param to. + * @param type Param type + * @param size Used for Objects (not Object ptr) to define the size of the object. + * @param flag Used to change the pass type. + * @param custom_register The register this argument is passed in instead of the stack. + * + * @error Invalid setup handle or too many params added (request upping the max in thread) + * @noreturn +*/ +native void DHookAddParam(Handle setup, HookParamType type, int size=-1, DHookPassFlag flag=DHookPass_ByVal, DHookRegister custom_register=DHookRegister_Default); + +/* Hook entity + * + * @param setup Setup handle to use to add the hook. + * @param post True to make the hook a post hook. (If you need to change the return value or need the return value use a post hook! If you need to change params and return use a pre and post hook!) + * @param entity Entity index to hook on. + * @param removalcb Callback for when the hook is removed (Entity hooks are auto-removed on entity destroyed and will call this callback) + * @param callback Optional callback function, if not set here must be set when creating the hook. + * + * @error Invalid setup handle, invalid entity, invalid hook type or invalid callback. + * @return -1 on fail a hookid on success +*/ +native int DHookEntity(Handle setup, bool post, int entity, DHookRemovalCB removalcb=INVALID_FUNCTION, DHookCallback callback=INVALID_FUNCTION); + +/* Hook gamerules + * + * @param setup Setup handle to use to add the hook. + * @param post True to make the hook a post hook. (If you need to change the return value or need the return value use a post hook! If you need to change params and return use a pre and post hook!) + * @param removalcb Callback for when the hook is removed (Game rules hooks are auto-removed on map end and will call this callback) + * @param callback Optional callback function, if not set here must be set when creating the hook. + * + * @error Invalid setup handle, failing to get gamerules pointer, invalid hook type or invalid callback. + * @return -1 on fail a hookid on success +*/ +native int DHookGamerules(Handle setup, bool post, DHookRemovalCB removalcb=INVALID_FUNCTION, DHookCallback callback=INVALID_FUNCTION); + +/* Hook a raw pointer + * + * @param setup Setup handle to use to add the hook. + * @param post True to make the hook a post hook. (If you need to change the return value or need the return value use a post hook! If you need to change params and return use a pre and post hook!) + * @param addr This pointer address. + * @param removalcb Callback for when the hook is removed (Entity hooks are auto-removed on entity destroyed and will call this callback) + * @param callback Optional callback function, if not set here must be set when creating the hook. + * + * @error Invalid setup handle, invalid address, invalid hook type or invalid callback. + * @return -1 on fail a hookid on success +*/ +native int DHookRaw(Handle setup, bool post, Address addr, DHookRemovalCB removalcb=INVALID_FUNCTION, DHookCallback callback=INVALID_FUNCTION); + +/* Remove hook by hook id + * + * @param hookid Hook id to remove + * + * @return true on success false otherwise + * @note This will not fire the removal callback! +*/ +native bool DHookRemoveHookID(int hookid); + +/* Get param value (Only use for: int, entity, bool or float param types) + * + * @param hParams Handle to params structure + * @param num Param number to get. (Example if the function has 2 params and you need the value of the first param num would be 1. 0 Will return the number of params stored) + * + * @error Invalid handle. Invalid param number. Invalid param type. + * @return value if num greater than 0. If 0 returns paramcount. +*/ +native any DHookGetParam(Handle hParams, int num); + +/* Get vector param value + * + * @param hParams Handle to params structure + * @param num Param number to get. (Example if the function has 2 params and you need the value of the first param num would be 1.) + * @param vec Vector buffer to store result. + * + * @error Invalid handle. Invalid param number. Invalid param type. + * @noreturn +*/ +native void DHookGetParamVector(Handle hParams, int num, float vec[3]); + +/* Get string param value + * + * @param hParams Handle to params structure + * @param num Param number to get. (Example if the function has 2 params and you need the value of the first param num would be 1.) + * @param buffer String buffer to store result + * @param size Buffer size + * + * @error Invalid handle. Invalid param number. Invalid param type. + * @noreturn +*/ +native void DHookGetParamString(Handle hParams, int num, char[] buffer, int size); + +/* Set param value (Only use for: int, entity, bool or float param types) + * + * @param hParams Handle to params structure + * @params num Param number to set (Example if the function has 2 params and you need to set the value of the first param num would be 1.) + * @param value Value to set it as (only pass int, bool, float or entity index) + * + * @error Invalid handle. Invalid param number. Invalid param type. + * @noreturn +*/ +native void DHookSetParam(Handle hParams, int num, any value); + +/* Set vector param value + * + * @param hParams Handle to params structure + * @params num Param number to set (Example if the function has 2 params and you need to set the value of the first param num would be 1.) + * @param vec Value to set vector as. + * + * @error Invalid handle. Invalid param number. Invalid param type. + * @noreturn +*/ +native void DHookSetParamVector(Handle hParams, int num, float vec[3]); + +/* Set string param value + * + * @param hParams Handle to params structure + * @params num Param number to set (Example if the function has 2 params and you need to set the value of the first param num would be 1.) + * @param value Value to set string as. + * + * @error Invalid handle. Invalid param number. Invalid param type. + * @noreturn +*/ +native void DHookSetParamString(Handle hParams, int num, char[] value); + +/* Get return value (Only use for: int, entity, bool or float return types) + * + * @param hReturn Handle to return structure + * + * @error Invalid Handle, invalid type. + * @return Returns default value if prehook returns actual value if post hook. +*/ +native any DHookGetReturn(Handle hReturn); + +/* Get return vector value + * + * @param hReturn Handle to return structure + * @param vec Vector buffer to store result in. (In pre hooks will be default value (0.0,0.0,0.0)) + * + * @error Invalid Handle, invalid type. + * @noreturn +*/ +native void DHookGetReturnVector(Handle hReturn, float vec[3]); + +/* Get return string value + * + * @param hReturn Handle to return structure + * @param buffer String buffer to store result in. (In pre hooks will be default value "") + * @param size String buffer size + * + * @error Invalid Handle, invalid type. + * @noreturn +*/ +native void DHookGetReturnString(Handle hReturn, char[] buffer, int size); + +/* Set return value (Only use for: int, entity, bool or float return types) + * + * @param hReturn Handle to return structure + * @param value Value to set return as + * + * @error Invalid Handle, invalid type. + * @noreturn +*/ +native void DHookSetReturn(Handle hReturn, any value); + +/* Set return vector value + * + * @param hReturn Handle to return structure + * @param vec Value to set return vector as + * + * @error Invalid Handle, invalid type. + * @noreturn +*/ +native void DHookSetReturnVector(Handle hReturn, float vec[3]); + +/* Set return string value + * + * @param hReturn Handle to return structure + * @param value Value to set return string as + * + * @error Invalid Handle, invalid type. + * @noreturn +*/ +native void DHookSetReturnString(Handle hReturn, char[] value); + +//WE SHOULD WRAP THESE AROUND STOCKS FOR NON PTR AS WE SUPPORT BOTH WITH THESE NATIVE'S + +/* Gets an objects variable value + * + * @param hParams Handle to params structure + * @param num Param number to get. + * @param offset Offset within the object to the var to get. + * @param type Type of var it is + * + * @error Invalid handle. Invalid param number. Invalid param type. Invalid Object type. + * @return Value of the objects var. If EHANDLE type or entity returns entity index. +*/ +native any DHookGetParamObjectPtrVar(Handle hParams, int num, int offset, ObjectValueType type); + +/* Sets an objects variable value + * + * @param hParams Handle to params structure + * @param num Param number to set. + * @param offset Offset within the object to the var to set. + * @param type Type of var it is + * @param value The value to set the var to. + * + * @error Invalid handle. Invalid param number. Invalid param type. Invalid Object type. + * @noreturn +*/ +native void DHookSetParamObjectPtrVar(Handle hParams, int num, int offset, ObjectValueType type, any value); + +/* Gets an objects vector variable value + * + * @param hParams Handle to params structure + * @param num Param number to get. + * @param offset Offset within the object to the var to get. + * @param type Type of var it is + * @param buffer Buffer to store the result vector + * + * @error Invalid handle. Invalid param number. Invalid param type. Invalid Object type. + * @noreturn +*/ +native void DHookGetParamObjectPtrVarVector(Handle hParams, int num, int offset, ObjectValueType type, float buffer[3]); + +/* Sets an objects vector variable value + * + * @param hParams Handle to params structure + * @param num Param number to set. + * @param offset Offset within the object to the var to set. + * @param type Type of var it is + * @param value The value to set the vector var to. + * + * @error Invalid handle. Invalid param number. Invalid param type. Invalid Object type. + * @noreturn +*/ +native void DHookSetParamObjectPtrVarVector(Handle hParams, int num, int offset, ObjectValueType type, float value[3]); + +/* Gets an objects string variable value + * + * @param hParams Handle to params structure + * @param num Param number to get. + * @param offset Offset within the object to the var to get. + * @param type Type of var it is + * @param buffer Buffer to store the result vector + * @param size Size of the buffer + * + * @error Invalid handle. Invalid param number. Invalid param type. Invalid Object type. + * @noreturn +*/ +native void DHookGetParamObjectPtrString(Handle hParams, int num, int offset, ObjectValueType type, char[] buffer, int size); + +/* Checks if a pointer param is null + * + * @param hParams Handle to params structure + * @param num Param number to check. + * + * @error Non pointer param + * @return True if null false otherwise. +*/ +native bool DHookIsNullParam(Handle hParams, int num); + +public Extension __ext_dhooks = +{ + name = "dhooks", + file = "dhooks.ext", +#if defined AUTOLOAD_EXTENSIONS + autoload = 1, +#else + autoload = 0, +#endif +#if defined REQUIRE_EXTENSIONS + required = 1, +#else + required = 0, +#endif +}; + +#if !defined REQUIRE_EXTENSIONS +public __ext_dhooks_SetNTVOptional() +{ + MarkNativeAsOptional("DHookAddEntityListener"); + MarkNativeAsOptional("DHookRemoveEntityListener"); + MarkNativeAsOptional("DHookCreate"); + MarkNativeAsOptional("DHookCreateDetour"); + MarkNativeAsOptional("DHookCreateFromConf"); + MarkNativeAsOptional("DHookSetFromConf"); + MarkNativeAsOptional("DHookEnableDetour"); + MarkNativeAsOptional("DHookDisableDetour"); + MarkNativeAsOptional("DHookAddParam"); + MarkNativeAsOptional("DHookEntity"); + MarkNativeAsOptional("DHookGamerules"); + MarkNativeAsOptional("DHookRaw"); + MarkNativeAsOptional("DHookRemoveHookID"); + MarkNativeAsOptional("DHookGetParam"); + MarkNativeAsOptional("DHookGetParamVector"); + MarkNativeAsOptional("DHookGetParamString"); + MarkNativeAsOptional("DHookSetParam"); + MarkNativeAsOptional("DHookSetParamVector"); + MarkNativeAsOptional("DHookSetParamString"); + MarkNativeAsOptional("DHookGetReturn"); + MarkNativeAsOptional("DHookGetReturnVector"); + MarkNativeAsOptional("DHookGetReturnString"); + MarkNativeAsOptional("DHookSetReturn"); + MarkNativeAsOptional("DHookSetReturnVector"); + MarkNativeAsOptional("DHookSetReturnString"); + MarkNativeAsOptional("DHookGetParamObjectPtrVar"); + MarkNativeAsOptional("DHookSetParamObjectPtrVar"); + MarkNativeAsOptional("DHookGetParamObjectPtrVarVector"); + MarkNativeAsOptional("DHookSetParamObjectPtrVarVector"); + MarkNativeAsOptional("DHookIsNullParam"); + MarkNativeAsOptional("DHookGetParamObjectPtrString"); +} +#endif \ No newline at end of file