2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#include "snapshot.h"
|
|
|
|
#include "compression.h"
|
2017-05-21 23:07:13 +00:00
|
|
|
#include "uuid_manager.h"
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2020-03-29 02:36:38 +00:00
|
|
|
#include <game/generated/protocol.h>
|
2020-06-12 19:22:54 +00:00
|
|
|
#include <game/generated/protocolglue.h>
|
2020-03-29 02:36:38 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// CSnapshot
|
|
|
|
|
|
|
|
CSnapshotItem *CSnapshot::GetItem(int Index)
|
|
|
|
{
|
|
|
|
return (CSnapshotItem *)(DataStart() + Offsets()[Index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSnapshot::GetItemSize(int Index)
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
if(Index == m_NumItems - 1)
|
2011-04-13 18:37:12 +00:00
|
|
|
return (m_DataSize - Offsets()[Index]) - sizeof(CSnapshotItem);
|
2020-09-26 19:41:58 +00:00
|
|
|
return (Offsets()[Index + 1] - Offsets()[Index]) - sizeof(CSnapshotItem);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
2017-05-21 23:07:13 +00:00
|
|
|
int CSnapshot::GetItemType(int Index)
|
|
|
|
{
|
|
|
|
int InternalType = GetItem(Index)->Type();
|
|
|
|
if(InternalType < OFFSET_UUID_TYPE)
|
|
|
|
{
|
|
|
|
return InternalType;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TypeItemIndex = GetItemIndex((0 << 16) | InternalType); // NETOBJTYPE_EX
|
|
|
|
if(TypeItemIndex == -1 || GetItemSize(TypeItemIndex) < (int)sizeof(CUuid))
|
|
|
|
{
|
|
|
|
return InternalType;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSnapshotItem *pTypeItem = GetItem(TypeItemIndex);
|
|
|
|
CUuid Uuid;
|
|
|
|
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
|
|
|
|
{
|
|
|
|
Uuid.m_aData[i * 4 + 0] = pTypeItem->Data()[i] >> 24;
|
|
|
|
Uuid.m_aData[i * 4 + 1] = pTypeItem->Data()[i] >> 16;
|
|
|
|
Uuid.m_aData[i * 4 + 2] = pTypeItem->Data()[i] >> 8;
|
|
|
|
Uuid.m_aData[i * 4 + 3] = pTypeItem->Data()[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_UuidManager.LookupUuid(Uuid);
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int CSnapshot::GetItemIndex(int Key)
|
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
// TODO: OPT: this should not be a linear search. very bad
|
|
|
|
for(int i = 0; i < m_NumItems; i++)
|
|
|
|
{
|
|
|
|
if(GetItem(i)->Key() == Key)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 10:30:02 +00:00
|
|
|
unsigned CSnapshot::Crc()
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2020-10-10 10:30:02 +00:00
|
|
|
unsigned int Crc = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int i = 0; i < m_NumItems; i++)
|
|
|
|
{
|
|
|
|
CSnapshotItem *pItem = GetItem(i);
|
|
|
|
int Size = GetItemSize(i);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
for(int b = 0; b < Size / 4; b++)
|
2010-05-29 07:25:38 +00:00
|
|
|
Crc += pItem->Data()[b];
|
|
|
|
}
|
|
|
|
return Crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
int Size = GetItemSize(i);
|
|
|
|
dbg_msg("snapshot", "\ttype=%d id=%d", pItem->Type(), pItem->ID());
|
2020-09-26 19:41:58 +00:00
|
|
|
for(int b = 0; b < Size / 4; b++)
|
2010-05-29 07:25:38 +00:00
|
|
|
dbg_msg("snapshot", "\t\t%3d %12d\t%08x", b, pItem->Data()[b], pItem->Data()[b]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSnapshotDelta
|
|
|
|
|
|
|
|
struct CItemList
|
|
|
|
{
|
|
|
|
int m_Num;
|
|
|
|
int m_aKeys[64];
|
|
|
|
int m_aIndex[64];
|
|
|
|
};
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
HASHLIST_SIZE = 256,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void GenerateHash(CItemList *pHashlist, CSnapshot *pSnapshot)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < HASHLIST_SIZE; i++)
|
|
|
|
pHashlist[i].m_Num = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int i = 0; i < pSnapshot->NumItems(); i++)
|
|
|
|
{
|
|
|
|
int Key = pSnapshot->GetItem(i)->Key();
|
2020-09-26 19:41:58 +00:00
|
|
|
int HashID = ((Key >> 12) & 0xf0) | (Key & 0xf);
|
2010-05-29 07:25:38 +00:00
|
|
|
if(pHashlist[HashID].m_Num != 64)
|
|
|
|
{
|
|
|
|
pHashlist[HashID].m_aIndex[pHashlist[HashID].m_Num] = i;
|
|
|
|
pHashlist[HashID].m_aKeys[pHashlist[HashID].m_Num] = Key;
|
|
|
|
pHashlist[HashID].m_Num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int GetItemIndexHashed(int Key, const CItemList *pHashlist)
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
int HashID = ((Key >> 12) & 0xf0) | (Key & 0xf);
|
|
|
|
for(int i = 0; i < pHashlist[HashID].m_Num; i++)
|
|
|
|
{
|
|
|
|
if(pHashlist[HashID].m_aKeys[i] == Key)
|
|
|
|
return pHashlist[HashID].m_aIndex[i];
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-09-12 12:58:44 +00:00
|
|
|
int CSnapshotDelta::DiffItem(int *pPast, int *pCurrent, int *pOut, int Size)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
int Needed = 0;
|
|
|
|
while(Size)
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
*pOut = *pCurrent - *pPast;
|
2010-05-29 07:25:38 +00:00
|
|
|
Needed |= *pOut;
|
|
|
|
pOut++;
|
|
|
|
pPast++;
|
|
|
|
pCurrent++;
|
|
|
|
Size--;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return Needed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSnapshotDelta::UndiffItem(int *pPast, int *pDiff, int *pOut, int Size)
|
|
|
|
{
|
|
|
|
while(Size)
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
*pOut = *pPast + *pDiff;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(*pDiff == 0)
|
|
|
|
m_aSnapshotDataRate[m_SnapshotCurrent] += 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
unsigned char aBuf[16];
|
2011-04-13 18:37:12 +00:00
|
|
|
unsigned char *pEnd = CVariableInt::Pack(aBuf, *pDiff);
|
2020-09-26 19:41:58 +00:00
|
|
|
m_aSnapshotDataRate[m_SnapshotCurrent] += (int)(pEnd - (unsigned char *)aBuf) * 8;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pOut++;
|
|
|
|
pPast++;
|
|
|
|
pDiff++;
|
|
|
|
Size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CSnapshotDelta::CSnapshotDelta()
|
|
|
|
{
|
|
|
|
mem_zero(m_aItemSizes, sizeof(m_aItemSizes));
|
|
|
|
mem_zero(m_aSnapshotDataRate, sizeof(m_aSnapshotDataRate));
|
|
|
|
mem_zero(m_aSnapshotDataUpdates, sizeof(m_aSnapshotDataUpdates));
|
|
|
|
m_SnapshotCurrent = 0;
|
|
|
|
mem_zero(&m_Empty, sizeof(m_Empty));
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
CSnapshotDelta::CSnapshotDelta(const CSnapshotDelta &Old)
|
2019-05-31 18:42:28 +00:00
|
|
|
{
|
2020-10-27 17:57:14 +00:00
|
|
|
mem_copy(m_aItemSizes, Old.m_aItemSizes, sizeof(m_aItemSizes));
|
|
|
|
mem_copy(m_aSnapshotDataRate, Old.m_aSnapshotDataRate, sizeof(m_aSnapshotDataRate));
|
|
|
|
mem_copy(m_aSnapshotDataUpdates, Old.m_aSnapshotDataUpdates, sizeof(m_aSnapshotDataUpdates));
|
|
|
|
mem_copy(&m_SnapshotCurrent, &Old.m_SnapshotCurrent, sizeof(m_SnapshotCurrent));
|
|
|
|
mem_copy(&m_Empty, &Old.m_Empty, sizeof(m_Empty));
|
2019-05-31 18:42:28 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CSnapshotDelta::SetStaticsize(int ItemType, int Size)
|
|
|
|
{
|
2020-10-11 16:14:12 +00:00
|
|
|
if(ItemType < 0 || ItemType >= MAX_NETOBJSIZES)
|
|
|
|
return;
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aItemSizes[ItemType] = Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
CSnapshotDelta::CData *CSnapshotDelta::EmptyDelta()
|
|
|
|
{
|
|
|
|
return &m_Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: OPT: this should be made much faster
|
|
|
|
int CSnapshotDelta::CreateDelta(CSnapshot *pFrom, CSnapshot *pTo, void *pDstData)
|
|
|
|
{
|
|
|
|
CData *pDelta = (CData *)pDstData;
|
2020-10-27 17:57:14 +00:00
|
|
|
int *pData = (int *)pDelta->m_aData;
|
2010-05-29 07:25:38 +00:00
|
|
|
int i, ItemSize, PastIndex;
|
|
|
|
CSnapshotItem *pFromItem;
|
|
|
|
CSnapshotItem *pCurItem;
|
|
|
|
CSnapshotItem *pPastItem;
|
|
|
|
int Count = 0;
|
|
|
|
int SizeCount = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pDelta->m_NumDeletedItems = 0;
|
|
|
|
pDelta->m_NumUpdateItems = 0;
|
|
|
|
pDelta->m_NumTempItems = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
CItemList aHashlist[HASHLIST_SIZE];
|
|
|
|
GenerateHash(aHashlist, pTo);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
// pack deleted stuff
|
|
|
|
for(i = 0; i < pFrom->NumItems(); i++)
|
|
|
|
{
|
|
|
|
pFromItem = pFrom->GetItem(i);
|
2020-10-27 17:57:14 +00:00
|
|
|
if(GetItemIndexHashed(pFromItem->Key(), aHashlist) == -1)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
// deleted
|
|
|
|
pDelta->m_NumDeletedItems++;
|
|
|
|
*pData = pFromItem->Key();
|
|
|
|
pData++;
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
GenerateHash(aHashlist, pFrom);
|
2020-10-10 10:47:16 +00:00
|
|
|
int aPastIndices[1024];
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
// fetch previous indices
|
|
|
|
// we do this as a separate pass because it helps the cache
|
2012-08-17 16:32:56 +00:00
|
|
|
const int NumItems = pTo->NumItems();
|
|
|
|
for(i = 0; i < NumItems; i++)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2011-04-13 18:37:12 +00:00
|
|
|
pCurItem = pTo->GetItem(i); // O(1) .. O(n)
|
2020-10-27 17:57:14 +00:00
|
|
|
aPastIndices[i] = GetItemIndexHashed(pCurItem->Key(), aHashlist); // O(n) .. O(n^n)
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2012-08-17 16:32:56 +00:00
|
|
|
for(i = 0; i < NumItems; i++)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
// do delta
|
|
|
|
ItemSize = pTo->GetItemSize(i); // O(1) .. O(n)
|
2011-04-13 18:37:12 +00:00
|
|
|
pCurItem = pTo->GetItem(i); // O(1) .. O(n)
|
2020-10-10 10:47:16 +00:00
|
|
|
PastIndex = aPastIndices[i];
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-10-11 16:14:12 +00:00
|
|
|
bool IncludeSize = pCurItem->Type() >= MAX_NETOBJSIZES || !m_aItemSizes[pCurItem->Type()];
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(PastIndex != -1)
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
int *pItemDataDst = pData + 3;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pPastItem = pFrom->GetItem(PastIndex);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-10-11 16:14:12 +00:00
|
|
|
if(!IncludeSize)
|
2020-09-26 19:41:58 +00:00
|
|
|
pItemDataDst = pData + 2;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(DiffItem(pPastItem->Data(), pCurItem->Data(), pItemDataDst, ItemSize / 4))
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
*pData++ = pCurItem->Type();
|
|
|
|
*pData++ = pCurItem->ID();
|
2020-10-11 16:14:12 +00:00
|
|
|
if(IncludeSize)
|
2020-09-26 19:41:58 +00:00
|
|
|
*pData++ = ItemSize / 4;
|
|
|
|
pData += ItemSize / 4;
|
2010-05-29 07:25:38 +00:00
|
|
|
pDelta->m_NumUpdateItems++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*pData++ = pCurItem->Type();
|
|
|
|
*pData++ = pCurItem->ID();
|
2020-10-11 16:14:12 +00:00
|
|
|
if(IncludeSize)
|
2020-09-26 19:41:58 +00:00
|
|
|
*pData++ = ItemSize / 4;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_copy(pData, pCurItem->Data(), ItemSize);
|
|
|
|
SizeCount += ItemSize;
|
2020-09-26 19:41:58 +00:00
|
|
|
pData += ItemSize / 4;
|
2010-05-29 07:25:38 +00:00
|
|
|
pDelta->m_NumUpdateItems++;
|
|
|
|
Count++;
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(0)
|
|
|
|
{
|
|
|
|
dbg_msg("snapshot", "%d %d %d",
|
|
|
|
pDelta->m_NumDeletedItems,
|
|
|
|
pDelta->m_NumUpdateItems,
|
|
|
|
pDelta->m_NumTempItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// TODO: pack temp stuff
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// finish
|
|
|
|
//mem_copy(pDelta->offsets, deleted, pDelta->num_deleted_items*sizeof(int));
|
|
|
|
//mem_copy(&(pDelta->offsets[pDelta->num_deleted_items]), update, pDelta->num_update_items*sizeof(int));
|
|
|
|
//mem_copy(&(pDelta->offsets[pDelta->num_deleted_items+pDelta->num_update_items]), temp, pDelta->num_temp_items*sizeof(int));
|
|
|
|
//mem_copy(pDelta->data_start(), data, data_size);
|
|
|
|
//pDelta->data_size = data_size;
|
|
|
|
* */
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(!pDelta->m_NumDeletedItems && !pDelta->m_NumUpdateItems && !pDelta->m_NumTempItems)
|
|
|
|
return 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
return (int)((char *)pData - (char *)pDstData);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int RangeCheck(void *pEnd, void *pPtr, int Size)
|
|
|
|
{
|
|
|
|
if((const char *)pPtr + Size > (const char *)pEnd)
|
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSnapshotDelta::UnpackDelta(CSnapshot *pFrom, CSnapshot *pTo, void *pSrcData, int DataSize)
|
|
|
|
{
|
|
|
|
CSnapshotBuilder Builder;
|
|
|
|
CData *pDelta = (CData *)pSrcData;
|
2020-10-27 17:57:14 +00:00
|
|
|
int *pData = (int *)pDelta->m_aData;
|
2010-05-29 07:25:38 +00:00
|
|
|
int *pEnd = (int *)(((char *)pSrcData + DataSize));
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CSnapshotItem *pFromItem;
|
|
|
|
int Keep, ItemSize;
|
|
|
|
int *pDeleted;
|
2011-02-12 10:40:36 +00:00
|
|
|
int ID, Type, Key;
|
2010-05-29 07:25:38 +00:00
|
|
|
int FromIndex;
|
|
|
|
int *pNewData;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
Builder.Init();
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// unpack deleted stuff
|
|
|
|
pDeleted = pData;
|
|
|
|
pData += pDelta->m_NumDeletedItems;
|
|
|
|
if(pData > pEnd)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// copy all non deleted stuff
|
|
|
|
for(int i = 0; i < pFrom->NumItems(); i++)
|
|
|
|
{
|
|
|
|
// dbg_assert(0, "fail!");
|
|
|
|
pFromItem = pFrom->GetItem(i);
|
2011-04-13 18:37:12 +00:00
|
|
|
ItemSize = pFrom->GetItemSize(i);
|
2010-05-29 07:25:38 +00:00
|
|
|
Keep = 1;
|
|
|
|
for(int d = 0; d < pDelta->m_NumDeletedItems; d++)
|
|
|
|
{
|
|
|
|
if(pDeleted[d] == pFromItem->Key())
|
|
|
|
{
|
|
|
|
Keep = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(Keep)
|
|
|
|
{
|
|
|
|
// keep it
|
|
|
|
mem_copy(
|
|
|
|
Builder.NewItem(pFromItem->Type(), pFromItem->ID(), ItemSize),
|
|
|
|
pFromItem->Data(), ItemSize);
|
|
|
|
}
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// unpack updated stuff
|
|
|
|
for(int i = 0; i < pDelta->m_NumUpdateItems; i++)
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
if(pData + 2 > pEnd)
|
2010-05-29 07:25:38 +00:00
|
|
|
return -1;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
Type = *pData++;
|
2020-10-11 16:14:12 +00:00
|
|
|
if(Type < 0)
|
|
|
|
return -1;
|
2011-02-12 10:40:36 +00:00
|
|
|
ID = *pData++;
|
2020-10-11 16:14:12 +00:00
|
|
|
if(Type < MAX_NETOBJSIZES && m_aItemSizes[Type])
|
2010-05-29 07:25:38 +00:00
|
|
|
ItemSize = m_aItemSizes[Type];
|
|
|
|
else
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
if(pData + 1 > pEnd)
|
2010-05-29 07:25:38 +00:00
|
|
|
return -2;
|
|
|
|
ItemSize = (*pData++) * 4;
|
|
|
|
}
|
|
|
|
m_SnapshotCurrent = Type;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(m_SnapshotCurrent < 0 || m_SnapshotCurrent > 0xFFFF)
|
|
|
|
return -3;
|
|
|
|
if(RangeCheck(pEnd, pData, ItemSize) || ItemSize < 0)
|
|
|
|
return -3;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
Key = (Type << 16) | ID;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// create the item if needed
|
|
|
|
pNewData = Builder.GetItemData(Key);
|
|
|
|
if(!pNewData)
|
2020-09-26 19:41:58 +00:00
|
|
|
pNewData = (int *)Builder.NewItem(Key >> 16, Key & 0xffff, ItemSize);
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
//if(range_check(pEnd, pNewData, ItemSize)) return -4;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
FromIndex = pFrom->GetItemIndex(Key);
|
|
|
|
if(FromIndex != -1)
|
|
|
|
{
|
|
|
|
// we got an update so we need pTo apply the diff
|
2020-09-26 19:41:58 +00:00
|
|
|
UndiffItem(pFrom->GetItem(FromIndex)->Data(), pData, pNewData, ItemSize / 4);
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aSnapshotDataUpdates[m_SnapshotCurrent]++;
|
|
|
|
}
|
|
|
|
else // no previous, just copy the pData
|
|
|
|
{
|
|
|
|
mem_copy(pNewData, pData, ItemSize);
|
2020-09-26 19:41:58 +00:00
|
|
|
m_aSnapshotDataRate[m_SnapshotCurrent] += ItemSize * 8;
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aSnapshotDataUpdates[m_SnapshotCurrent]++;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
pData += ItemSize / 4;
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// finish up
|
|
|
|
return Builder.Finish(pTo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSnapshotStorage
|
|
|
|
|
|
|
|
void CSnapshotStorage::Init()
|
|
|
|
{
|
|
|
|
m_pFirst = 0;
|
|
|
|
m_pLast = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSnapshotStorage::PurgeAll()
|
|
|
|
{
|
|
|
|
CHolder *pHolder = m_pFirst;
|
|
|
|
CHolder *pNext;
|
|
|
|
|
|
|
|
while(pHolder)
|
|
|
|
{
|
|
|
|
pNext = pHolder->m_pNext;
|
2018-04-09 09:56:39 +00:00
|
|
|
free(pHolder);
|
2010-05-29 07:25:38 +00:00
|
|
|
pHolder = pNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
// no more snapshots in storage
|
|
|
|
m_pFirst = 0;
|
|
|
|
m_pLast = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSnapshotStorage::PurgeUntil(int Tick)
|
|
|
|
{
|
|
|
|
CHolder *pHolder = m_pFirst;
|
|
|
|
CHolder *pNext;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
while(pHolder)
|
|
|
|
{
|
|
|
|
pNext = pHolder->m_pNext;
|
|
|
|
if(pHolder->m_Tick >= Tick)
|
|
|
|
return; // no more to remove
|
2018-04-09 09:56:39 +00:00
|
|
|
free(pHolder);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// did we come to the end of the list?
|
2020-09-26 19:41:58 +00:00
|
|
|
if(!pNext)
|
2011-04-13 18:37:12 +00:00
|
|
|
break;
|
2010-05-29 07:25:38 +00:00
|
|
|
|
|
|
|
m_pFirst = pNext;
|
|
|
|
pNext->m_pPrev = 0x0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pHolder = pNext;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// no more snapshots in storage
|
|
|
|
m_pFirst = 0;
|
|
|
|
m_pLast = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSnapshotStorage::Add(int Tick, int64 Tagtime, int DataSize, void *pData, int CreateAlt)
|
|
|
|
{
|
|
|
|
// allocate memory for holder + snapshot_data
|
2020-09-26 19:41:58 +00:00
|
|
|
int TotalSize = sizeof(CHolder) + DataSize;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
if(CreateAlt)
|
|
|
|
TotalSize += DataSize;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2018-04-09 09:56:39 +00:00
|
|
|
CHolder *pHolder = (CHolder *)malloc(TotalSize);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// set data
|
|
|
|
pHolder->m_Tick = Tick;
|
|
|
|
pHolder->m_Tagtime = Tagtime;
|
|
|
|
pHolder->m_SnapSize = DataSize;
|
2020-09-26 19:41:58 +00:00
|
|
|
pHolder->m_pSnap = (CSnapshot *)(pHolder + 1);
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_copy(pHolder->m_pSnap, pData, DataSize);
|
|
|
|
|
|
|
|
if(CreateAlt) // create alternative if wanted
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
pHolder->m_pAltSnap = (CSnapshot *)(((char *)pHolder->m_pSnap) + DataSize);
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_copy(pHolder->m_pAltSnap, pData, DataSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pHolder->m_pAltSnap = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// link
|
|
|
|
pHolder->m_pNext = 0;
|
|
|
|
pHolder->m_pPrev = m_pLast;
|
|
|
|
if(m_pLast)
|
|
|
|
m_pLast->m_pNext = pHolder;
|
|
|
|
else
|
|
|
|
m_pFirst = pHolder;
|
|
|
|
m_pLast = pHolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSnapshotStorage::Get(int Tick, int64 *pTagtime, CSnapshot **ppData, CSnapshot **ppAltData)
|
|
|
|
{
|
|
|
|
CHolder *pHolder = m_pFirst;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
while(pHolder)
|
|
|
|
{
|
|
|
|
if(pHolder->m_Tick == Tick)
|
|
|
|
{
|
|
|
|
if(pTagtime)
|
|
|
|
*pTagtime = pHolder->m_Tagtime;
|
|
|
|
if(ppData)
|
|
|
|
*ppData = pHolder->m_pSnap;
|
|
|
|
if(ppAltData)
|
2012-08-17 16:32:56 +00:00
|
|
|
*ppAltData = pHolder->m_pAltSnap;
|
2010-05-29 07:25:38 +00:00
|
|
|
return pHolder->m_SnapSize;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
pHolder = pHolder->m_pNext;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSnapshotBuilder
|
2017-05-21 23:07:13 +00:00
|
|
|
CSnapshotBuilder::CSnapshotBuilder()
|
|
|
|
{
|
|
|
|
m_NumExtendedItemTypes = 0;
|
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
|
2020-03-29 02:36:38 +00:00
|
|
|
void CSnapshotBuilder::Init(bool Sixup)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
m_DataSize = 0;
|
|
|
|
m_NumItems = 0;
|
2020-03-29 02:36:38 +00:00
|
|
|
m_Sixup = Sixup;
|
2017-05-21 23:07:13 +00:00
|
|
|
|
|
|
|
for(int i = 0; i < m_NumExtendedItemTypes; i++)
|
|
|
|
{
|
|
|
|
AddExtendedItemType(i);
|
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
2011-04-13 18:37:12 +00:00
|
|
|
CSnapshotItem *CSnapshotBuilder::GetItem(int Index)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
return (CSnapshotItem *)&(m_aData[m_aOffsets[Index]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int *CSnapshotBuilder::GetItemData(int Key)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < m_NumItems; i++)
|
|
|
|
{
|
|
|
|
if(GetItem(i)->Key() == Key)
|
2019-04-11 10:21:42 +00:00
|
|
|
return GetItem(i)->Data();
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-27 17:57:14 +00:00
|
|
|
int CSnapshotBuilder::Finish(void *pSnapData)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2020-06-16 15:19:53 +00:00
|
|
|
//dbg_msg("snap", "---------------------------");
|
2010-05-29 07:25:38 +00:00
|
|
|
// flattern and make the snapshot
|
2020-10-27 17:57:14 +00:00
|
|
|
CSnapshot *pSnap = (CSnapshot *)pSnapData;
|
2020-09-26 19:41:58 +00:00
|
|
|
int OffsetSize = sizeof(int) * m_NumItems;
|
2010-05-29 07:25:38 +00:00
|
|
|
pSnap->m_DataSize = m_DataSize;
|
|
|
|
pSnap->m_NumItems = m_NumItems;
|
|
|
|
mem_copy(pSnap->Offsets(), m_aOffsets, OffsetSize);
|
|
|
|
mem_copy(pSnap->DataStart(), m_aData, m_DataSize);
|
|
|
|
return sizeof(CSnapshot) + OffsetSize + m_DataSize;
|
|
|
|
}
|
|
|
|
|
2017-05-21 23:07:13 +00:00
|
|
|
static int GetTypeFromIndex(int Index)
|
|
|
|
{
|
|
|
|
return CSnapshot::MAX_TYPE - Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CSnapshotBuilder::AddExtendedItemType(int Index)
|
|
|
|
{
|
|
|
|
dbg_assert(0 <= Index && Index < m_NumExtendedItemTypes, "index out of range");
|
|
|
|
int TypeID = m_aExtendedItemTypes[Index];
|
|
|
|
CUuid Uuid = g_UuidManager.GetUuid(TypeID);
|
|
|
|
int *pUuidItem = (int *)NewItem(0, GetTypeFromIndex(Index), sizeof(Uuid)); // NETOBJTYPE_EX
|
|
|
|
for(int i = 0; i < (int)sizeof(CUuid) / 4; i++)
|
|
|
|
{
|
|
|
|
pUuidItem[i] =
|
|
|
|
(Uuid.m_aData[i * 4 + 0] << 24) |
|
|
|
|
(Uuid.m_aData[i * 4 + 1] << 16) |
|
|
|
|
(Uuid.m_aData[i * 4 + 2] << 8) |
|
|
|
|
(Uuid.m_aData[i * 4 + 3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CSnapshotBuilder::GetExtendedItemTypeIndex(int TypeID)
|
|
|
|
{
|
|
|
|
for(int i = 0; i < m_NumExtendedItemTypes; i++)
|
|
|
|
{
|
|
|
|
if(m_aExtendedItemTypes[i] == TypeID)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbg_assert(m_NumExtendedItemTypes < MAX_EXTENDED_ITEM_TYPES, "too many extended item types");
|
|
|
|
int Index = m_NumExtendedItemTypes;
|
|
|
|
m_aExtendedItemTypes[Index] = TypeID;
|
|
|
|
m_NumExtendedItemTypes++;
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void *CSnapshotBuilder::NewItem(int Type, int ID, int Size)
|
|
|
|
{
|
2011-01-18 17:09:09 +00:00
|
|
|
if(m_DataSize + sizeof(CSnapshotItem) + Size >= CSnapshot::MAX_SIZE ||
|
2020-09-26 19:41:58 +00:00
|
|
|
m_NumItems + 1 >= MAX_ITEMS)
|
2011-01-18 17:09:09 +00:00
|
|
|
{
|
|
|
|
dbg_assert(m_DataSize < CSnapshot::MAX_SIZE, "too much data");
|
|
|
|
dbg_assert(m_NumItems < MAX_ITEMS, "too many items");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-08-04 17:09:37 +00:00
|
|
|
bool Extended = false;
|
2017-05-21 23:07:13 +00:00
|
|
|
if(Type >= OFFSET_UUID)
|
|
|
|
{
|
2020-08-04 17:09:37 +00:00
|
|
|
Extended = true;
|
2017-05-21 23:07:13 +00:00
|
|
|
Type = GetTypeFromIndex(GetExtendedItemTypeIndex(Type));
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CSnapshotItem *pObj = (CSnapshotItem *)(m_aData + m_DataSize);
|
|
|
|
|
2020-08-04 17:09:37 +00:00
|
|
|
if(m_Sixup && !Extended)
|
2020-03-29 02:36:38 +00:00
|
|
|
{
|
2020-06-12 19:22:54 +00:00
|
|
|
if(Type >= 0)
|
|
|
|
Type = Obj_SixToSeven(Type);
|
|
|
|
else
|
|
|
|
Type *= -1;
|
|
|
|
|
2020-09-26 19:41:58 +00:00
|
|
|
if(Type < 0)
|
|
|
|
return pObj;
|
2020-03-29 02:36:38 +00:00
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
mem_zero(pObj, sizeof(CSnapshotItem) + Size);
|
2020-09-26 19:41:58 +00:00
|
|
|
pObj->m_TypeAndID = (Type << 16) | ID;
|
2010-05-29 07:25:38 +00:00
|
|
|
m_aOffsets[m_NumItems] = m_DataSize;
|
|
|
|
m_DataSize += sizeof(CSnapshotItem) + Size;
|
|
|
|
m_NumItems++;
|
|
|
|
|
|
|
|
return pObj->Data();
|
|
|
|
}
|