Use a UUID, Cleanup code and UI

This commit is contained in:
Learath2 2019-12-17 15:44:54 +01:00
parent 7cdd050fee
commit 3784f5bea4
3 changed files with 51 additions and 20 deletions

View file

@ -4,6 +4,7 @@
#define ENGINE_DEMO_H
#include <base/hash.h>
#include <engine/shared/uuid_manager.h>
#include "kernel.h"
enum
@ -15,6 +16,14 @@ const double g_aSpeeds[] = {0.1, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 2.0, 3.0, 4.0,
typedef bool (*DEMOFUNC_FILTER)(const void *pData, int DataSize, void *pUser);
// TODO: Properly extend demo format using uuids
static const CUuid SHA256_EXTENSION = {{
// "6be6da4a-cebd-380c-9b5b-1289c842d780"
// "demoitem-sha256@ddnet.tw"
0x6b, 0xe6, 0xda, 0x4a, 0xce, 0xbd, 0x38, 0x0c,
0x9b, 0x5b, 0x12, 0x89, 0xc8, 0x42, 0xd7, 0x80
}};
struct CDemoHeader
{
unsigned char m_aMarker[7];

View file

@ -17,7 +17,7 @@
static const unsigned char gs_aHeaderMarker[7] = {'T', 'W', 'D', 'E', 'M', 'O', 0};
static const unsigned char gs_ActVersion = 6;
static const unsigned char gs_OldVersion = 3;
static const unsigned char gs_SHAVersion = 6;
static const unsigned char gs_Sha256Version = 6;
static const unsigned char gs_VersionTickCompression = 5; // demo files with this version or higher will use `CHUNKTICKFLAG_TICK_COMPRESSED`
static const int gs_LengthOffset = 152;
static const int gs_NumMarkersOffset = 176;
@ -121,6 +121,9 @@ int CDemoRecorder::Start(class IStorage *pStorage, class IConsole *pConsole, con
str_timestamp(Header.m_aTimestamp, sizeof(Header.m_aTimestamp));
io_write(DemoFile, &Header, sizeof(Header));
io_write(DemoFile, &TimelineMarkers, sizeof(TimelineMarkers)); // fill this on stop
//Write Sha256
io_write(DemoFile, SHA256_EXTENSION.m_aData, sizeof(SHA256_EXTENSION.m_aData));
io_write(DemoFile, &Sha256, sizeof(SHA256_DIGEST));
if(m_NoMapData)
@ -719,9 +722,23 @@ int CDemoPlayer::Load(class IStorage *pStorage, class IConsole *pConsole, const
else if(m_Info.m_Header.m_Version > gs_OldVersion)
io_read(m_File, &m_Info.m_TimelineMarkers, sizeof(m_Info.m_TimelineMarkers));
SHA256_DIGEST Sha = {};
if(m_Info.m_Header.m_Version >= gs_SHAVersion)
io_read(m_File, &Sha, sizeof(SHA256_DIGEST)); // need a safe read
SHA256_DIGEST Sha256 = SHA256_ZEROED;
if(m_Info.m_Header.m_Version >= gs_Sha256Version)
{
CUuid ExtensionUuid = {};
io_read(m_File, &ExtensionUuid.m_aData, sizeof(ExtensionUuid.m_aData));
if(ExtensionUuid == SHA256_EXTENSION)
{
io_read(m_File, &Sha256, sizeof(SHA256_DIGEST)); // need a safe read
}
else
{
// This hopes whatever happened during the version increment didn't add something here
dbg_msg("demo", "demo version incremented, but not by ddnet");
io_seek(m_File, -(int)sizeof(ExtensionUuid.m_aData), IOSEEK_CUR);
}
}
// get demo type
if(!str_comp(m_Info.m_Header.m_aType, "client"))
@ -744,7 +761,7 @@ int CDemoPlayer::Load(class IStorage *pStorage, class IConsole *pConsole, const
// store map information
m_MapInfo.m_Crc = Crc;
m_MapInfo.m_Sha256 = Sha;
m_MapInfo.m_Sha256 = Sha256;
m_MapInfo.m_Size = MapSize;
str_copy(m_MapInfo.m_aName, m_Info.m_Header.m_aMapName, sizeof(m_MapInfo.m_aName));
@ -787,18 +804,18 @@ void CDemoPlayer::ExtractMap(class IStorage *pStorage)
io_seek(m_File, CurSeek, IOSEEK_START);
// handle sha256
SHA256_DIGEST Sha = {};
if(m_Info.m_Header.m_Version >= gs_SHAVersion)
Sha = m_MapInfo.m_Sha256;
SHA256_DIGEST Sha256 = SHA256_ZEROED;
if(m_Info.m_Header.m_Version >= gs_Sha256Version)
Sha256 = m_MapInfo.m_Sha256;
else
{
Sha = sha256(pMapData, m_MapInfo.m_Size);
m_MapInfo.m_Sha256 = Sha;
Sha256 = sha256(pMapData, m_MapInfo.m_Size);
m_MapInfo.m_Sha256 = Sha256;
}
// construct name
char aSha[SHA256_MAXSTRSIZE], aMapFilename[128];
sha256_str(Sha, aSha, sizeof(aSha));
sha256_str(Sha256, aSha, sizeof(aSha));
str_format(aMapFilename, sizeof(aMapFilename), "downloadedmaps/%s_%08x_%s.map", m_Info.m_Header.m_aMapName, m_MapInfo.m_Crc, aSha);
// save map
@ -995,10 +1012,10 @@ bool CDemoPlayer::GetDemoInfo(class IStorage *pStorage, const char *pFilename, i
str_copy(pMapInfo->m_aName, pDemoHeader->m_aMapName, sizeof(pMapInfo->m_aName));
pMapInfo->m_Crc = (pDemoHeader->m_aMapCrc[0]<<24) | (pDemoHeader->m_aMapCrc[1]<<16) | (pDemoHeader->m_aMapCrc[2]<<8) | (pDemoHeader->m_aMapCrc[3]);
if(pDemoHeader->m_Version >= gs_SHAVersion)
if(pDemoHeader->m_Version >= gs_Sha256Version)
io_read(File, &pMapInfo->m_Sha256, SHA256_DIGEST_LENGTH);
else
pMapInfo->m_Sha256 = {};
pMapInfo->m_Sha256 = SHA256_ZEROED;
pMapInfo->m_Size = (pDemoHeader->m_aMapSize[0]<<24) | (pDemoHeader->m_aMapSize[1]<<16) | (pDemoHeader->m_aMapSize[2]<<8) | (pDemoHeader->m_aMapSize[3]);

View file

@ -935,13 +935,18 @@ void CMenus::RenderDemoList(CUIRect MainView)
UI()->DoLabelScaled(&Right, aBuf, 14.0f, -1);
Labels.HSplitTop(5.0f, 0, &Labels);
Labels.HSplitTop(20.0f, &Left, &Labels);
Left.VSplitLeft(150.0f, &Left, &Right);
UI()->DoLabelScaled(&Left, Localize("SHA256:"), 14.0f, -1);
char aSha[SHA256_MAXSTRSIZE];
sha256_str(m_lDemos[m_DemolistSelectedIndex].m_MapInfo.m_Sha256, aSha, sizeof(aSha)/2);
UI()->DoLabelScaled(&Right, aSha, 14.0f, -1);
Labels.HSplitTop(5.0f, 0, &Labels);
Labels.HSplitTop(20.0f, &Left, &Labels);
if(m_lDemos[m_DemolistSelectedIndex].m_MapInfo.m_Sha256 != SHA256_ZEROED)
{
Left.VSplitLeft(150.0f, &Left, &Right);
UI()->DoLabelScaled(&Left, Localize("SHA256:"), 14.0f, -1);
char aSha[SHA256_MAXSTRSIZE];
sha256_str(m_lDemos[m_DemolistSelectedIndex].m_MapInfo.m_Sha256, aSha, sizeof(aSha)/2);
UI()->DoLabelScaled(&Right, aSha, 14.0f, -1);
Labels.HSplitTop(5.0f, 0, &Labels);
Labels.HSplitTop(20.0f, &Left, &Labels);
}
Left.VSplitLeft(150.0f, &Left, &Right);
UI()->DoLabelScaled(&Left, Localize("Netversion:"), 14.0f, -1);
UI()->DoLabelScaled(&Right, m_lDemos[m_DemolistSelectedIndex].m_Info.m_aNetversion, 14.0f, -1);