MULTIDAMAGE: make check entity safe

ApplyMultiDamage: Fixed potential crash when in TakeDamage hook causes another additional damage
This commit is contained in:
s1lentq 2024-05-31 23:21:37 +07:00
parent 7372573c89
commit c7be8bfe7c
2 changed files with 12 additions and 8 deletions

View File

@ -79,7 +79,7 @@ LINK_HOOK_VOID_CHAIN2(ClearMultiDamage)
// Resets the global multi damage accumulator // Resets the global multi damage accumulator
void EXT_FUNC __API_HOOK(ClearMultiDamage)() void EXT_FUNC __API_HOOK(ClearMultiDamage)()
{ {
gMultiDamage.pEntity = nullptr; gMultiDamage.hEntity = nullptr;
gMultiDamage.amount = 0; gMultiDamage.amount = 0;
gMultiDamage.type = 0; gMultiDamage.type = 0;
} }
@ -89,11 +89,15 @@ LINK_HOOK_VOID_CHAIN(ApplyMultiDamage, (entvars_t *pevInflictor, entvars_t *pevA
// Inflicts contents of global multi damage register on gMultiDamage.pEntity // Inflicts contents of global multi damage register on gMultiDamage.pEntity
void EXT_FUNC __API_HOOK(ApplyMultiDamage)(entvars_t *pevInflictor, entvars_t *pevAttacker) void EXT_FUNC __API_HOOK(ApplyMultiDamage)(entvars_t *pevInflictor, entvars_t *pevAttacker)
{ {
if (!gMultiDamage.pEntity) EntityHandle<CBaseEntity> hEnt = gMultiDamage.hEntity;
if (!hEnt)
return; return;
gMultiDamage.pEntity->TakeDamage(pevInflictor, pevAttacker, gMultiDamage.amount, gMultiDamage.type); hEnt->TakeDamage(pevInflictor, pevAttacker, gMultiDamage.amount, gMultiDamage.type);
gMultiDamage.pEntity->ResetDmgPenetrationLevel();
// check again, the entity may be removed after taking damage
if (hEnt)
hEnt->ResetDmgPenetrationLevel();
} }
LINK_HOOK_VOID_CHAIN(AddMultiDamage, (entvars_t *pevInflictor, CBaseEntity *pEntity, float flDamage, int bitsDamageType), pevInflictor, pEntity, flDamage, bitsDamageType) LINK_HOOK_VOID_CHAIN(AddMultiDamage, (entvars_t *pevInflictor, CBaseEntity *pEntity, float flDamage, int bitsDamageType), pevInflictor, pEntity, flDamage, bitsDamageType)
@ -105,17 +109,17 @@ void EXT_FUNC __API_HOOK(AddMultiDamage)(entvars_t *pevInflictor, CBaseEntity *p
gMultiDamage.type |= bitsDamageType; gMultiDamage.type |= bitsDamageType;
if (pEntity != gMultiDamage.pEntity) if (pEntity != gMultiDamage.hEntity)
{ {
#ifdef REGAMEDLL_FIXES #ifdef REGAMEDLL_FIXES
if (gMultiDamage.pEntity) // avoid api calls with null default pEntity if (gMultiDamage.hEntity) // avoid api calls with null default pEntity
#endif #endif
{ {
// UNDONE: wrong attacker! // UNDONE: wrong attacker!
ApplyMultiDamage(pevInflictor, pevInflictor); ApplyMultiDamage(pevInflictor, pevInflictor);
} }
gMultiDamage.pEntity = pEntity; gMultiDamage.hEntity = pEntity;
gMultiDamage.amount = 0; gMultiDamage.amount = 0;
} }

View File

@ -133,7 +133,7 @@ struct AmmoInfo
struct MULTIDAMAGE struct MULTIDAMAGE
{ {
CBaseEntity *pEntity; EntityHandle<CBaseEntity> hEntity;
float amount; float amount;
int type; int type;
}; };