Simplify usage of IClient::SnapGetItem function

Let the `IClient::SnapGetItem` function return an `IClient::CSnapItem` directly instead of passing it as a pointer. Add the item data pointer as a member variable to `IClient::CSnapItem` instead of returning it separately. Therefore, the separate data pointer of the class `CSnapEntities` is also not necessary anymore.

Consistently mark the snapshot items and data as `const`. The C-style cast to `void *` in the `IClient::SnapGetItem` function was previously implicitly casting away the `const` of the snapshot pointers.
This commit is contained in:
Robert Müller 2024-07-14 12:28:19 +02:00
parent 7014021909
commit e9646ba157
7 changed files with 54 additions and 53 deletions

View file

@ -107,6 +107,7 @@ public:
public:
int m_Type;
int m_Id;
const void *m_pData;
int m_DataSize;
};
@ -227,7 +228,7 @@ public:
// TODO: Refactor: should redo this a bit i think, too many virtual calls
virtual int SnapNumItems(int SnapId) const = 0;
virtual const void *SnapFindItem(int SnapId, int Type, int Id) const = 0;
virtual void *SnapGetItem(int SnapId, int Index, CSnapItem *pItem) const = 0;
virtual CSnapItem SnapGetItem(int SnapId, int Index) const = 0;
virtual int SnapItemSize(int SnapId, int Index) const = 0;
virtual void SnapSetStaticsize(int ItemType, int Size) = 0;

View file

@ -699,15 +699,17 @@ void CClient::LoadDebugFont()
// ---
void *CClient::SnapGetItem(int SnapId, int Index, CSnapItem *pItem) const
IClient::CSnapItem CClient::SnapGetItem(int SnapId, int Index) const
{
dbg_assert(SnapId >= 0 && SnapId < NUM_SNAPSHOT_TYPES, "invalid SnapId");
const CSnapshot *pSnapshot = m_aapSnapshots[g_Config.m_ClDummy][SnapId]->m_pAltSnap;
const CSnapshotItem *pSnapshotItem = pSnapshot->GetItem(Index);
pItem->m_DataSize = pSnapshot->GetItemSize(Index);
pItem->m_Type = pSnapshot->GetItemType(Index);
pItem->m_Id = pSnapshotItem->Id();
return (void *)pSnapshotItem->Data();
CSnapItem Item;
Item.m_Type = pSnapshot->GetItemType(Index);
Item.m_Id = pSnapshotItem->Id();
Item.m_pData = pSnapshotItem->Data();
Item.m_DataSize = pSnapshot->GetItemSize(Index);
return Item;
}
int CClient::SnapItemSize(int SnapId, int Index) const

View file

@ -326,7 +326,7 @@ public:
// ---
int GetPredictionTime() override;
void *SnapGetItem(int SnapId, int Index, CSnapItem *pItem) const override;
CSnapItem SnapGetItem(int SnapId, int Index) const override;
int SnapItemSize(int SnapId, int Index) const override;
const void *SnapFindItem(int SnapId, int Type, int Id) const override;
int SnapNumItems(int SnapId) const override;

View file

