Skip to content

Commit

Permalink
Add SHA to demos. Bump Demo Version
Browse files Browse the repository at this point in the history
  • Loading branch information
Learath2 committed Oct 14, 2019
1 parent b2cefb2 commit 7cdd050
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 28 deletions.
6 changes: 4 additions & 2 deletions src/engine/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3421,10 +3421,12 @@ const char *CClient::DemoPlayer_Play(const char *pFilename, int StorageType)

// load map
Crc = m_DemoPlayer.GetMapInfo()->m_Crc;
pError = LoadMapSearch(m_DemoPlayer.Info()->m_Header.m_aMapName, 0, Crc);
SHA256_DIGEST Sha = m_DemoPlayer.GetMapInfo()->m_Sha256;
pError = LoadMapSearch(m_DemoPlayer.Info()->m_Header.m_aMapName, &Sha, Crc);
if(pError)
{
SHA256_DIGEST Sha = m_DemoPlayer.ExtractMap(Storage());
m_DemoPlayer.ExtractMap(Storage());
Sha = m_DemoPlayer.GetMapInfo()->m_Sha256;
pError = LoadMapSearch(m_DemoPlayer.Info()->m_Header.m_aMapName, &Sha, Crc);
if(pError)
{
Expand Down
11 changes: 10 additions & 1 deletion src/engine/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#ifndef ENGINE_DEMO_H
#define ENGINE_DEMO_H

#include <base/hash.h>
#include "kernel.h"

enum
Expand Down Expand Up @@ -33,6 +34,14 @@ struct CTimelineMarkers
char m_aTimelineMarkers[MAX_TIMELINE_MARKERS][4];
};

struct CMapInfo
{
char m_aName[128];
SHA256_DIGEST m_Sha256;
int m_Crc;
int m_Size;
};

class IDemoPlayer : public IInterface
{
MACRO_INTERFACE("demoplayer", 0)
Expand Down Expand Up @@ -69,7 +78,7 @@ class IDemoPlayer : public IInterface
virtual bool IsPlaying() const = 0;
virtual const CInfo *BaseInfo() const = 0;
virtual void GetDemoName(char *pBuffer, int BufferSize) const = 0;
virtual bool GetDemoInfo(class IStorage *pStorage, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers) const = 0;
virtual bool GetDemoInfo(class IStorage *pStorage, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers, CMapInfo *pMapInfo) const = 0;
virtual int GetDemoType() const = 0;
};

Expand Down
46 changes: 35 additions & 11 deletions src/engine/shared/demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
#include "snapshot.h"

static const unsigned char gs_aHeaderMarker[7] = {'T', 'W', 'D', 'E', 'M', 'O', 0};
static const unsigned char gs_ActVersion = 5;
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_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;
Expand Down Expand Up @@ -120,6 +121,7 @@ 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
io_write(DemoFile, &Sha256, sizeof(SHA256_DIGEST));

if(m_NoMapData)
{
Expand Down Expand Up @@ -717,6 +719,10 @@ 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

// get demo type
if(!str_comp(m_Info.m_Header.m_aType, "client"))
m_DemoType = DEMOTYPE_CLIENT;
Expand All @@ -738,10 +744,10 @@ 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_Size = MapSize;
str_copy(m_MapInfo.m_aName, m_Info.m_Header.m_aMapName, sizeof(m_MapInfo.m_aName));


if(m_Info.m_Header.m_Version > gs_OldVersion)
{
// get timeline markers
Expand All @@ -767,10 +773,10 @@ int CDemoPlayer::Load(class IStorage *pStorage, class IConsole *pConsole, const
return 0;
}

SHA256_DIGEST CDemoPlayer::ExtractMap(class IStorage *pStorage)
void CDemoPlayer::ExtractMap(class IStorage *pStorage)
{
if(!m_MapInfo.m_Size)
return {};
return;

long CurSeek = io_tell(m_File);

Expand All @@ -780,8 +786,17 @@ SHA256_DIGEST CDemoPlayer::ExtractMap(class IStorage *pStorage)
io_read(m_File, pMapData, m_MapInfo.m_Size);
io_seek(m_File, CurSeek, IOSEEK_START);

// calculate sha, should probably add to demo header
SHA256_DIGEST Sha = sha256(pMapData, m_MapInfo.m_Size);
// handle sha256
SHA256_DIGEST Sha = {};
if(m_Info.m_Header.m_Version >= gs_SHAVersion)
Sha = m_MapInfo.m_Sha256;
else
{
Sha = sha256(pMapData, m_MapInfo.m_Size);
m_MapInfo.m_Sha256 = Sha;
}

// construct name
char aSha[SHA256_MAXSTRSIZE], aMapFilename[128];
sha256_str(Sha, aSha, sizeof(aSha));
str_format(aMapFilename, sizeof(aMapFilename), "downloadedmaps/%s_%08x_%s.map", m_Info.m_Header.m_aMapName, m_MapInfo.m_Crc, aSha);
Expand All @@ -793,8 +808,6 @@ SHA256_DIGEST CDemoPlayer::ExtractMap(class IStorage *pStorage)

// free data
free(pMapData);

return Sha;
}

int CDemoPlayer::NextFrame()
Expand Down Expand Up @@ -964,9 +977,9 @@ void CDemoPlayer::GetDemoName(char *pBuffer, int BufferSize) const
str_copy(pBuffer, pExtractedName, Length);
}

bool CDemoPlayer::GetDemoInfo(class IStorage *pStorage, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers) const
bool CDemoPlayer::GetDemoInfo(class IStorage *pStorage, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers, CMapInfo *pMapInfo) const
{
if(!pDemoHeader || !pTimelineMarkers)
if(!pDemoHeader || !pTimelineMarkers || !pMapInfo)
return false;

mem_zero(pDemoHeader, sizeof(CDemoHeader));
Expand All @@ -978,6 +991,17 @@ bool CDemoPlayer::GetDemoInfo(class IStorage *pStorage, const char *pFilename, i

io_read(File, pDemoHeader, sizeof(CDemoHeader));
io_read(File, pTimelineMarkers, sizeof(CTimelineMarkers));

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)
io_read(File, &pMapInfo->m_Sha256, SHA256_DIGEST_LENGTH);
else
pMapInfo->m_Sha256 = {};

pMapInfo->m_Size = (pDemoHeader->m_aMapSize[0]<<24) | (pDemoHeader->m_aMapSize[1]<<16) | (pDemoHeader->m_aMapSize[2]<<8) | (pDemoHeader->m_aMapSize[3]);

io_close(File);
return !(mem_comp(pDemoHeader->m_aMarker, gs_aHeaderMarker, sizeof(gs_aHeaderMarker)) || pDemoHeader->m_Version < gs_OldVersion);
}
Expand Down Expand Up @@ -1014,7 +1038,7 @@ void CDemoEditor::Slice(const char *pDemo, const char *pDst, int StartTick, int
if (m_pDemoPlayer->Load(m_pStorage, m_pConsole, pDemo, IStorage::TYPE_ALL) == -1)
return;

const CDemoPlayer::CMapInfo *pMapInfo = m_pDemoPlayer->GetMapInfo();
const CMapInfo *pMapInfo = m_pDemoPlayer->GetMapInfo();
SHA256_DIGEST Fake;
for(unsigned i = 0; i < sizeof(Fake.data); i++)
{
Expand Down
12 changes: 2 additions & 10 deletions src/engine/shared/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ class CDemoPlayer : public IDemoPlayer
float m_TickTime;
};

struct CMapInfo
{
char m_aName[128];
SHA256_DIGEST m_Sha256;
int m_Crc;
int m_Size;
};

private:
IListener *m_pListener;

Expand Down Expand Up @@ -129,7 +121,7 @@ class CDemoPlayer : public IDemoPlayer
void SetListener(IListener *pListener);

int Load(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename, int StorageType);
SHA256_DIGEST ExtractMap(class IStorage *pStorage);
void ExtractMap(class IStorage *pStorage);
int Play();
void Pause();
void Unpause();
Expand All @@ -141,7 +133,7 @@ class CDemoPlayer : public IDemoPlayer
int SetPos(int WantedTick);
const CInfo *BaseInfo() const { return &m_Info.m_Info; }
void GetDemoName(char *pBuffer, int BufferSize) const;
bool GetDemoInfo(class IStorage *pStorage, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers) const;
bool GetDemoInfo(class IStorage *pStorage, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers, CMapInfo *pMapInfo) const;
const char *GetDemoFileName() { return m_aFilename; };
int GetDemoType() const;

Expand Down
1 change: 1 addition & 0 deletions src/game/client/components/menus.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class CMenus : public CComponent
bool m_Valid;
CDemoHeader m_Info;
CTimelineMarkers m_TimelineMarkers;
CMapInfo m_MapInfo;

int NumMarkers() const
{
Expand Down
14 changes: 10 additions & 4 deletions src/game/client/components/menus_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <base/tl/string.h>

#include <base/math.h>
#include <base/hash.h>

#include <engine/demo.h>
#include <engine/keys.h>
Expand Down Expand Up @@ -805,7 +806,7 @@ bool CMenus::FetchHeader(CDemoItem &Item)
{
char aBuffer[512];
str_format(aBuffer, sizeof(aBuffer), "%s/%s", m_aCurrentDemoFolder, Item.m_aFilename);
Item.m_Valid = DemoPlayer()->GetDemoInfo(Storage(), aBuffer, Item.m_StorageType, &Item.m_Info, &Item.m_TimelineMarkers);
Item.m_Valid = DemoPlayer()->GetDemoInfo(Storage(), aBuffer, Item.m_StorageType, &Item.m_Info, &Item.m_TimelineMarkers, &Item.m_MapInfo);
Item.m_InfosLoaded = true;
}
return Item.m_Valid;
Expand Down Expand Up @@ -930,13 +931,18 @@ void CMenus::RenderDemoList(CUIRect MainView)
Labels.HSplitTop(20.0f, &Left, &Labels);
Left.VSplitLeft(150.0f, &Left, &Right);
UI()->DoLabelScaled(&Left, Localize("Crc:"), 14.0f, -1);
unsigned Crc = (m_lDemos[m_DemolistSelectedIndex].m_Info.m_aMapCrc[0]<<24) | (m_lDemos[m_DemolistSelectedIndex].m_Info.m_aMapCrc[1]<<16) |
(m_lDemos[m_DemolistSelectedIndex].m_Info.m_aMapCrc[2]<<8) | (m_lDemos[m_DemolistSelectedIndex].m_Info.m_aMapCrc[3]);
str_format(aBuf, sizeof(aBuf), "%08x", Crc);
str_format(aBuf, sizeof(aBuf), "%08x", m_lDemos[m_DemolistSelectedIndex].m_MapInfo.m_Crc);
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);
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);
}
Expand Down

0 comments on commit 7cdd050

Please sign in to comment.