Skip to content

Commit

Permalink
Constify snap more (inspired by upstream)
Browse files Browse the repository at this point in the history
Incorporate const added in those upstream commits:
teeworlds/teeworlds@d86d576
teeworlds/teeworlds@e6b8518
  • Loading branch information
ChillerDragon committed Oct 9, 2022
1 parent b228585 commit 9982a7b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/engine/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ class IClient : public IInterface

// 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;

Expand Down
8 changes: 4 additions & 4 deletions src/engine/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/engine/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ class CClient : public IClient, public CDemoPlayer::IListener
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;

Expand Down
24 changes: 12 additions & 12 deletions src/engine/shared/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]);
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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++)
Expand All @@ -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++)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down Expand Up @@ -304,15 +304,15 @@ 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()];

if(PastIndex != -1)
{
int *pItemDataDst = pData + 3;

CSnapshotItem *pPastItem = pFrom->GetItem(PastIndex);
const CSnapshotItem *pPastItem = pFrom->GetItem(PastIndex);

if(!IncludeSize)
pItemDataDst = pData + 2;
Expand Down Expand Up @@ -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++)
Expand Down
14 changes: 9 additions & 5 deletions src/engine/shared/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -48,12 +52,12 @@ class CSnapshot
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();
Expand Down Expand Up @@ -84,10 +88,10 @@ class CSnapshotDelta
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]; }
Expand Down
2 changes: 1 addition & 1 deletion src/game/client/projectile_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 9982a7b

Please sign in to comment.