From 9982a7bad35bf1ff7a7c80f2ac8caab93be39ba2 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sun, 9 Oct 2022 12:59:30 +0200 Subject: [PATCH] Constify snap more (inspired by upstream) Incorporate const added in those upstream commits: https://github.com/teeworlds/teeworlds/commit/d86d576217eeb843c822568f6b8f97ac852ac0d7 https://github.com/teeworlds/teeworlds/commit/e6b8518b49c353963af35efad8465d9066fb819e --- src/engine/client.h | 2 +- src/engine/client/client.cpp | 8 ++++---- src/engine/client/client.h | 2 +- src/engine/shared/snapshot.cpp | 24 ++++++++++++------------ src/engine/shared/snapshot.h | 14 +++++++++----- src/game/client/projectile_data.cpp | 2 +- 6 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/engine/client.h b/src/engine/client.h index fdc114baa..fb0186a93 100644 --- a/src/engine/client.h +++ b/src/engine/client.h @@ -219,7 +219,7 @@ public: // TODO: Refactor: should redo this a bit i think, too many virtual calls virtual int SnapNumItems(int SnapID) const = 0; - virtual void *SnapFindItem(int SnapID, int Type, int ID) 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 int SnapItemSize(int SnapID, int Index) const = 0; diff --git a/src/engine/client/client.cpp b/src/engine/client/client.cpp index 0dfb85816..f3a306109 100644 --- a/src/engine/client/client.cpp +++ b/src/engine/client/client.cpp @@ -982,7 +982,7 @@ int CClient::LoadData() void *CClient::SnapGetItem(int SnapID, int Index, CSnapItem *pItem) const { dbg_assert(SnapID >= 0 && SnapID < NUM_SNAPSHOT_TYPES, "invalid SnapID"); - CSnapshotItem *pSnapshotItem = m_aapSnapshots[g_Config.m_ClDummy][SnapID]->m_pAltSnap->GetItem(Index); + const CSnapshotItem *pSnapshotItem = m_aapSnapshots[g_Config.m_ClDummy][SnapID]->m_pAltSnap->GetItem(Index); pItem->m_DataSize = m_aapSnapshots[g_Config.m_ClDummy][SnapID]->m_pAltSnap->GetItemSize(Index); pItem->m_Type = m_aapSnapshots[g_Config.m_ClDummy][SnapID]->m_pAltSnap->GetItemType(Index); pItem->m_ID = pSnapshotItem->ID(); @@ -995,7 +995,7 @@ int CClient::SnapItemSize(int SnapID, int Index) const return m_aapSnapshots[g_Config.m_ClDummy][SnapID]->m_pAltSnap->GetItemSize(Index); } -void *CClient::SnapFindItem(int SnapID, int Type, int ID) const +const void *CClient::SnapFindItem(int SnapID, int Type, int ID) const { if(!m_aapSnapshots[g_Config.m_ClDummy][SnapID]) return 0x0; @@ -2259,10 +2259,10 @@ int CClient::UnpackAndValidateSnapshot(CSnapshot *pFrom, CSnapshot *pTo) int Num = pFrom->NumItems(); for(int Index = 0; Index < Num; Index++) { - CSnapshotItem *pFromItem = pFrom->GetItem(Index); + const CSnapshotItem *pFromItem = pFrom->GetItem(Index); const int FromItemSize = pFrom->GetItemSize(Index); const int ItemType = pFrom->GetItemType(Index); - void *pData = pFromItem->Data(); + const void *pData = pFromItem->Data(); Unpacker.Reset(pData, FromItemSize); void *pRawObj = pNetObjHandler->SecureUnpackObj(ItemType, &Unpacker); diff --git a/src/engine/client/client.h b/src/engine/client/client.h index a877d57b8..2f79ca3d6 100644 --- a/src/engine/client/client.h +++ b/src/engine/client/client.h @@ -376,7 +376,7 @@ public: int GetPredictionTime() override; void *SnapGetItem(int SnapID, int Index, CSnapItem *pItem) const override; int SnapItemSize(int SnapID, int Index) const override; - void *SnapFindItem(int SnapID, int Type, int ID) 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/engine/shared/snapshot.cpp b/src/engine/shared/snapshot.cpp index 8f4100314..4dfe1fe00 100644 --- a/src/engine/shared/snapshot.cpp +++ b/src/engine/shared/snapshot.cpp @@ -12,9 +12,9 @@ // CSnapshot -CSnapshotItem *CSnapshot::GetItem(int Index) const +const CSnapshotItem *CSnapshot::GetItem(int Index) const { - return (CSnapshotItem *)(DataStart() + Offsets()[Index]); + return (const CSnapshotItem *)(DataStart() + Offsets()[Index]); } int CSnapshot::GetItemSize(int Index) const @@ -42,7 +42,7 @@ int CSnapshot::GetExternalItemType(int InternalType) const { return InternalType; } - CSnapshotItem *pTypeItem = GetItem(TypeItemIndex); + const CSnapshotItem *pTypeItem = GetItem(TypeItemIndex); CUuid Uuid; for(int i = 0; i < (int)sizeof(CUuid) / 4; i++) int_to_bytes_be(&Uuid.m_aData[i * 4], pTypeItem->Data()[i]); @@ -61,7 +61,7 @@ int CSnapshot::GetItemIndex(int Key) const return -1; } -void *CSnapshot::FindItem(int Type, int ID) const +const void *CSnapshot::FindItem(int Type, int ID) const { int InternalType = Type; if(Type >= OFFSET_UUID) @@ -74,7 +74,7 @@ void *CSnapshot::FindItem(int Type, int ID) const bool Found = false; for(int i = 0; i < m_NumItems; i++) { - CSnapshotItem *pItem = GetItem(i); + const CSnapshotItem *pItem = GetItem(i); if(pItem->Type() == 0 && pItem->ID() >= OFFSET_UUID_TYPE) // NETOBJTYPE_EX { if(mem_comp(pItem->Data(), aTypeUuidItem, sizeof(CUuid)) == 0) @@ -100,7 +100,7 @@ unsigned CSnapshot::Crc() for(int i = 0; i < m_NumItems; i++) { - CSnapshotItem *pItem = GetItem(i); + const CSnapshotItem *pItem = GetItem(i); int Size = GetItemSize(i); for(int b = 0; b < Size / 4; b++) @@ -114,7 +114,7 @@ void CSnapshot::DebugDump() dbg_msg("snapshot", "data_size=%d num_items=%d", m_DataSize, m_NumItems); for(int i = 0; i < m_NumItems; i++) { - CSnapshotItem *pItem = GetItem(i); + const CSnapshotItem *pItem = GetItem(i); int Size = GetItemSize(i); dbg_msg("snapshot", "\ttype=%d id=%d", pItem->Type(), pItem->ID()); for(int b = 0; b < Size / 4; b++) @@ -196,7 +196,7 @@ static int GetItemIndexHashed(int Key, const CItemList *pHashlist) return -1; } -int CSnapshotDelta::DiffItem(int *pPast, int *pCurrent, int *pOut, int Size) +int CSnapshotDelta::DiffItem(const int *pPast, const int *pCurrent, int *pOut, int Size) { int Needed = 0; while(Size) @@ -212,7 +212,7 @@ int CSnapshotDelta::DiffItem(int *pPast, int *pCurrent, int *pOut, int Size) return Needed; } -void CSnapshotDelta::UndiffItem(int *pPast, int *pDiff, int *pOut, int Size, int *pDataRate) +void CSnapshotDelta::UndiffItem(const int *pPast, int *pDiff, int *pOut, int Size, int *pDataRate) { while(Size) { @@ -304,7 +304,7 @@ int CSnapshotDelta::CreateDelta(CSnapshot *pFrom, CSnapshot *pTo, void *pDstData { // do delta const int ItemSize = pTo->GetItemSize(i); // O(1) .. O(n) - CSnapshotItem *pCurItem = pTo->GetItem(i); // O(1) .. O(n) + const CSnapshotItem *pCurItem = pTo->GetItem(i); // O(1) .. O(n) const int PastIndex = aPastIndices[i]; const bool IncludeSize = pCurItem->Type() >= MAX_NETOBJSIZES || !m_aItemSizes[pCurItem->Type()]; @@ -312,7 +312,7 @@ int CSnapshotDelta::CreateDelta(CSnapshot *pFrom, CSnapshot *pTo, void *pDstData { int *pItemDataDst = pData + 3; - CSnapshotItem *pPastItem = pFrom->GetItem(PastIndex); + const CSnapshotItem *pPastItem = pFrom->GetItem(PastIndex); if(!IncludeSize) pItemDataDst = pData + 2; @@ -373,7 +373,7 @@ int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, const void *pS // copy all non deleted stuff for(int i = 0; i < pFrom->NumItems(); i++) { - CSnapshotItem *pFromItem = pFrom->GetItem(i); + const CSnapshotItem *pFromItem = pFrom->GetItem(i); const int ItemSize = pFrom->GetItemSize(i); bool Keep = true; for(int d = 0; d < pDelta->m_NumDeletedItems; d++) diff --git a/src/engine/shared/snapshot.h b/src/engine/shared/snapshot.h index 1b8f96781..bb126a136 100644 --- a/src/engine/shared/snapshot.h +++ b/src/engine/shared/snapshot.h @@ -10,10 +10,14 @@ class CSnapshotItem { + friend class CSnapshotBuilder; + + int *Data() { return (int *)(this + 1); } + public: int m_TypeAndID; - int *Data() { return (int *)(this + 1); } + const int *Data() const { return (int *)(this + 1); } int Type() const { return m_TypeAndID >> 16; } int ID() const { return m_TypeAndID & 0xffff; } int Key() const { return m_TypeAndID; } @@ -48,12 +52,12 @@ public: m_NumItems = 0; } int NumItems() const { return m_NumItems; } - CSnapshotItem *GetItem(int Index) const; + const CSnapshotItem *GetItem(int Index) const; int GetItemSize(int Index) const; int GetItemIndex(int Key) const; int GetItemType(int Index) const; int GetExternalItemType(int InternalType) const; - void *FindItem(int Type, int ID) const; + const void *FindItem(int Type, int ID) const; unsigned Crc(); void DebugDump(); @@ -84,10 +88,10 @@ private: int m_aSnapshotDataUpdates[CSnapshot::MAX_TYPE + 1]; CData m_Empty; - static void UndiffItem(int *pPast, int *pDiff, int *pOut, int Size, int *pDataRate); + static void UndiffItem(const int *pPast, int *pDiff, int *pOut, int Size, int *pDataRate); public: - static int DiffItem(int *pPast, int *pCurrent, int *pOut, int Size); + static int DiffItem(const int *pPast, const int *pCurrent, int *pOut, int Size); CSnapshotDelta(); CSnapshotDelta(const CSnapshotDelta &Old); int GetDataRate(int Index) const { return m_aSnapshotDataRate[Index]; } diff --git a/src/game/client/projectile_data.cpp b/src/game/client/projectile_data.cpp index edf80d6b1..1922e57a7 100644 --- a/src/game/client/projectile_data.cpp +++ b/src/game/client/projectile_data.cpp @@ -67,7 +67,7 @@ void SnapshotRemoveExtraProjectileInfo(unsigned char *pData) CSnapshot *pSnap = (CSnapshot *)pData; for(int Index = 0; Index < pSnap->NumItems(); Index++) { - CSnapshotItem *pItem = pSnap->GetItem(Index); + const CSnapshotItem *pItem = pSnap->GetItem(Index); if(pItem->Type() == NETOBJTYPE_PROJECTILE) { CNetObj_Projectile *pProj = (CNetObj_Projectile *)((void *)pItem->Data());