From e9646ba1576074c22960a3f8eaf20bb28cff221c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 14 Jul 2024 12:28:19 +0200 Subject: [PATCH 1/2] 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. --- src/engine/client.h | 3 +- src/engine/client/client.cpp | 12 +++--- src/engine/client/client.h | 2 +- src/game/client/components/items.cpp | 7 ++- src/game/client/gameclient.cpp | 64 ++++++++++++++-------------- src/game/client/gameclient.h | 1 - src/tools/demo_extract_chat.cpp | 18 ++++---- 7 files changed, 54 insertions(+), 53 deletions(-) diff --git a/src/engine/client.h b/src/engine/client.h index c07b6e640..99108fe0e 100644 --- a/src/engine/client.h +++ b/src/engine/client.h @@ -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; diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 17a54b256..c0fb68bfd 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -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 diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 0cf3e7b57..8144219b7 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -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; diff --git a/src/game/client/components/items.cpp b/src/game/client/components/items.cpp index 93ce892a6..d58dea182 100644 --- a/src/game/client/components/items.cpp +++ b/src/game/client/components/items.cpp @@ -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(pPrev), static_cast(pData), + RenderFlag(static_cast(pPrev), static_cast(Item.m_pData), static_cast(pPrevGameData), m_pClient->m_Snap.m_pGameDataObj); } } diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index 2e52b3d29..bdf29ac8f 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -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}); } } diff --git a/src/game/client/gameclient.h b/src/game/client/gameclient.h index 69322c7e3..0d4eae079 100644 --- a/src/game/client/gameclient.h +++ b/src/game/client/gameclient.h @@ -104,7 +104,6 @@ class CSnapEntities { public: IClient::CSnapItem m_Item; - const void *m_pData; const CNetObj_EntityEx *m_pDataEx; }; diff --git a/src/tools/demo_extract_chat.cpp b/src/tools/demo_extract_chat.cpp index a0137c57b..bd9777a06 100644 --- a/src/tools/demo_extract_chat.cpp +++ b/src/tools/demo_extract_chat.cpp @@ -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) { From 2f6c3abc9073b6886cc31c956ee713b02137e223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sun, 14 Jul 2024 12:31:52 +0200 Subject: [PATCH 2/2] Remove unnecessary `IClient::SnapItemSize` function The item data size is already returned by the `IClient::SnapGetItem` function and getting only the size is not currently useful, so the additional `IClient::SnapItemSize` function is unnecessary. --- src/engine/client.h | 1 - src/engine/client/client.cpp | 6 ------ src/engine/client/client.h | 1 - src/game/client/gameclient.cpp | 2 +- 4 files changed, 1 insertion(+), 9 deletions(-) diff --git a/src/engine/client.h b/src/engine/client.h index 99108fe0e..69bd871b1 100644 --- a/src/engine/client.h +++ b/src/engine/client.h @@ -229,7 +229,6 @@ public: virtual int SnapNumItems(int SnapId) const = 0; virtual const void *SnapFindItem(int SnapId, int Type, int Id) 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; diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index c0fb68bfd..00b60790b 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -712,12 +712,6 @@ IClient::CSnapItem CClient::SnapGetItem(int SnapId, int Index) const return Item; } -int CClient::SnapItemSize(int SnapId, int Index) const -{ - dbg_assert(SnapId >= 0 && SnapId < NUM_SNAPSHOT_TYPES, "invalid SnapId"); - return m_aapSnapshots[g_Config.m_ClDummy][SnapId]->m_pAltSnap->GetItemSize(Index); -} - const void *CClient::SnapFindItem(int SnapId, int Type, int Id) const { if(!m_aapSnapshots[g_Config.m_ClDummy][SnapId]) diff --git a/src/engine/client/client.h b/src/engine/client/client.h index 8144219b7..d8080ae69 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -327,7 +327,6 @@ public: int GetPredictionTime() 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; void SnapSetStaticsize(int ItemType, int Size) override; diff --git a/src/game/client/gameclient.cpp b/src/game/client/gameclient.cpp index bdf29ac8f..af84e4912 100644 --- a/src/game/client/gameclient.cpp +++ b/src/game/client/gameclient.cpp @@ -1616,7 +1616,7 @@ void CGameClient::OnNewSnapshot() FoundGameInfoEx = true; CServerInfo ServerInfo; Client()->GetServerInfo(&ServerInfo); - m_GameInfo = GetGameInfo((const CNetObj_GameInfoEx *)Item.m_pData, Client()->SnapItemSize(IClient::SNAP_CURRENT, i), &ServerInfo); + m_GameInfo = GetGameInfo((const CNetObj_GameInfoEx *)Item.m_pData, Item.m_DataSize, &ServerInfo); } else if(Item.m_Type == NETOBJTYPE_GAMEDATA) {