Skip to content

Commit

Permalink
Use safe pointer for members m_rghPlayerItems, m_pActiveItem etc
Browse files Browse the repository at this point in the history
  • Loading branch information
s1lentq committed Jan 9, 2023
1 parent 5b1b957 commit 2cfa4fc
Show file tree
Hide file tree
Showing 47 changed files with 1,202 additions and 1,167 deletions.
6 changes: 3 additions & 3 deletions regamedll/dlls/API/CSPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ EXT_FUNC bool CCSPlayer::RemovePlayerItemEx(const char* pszItemName, bool bRemov

if (pItem->IsWeapon())
{
if (pItem == pPlayer->m_pActiveItem) {
if (pItem == pPlayer->m_hActiveItem) {
((CBasePlayerWeapon *)pItem)->RetireWeapon();
}

Expand All @@ -236,7 +236,7 @@ EXT_FUNC bool CCSPlayer::RemovePlayerItemEx(const char* pszItemName, bool bRemov

pItem->Kill();

if (!pPlayer->m_rgpPlayerItems[PRIMARY_WEAPON_SLOT]) {
if (!pPlayer->m_rghPlayerItems[PRIMARY_WEAPON_SLOT]) {
pPlayer->m_bHasPrimary = false;
}

Expand Down Expand Up @@ -323,7 +323,7 @@ EXT_FUNC bool CCSPlayer::RemoveShield()
bool bIsProtectedShield = pPlayer->IsProtectedByShield();
pPlayer->RemoveShield();

CBasePlayerWeapon *pWeapon = static_cast<CBasePlayerWeapon *>(pPlayer->m_pActiveItem);
CBasePlayerWeapon *pWeapon = pPlayer->m_hActiveItem.Get<CBasePlayerWeapon>();
if (pWeapon && pWeapon->IsWeapon())
{
if (!pWeapon->CanHolster())
Expand Down
18 changes: 9 additions & 9 deletions regamedll/dlls/bot/cs_bot_weapon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ bool CCSBot::IsUsingMachinegun() const
// Return true if primary weapon doesn't exist or is totally out of ammo
bool CCSBot::IsPrimaryWeaponEmpty() const
{
CBasePlayerWeapon *pCurrentWeapon = static_cast<CBasePlayerWeapon *>(m_rgpPlayerItems[PRIMARY_WEAPON_SLOT]);
CBasePlayerWeapon *pCurrentWeapon = m_rghPlayerItems[PRIMARY_WEAPON_SLOT].Get<CBasePlayerWeapon>();
if (!pCurrentWeapon)
return true;

Expand All @@ -395,7 +395,7 @@ bool CCSBot::IsPrimaryWeaponEmpty() const
// Return true if pistol doesn't exist or is totally out of ammo
bool CCSBot::IsPistolEmpty() const
{
CBasePlayerWeapon *pCurrentWeapon = static_cast<CBasePlayerWeapon *>(m_rgpPlayerItems[PISTOL_SLOT]);
CBasePlayerWeapon *pCurrentWeapon = m_rghPlayerItems[PISTOL_SLOT].Get<CBasePlayerWeapon>();
if (!pCurrentWeapon)
return true;

Expand Down Expand Up @@ -440,7 +440,7 @@ void CCSBot::EquipBestWeapon(bool mustEquip)
if (!mustEquip && m_equipTimer.GetElapsedTime() < minEquipInterval)
return;

CBasePlayerWeapon *pPrimary = static_cast<CBasePlayerWeapon *>(m_rgpPlayerItems[PRIMARY_WEAPON_SLOT]);
CBasePlayerWeapon *pPrimary = m_rghPlayerItems[PRIMARY_WEAPON_SLOT].Get<CBasePlayerWeapon>();
if (pPrimary)
{
WeaponClassType weaponClass = WeaponIDToWeaponClass(pPrimary->m_iId);
Expand All @@ -459,7 +459,7 @@ void CCSBot::EquipBestWeapon(bool mustEquip)

if (TheCSBots()->AllowPistols())
{
if (DoEquip(static_cast<CBasePlayerWeapon *>(m_rgpPlayerItems[PISTOL_SLOT])))
if (DoEquip(m_rghPlayerItems[PISTOL_SLOT].Get<CBasePlayerWeapon>()))
return;
}

Expand All @@ -476,7 +476,7 @@ void CCSBot::EquipPistol()

if (TheCSBots()->AllowPistols() && !IsUsingPistol())
{
CBasePlayerWeapon *pistol = static_cast<CBasePlayerWeapon *>(m_rgpPlayerItems[PISTOL_SLOT]);
CBasePlayerWeapon *pistol = m_rghPlayerItems[PISTOL_SLOT].Get<CBasePlayerWeapon>();
DoEquip(pistol);
}
}
Expand All @@ -486,7 +486,7 @@ void CCSBot::EquipKnife()
{
if (!IsUsingKnife())
{
CBasePlayerWeapon *pKnife = static_cast<CBasePlayerWeapon *>(m_rgpPlayerItems[KNIFE_SLOT]);
CBasePlayerWeapon *pKnife = m_rghPlayerItems[KNIFE_SLOT].Get<CBasePlayerWeapon>();
if (pKnife)
{
SelectItem(STRING(pKnife->pev->classname));
Expand All @@ -497,7 +497,7 @@ void CCSBot::EquipKnife()
// Return true if we have a grenade in our inventory
bool CCSBot::HasGrenade() const
{
CBasePlayerWeapon *pGrenade = static_cast<CBasePlayerWeapon *>(m_rgpPlayerItems[GRENADE_SLOT]);
CBasePlayerWeapon *pGrenade = m_rghPlayerItems[GRENADE_SLOT].Get<CBasePlayerWeapon>();
return pGrenade != nullptr;
}

Expand All @@ -513,7 +513,7 @@ bool CCSBot::EquipGrenade(bool noSmoke)

if (HasGrenade())
{
CBasePlayerWeapon *pGrenade = static_cast<CBasePlayerWeapon *>(m_rgpPlayerItems[GRENADE_SLOT]);
CBasePlayerWeapon *pGrenade = m_rghPlayerItems[GRENADE_SLOT].Get<CBasePlayerWeapon>();
if (pGrenade)
{
if (noSmoke && pGrenade->m_iId == WEAPON_SMOKEGRENADE)
Expand Down Expand Up @@ -808,7 +808,7 @@ void CCSBot::OnTouchingWeapon(CWeaponBox *box)
// right now we only care about primary weapons on the ground
if (pDroppedWeapon)
{
CBasePlayerWeapon *pWeapon = static_cast<CBasePlayerWeapon *>(m_rgpPlayerItems[PRIMARY_WEAPON_SLOT]);
CBasePlayerWeapon *pWeapon = m_rghPlayerItems[PRIMARY_WEAPON_SLOT].Get<CBasePlayerWeapon>();

// if the gun on the ground is the same one we have, dont bother
if (pWeapon && pWeapon->IsWeapon() && pDroppedWeapon->m_iId != pWeapon->m_iId)
Expand Down
4 changes: 2 additions & 2 deletions regamedll/dlls/bot/states/cs_bot_buy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

bool HasDefaultPistol(CCSBot *me)
{
CBasePlayerWeapon *pSecondary = static_cast<CBasePlayerWeapon *>(me->m_rgpPlayerItems[PISTOL_SLOT]);
CBasePlayerWeapon *pSecondary = me->m_rghPlayerItems[PISTOL_SLOT].Get<CBasePlayerWeapon>();

if (!pSecondary)
return false;
Expand Down Expand Up @@ -101,7 +101,7 @@ void BuyState::OnEnter(CCSBot *me)

if (TheCSBots()->AllowPistols())
{
CBasePlayerWeapon *pSecondary = static_cast<CBasePlayerWeapon *>(me->m_rgpPlayerItems[PISTOL_SLOT]);
CBasePlayerWeapon *pSecondary = me->m_rghPlayerItems[PISTOL_SLOT].Get<CBasePlayerWeapon>();

// check if we have a pistol
if (pSecondary)
Expand Down
26 changes: 13 additions & 13 deletions regamedll/dlls/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1061,10 +1061,10 @@ bool CanBuyThis(CBasePlayer *pPlayer, int iWeapon)
if (pPlayer->HasShield() && iWeapon == WEAPON_SHIELDGUN)
return false;

if (pPlayer->m_rgpPlayerItems[PISTOL_SLOT] && pPlayer->m_rgpPlayerItems[PISTOL_SLOT]->m_iId == WEAPON_ELITE && iWeapon == WEAPON_SHIELDGUN)
if (pPlayer->m_rghPlayerItems[PISTOL_SLOT] && pPlayer->m_rghPlayerItems[PISTOL_SLOT]->m_iId == WEAPON_ELITE && iWeapon == WEAPON_SHIELDGUN)
return false;

if (pPlayer->m_rgpPlayerItems[PRIMARY_WEAPON_SLOT] && pPlayer->m_rgpPlayerItems[PRIMARY_WEAPON_SLOT]->m_iId == iWeapon)
if (pPlayer->m_rghPlayerItems[PRIMARY_WEAPON_SLOT] && pPlayer->m_rghPlayerItems[PRIMARY_WEAPON_SLOT]->m_iId == iWeapon)
{
if (g_bClientPrintEnable)
{
Expand All @@ -1074,7 +1074,7 @@ bool CanBuyThis(CBasePlayer *pPlayer, int iWeapon)
return false;
}

if (pPlayer->m_rgpPlayerItems[PISTOL_SLOT] && pPlayer->m_rgpPlayerItems[PISTOL_SLOT]->m_iId == iWeapon)
if (pPlayer->m_rghPlayerItems[PISTOL_SLOT] && pPlayer->m_rghPlayerItems[PISTOL_SLOT]->m_iId == iWeapon)
{
if (g_bClientPrintEnable)
{
Expand Down Expand Up @@ -2317,19 +2317,19 @@ bool BuyAmmo(CBasePlayer *pPlayer, int nSlot, bool bBlinkMoney)
// nSlot == 1 : Primary weapons
// nSlot == 2 : Secondary weapons

CBasePlayerItem *pItem = pPlayer->m_rgpPlayerItems[nSlot];
CBasePlayerItem *pItem = pPlayer->m_rghPlayerItems[nSlot].GetPtr();

if (pPlayer->HasShield())
{
if (pPlayer->m_rgpPlayerItems[PISTOL_SLOT])
pItem = pPlayer->m_rgpPlayerItems[PISTOL_SLOT];
if (pPlayer->m_rghPlayerItems[PISTOL_SLOT])
pItem = pPlayer->m_rghPlayerItems[PISTOL_SLOT];
}

if (pItem)
{
while (BuyGunAmmo(pPlayer, pItem, bBlinkMoney))
{
pItem = pItem->m_pNext;
pItem = pItem->m_hNext.GetPtr();

if (!pItem)
{
Expand Down Expand Up @@ -3409,7 +3409,7 @@ void EXT_FUNC InternalCommand(edict_t *pEntity, const char *pcmd, const char *pa
// player is dropping an item.
if (pPlayer->HasShield())
{
if (pPlayer->m_pActiveItem && pPlayer->m_pActiveItem->m_iId == WEAPON_C4)
if (pPlayer->m_hActiveItem && pPlayer->m_hActiveItem->m_iId == WEAPON_C4)
{
pPlayer->DropPlayerItem("weapon_c4");
}
Expand Down Expand Up @@ -4797,11 +4797,11 @@ int EXT_FUNC GetWeaponData(edict_t *pEdict, struct weapon_data_s *info)
// go through all of the weapons and make a list of the ones to pack
for (int i = 0; i < MAX_ITEM_TYPES; i++)
{
auto pPlayerItem = pPlayer->m_rgpPlayerItems[i];
CBasePlayerItem *pPlayerItem = pPlayer->m_rghPlayerItems[i].GetPtr();
while (pPlayerItem)
{
// there's a weapon here. Should I pack it?
auto weapon = (CBasePlayerWeapon *)pPlayerItem->GetWeaponPtr();
CBasePlayerWeapon *weapon = static_cast<CBasePlayerWeapon *>(pPlayerItem->GetWeaponPtr());
if (weapon && weapon->UseDecrement())
{
// Get The ID
Expand Down Expand Up @@ -4835,7 +4835,7 @@ int EXT_FUNC GetWeaponData(edict_t *pEdict, struct weapon_data_s *info)
}
}

pPlayerItem = pPlayerItem->m_pNext;
pPlayerItem = pPlayerItem->m_hNext.GetPtr();
}
}
#else
Expand Down Expand Up @@ -4944,12 +4944,12 @@ void EXT_FUNC UpdateClientData(const edict_t *ent, int sendweapons, struct clien
cd->iuser3 = iUser3;
}

if (pPlayer->m_pActiveItem)
if (pPlayer->m_hActiveItem)
{
ItemInfo II;
Q_memset(&II, 0, sizeof(II));

CBasePlayerWeapon *weapon = (CBasePlayerWeapon *)pPlayer->m_pActiveItem->GetWeaponPtr();
CBasePlayerWeapon *weapon = pPlayer->m_hActiveItem.Get<CBasePlayerWeapon>();
if (weapon && weapon->UseDecrement() &&
#ifdef REGAMEDLL_API
weapon->CSPlayerItem()->GetItemInfo(&II)
Expand Down
7 changes: 7 additions & 0 deletions regamedll/dlls/ehandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class EntityHandle
// NOTE: this is a unsafe method
template <typename R>
R *Get() const;
T *GetPtr() const;

edict_t *Get() const;
edict_t *Set(edict_t *pEdict);
Expand Down Expand Up @@ -104,6 +105,12 @@ EntityHandle<T>::EntityHandle(const edict_t *pEdict)
Set(const_cast<edict_t *>(pEdict));
}

template <typename T>
inline T *EntityHandle<T>::GetPtr() const
{
return GET_PRIVATE<T>(Get());
}

template <typename T>
template <typename R>
inline R *EntityHandle<T>::Get() const
Expand Down
10 changes: 5 additions & 5 deletions regamedll/dlls/func_tank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ BOOL CFuncTank::StartControl(CBasePlayer *pController)

m_pController = pController;

if (m_pController->m_pActiveItem)
if (m_pController->m_hActiveItem)
{
m_pController->m_pActiveItem->Holster();
m_pController->m_hActiveItem->Holster();
m_pController->pev->weaponmodel = 0;

#ifdef BUILD_LATEST_FIXES
Expand Down Expand Up @@ -267,9 +267,9 @@ void CFuncTank::StopControl()
if (!m_pController)
return;

if (m_pController->m_pActiveItem)
if (m_pController->m_hActiveItem)
{
m_pController->m_pActiveItem->Deploy();
m_pController->m_hActiveItem->Deploy();

if (m_pController->IsPlayer())
{
Expand All @@ -280,7 +280,7 @@ void CFuncTank::StopControl()
ALERT(at_console, "stopped using TANK\n");

#ifdef REGAMEDLL_FIXES
if (m_pController->m_pActiveItem)
if (m_pController->m_hActiveItem)
#endif
{
m_pController->m_iHideHUD &= ~HIDEHUD_WEAPONS;
Expand Down
6 changes: 3 additions & 3 deletions regamedll/dlls/h_cycler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ void CWeaponCycler::Spawn()

BOOL CWeaponCycler::Deploy()
{
m_pPlayer->pev->viewmodel = m_iszModel;
m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 1.0f;
m_hPlayer->pev->viewmodel = m_iszModel;
m_hPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 1.0f;

SendWeaponAnim(0);
m_iClip = 0;
Expand All @@ -261,7 +261,7 @@ BOOL CWeaponCycler::Deploy()

void CWeaponCycler::Holster(int skiplocal)
{
m_pPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 0.5f;
m_hPlayer->m_flNextAttack = UTIL_WeaponTimeBase() + 0.5f;
}

void CWeaponCycler::PrimaryAttack()
Expand Down
22 changes: 11 additions & 11 deletions regamedll/dlls/multiplay_gamerules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3242,7 +3242,7 @@ BOOL EXT_FUNC CHalfLifeMultiplay::__API_HOOK(FShouldSwitchWeapon)(CBasePlayer *p
return FALSE;
}

if (!pPlayer->m_pActiveItem)
if (!pPlayer->m_hActiveItem)
{
// player doesn't have an active item!
return TRUE;
Expand All @@ -3256,7 +3256,7 @@ BOOL EXT_FUNC CHalfLifeMultiplay::__API_HOOK(FShouldSwitchWeapon)(CBasePlayer *p
return FALSE;
#endif

if (!pPlayer->m_pActiveItem->CanHolster())
if (!pPlayer->m_hActiveItem->CanHolster())
{
// can't put away the active item.
return FALSE;
Expand All @@ -3268,12 +3268,12 @@ BOOL EXT_FUNC CHalfLifeMultiplay::__API_HOOK(FShouldSwitchWeapon)(CBasePlayer *p
if (pWeapon->iFlags() & ITEM_FLAG_NOFIREUNDERWATER)
return FALSE;

if (pPlayer->m_pActiveItem->iFlags() & ITEM_FLAG_NOFIREUNDERWATER)
if (pPlayer->m_hActiveItem->iFlags() & ITEM_FLAG_NOFIREUNDERWATER)
return TRUE;
}
#endif

if (pWeapon->iWeight() > pPlayer->m_pActiveItem->iWeight())
if (pWeapon->iWeight() > pPlayer->m_hActiveItem->iWeight())
return TRUE;

return FALSE;
Expand All @@ -3300,7 +3300,7 @@ BOOL EXT_FUNC CHalfLifeMultiplay::__API_HOOK(GetNextBestWeapon)(CBasePlayer *pPl

for (i = 0; i < MAX_ITEM_TYPES; i++)
{
pCheck = pPlayer->m_rgpPlayerItems[i];
pCheck = pPlayer->m_rghPlayerItems[i];

while (pCheck)
{
Expand All @@ -3323,7 +3323,7 @@ BOOL EXT_FUNC CHalfLifeMultiplay::__API_HOOK(GetNextBestWeapon)(CBasePlayer *pPl
}
}

pCheck = pCheck->m_pNext;
pCheck = pCheck->m_hNext.GetPtr();
}
}

Expand Down Expand Up @@ -3709,10 +3709,10 @@ void CHalfLifeMultiplay::PlayerThink(CBasePlayer *pPlayer)
pPlayer->m_bCanShoot = true;
}

if (pPlayer->m_pActiveItem && pPlayer->m_pActiveItem->IsWeapon())
if (pPlayer->m_hActiveItem && pPlayer->m_hActiveItem->IsWeapon())
{
CBasePlayerWeapon *pWeapon = static_cast<CBasePlayerWeapon *>(pPlayer->m_pActiveItem->GetWeaponPtr());
if (pWeapon->m_iWeaponState & WPNSTATE_SHIELD_DRAWN
CBasePlayerWeapon *pWeapon = pPlayer->m_hActiveItem.Get<CBasePlayerWeapon>();
if (pWeapon && pWeapon->m_iWeaponState & WPNSTATE_SHIELD_DRAWN
#ifdef REGAMEDLL_ADD
|| ((pWeapon->iFlags() & ITEM_FLAG_NOFIREUNDERWATER) && pPlayer->pev->waterlevel == 3)
#endif
Expand Down Expand Up @@ -4078,9 +4078,9 @@ void EXT_FUNC CHalfLifeMultiplay::__API_HOOK(DeathNotice)(CBasePlayer *pVictim,
CBasePlayer *pAttacker = CBasePlayer::Instance(pKiller);
if (pAttacker && pAttacker->IsPlayer())
{
if (pAttacker->m_pActiveItem)
if (pAttacker->m_hActiveItem)
{
killer_weapon_name = pAttacker->m_pActiveItem->pszName();
killer_weapon_name = pAttacker->m_hActiveItem->pszName();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion regamedll/dlls/observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ void CBasePlayer::Observer_CheckProperties()
if (!target)
return;

int weapon = target->m_pActiveItem ? target->m_pActiveItem->m_iId : 0;
int weapon = target->m_hActiveItem ? target->m_hActiveItem->m_iId : 0;
int targetBombState = STATUSICON_HIDE;

// use fov of tracked client
Expand Down
Loading

0 comments on commit 2cfa4fc

Please sign in to comment.