From 77a3f2b25ba6467999a013be20aefca335d975e3 Mon Sep 17 00:00:00 2001 From: s1lentq Date: Tue, 6 Aug 2024 07:19:58 +0700 Subject: [PATCH] try fix a crash due to the deletion of an item in an unusual way related #870, #752, #924, #957 --- regamedll/dlls/cbase.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/regamedll/dlls/cbase.cpp b/regamedll/dlls/cbase.cpp index ebfd2fdc..5be847bd 100644 --- a/regamedll/dlls/cbase.cpp +++ b/regamedll/dlls/cbase.cpp @@ -1598,6 +1598,30 @@ void OnFreeEntPrivateData(edict_t *pEnt) if (!pEntity) return; +#ifdef REGAMEDLL_FIXES + // FIXED: Ensure this item will be removed from the owner player's inventory 'm_rgpPlayerItems[]' + // to avoid dangling pointers + CBasePlayerItem *pItem = dynamic_cast(pEntity); + if (pItem) + { + CBasePlayer *pOwner = GET_PRIVATE(pItem->pev->owner); + if (pOwner && pOwner->IsPlayer()) + { + if (pOwner->m_pActiveItem == pItem && pItem->IsWeapon()) + ((CBasePlayerWeapon *)pItem)->RetireWeapon(); + + if (pOwner->RemovePlayerItem(pItem)) + { + // Ammo must be dropped, otherwise grenades cannot be buy or picked up + if (IsGrenadeWeapon(pItem->m_iId) || pItem->m_iId == WEAPON_C4) + pOwner->m_rgAmmo[pItem->PrimaryAmmoIndex()] = 0; + + pOwner->pev->weapons &= ~(1 << pItem->m_iId); + } + } + } +#endif + #ifdef REGAMEDLL_API pEntity->OnDestroy(); #endif