From 4ee8a0f1feaaaea22dd73f43132830b243082a17 Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Mon, 21 Jun 2021 01:34:44 -0300 Subject: [PATCH 01/15] Implement "CBaseEntity::Fire" hooks. * Implement "CBaseEntity::Fire" hooks. * Added new hook table "GamedllFunc_CBaseEntity". * Added new hookchain argument type "ATYPE_VECTOR" --- .../extra/amxmodx/scripting/include/reapi.inc | 8 +++-- .../scripting/include/reapi_gamedll_const.inc | 28 +++++++++++++++ reapi/include/cssdk/dlls/regamedll_api.h | 17 +++++++++ reapi/src/hook_callback.cpp | 36 +++++++++++++++++++ reapi/src/hook_callback.h | 13 +++++-- reapi/src/hook_list.cpp | 9 +++++ reapi/src/hook_list.h | 12 +++++++ 7 files changed, 118 insertions(+), 5 deletions(-) diff --git a/reapi/extra/amxmodx/scripting/include/reapi.inc b/reapi/extra/amxmodx/scripting/include/reapi.inc index 86755b81..45338253 100644 --- a/reapi/extra/amxmodx/scripting/include/reapi.inc +++ b/reapi/extra/amxmodx/scripting/include/reapi.inc @@ -24,7 +24,8 @@ enum hooks_tables_e ht_grenade, ht_weaponbox, ht_weapon, - ht_gib + ht_gib, + ht_cbaseentity }; enum members_tables_e @@ -128,7 +129,8 @@ enum AType ATYPE_CLASSPTR, ATYPE_EDICT, ATYPE_EVARS, - ATYPE_BOOL + ATYPE_BOOL, + ATYPE_VECTOR }; enum HookChain @@ -146,7 +148,7 @@ enum HookChain * * @return Returns a hook handle. Use EnableHookChain/DisableHookChain to toggle the forward on or off */ -native HookChain:RegisterHookChain({EngineFunc, GamedllFunc, GamedllFunc_CBaseAnimating, GamedllFunc_CBasePlayer, GamedllFunc_CSGameRules, GamedllFunc_CGrenade, GamedllFunc_CWeaponBox, ReCheckerFunc, GamedllFunc_CBasePlayerWeapon, GamedllFunc_CGib}:function_id, const callback[], post = 0); +native HookChain:RegisterHookChain({EngineFunc, GamedllFunc, GamedllFunc_CBaseAnimating, GamedllFunc_CBasePlayer, GamedllFunc_CSGameRules, GamedllFunc_CGrenade, GamedllFunc_CWeaponBox, ReCheckerFunc, GamedllFunc_CBasePlayerWeapon, GamedllFunc_CGib, GamedllFunc_CBaseEntity}:function_id, const callback[], post = 0); /* * Stops a hook from triggering. diff --git a/reapi/extra/amxmodx/scripting/include/reapi_gamedll_const.inc b/reapi/extra/amxmodx/scripting/include/reapi_gamedll_const.inc index 0afc09d0..d31c51c0 100644 --- a/reapi/extra/amxmodx/scripting/include/reapi_gamedll_const.inc +++ b/reapi/extra/amxmodx/scripting/include/reapi_gamedll_const.inc @@ -844,6 +844,34 @@ enum GamedllFunc_CGib RG_CGib_WaitTillLand, }; +/** +* GamedllFunc CBaseEntity +*/ +enum GamedllFunc_CBaseEntity +{ + /* + * Description: - + * Return type: void + * Params: (pEntity, cShots, Float:vecSrc[3], Float:vecDirShooting[3], Float:vecSpread[3], Float:flDistance, iBulletType, iTracerFreq, iDamage, pevAttacker) + + */ + RG_CBaseEntity_FireBullets = BEGIN_FUNC_REGION(cbaseentity), + + /* + * Description: - + * Return type: void + * Params: (pEntity, cShots, Float:vecSrc[3], Float:vecDirShooting[3], Float:vecSpread[3], Float:flDistance, iTracerFreq, iDamage, pevAttacker) + */ + RG_CBaseEntity_FireBuckshots, + + /* + * Description: - + * Return type: Vector [3] + * Params: (pEntity, Float:vecSrc[3], Float:vecDirShooting[3], Float:vecSpread, Float:flDistance, iPenetration, iBulletType, iDamage, Float:flRangeModifier, pevAttacker, bool:bPistol, shared_rand) + */ + RG_CBaseEntity_FireBullets3, +}; + /** * GamedllFunc CSGameRules */ diff --git a/reapi/include/cssdk/dlls/regamedll_api.h b/reapi/include/cssdk/dlls/regamedll_api.h index e3af3029..a3597c89 100644 --- a/reapi/include/cssdk/dlls/regamedll_api.h +++ b/reapi/include/cssdk/dlls/regamedll_api.h @@ -489,6 +489,19 @@ typedef IHookChainRegistryClass IReGameHookRegi typedef IHookChainClass IReGameHook_CGib_WaitTillLand; typedef IHookChainRegistryClass IReGameHookRegistry_CGib_WaitTillLand; +// CBaseEntity::FireBullets hook +typedef IHookChainClass IReGameHook_CBaseEntity_FireBullets; +typedef IHookChainRegistryClass IReGameHookRegistry_CBaseEntity_FireBullets; + +// CBaseEntity::FireBuckshots hook +typedef IHookChainClass IReGameHook_CBaseEntity_FireBuckshots; +typedef IHookChainRegistryClass IReGameHookRegistry_CBaseEntity_FireBuckshots; + +// CBaseEntity::FireBullets3 hook +typedef IHookChainClass IReGameHook_CBaseEntity_FireBullets3; +typedef IHookChainRegistryClass IReGameHookRegistry_CBaseEntity_FireBullets3; + + class IReGameHookchains { public: virtual ~IReGameHookchains() {} @@ -611,6 +624,10 @@ class IReGameHookchains { virtual IReGameHookRegistry_CGib_Spawn *CGib_Spawn() = 0; virtual IReGameHookRegistry_CGib_BounceGibTouch *CGib_BounceGibTouch() = 0; virtual IReGameHookRegistry_CGib_WaitTillLand *CGib_WaitTillLand() = 0; + + virtual IReGameHookRegistry_CBaseEntity_FireBullets *CBaseEntity_FireBullets() = 0; + virtual IReGameHookRegistry_CBaseEntity_FireBuckshots *CBaseEntity_FireBuckshots() = 0; + virtual IReGameHookRegistry_CBaseEntity_FireBullets3 *CBaseEntity_FireBullets3() = 0; }; struct ReGameFuncs_t { diff --git a/reapi/src/hook_callback.cpp b/reapi/src/hook_callback.cpp index 4ca6a0a2..029b6b06 100644 --- a/reapi/src/hook_callback.cpp +++ b/reapi/src/hook_callback.cpp @@ -1135,6 +1135,42 @@ void SpawnRandomGibs(IReGameHook_SpawnRandomGibs *chain, entvars_t *pevVictim, i callVoidForward(RG_SpawnRandomGibs, original, indexOfEdict(pevVictim), cGibs, human); } +void CBaseEntity_FireBullets(IReGameHook_CBaseEntity_FireBullets *chain, CBaseEntity *pEntity, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iBulletType, int iTracerFreq, int iDamage, entvars_t *pevAttacker) +{ + Vector vecSrcCopy(vecSrc), vecDirShootingCopy(vecDirShooting), vecSpreadCopy(vecSpread); + + auto original = [chain, &vecSrcCopy, &vecDirShootingCopy, &vecSpreadCopy](int _pEntity, ULONG _cShots, cell _vecSrc, cell _vecDirShooting, cell _vecSpread, float _flDistance, int _iBulletType, int _iTracerFreq, int _iDamage, int _pevAttacker) + { + chain->callNext(getPrivate(_pEntity), _cShots, vecSrcCopy, vecDirShootingCopy, vecSpreadCopy, _flDistance, _iBulletType, _iTracerFreq, _iDamage, PEV(_pevAttacker)); + }; + + callVoidForward(RG_CBaseEntity_FireBullets, original, indexOfEdict(pEntity->pev), cShots, getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), getAmxVector(vecSpreadCopy), flDistance, iBulletType, iTracerFreq, iDamage, indexOfEdict(pevAttacker)); +} + +void CBaseEntity_FireBuckshots(IReGameHook_CBaseEntity_FireBuckshots *chain, CBaseEntity *pEntity, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iTracerFreq, int iDamage, entvars_t *pevAttacker) +{ + Vector vecSrcCopy(vecSrc), vecDirShootingCopy(vecDirShooting), vecSpreadCopy(vecSpread); + + auto original = [chain, &vecSrcCopy, &vecDirShootingCopy, &vecSpreadCopy](int _pEntity, ULONG _cShots, cell _vecSrc, cell _vecDirShooting, cell _vecSpread, float _flDistance, int _iTracerFreq, int _iDamage, int _pevAttacker) + { + chain->callNext(getPrivate(_pEntity), _cShots, vecSrcCopy, vecDirShootingCopy, vecSpreadCopy, _flDistance, _iTracerFreq, _iDamage, PEV(_pevAttacker)); + }; + + callVoidForward(RG_CBaseEntity_FireBuckshots, original, indexOfEdict(pEntity->pev), cShots, getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), getAmxVector(vecSpreadCopy), flDistance, iTracerFreq, iDamage, indexOfEdict(pevAttacker)); +} + +Vector &CBaseEntity_FireBullets3(IReGameHook_CBaseEntity_FireBullets3 *chain, CBaseEntity *pEntity, Vector &vecSrc, Vector &vecDirShooting, float vecSpread, float flDistance, int iPenetration, int iBulletType, int iDamage, float flRangeModifier, entvars_t *pevAttacker, bool bPistol, int shared_rand) +{ + Vector vecSrcCopy(vecSrc), vecDirShootingCopy(vecDirShooting); + + auto original = [chain, &vecSrcCopy, &vecDirShootingCopy](int _pEntity, cell _vecSrc, cell _vecDirShooting, float _vecSpread, float _flDistance, int _iPenetration, int _iBulletType, int _iDamage, float _flRangeModifier, int _pevAttacker, bool _bPistol, int _shared_rand) + { + return chain->callNext(getPrivate(_pEntity), vecSrcCopy, vecDirShootingCopy, _vecSpread, _flDistance, _iPenetration, _iBulletType, _iDamage, _flRangeModifier, PEV(_pevAttacker), _bPistol, _shared_rand); + }; + + return callForward(RG_CBaseEntity_FireBullets3, original, indexOfEdict(pEntity->pev), getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), vecSpread, flDistance, iPenetration, iBulletType, iDamage, flRangeModifier, indexOfEdict(pevAttacker), bPistol, shared_rand); +} + void CGib_Spawn(IReGameHook_CGib_Spawn *chain, CGib *pthis, const char *szGibModel) { auto original = [chain](int _pthis, const char *_szGibModel) diff --git a/reapi/src/hook_callback.h b/reapi/src/hook_callback.h index e3295299..0533733f 100644 --- a/reapi/src/hook_callback.h +++ b/reapi/src/hook_callback.h @@ -20,7 +20,8 @@ enum AType : uint8 ATYPE_CLASSPTR, ATYPE_EDICT, ATYPE_EVARS, - ATYPE_BOOL + ATYPE_BOOL, + ATYPE_VECTOR }; struct retval_t @@ -36,11 +37,13 @@ struct retval_t CBaseEntity* _classptr; edict_t* _edict; entvars_t* _pev; + Vector _vector; }; }; inline AType getApiType(int) { return ATYPE_INTEGER; } inline AType getApiType(unsigned) { return ATYPE_INTEGER; } +inline AType getApiType(ULONG) { return ATYPE_INTEGER; } inline AType getApiType(float) { return ATYPE_FLOAT; } inline AType getApiType(const char *) { return ATYPE_STRING; } inline AType getApiType(char[]) { return ATYPE_STRING; } @@ -48,6 +51,7 @@ inline AType getApiType(CBaseEntity *) { return ATYPE_CLASSPTR; } inline AType getApiType(edict_t *) { return ATYPE_EDICT; } inline AType getApiType(entvars_t *) { return ATYPE_EVARS; } inline AType getApiType(bool) { return ATYPE_BOOL; } +inline AType getApiType(Vector) { return ATYPE_VECTOR; } template inline AType getApiType(T *) { return ATYPE_INTEGER; } @@ -291,7 +295,7 @@ NOINLINE R DLLEXPORT _callForward(hook_t* hook, original_t original, f_args&&... template R callForward(size_t func, original_t original, f_args&&... args) { - static_assert(sizeof(R) <= sizeof(int), "invalid hookchain return type size > sizeof(int)"); + static_assert(sizeof(R) <= sizeof(Vector), "invalid hookchain return type size > sizeof(Vector)"); hookctx_t hookCtx(sizeof...(args), args...); hookctx_t* save = g_hookCtx; @@ -465,6 +469,11 @@ void CGib_Spawn(IReGameHook_CGib_Spawn *chain, CGib *pthis, const char *szGibMod void CGib_BounceGibTouch(IReGameHook_CGib_BounceGibTouch *chain, CGib *pthis, CBaseEntity *pOther); void CGib_WaitTillLand(IReGameHook_CGib_WaitTillLand *chain, CGib *pthis); +// regamedll functions - cbaseentity +void CBaseEntity_FireBullets(IReGameHook_CBaseEntity_FireBullets *chain, CBaseEntity *pPlayer, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iBulletType, int iTracerFreq, int iDamage, entvars_t *pevAttacker); +void CBaseEntity_FireBuckshots(IReGameHook_CBaseEntity_FireBuckshots *chain, CBaseEntity *pPlayer, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iTracerFreq, int iDamage, entvars_t *pevAttacker); +Vector &CBaseEntity_FireBullets3(IReGameHook_CBaseEntity_FireBullets3 *chain, CBaseEntity *pPlayer, Vector &vecSrc, Vector &vecDirShooting, float vecSpread, float flDistance, int iPenetration, int iBulletType, int iDamage, float flRangeModifier, entvars_t *pevAttacker, bool bPistol, int shared_rand); + extern int g_iClientStartSpeak; extern int g_iClientStopSpeak; diff --git a/reapi/src/hook_list.cpp b/reapi/src/hook_list.cpp index 568293ee..ffbbe881 100644 --- a/reapi/src/hook_list.cpp +++ b/reapi/src/hook_list.cpp @@ -4,6 +4,7 @@ inline size_t getFwdParamType(void(*)(int)) { return FP_CELL inline size_t getFwdParamType(void(*)(size_t)) { return FP_CELL; } inline size_t getFwdParamType(void(*)(short)) { return FP_CELL; } inline size_t getFwdParamType(void(*)(unsigned short)) { return FP_CELL; } +inline size_t getFwdParamType(void(*)(ULONG)) { return FP_CELL; } inline size_t getFwdParamType(void(*)(bool)) { return FP_CELL; } inline size_t getFwdParamType(void(*)(Vector&)) { return FP_ARRAY; } inline size_t getFwdParamType(void(*)(TeamName)) { return FP_CELL; } @@ -221,6 +222,12 @@ hook_t hooklist_gib[] = { DLL(CGib_WaitTillLand), }; +hook_t hooklist_cbaseentity[] = { + DLL(CBaseEntity_FireBullets), + DLL(CBaseEntity_FireBuckshots), + DLL(CBaseEntity_FireBullets3), +}; + #define RCHECK(h,...) { {}, {}, #h, "ReChecker", [](){ return api_cfg.hasRechecker(); }, ((!(RC_##h & (MAX_REGION_RANGE - 1)) ? regfunc::current_cell = 1, true : false) || (RC_##h & (MAX_REGION_RANGE - 1)) == regfunc::current_cell++) ? regfunc(h##__VA_ARGS__) : regfunc(#h#__VA_ARGS__), [](){ g_RecheckerHookchains->h()->registerHook(&h); }, [](){ g_RecheckerHookchains->h()->unregisterHook(&h); }, false} hook_t hooklist_rechecker[] = { RCHECK(FileConsistencyProcess, _AMXX), @@ -246,6 +253,7 @@ hook_t* hooklist_t::getHookSafe(size_t hook) CASE(weaponbox) CASE(weapon) CASE(gib) + CASE(cbaseentity) } return nullptr; @@ -265,6 +273,7 @@ void hooklist_t::clear() FOREACH_CLEAR(weaponbox); FOREACH_CLEAR(weapon); FOREACH_CLEAR(gib); + FOREACH_CLEAR(cbaseentity); } void hook_t::clear() diff --git a/reapi/src/hook_list.h b/reapi/src/hook_list.h index 59cd4e62..0e4841ff 100644 --- a/reapi/src/hook_list.h +++ b/reapi/src/hook_list.h @@ -37,6 +37,7 @@ extern hook_t hooklist_grenade[]; extern hook_t hooklist_weaponbox[]; extern hook_t hooklist_weapon[]; extern hook_t hooklist_gib[]; +extern hook_t hooklist_cbaseentity[]; enum { @@ -63,6 +64,7 @@ struct hooklist_t CASE(weaponbox) CASE(weapon) CASE(gib) + CASE(cbaseentity) } #undef CASE @@ -85,6 +87,7 @@ struct hooklist_t ht_weaponbox, ht_weapon, ht_gib, + ht_cbaseentity, }; }; @@ -262,6 +265,15 @@ enum GamedllFunc_CGib // [...] }; +enum GamedllFunc_CBaseEntity +{ + RG_CBaseEntity_FireBullets = BEGIN_FUNC_REGION(cbaseentity), + RG_CBaseEntity_FireBuckshots, + RG_CBaseEntity_FireBullets3, + + // [...] +}; + enum ReCheckerFunc { RC_FileConsistencyProcess = BEGIN_FUNC_REGION(rechecker), From b216d277eb7d27ffc5f98fb3ccb3ef71db392ec1 Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Mon, 21 Jun 2021 14:56:34 -0300 Subject: [PATCH 02/15] Minor change - Delete code --- reapi/src/hook_callback.cpp | 36 ------------------------------------ 1 file changed, 36 deletions(-) diff --git a/reapi/src/hook_callback.cpp b/reapi/src/hook_callback.cpp index 029b6b06..4ca6a0a2 100644 --- a/reapi/src/hook_callback.cpp +++ b/reapi/src/hook_callback.cpp @@ -1135,42 +1135,6 @@ void SpawnRandomGibs(IReGameHook_SpawnRandomGibs *chain, entvars_t *pevVictim, i callVoidForward(RG_SpawnRandomGibs, original, indexOfEdict(pevVictim), cGibs, human); } -void CBaseEntity_FireBullets(IReGameHook_CBaseEntity_FireBullets *chain, CBaseEntity *pEntity, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iBulletType, int iTracerFreq, int iDamage, entvars_t *pevAttacker) -{ - Vector vecSrcCopy(vecSrc), vecDirShootingCopy(vecDirShooting), vecSpreadCopy(vecSpread); - - auto original = [chain, &vecSrcCopy, &vecDirShootingCopy, &vecSpreadCopy](int _pEntity, ULONG _cShots, cell _vecSrc, cell _vecDirShooting, cell _vecSpread, float _flDistance, int _iBulletType, int _iTracerFreq, int _iDamage, int _pevAttacker) - { - chain->callNext(getPrivate(_pEntity), _cShots, vecSrcCopy, vecDirShootingCopy, vecSpreadCopy, _flDistance, _iBulletType, _iTracerFreq, _iDamage, PEV(_pevAttacker)); - }; - - callVoidForward(RG_CBaseEntity_FireBullets, original, indexOfEdict(pEntity->pev), cShots, getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), getAmxVector(vecSpreadCopy), flDistance, iBulletType, iTracerFreq, iDamage, indexOfEdict(pevAttacker)); -} - -void CBaseEntity_FireBuckshots(IReGameHook_CBaseEntity_FireBuckshots *chain, CBaseEntity *pEntity, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iTracerFreq, int iDamage, entvars_t *pevAttacker) -{ - Vector vecSrcCopy(vecSrc), vecDirShootingCopy(vecDirShooting), vecSpreadCopy(vecSpread); - - auto original = [chain, &vecSrcCopy, &vecDirShootingCopy, &vecSpreadCopy](int _pEntity, ULONG _cShots, cell _vecSrc, cell _vecDirShooting, cell _vecSpread, float _flDistance, int _iTracerFreq, int _iDamage, int _pevAttacker) - { - chain->callNext(getPrivate(_pEntity), _cShots, vecSrcCopy, vecDirShootingCopy, vecSpreadCopy, _flDistance, _iTracerFreq, _iDamage, PEV(_pevAttacker)); - }; - - callVoidForward(RG_CBaseEntity_FireBuckshots, original, indexOfEdict(pEntity->pev), cShots, getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), getAmxVector(vecSpreadCopy), flDistance, iTracerFreq, iDamage, indexOfEdict(pevAttacker)); -} - -Vector &CBaseEntity_FireBullets3(IReGameHook_CBaseEntity_FireBullets3 *chain, CBaseEntity *pEntity, Vector &vecSrc, Vector &vecDirShooting, float vecSpread, float flDistance, int iPenetration, int iBulletType, int iDamage, float flRangeModifier, entvars_t *pevAttacker, bool bPistol, int shared_rand) -{ - Vector vecSrcCopy(vecSrc), vecDirShootingCopy(vecDirShooting); - - auto original = [chain, &vecSrcCopy, &vecDirShootingCopy](int _pEntity, cell _vecSrc, cell _vecDirShooting, float _vecSpread, float _flDistance, int _iPenetration, int _iBulletType, int _iDamage, float _flRangeModifier, int _pevAttacker, bool _bPistol, int _shared_rand) - { - return chain->callNext(getPrivate(_pEntity), vecSrcCopy, vecDirShootingCopy, _vecSpread, _flDistance, _iPenetration, _iBulletType, _iDamage, _flRangeModifier, PEV(_pevAttacker), _bPistol, _shared_rand); - }; - - return callForward(RG_CBaseEntity_FireBullets3, original, indexOfEdict(pEntity->pev), getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), vecSpread, flDistance, iPenetration, iBulletType, iDamage, flRangeModifier, indexOfEdict(pevAttacker), bPistol, shared_rand); -} - void CGib_Spawn(IReGameHook_CGib_Spawn *chain, CGib *pthis, const char *szGibModel) { auto original = [chain](int _pthis, const char *_szGibModel) From 45094585ebd73acf31222f08df2bae93c609f5ee Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Mon, 21 Jun 2021 14:57:45 -0300 Subject: [PATCH 03/15] Minor change - Restore code --- reapi/src/hook_callback.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/reapi/src/hook_callback.cpp b/reapi/src/hook_callback.cpp index 4ca6a0a2..8f1adcfe 100644 --- a/reapi/src/hook_callback.cpp +++ b/reapi/src/hook_callback.cpp @@ -1165,6 +1165,42 @@ void CGib_WaitTillLand(IReGameHook_CGib_WaitTillLand *chain, CGib *pthis) callVoidForward(RG_CGib_WaitTillLand, original, indexOfEdict(pthis->pev)); } +void CBaseEntity_FireBullets(IReGameHook_CBaseEntity_FireBullets *chain, CBaseEntity *pEntity, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iBulletType, int iTracerFreq, int iDamage, entvars_t *pevAttacker) +{ + Vector vecSrcCopy(vecSrc), vecDirShootingCopy(vecDirShooting), vecSpreadCopy(vecSpread); + + auto original = [chain, &vecSrcCopy, &vecDirShootingCopy, &vecSpreadCopy](int _pEntity, ULONG _cShots, cell _vecSrc, cell _vecDirShooting, cell _vecSpread, float _flDistance, int _iBulletType, int _iTracerFreq, int _iDamage, int _pevAttacker) + { + chain->callNext(getPrivate(_pEntity), _cShots, vecSrcCopy, vecDirShootingCopy, vecSpreadCopy, _flDistance, _iBulletType, _iTracerFreq, _iDamage, PEV(_pevAttacker)); + }; + + callVoidForward(RG_CBaseEntity_FireBullets, original, indexOfEdict(pEntity->pev), cShots, getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), getAmxVector(vecSpreadCopy), flDistance, iBulletType, iTracerFreq, iDamage, indexOfEdict(pevAttacker)); +} + +void CBaseEntity_FireBuckshots(IReGameHook_CBaseEntity_FireBuckshots *chain, CBaseEntity *pEntity, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iTracerFreq, int iDamage, entvars_t *pevAttacker) +{ + Vector vecSrcCopy(vecSrc), vecDirShootingCopy(vecDirShooting), vecSpreadCopy(vecSpread); + + auto original = [chain, &vecSrcCopy, &vecDirShootingCopy, &vecSpreadCopy](int _pEntity, ULONG _cShots, cell _vecSrc, cell _vecDirShooting, cell _vecSpread, float _flDistance, int _iTracerFreq, int _iDamage, int _pevAttacker) + { + chain->callNext(getPrivate(_pEntity), _cShots, vecSrcCopy, vecDirShootingCopy, vecSpreadCopy, _flDistance, _iTracerFreq, _iDamage, PEV(_pevAttacker)); + }; + + callVoidForward(RG_CBaseEntity_FireBuckshots, original, indexOfEdict(pEntity->pev), cShots, getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), getAmxVector(vecSpreadCopy), flDistance, iTracerFreq, iDamage, indexOfEdict(pevAttacker)); +} + +Vector &CBaseEntity_FireBullets3(IReGameHook_CBaseEntity_FireBullets3 *chain, CBaseEntity *pEntity, Vector &vecSrc, Vector &vecDirShooting, float vecSpread, float flDistance, int iPenetration, int iBulletType, int iDamage, float flRangeModifier, entvars_t *pevAttacker, bool bPistol, int shared_rand) +{ + Vector vecSrcCopy(vecSrc), vecDirShootingCopy(vecDirShooting); + + auto original = [chain, &vecSrcCopy, &vecDirShootingCopy](int _pEntity, cell _vecSrc, cell _vecDirShooting, float _vecSpread, float _flDistance, int _iPenetration, int _iBulletType, int _iDamage, float _flRangeModifier, int _pevAttacker, bool _bPistol, int _shared_rand) + { + return chain->callNext(getPrivate(_pEntity), vecSrcCopy, vecDirShootingCopy, _vecSpread, _flDistance, _iPenetration, _iBulletType, _iDamage, _flRangeModifier, PEV(_pevAttacker), _bPistol, _shared_rand); + }; + + return callForward(RG_CBaseEntity_FireBullets3, original, indexOfEdict(pEntity->pev), getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), vecSpread, flDistance, iPenetration, iBulletType, iDamage, flRangeModifier, indexOfEdict(pevAttacker), bPistol, shared_rand); +} + int g_iClientStartSpeak, g_iClientStopSpeak; void OnClientStartSpeak(size_t clientIndex) From 1c2999706e3064951ce71423330371b787105681 Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Mon, 21 Jun 2021 20:28:38 -0300 Subject: [PATCH 04/15] Revert changes --- reapi/src/hook_callback.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reapi/src/hook_callback.h b/reapi/src/hook_callback.h index 0533733f..221a8045 100644 --- a/reapi/src/hook_callback.h +++ b/reapi/src/hook_callback.h @@ -295,7 +295,7 @@ NOINLINE R DLLEXPORT _callForward(hook_t* hook, original_t original, f_args&&... template R callForward(size_t func, original_t original, f_args&&... args) { - static_assert(sizeof(R) <= sizeof(Vector), "invalid hookchain return type size > sizeof(Vector)"); + static_assert(sizeof(R) <= sizeof(int), "invalid hookchain return type size > sizeof(int)"); hookctx_t hookCtx(sizeof...(args), args...); hookctx_t* save = g_hookCtx; From e7f3fafa3e67c36f09f2507493e4b0ddfed1fc2e Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Mon, 21 Jun 2021 20:30:30 -0300 Subject: [PATCH 05/15] Added callVectorForward function --- reapi/src/hook_callback.cpp | 2 +- reapi/src/hook_callback.h | 86 +++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/reapi/src/hook_callback.cpp b/reapi/src/hook_callback.cpp index 8f1adcfe..3615d26d 100644 --- a/reapi/src/hook_callback.cpp +++ b/reapi/src/hook_callback.cpp @@ -1198,7 +1198,7 @@ Vector &CBaseEntity_FireBullets3(IReGameHook_CBaseEntity_FireBullets3 *chain, CB return chain->callNext(getPrivate(_pEntity), vecSrcCopy, vecDirShootingCopy, _vecSpread, _flDistance, _iPenetration, _iBulletType, _iDamage, _flRangeModifier, PEV(_pevAttacker), _bPistol, _shared_rand); }; - return callForward(RG_CBaseEntity_FireBullets3, original, indexOfEdict(pEntity->pev), getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), vecSpread, flDistance, iPenetration, iBulletType, iDamage, flRangeModifier, indexOfEdict(pevAttacker), bPistol, shared_rand); + return callVectorForward(RG_CBaseEntity_FireBullets3, original, indexOfEdict(pEntity->pev), getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), vecSpread, flDistance, iPenetration, iBulletType, iDamage, flRangeModifier, indexOfEdict(pevAttacker), bPistol, shared_rand); } int g_iClientStartSpeak, g_iClientStopSpeak; diff --git a/reapi/src/hook_callback.h b/reapi/src/hook_callback.h index 221a8045..ecef26a5 100644 --- a/reapi/src/hook_callback.h +++ b/reapi/src/hook_callback.h @@ -311,6 +311,92 @@ R callForward(size_t func, original_t original, f_args&&... args) return ret; } +template +NOINLINE R DLLEXPORT _callVectorForward(hook_t* hook, original_t original, f_args&&... args) +{ + auto hookCtx = g_hookCtx; + hookCtx->reset(getApiType(R())); + + int hc_state = HC_CONTINUE; + + hook->wasCalled = false; + + for (auto fwd : hook->pre) + { + if (likely(fwd->GetState() == FSTATE_ENABLED)) + { + hookCtx->SetId(fwd->GetIndex()); // set current handler hook + auto ret = g_amxxapi.ExecuteForward(fwd->GetFwdIndex(), std::forward(args)...); + hookCtx->ResetId(); + + if (unlikely(ret != HC_SUPERCEDE && ret != HC_BREAK)) { + continue; + } + + if (unlikely(!hookCtx->retVal.set)) { + fwd->Error(AMX_ERR_ASSERT, "Can't suppress original function call without new return value set, so you must call SetHookChainReturn."); + continue; + } + + if (unlikely(ret == HC_BREAK)) { + return *(R*)&hookCtx->retVal._vector; + } + + if (unlikely(ret > hc_state)) + hc_state = ret; + } + } + + if (likely(hc_state != HC_SUPERCEDE)) + { + g_hookCtx = nullptr; + auto retVal = original(std::forward(args)...); + g_hookCtx = hookCtx; + hook->wasCalled = true; + + if (unlikely(!hookCtx->retVal.set)) { + hookCtx->retVal._vector = *(Vector*)&retVal; + hookCtx->retVal.set = true; + } + } + + for (auto fwd : hook->post) + { + if (likely(fwd->GetState() == FSTATE_ENABLED)) + { + hookCtx->SetId(fwd->GetIndex()); // set current handler hook + auto ret = g_amxxapi.ExecuteForward(fwd->GetFwdIndex(), std::forward(args)...); + hookCtx->ResetId(); + + if (unlikely(ret == HC_BREAK)) + break; + } + } + + hook->wasCalled = false; + + return *(R*)&hookCtx->retVal._vector; +} + +template +R callVectorForward(size_t func, original_t original, f_args&&... args) +{ + static_assert(sizeof(R) <= sizeof(Vector), "invalid hookchain return type size > sizeof(Vector)"); + + hookctx_t hookCtx(sizeof...(args), args...); + hookctx_t* save = g_hookCtx; + + g_hookCtx = &hookCtx; + auto ret = _callVectorForward(g_hookManager.getHookFast(func), original, args...); + g_hookCtx = save; + + if (hasStringArgs(args...)) { + hookCtx.clear_temp_strings(); + } + + return ret; +} + template struct hookdata_t { From a661a40edcdfbb1a9a729653463a7d7b6d28f3ae Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Mon, 21 Jun 2021 20:31:46 -0300 Subject: [PATCH 06/15] Added ATYPE_VECTOR to array in getATypeStr function --- reapi/src/reapi_utils.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/reapi/src/reapi_utils.cpp b/reapi/src/reapi_utils.cpp index e0b20162..77c0693a 100644 --- a/reapi/src/reapi_utils.cpp +++ b/reapi/src/reapi_utils.cpp @@ -231,7 +231,8 @@ const char *getATypeStr(AType type) "ATYPE_CLASSPTR", "ATYPE_EDICT", "ATYPE_EVARS", - "ATYPE_BOOL" + "ATYPE_BOOL", + "ATYPE_VECTOR" }; if (type >= arraysize(s_ATypes)) From 75fbb36392154dbaf20b9a9b2a3856339f11c9a5 Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Mon, 21 Jun 2021 20:34:16 -0300 Subject: [PATCH 07/15] Added ATYPE_VECTOR case in Get|Set natives --- reapi/src/natives/natives_hookchains.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/reapi/src/natives/natives_hookchains.cpp b/reapi/src/natives/natives_hookchains.cpp index 058c8e72..94ecbd71 100644 --- a/reapi/src/natives/natives_hookchains.cpp +++ b/reapi/src/natives/natives_hookchains.cpp @@ -165,6 +165,15 @@ cell AMX_NATIVE_CALL SetHookChainReturn(AMX *amx, cell *params) case ATYPE_EVARS: retVal._pev = PEV(*srcAddr); break; + case ATYPE_VECTOR: + { + REAL fX = *((float*)&srcAddr[0]); + REAL fY = *((float*)&srcAddr[1]); + REAL fZ = *((float*)&srcAddr[2]); + Vector vSet = Vector(fX, fY, fZ); + retVal._vector = vSet; + break; + } default: return FALSE; } @@ -236,6 +245,14 @@ cell AMX_NATIVE_CALL GetHookChainReturn(AMX *amx, cell *params) return indexOfEdict(retVal._edict); case ATYPE_EVARS: return indexOfEdict(retVal._pev); + case ATYPE_VECTOR: + { + Vector& vRetVal = retVal._vector; + dstAddr[0] = *((cell*)&vRetVal.x); + dstAddr[1] = *((cell*)&vRetVal.y); + dstAddr[2] = *((cell*)&vRetVal.z); + return TRUE; + } default: return FALSE; } From 9ca4380a01756e119919ba1847fe66b64b522930 Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Mon, 21 Jun 2021 23:20:57 -0300 Subject: [PATCH 08/15] Fix typo --- reapi/src/hook_callback.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/reapi/src/hook_callback.h b/reapi/src/hook_callback.h index ecef26a5..aa68a517 100644 --- a/reapi/src/hook_callback.h +++ b/reapi/src/hook_callback.h @@ -556,9 +556,9 @@ void CGib_BounceGibTouch(IReGameHook_CGib_BounceGibTouch *chain, CGib *pthis, CB void CGib_WaitTillLand(IReGameHook_CGib_WaitTillLand *chain, CGib *pthis); // regamedll functions - cbaseentity -void CBaseEntity_FireBullets(IReGameHook_CBaseEntity_FireBullets *chain, CBaseEntity *pPlayer, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iBulletType, int iTracerFreq, int iDamage, entvars_t *pevAttacker); -void CBaseEntity_FireBuckshots(IReGameHook_CBaseEntity_FireBuckshots *chain, CBaseEntity *pPlayer, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iTracerFreq, int iDamage, entvars_t *pevAttacker); -Vector &CBaseEntity_FireBullets3(IReGameHook_CBaseEntity_FireBullets3 *chain, CBaseEntity *pPlayer, Vector &vecSrc, Vector &vecDirShooting, float vecSpread, float flDistance, int iPenetration, int iBulletType, int iDamage, float flRangeModifier, entvars_t *pevAttacker, bool bPistol, int shared_rand); +void CBaseEntity_FireBullets(IReGameHook_CBaseEntity_FireBullets *chain, CBaseEntity *pEntity, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iBulletType, int iTracerFreq, int iDamage, entvars_t *pevAttacker); +void CBaseEntity_FireBuckshots(IReGameHook_CBaseEntity_FireBuckshots *chain, CBaseEntity *pEntity, ULONG cShots, Vector &vecSrc, Vector &vecDirShooting, Vector &vecSpread, float flDistance, int iTracerFreq, int iDamage, entvars_t *pevAttacker); +Vector &CBaseEntity_FireBullets3(IReGameHook_CBaseEntity_FireBullets3 *chain, CBaseEntity *pEntity, Vector &vecSrc, Vector &vecDirShooting, float vecSpread, float flDistance, int iPenetration, int iBulletType, int iDamage, float flRangeModifier, entvars_t *pevAttacker, bool bPistol, int shared_rand); extern int g_iClientStartSpeak; extern int g_iClientStopSpeak; From 189814ecb218c7f50646e845b42b97c6f3ec8e23 Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Mon, 21 Jun 2021 23:35:06 -0300 Subject: [PATCH 09/15] Fix warning C4172 --- reapi/src/hook_callback.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/reapi/src/hook_callback.cpp b/reapi/src/hook_callback.cpp index 3615d26d..5d19ca28 100644 --- a/reapi/src/hook_callback.cpp +++ b/reapi/src/hook_callback.cpp @@ -1198,7 +1198,10 @@ Vector &CBaseEntity_FireBullets3(IReGameHook_CBaseEntity_FireBullets3 *chain, CB return chain->callNext(getPrivate(_pEntity), vecSrcCopy, vecDirShootingCopy, _vecSpread, _flDistance, _iPenetration, _iBulletType, _iDamage, _flRangeModifier, PEV(_pevAttacker), _bPistol, _shared_rand); }; - return callVectorForward(RG_CBaseEntity_FireBullets3, original, indexOfEdict(pEntity->pev), getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), vecSpread, flDistance, iPenetration, iBulletType, iDamage, flRangeModifier, indexOfEdict(pevAttacker), bPistol, shared_rand); + static Vector vecRet; + vecRet = callVectorForward(RG_CBaseEntity_FireBullets3, original, indexOfEdict(pEntity->pev), getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), vecSpread, flDistance, iPenetration, iBulletType, iDamage, flRangeModifier, indexOfEdict(pevAttacker), bPistol, shared_rand); + + return vecRet; } int g_iClientStartSpeak, g_iClientStopSpeak; From 6893067a264187e00ff201996b6c14391242a900 Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Tue, 22 Jun 2021 01:55:44 -0300 Subject: [PATCH 10/15] Fix warning C2280 compiler --- reapi/src/hook_callback.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/reapi/src/hook_callback.h b/reapi/src/hook_callback.h index aa68a517..5de5a21c 100644 --- a/reapi/src/hook_callback.h +++ b/reapi/src/hook_callback.h @@ -37,7 +37,7 @@ struct retval_t CBaseEntity* _classptr; edict_t* _edict; entvars_t* _pev; - Vector _vector; + Vector* _vector; }; }; @@ -355,7 +355,7 @@ NOINLINE R DLLEXPORT _callVectorForward(hook_t* hook, original_t original, f_arg hook->wasCalled = true; if (unlikely(!hookCtx->retVal.set)) { - hookCtx->retVal._vector = *(Vector*)&retVal; + hookCtx->retVal._vector = &(Vector)retVal; hookCtx->retVal.set = true; } } From 469918e9fee866d13e745ebd85b7b978754992ef Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Tue, 22 Jun 2021 01:57:29 -0300 Subject: [PATCH 11/15] Little fixes in [Get|Set]HookChainReturn natives --- reapi/src/natives/natives_hookchains.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/reapi/src/natives/natives_hookchains.cpp b/reapi/src/natives/natives_hookchains.cpp index 94ecbd71..f1f1a26d 100644 --- a/reapi/src/natives/natives_hookchains.cpp +++ b/reapi/src/natives/natives_hookchains.cpp @@ -171,7 +171,7 @@ cell AMX_NATIVE_CALL SetHookChainReturn(AMX *amx, cell *params) REAL fY = *((float*)&srcAddr[1]); REAL fZ = *((float*)&srcAddr[2]); Vector vSet = Vector(fX, fY, fZ); - retVal._vector = vSet; + retVal._vector = &vSet; break; } default: @@ -247,10 +247,10 @@ cell AMX_NATIVE_CALL GetHookChainReturn(AMX *amx, cell *params) return indexOfEdict(retVal._pev); case ATYPE_VECTOR: { - Vector& vRetVal = retVal._vector; - dstAddr[0] = *((cell*)&vRetVal.x); - dstAddr[1] = *((cell*)&vRetVal.y); - dstAddr[2] = *((cell*)&vRetVal.z); + Vector* vRetVal = retVal._vector; + *dstAddr++ = *((cell*)&vRetVal->x); + *dstAddr++ = *((cell*)&vRetVal->y); + *dstAddr++ = *((cell*)&vRetVal->z); return TRUE; } default: From 38a34349c639343c3adae3da635af6f49c6cfebc Mon Sep 17 00:00:00 2001 From: s1lentq Date: Tue, 22 Jun 2021 22:01:11 +0700 Subject: [PATCH 12/15] Reworked return type Vector to hookchains --- reapi/src/hook_callback.cpp | 13 +-- reapi/src/hook_callback.h | 106 +++-------------------- reapi/src/natives/natives_hookchains.cpp | 28 +++--- reapi/src/natives/natives_hookchains.h | 15 ++++ reapi/src/natives/natives_misc.cpp | 2 +- 5 files changed, 49 insertions(+), 115 deletions(-) diff --git a/reapi/src/hook_callback.cpp b/reapi/src/hook_callback.cpp index 5d19ca28..1307ffb9 100644 --- a/reapi/src/hook_callback.cpp +++ b/reapi/src/hook_callback.cpp @@ -1193,15 +1193,18 @@ Vector &CBaseEntity_FireBullets3(IReGameHook_CBaseEntity_FireBullets3 *chain, CB { Vector vecSrcCopy(vecSrc), vecDirShootingCopy(vecDirShooting); - auto original = [chain, &vecSrcCopy, &vecDirShootingCopy](int _pEntity, cell _vecSrc, cell _vecDirShooting, float _vecSpread, float _flDistance, int _iPenetration, int _iBulletType, int _iDamage, float _flRangeModifier, int _pevAttacker, bool _bPistol, int _shared_rand) + auto original = [chain, &vecSrcCopy, &vecDirShootingCopy](int _pEntity, cell _vecSrc, cell _vecDirShooting, float _vecSpread, float _flDistance, int _iPenetration, int _iBulletType, int _iDamage, float _flRangeModifier, int _pevAttacker, bool _bPistol, int _shared_rand) -> Vector& { return chain->callNext(getPrivate(_pEntity), vecSrcCopy, vecDirShootingCopy, _vecSpread, _flDistance, _iPenetration, _iBulletType, _iDamage, _flRangeModifier, PEV(_pevAttacker), _bPistol, _shared_rand); }; - static Vector vecRet; - vecRet = callVectorForward(RG_CBaseEntity_FireBullets3, original, indexOfEdict(pEntity->pev), getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), vecSpread, flDistance, iPenetration, iBulletType, iDamage, flRangeModifier, indexOfEdict(pevAttacker), bPistol, shared_rand); - - return vecRet; + return callForward(RG_CBaseEntity_FireBullets3, original, + indexOfEdict(pEntity->pev), + getAmxVector(vecSrcCopy), getAmxVector(vecDirShootingCopy), + vecSpread, flDistance, iPenetration, iBulletType, iDamage, flRangeModifier, + indexOfEdict(pevAttacker), + bPistol, + shared_rand); } int g_iClientStartSpeak, g_iClientStopSpeak; diff --git a/reapi/src/hook_callback.h b/reapi/src/hook_callback.h index 5de5a21c..a0735b03 100644 --- a/reapi/src/hook_callback.h +++ b/reapi/src/hook_callback.h @@ -218,8 +218,10 @@ void callVoidForward(size_t func, original_t original, f_args&&... args) template NOINLINE R DLLEXPORT _callForward(hook_t* hook, original_t original, f_args&&... args) { + using R2 = std::decay::type; + auto hookCtx = g_hookCtx; - hookCtx->reset(getApiType(R())); + hookCtx->reset(getApiType(R2())); int hc_state = HC_CONTINUE; @@ -243,7 +245,7 @@ NOINLINE R DLLEXPORT _callForward(hook_t* hook, original_t original, f_args&&... } if (unlikely(ret == HC_BREAK)) { - return *(R *)&hookCtx->retVal._integer; + return *(R2 *)&hookCtx->retVal._integer; } if (unlikely(ret > hc_state)) @@ -254,7 +256,7 @@ NOINLINE R DLLEXPORT _callForward(hook_t* hook, original_t original, f_args&&... if (likely(hc_state != HC_SUPERCEDE)) { g_hookCtx = nullptr; - auto retVal = original(std::forward(args)...); + R retVal = original(std::forward(args)...); g_hookCtx = hookCtx; hook->wasCalled = true; @@ -269,6 +271,9 @@ NOINLINE R DLLEXPORT _callForward(hook_t* hook, original_t original, f_args&&... case sizeof(int32): hookCtx->retVal._integer = *(int32 *)&retVal; break; + case sizeof(Vector): + hookCtx->retVal._vector = (Vector *)&retVal; + break; } hookCtx->retVal.set = true; } @@ -289,105 +294,20 @@ NOINLINE R DLLEXPORT _callForward(hook_t* hook, original_t original, f_args&&... hook->wasCalled = false; - return *(R *)&hookCtx->retVal._integer; + if (std::is_reference::value) + return *(R2 *)hookCtx->retVal._integer; + else + return *(R2 *)&hookCtx->retVal._integer; } template R callForward(size_t func, original_t original, f_args&&... args) { - static_assert(sizeof(R) <= sizeof(int), "invalid hookchain return type size > sizeof(int)"); - - hookctx_t hookCtx(sizeof...(args), args...); - hookctx_t* save = g_hookCtx; - - g_hookCtx = &hookCtx; - auto ret = _callForward(g_hookManager.getHookFast(func), original, args...); - g_hookCtx = save; - - if (hasStringArgs(args...)) { - hookCtx.clear_temp_strings(); - } - - return ret; -} - -template -NOINLINE R DLLEXPORT _callVectorForward(hook_t* hook, original_t original, f_args&&... args) -{ - auto hookCtx = g_hookCtx; - hookCtx->reset(getApiType(R())); - - int hc_state = HC_CONTINUE; - - hook->wasCalled = false; - - for (auto fwd : hook->pre) - { - if (likely(fwd->GetState() == FSTATE_ENABLED)) - { - hookCtx->SetId(fwd->GetIndex()); // set current handler hook - auto ret = g_amxxapi.ExecuteForward(fwd->GetFwdIndex(), std::forward(args)...); - hookCtx->ResetId(); - - if (unlikely(ret != HC_SUPERCEDE && ret != HC_BREAK)) { - continue; - } - - if (unlikely(!hookCtx->retVal.set)) { - fwd->Error(AMX_ERR_ASSERT, "Can't suppress original function call without new return value set, so you must call SetHookChainReturn."); - continue; - } - - if (unlikely(ret == HC_BREAK)) { - return *(R*)&hookCtx->retVal._vector; - } - - if (unlikely(ret > hc_state)) - hc_state = ret; - } - } - - if (likely(hc_state != HC_SUPERCEDE)) - { - g_hookCtx = nullptr; - auto retVal = original(std::forward(args)...); - g_hookCtx = hookCtx; - hook->wasCalled = true; - - if (unlikely(!hookCtx->retVal.set)) { - hookCtx->retVal._vector = &(Vector)retVal; - hookCtx->retVal.set = true; - } - } - - for (auto fwd : hook->post) - { - if (likely(fwd->GetState() == FSTATE_ENABLED)) - { - hookCtx->SetId(fwd->GetIndex()); // set current handler hook - auto ret = g_amxxapi.ExecuteForward(fwd->GetFwdIndex(), std::forward(args)...); - hookCtx->ResetId(); - - if (unlikely(ret == HC_BREAK)) - break; - } - } - - hook->wasCalled = false; - - return *(R*)&hookCtx->retVal._vector; -} - -template -R callVectorForward(size_t func, original_t original, f_args&&... args) -{ - static_assert(sizeof(R) <= sizeof(Vector), "invalid hookchain return type size > sizeof(Vector)"); - hookctx_t hookCtx(sizeof...(args), args...); hookctx_t* save = g_hookCtx; g_hookCtx = &hookCtx; - auto ret = _callVectorForward(g_hookManager.getHookFast(func), original, args...); + R ret = _callForward(g_hookManager.getHookFast(func), original, args...); g_hookCtx = save; if (hasStringArgs(args...)) { diff --git a/reapi/src/natives/natives_hookchains.cpp b/reapi/src/natives/natives_hookchains.cpp index f1f1a26d..e0a1d9c9 100644 --- a/reapi/src/natives/natives_hookchains.cpp +++ b/reapi/src/natives/natives_hookchains.cpp @@ -102,6 +102,9 @@ cell AMX_NATIVE_CALL DisableHookChain(AMX *amx, cell *params) return TRUE; } +static CTempAnyData s_tmpVectors; +static CTempAnyData s_tmpStrings; + /* * Sets the return value of a hookchain. * @@ -148,12 +151,9 @@ cell AMX_NATIVE_CALL SetHookChainReturn(AMX *amx, cell *params) case ATYPE_STRING: { - if (retVal._string != nullptr) - delete[] retVal._string; - size_t len; const char *dest = getAmxString(srcAddr, string, &len); - retVal._string = strcpy(new char[len + 1], dest); + retVal._string = strcpy(s_tmpStrings.Alloc(), dest); break; } case ATYPE_CLASSPTR: @@ -166,14 +166,9 @@ cell AMX_NATIVE_CALL SetHookChainReturn(AMX *amx, cell *params) retVal._pev = PEV(*srcAddr); break; case ATYPE_VECTOR: - { - REAL fX = *((float*)&srcAddr[0]); - REAL fY = *((float*)&srcAddr[1]); - REAL fZ = *((float*)&srcAddr[2]); - Vector vSet = Vector(fX, fY, fZ); - retVal._vector = &vSet; + retVal._vector = s_tmpVectors.Alloc(); + *retVal._vector = *(Vector *)srcAddr; break; - } default: return FALSE; } @@ -233,7 +228,7 @@ cell AMX_NATIVE_CALL GetHookChainReturn(AMX *amx, cell *params) return retVal._integer != 0 ? TRUE : FALSE; case ATYPE_STRING: { - if (PARAMS_COUNT != 2) + if (PARAMS_COUNT != 3) return FALSE; setAmxString(dstAddr, retVal._string, params[arg_maxlen]); @@ -247,10 +242,11 @@ cell AMX_NATIVE_CALL GetHookChainReturn(AMX *amx, cell *params) return indexOfEdict(retVal._pev); case ATYPE_VECTOR: { - Vector* vRetVal = retVal._vector; - *dstAddr++ = *((cell*)&vRetVal->x); - *dstAddr++ = *((cell*)&vRetVal->y); - *dstAddr++ = *((cell*)&vRetVal->z); + if (PARAMS_COUNT != 2) + return FALSE; + + Vector &pDest = *(Vector *)dstAddr; + pDest = *retVal._vector; return TRUE; } default: diff --git a/reapi/src/natives/natives_hookchains.h b/reapi/src/natives/natives_hookchains.h index c6e0c240..ebc7c8fa 100644 --- a/reapi/src/natives/natives_hookchains.h +++ b/reapi/src/natives/natives_hookchains.h @@ -4,3 +4,18 @@ #define NATIVE_MEMBER_REQUIRE(a,x) if (!api_cfg.has##x()) { AMXX_LogError(amx, AMX_ERR_NATIVE, "Member (%s) is not available, required %s", memberlist[a]->member_name, #x); return 0; } void RegisterNatives_HookChains(); + +template +class CTempAnyData +{ +public: + T *Alloc() + { + m_current = (m_current + 1) % BUF_MAX; + return m_data[m_current]; + } + +private: + size_t m_current = 0; + T m_data[BUF_MAX][BUF_SIZE]; +}; diff --git a/reapi/src/natives/natives_misc.cpp b/reapi/src/natives/natives_misc.cpp index e159cdc5..9d1719eb 100644 --- a/reapi/src/natives/natives_misc.cpp +++ b/reapi/src/natives/natives_misc.cpp @@ -458,7 +458,7 @@ cell AMX_NATIVE_CALL rg_round_end(AMX *amx, cell *params) { CSGameRules()->EndRoundMessage(message, _event); CSGameRules()->TerminateRound(_tmDelay, _winStatus); - return TRUE; + return true; }, winstatus, event, tmDelay); } From 83aead7e5069c81b39a7963208e2bea4aa9406a7 Mon Sep 17 00:00:00 2001 From: s1lentq Date: Tue, 22 Jun 2021 22:13:13 +0700 Subject: [PATCH 13/15] Fix linux build --- reapi/src/hook_callback.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reapi/src/hook_callback.h b/reapi/src/hook_callback.h index a0735b03..32e11827 100644 --- a/reapi/src/hook_callback.h +++ b/reapi/src/hook_callback.h @@ -218,7 +218,7 @@ void callVoidForward(size_t func, original_t original, f_args&&... args) template NOINLINE R DLLEXPORT _callForward(hook_t* hook, original_t original, f_args&&... args) { - using R2 = std::decay::type; + using R2 = typename std::decay::type; auto hookCtx = g_hookCtx; hookCtx->reset(getApiType(R2())); From de5720098cd467ca70ab3f5e6f84a7c1fd9bf7cc Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Tue, 22 Jun 2021 13:35:59 -0300 Subject: [PATCH 14/15] Added ATYPE_VECTOR in SetHookChainArg native --- reapi/src/hook_callback.h | 1 + reapi/src/natives/natives_hookchains.cpp | 3 +++ 2 files changed, 4 insertions(+) diff --git a/reapi/src/hook_callback.h b/reapi/src/hook_callback.h index 32e11827..db25ab07 100644 --- a/reapi/src/hook_callback.h +++ b/reapi/src/hook_callback.h @@ -52,6 +52,7 @@ inline AType getApiType(edict_t *) { return ATYPE_EDICT; } inline AType getApiType(entvars_t *) { return ATYPE_EVARS; } inline AType getApiType(bool) { return ATYPE_BOOL; } inline AType getApiType(Vector) { return ATYPE_VECTOR; } +inline AType getApiType(Vector&) { return ATYPE_VECTOR; } template inline AType getApiType(T *) { return ATYPE_INTEGER; } diff --git a/reapi/src/natives/natives_hookchains.cpp b/reapi/src/natives/natives_hookchains.cpp index e0a1d9c9..66e4a5eb 100644 --- a/reapi/src/natives/natives_hookchains.cpp +++ b/reapi/src/natives/natives_hookchains.cpp @@ -322,6 +322,9 @@ cell AMX_NATIVE_CALL SetHookChainArg(AMX *amx, cell *params) case ATYPE_EVARS: *(entvars_t **)destAddr = PEV(*srcAddr); break; + case ATYPE_VECTOR: + *(Vector **)destAddr = (Vector *)*srcAddr; + break; } return TRUE; From 6931d8649363c581fb799e66fc59715f8ee1ef57 Mon Sep 17 00:00:00 2001 From: FEDERICOMB <41979395+FEDERICOMB96@users.noreply.github.com> Date: Tue, 22 Jun 2021 14:59:53 -0300 Subject: [PATCH 15/15] Revert changes --- reapi/src/hook_callback.h | 1 - reapi/src/natives/natives_hookchains.cpp | 3 --- 2 files changed, 4 deletions(-) diff --git a/reapi/src/hook_callback.h b/reapi/src/hook_callback.h index db25ab07..32e11827 100644 --- a/reapi/src/hook_callback.h +++ b/reapi/src/hook_callback.h @@ -52,7 +52,6 @@ inline AType getApiType(edict_t *) { return ATYPE_EDICT; } inline AType getApiType(entvars_t *) { return ATYPE_EVARS; } inline AType getApiType(bool) { return ATYPE_BOOL; } inline AType getApiType(Vector) { return ATYPE_VECTOR; } -inline AType getApiType(Vector&) { return ATYPE_VECTOR; } template inline AType getApiType(T *) { return ATYPE_INTEGER; } diff --git a/reapi/src/natives/natives_hookchains.cpp b/reapi/src/natives/natives_hookchains.cpp index 66e4a5eb..e0a1d9c9 100644 --- a/reapi/src/natives/natives_hookchains.cpp +++ b/reapi/src/natives/natives_hookchains.cpp @@ -322,9 +322,6 @@ cell AMX_NATIVE_CALL SetHookChainArg(AMX *amx, cell *params) case ATYPE_EVARS: *(entvars_t **)destAddr = PEV(*srcAddr); break; - case ATYPE_VECTOR: - *(Vector **)destAddr = (Vector *)*srcAddr; - break; } return TRUE;