@ -414,7 +414,7 @@ void CItems::OnRender()
for(const CSnapEntities &Ent : m_pClient->SnapEntities())
{
const IClient::CSnapItem Item = Ent.m_Item;
const void *pData = Ent.m_pData;
const void *pData = Item.m_pData;
const CNetObj_EntityEx *pEntEx = Ent.m_pDataEx;
if(Item.m_Type == NETOBJTYPE_PROJECTILE || Item.m_Type == NETOBJTYPE_DDRACEPROJECTILE || Item.m_Type == NETOBJTYPE_DDNETPROJECTILE)
@ -523,8 +523,7 @@ void CItems::OnRender()
// render flag
for(int i = 0; i < Num; i++)
{
IClient::CSnapItem Item;
const void *pData = Client()->SnapGetItem(IClient::SNAP_CURRENT, i, &Item);
const IClient::CSnapItem Item = Client()->SnapGetItem(IClient::SNAP_CURRENT, i);
if(Item.m_Type == NETOBJTYPE_FLAG)
{
@ -532,7 +531,7 @@ void CItems::OnRender()
if(pPrev)
{
const void *pPrevGameData = Client()->SnapFindItem(IClient::SNAP_PREV, NETOBJTYPE_GAMEDATA, m_pClient->m_Snap.m_GameDataSnapId);
RenderFlag(static_cast<const CNetObj_Flag *>(pPrev), static_cast<const CNetObj_Flag *>(pData),
RenderFlag(static_cast<const CNetObj_Flag *>(pPrev), static_cast<const CNetObj_Flag *>(Item.m_pData),
static_cast<const CNetObj_GameData *>(pPrevGameData), m_pClient->m_Snap.m_pGameDataObj);
}
}

View file

@ -1112,45 +1112,44 @@ void CGameClient::ProcessEvents()
int Num = Client()->SnapNumItems(SnapType);
for(int Index = 0; Index < Num; Index++)
{
IClient::CSnapItem Item;
const void *pData = Client()->SnapGetItem(SnapType, Index, &Item);
const IClient::CSnapItem Item = Client()->SnapGetItem(SnapType, Index);
// We don't have enough info about us, others, to know a correct alpha value.
float Alpha = 1.0f;
if(Item.m_Type == NETEVENTTYPE_DAMAGEIND)
{
CNetEvent_DamageInd *pEvent = (CNetEvent_DamageInd *)pData;
const CNetEvent_DamageInd *pEvent = (const CNetEvent_DamageInd *)Item.m_pData;
m_Effects.DamageIndicator(vec2(pEvent->m_X, pEvent->m_Y), direction(pEvent->m_Angle / 256.0f), Alpha);
}
else if(Item.m_Type == NETEVENTTYPE_EXPLOSION)
{
CNetEvent_Explosion *pEvent = (CNetEvent_Explosion *)pData;
const CNetEvent_Explosion *pEvent = (const CNetEvent_Explosion *)Item.m_pData;
m_Effects.Explosion(vec2(pEvent->m_X, pEvent->m_Y), Alpha);
}
else if(Item.m_Type == NETEVENTTYPE_HAMMERHIT)
{
CNetEvent_HammerHit *pEvent = (CNetEvent_HammerHit *)pData;
const CNetEvent_HammerHit *pEvent = (const CNetEvent_HammerHit *)Item.m_pData;
m_Effects.HammerHit(vec2(pEvent->m_X, pEvent->m_Y), Alpha);
}
else if(Item.m_Type == NETEVENTTYPE_FINISH)
{
CNetEvent_Finish *pEvent = (CNetEvent_Finish *)pData;
const CNetEvent_Finish *pEvent = (const CNetEvent_Finish *)Item.m_pData;
m_Effects.FinishConfetti(vec2(pEvent->m_X, pEvent->m_Y), Alpha);
}
else if(Item.m_Type == NETEVENTTYPE_SPAWN)
{
CNetEvent_Spawn *pEvent = (CNetEvent_Spawn *)pData;
const CNetEvent_Spawn *pEvent = (const CNetEvent_Spawn *)Item.m_pData;
m_Effects.PlayerSpawn(vec2(pEvent->m_X, pEvent->m_Y), Alpha);
}
else if(Item.m_Type == NETEVENTTYPE_DEATH)
{
CNetEvent_Death *pEvent = (CNetEvent_Death *)pData;
const CNetEvent_Death *pEvent = (const CNetEvent_Death *)Item.m_pData;
m_Effects.PlayerDeath(vec2(pEvent->m_X, pEvent->m_Y), pEvent->m_ClientId, Alpha);
}
else if(Item.m_Type == NETEVENTTYPE_SOUNDWORLD)
{
CNetEvent_SoundWorld *pEvent = (CNetEvent_SoundWorld *)pData;
const CNetEvent_SoundWorld *pEvent = (const CNetEvent_SoundWorld *)Item.m_pData;
if(!Config()->m_SndGame)
continue;
@ -1393,12 +1392,11 @@ void CGameClient::OnNewSnapshot()
int Num = Client()->SnapNumItems(IClient::SNAP_CURRENT);
for(int i = 0; i < Num; i++)
{
IClient::CSnapItem Item;
const void *pData = Client()->SnapGetItem(IClient::SNAP_CURRENT, i, &Item);
const IClient::CSnapItem Item = Client()->SnapGetItem(IClient::SNAP_CURRENT, i);
if(Item.m_Type == NETOBJTYPE_CLIENTINFO)
{
const CNetObj_ClientInfo *pInfo = (const CNetObj_ClientInfo *)pData;
const CNetObj_ClientInfo *pInfo = (const CNetObj_ClientInfo *)Item.m_pData;
int ClientId = Item.m_Id;
if(ClientId < MAX_CLIENTS)
{
@ -1443,7 +1441,7 @@ void CGameClient::OnNewSnapshot()
}
else if(Item.m_Type == NETOBJTYPE_PLAYERINFO)
{
const CNetObj_PlayerInfo *pInfo = (const CNetObj_PlayerInfo *)pData;
const CNetObj_PlayerInfo *pInfo = (const CNetObj_PlayerInfo *)Item.m_pData;
if(pInfo->m_ClientId < MAX_CLIENTS && pInfo->m_ClientId == Item.m_Id)
{
@ -1477,7 +1475,7 @@ void CGameClient::OnNewSnapshot()
else if(Item.m_Type == NETOBJTYPE_DDNETPLAYER)
{
m_ReceivedDDNetPlayer = true;
const CNetObj_DDNetPlayer *pInfo = (const CNetObj_DDNetPlayer *)pData;
const CNetObj_DDNetPlayer *pInfo = (const CNetObj_DDNetPlayer *)Item.m_pData;
if(Item.m_Id < MAX_CLIENTS)
{
m_aClients[Item.m_Id].m_AuthLevel = pInfo->m_AuthLevel;
@ -1496,7 +1494,7 @@ void CGameClient::OnNewSnapshot()
if(Item.m_Id < MAX_CLIENTS)
{
const void *pOld = Client()->SnapFindItem(IClient::SNAP_PREV, NETOBJTYPE_CHARACTER, Item.m_Id);
m_Snap.m_aCharacters[Item.m_Id].m_Cur = *((const CNetObj_Character *)pData);
m_Snap.m_aCharacters[Item.m_Id].m_Cur = *((const CNetObj_Character *)Item.m_pData);
if(pOld)
{
m_Snap.m_aCharacters[Item.m_Id].m_Active = true;
@ -1520,7 +1518,7 @@ void CGameClient::OnNewSnapshot()
if(EvolveCur && m_Snap.m_aCharacters[Item.m_Id].m_Cur.m_Tick)
Evolve(&m_Snap.m_aCharacters[Item.m_Id].m_Cur, Client()->GameTick(g_Config.m_ClDummy));
m_aClients[Item.m_Id].m_Snapped = *((const CNetObj_Character *)pData);
m_aClients[Item.m_Id].m_Snapped = *((const CNetObj_Character *)Item.m_pData);
m_aClients[Item.m_Id].m_Evolved = m_Snap.m_aCharacters[Item.m_Id].m_Cur;
}
else
@ -1531,7 +1529,7 @@ void CGameClient::OnNewSnapshot()
}
else if(Item.m_Type == NETOBJTYPE_DDNETCHARACTER)
{
const CNetObj_DDNetCharacter *pCharacterData = (const CNetObj_DDNetCharacter *)pData;
const CNetObj_DDNetCharacter *pCharacterData = (const CNetObj_DDNetCharacter *)Item.m_pData;
if(Item.m_Id < MAX_CLIENTS)
{
@ -1576,7 +1574,7 @@ void CGameClient::OnNewSnapshot()
}
else if(Item.m_Type == NETOBJTYPE_SPECCHAR)
{
const CNetObj_SpecChar *pSpecCharData = (const CNetObj_SpecChar *)pData;
const CNetObj_SpecChar *pSpecCharData = (const CNetObj_SpecChar *)Item.m_pData;
if(Item.m_Id < MAX_CLIENTS)
{
@ -1588,14 +1586,14 @@ void CGameClient::OnNewSnapshot()
}
else if(Item.m_Type == NETOBJTYPE_SPECTATORINFO)
{
m_Snap.m_pSpectatorInfo = (const CNetObj_SpectatorInfo *)pData;
m_Snap.m_pSpectatorInfo = (const CNetObj_SpectatorInfo *)Item.m_pData;
m_Snap.m_pPrevSpectatorInfo = (const CNetObj_SpectatorInfo *)Client()->SnapFindItem(IClient::SNAP_PREV, NETOBJTYPE_SPECTATORINFO, Item.m_Id);
m_Snap.m_SpecInfo.m_SpectatorId = m_Snap.m_pSpectatorInfo->m_SpectatorId;
}
else if(Item.m_Type == NETOBJTYPE_GAMEINFO)
{
m_Snap.m_pGameInfoObj = (const CNetObj_GameInfo *)pData;
m_Snap.m_pGameInfoObj = (const CNetObj_GameInfo *)Item.m_pData;
bool CurrentTickGameOver = (bool)(m_Snap.m_pGameInfoObj->m_GameStateFlags & GAMESTATEFLAG_GAMEOVER);
if(!m_GameOver && CurrentTickGameOver)
OnGameOver();
@ -1618,11 +1616,11 @@ void CGameClient::OnNewSnapshot()
FoundGameInfoEx = true;
CServerInfo ServerInfo;
Client()->GetServerInfo(&ServerInfo);
m_GameInfo = GetGameInfo((const CNetObj_GameInfoEx *)pData, Client()->SnapItemSize(IClient::SNAP_CURRENT, i), &ServerInfo);
m_GameInfo = GetGameInfo((const CNetObj_GameInfoEx *)Item.m_pData, Client()->SnapItemSize(IClient::SNAP_CURRENT, i), &ServerInfo);
}
else if(Item.m_Type == NETOBJTYPE_GAMEDATA)
{
m_Snap.m_pGameDataObj = (const CNetObj_GameData *)pData;
m_Snap.m_pGameDataObj = (const CNetObj_GameData *)Item.m_pData;
m_Snap.m_GameDataSnapId = Item.m_Id;
if(m_Snap.m_pGameDataObj->m_FlagCarrierRed == FLAG_TAKEN)
{
@ -1647,14 +1645,14 @@ void CGameClient::OnNewSnapshot()
m_LastFlagCarrierBlue = m_Snap.m_pGameDataObj->m_FlagCarrierBlue;
}
else if(Item.m_Type == NETOBJTYPE_FLAG)
m_Snap.m_apFlags[Item.m_Id % 2] = (const CNetObj_Flag *)pData;
m_Snap.m_apFlags[Item.m_Id % 2] = (const CNetObj_Flag *)Item.m_pData;
else if(Item.m_Type == NETOBJTYPE_SWITCHSTATE)
{
if(Item.m_DataSize < 36)
{
continue;
}
const CNetObj_SwitchState *pSwitchStateData = (const CNetObj_SwitchState *)pData;
const CNetObj_SwitchState *pSwitchStateData = (const CNetObj_SwitchState *)Item.m_pData;
int Team = clamp(Item.m_Id, (int)TEAM_FLOCK, (int)TEAM_SUPER - 1);
int HighestSwitchNumber = clamp(pSwitchStateData->m_HighestSwitchNumber, 0, 255);
@ -2694,7 +2692,7 @@ void CGameClient::UpdatePrediction()
}
for(const CSnapEntities &EntData : SnapEntities())
m_GameWorld.NetObjAdd(EntData.m_Item.m_Id, EntData.m_Item.m_Type, EntData.m_pData, EntData.m_pDataEx);
m_GameWorld.NetObjAdd(EntData.m_Item.m_Id, EntData.m_Item.m_Type, EntData.m_Item.m_pData, EntData.m_pDataEx);
m_GameWorld.NetObjEnd();
}
@ -3580,12 +3578,11 @@ void CGameClient::SnapCollectEntities()
for(int Index = 0; Index < NumSnapItems; Index++)
{
IClient::CSnapItem Item;
const void *pData = Client()->SnapGetItem(IClient::SNAP_CURRENT, Index, &Item);
const IClient::CSnapItem Item = Client()->SnapGetItem(IClient::SNAP_CURRENT, Index);
if(Item.m_Type == NETOBJTYPE_ENTITYEX)
vItemEx.push_back({Item, pData, 0});
vItemEx.push_back({Item, nullptr});
else if(Item.m_Type == NETOBJTYPE_PICKUP || Item.m_Type == NETOBJTYPE_DDNETPICKUP || Item.m_Type == NETOBJTYPE_LASER || Item.m_Type == NETOBJTYPE_DDNETLASER || Item.m_Type == NETOBJTYPE_PROJECTILE || Item.m_Type == NETOBJTYPE_DDRACEPROJECTILE || Item.m_Type == NETOBJTYPE_DDNETPROJECTILE)
vItemData.push_back({Item, pData, 0});
vItemData.push_back({Item, nullptr});
}
// sort by id
@ -3607,13 +3604,14 @@ void CGameClient::SnapCollectEntities()
size_t IndexEx = 0;
for(const CSnapEntities &Ent : vItemData)
{
const CNetObj_EntityEx *pDataEx = 0;
while(IndexEx < vItemEx.size() && vItemEx[IndexEx].m_Item.m_Id < Ent.m_Item.m_Id)
IndexEx++;
if(IndexEx < vItemEx.size() && vItemEx[IndexEx].m_Item.m_Id == Ent.m_Item.m_Id)
pDataEx = (const CNetObj_EntityEx *)vItemEx[IndexEx].m_pData;
m_vSnapEntities.push_back({Ent.m_Item, Ent.m_pData, pDataEx});
const CNetObj_EntityEx *pDataEx = nullptr;
if(IndexEx < vItemEx.size() && vItemEx[IndexEx].m_Item.m_Id == Ent.m_Item.m_Id)
pDataEx = (const CNetObj_EntityEx *)vItemEx[IndexEx].m_Item.m_pData;
m_vSnapEntities.push_back({Ent.m_Item, pDataEx});
}
}

View file

@ -104,7 +104,6 @@ class CSnapEntities
{
public:
IClient::CSnapItem m_Item;
const void *m_pData;
const CNetObj_EntityEx *m_pDataEx;
};

View file

@ -71,14 +71,17 @@ public:
return m_apAltSnapshots[SnapId]->NumItems();
}
void *SnapGetItem(int SnapId, int Index, IClient::CSnapItem *pItem)
IClient::CSnapItem SnapGetItem(int SnapId, int Index)
{
dbg_assert(SnapId >= 0 && SnapId < IClient::NUM_SNAPSHOT_TYPES, "invalid SnapId");
const CSnapshot *pSnapshot = m_apAltSnapshots[SnapId];
const CSnapshotItem *pSnapshotItem = m_apAltSnapshots[SnapId]->GetItem(Index);
pItem->m_DataSize = m_apAltSnapshots[SnapId]->GetItemSize(Index);
pItem->m_Type = m_apAltSnapshots[SnapId]->GetItemType(Index);
pItem->m_Id = pSnapshotItem->Id();
return (void *)pSnapshotItem->Data();
IClient::CSnapItem Item;
Item.m_Type = pSnapshot->GetItemType(Index);
Item.m_Id = pSnapshotItem->Id();
Item.m_pData = pSnapshotItem->Data();
Item.m_DataSize = pSnapshot->GetItemSize(Index);
return Item;
}
void OnNewSnapshot()
@ -86,12 +89,11 @@ public:
int Num = SnapNumItems(IClient::SNAP_CURRENT);
for(int i = 0; i < Num; i++)
{
IClient::CSnapItem Item;
const void *pData = SnapGetItem(IClient::SNAP_CURRENT, i, &Item);
const IClient::CSnapItem Item = SnapGetItem(IClient::SNAP_CURRENT, i);
if(Item.m_Type == NETOBJTYPE_CLIENTINFO)
{
const CNetObj_ClientInfo *pInfo = (const CNetObj_ClientInfo *)pData;
const CNetObj_ClientInfo *pInfo = (const CNetObj_ClientInfo *)Item.m_pData;
int ClientId = Item.m_Id;
if(ClientId < MAX_CLIENTS)
{