Skip to content

Commit

Permalink
Merge pull request ddnet#7358 from Robyt3/Engine-Demo-Validation
Browse files Browse the repository at this point in the history
Validate ticks when reading demo chunk headers, check for all file errors in demo player, show demo error popup
  • Loading branch information
def- authored Oct 19, 2023
2 parents dad2c14 + 0e4f174 commit 5987d8d
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 169 deletions.
2 changes: 1 addition & 1 deletion src/base/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ char *io_read_all_str(IOHANDLE io)
return (char *)buffer;
}

unsigned io_skip(IOHANDLE io, int size)
int io_skip(IOHANDLE io, int size)
{
return io_seek(io, size, IOSEEK_CUR);
}
Expand Down
4 changes: 2 additions & 2 deletions src/base/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,9 @@ char *io_read_all_str(IOHANDLE io);
* @param io Handle to the file.
* @param size Number of bytes to skip.
*
* @return Number of bytes skipped.
* @return 0 on success.
*/
unsigned io_skip(IOHANDLE io, int size);
int io_skip(IOHANDLE io, int size);

/**
* Writes data from a buffer to file.
Expand Down
28 changes: 16 additions & 12 deletions src/engine/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,9 @@ void CClient::Connect(const char *pAddress, const char *pPassword)

void CClient::DisconnectWithReason(const char *pReason)
{
if(pReason != nullptr && pReason[0] == '\0')
pReason = nullptr;

char aBuf[512];
str_format(aBuf, sizeof(aBuf), "disconnecting. reason='%s'", pReason ? pReason : "unknown");
m_pConsole->Print(IConsole::OUTPUT_LEVEL_STANDARD, "client", aBuf, gs_ClientNetworkPrintColor);
Expand Down Expand Up @@ -2437,7 +2440,13 @@ void CClient::Update()
else
{
// disconnect on error
Disconnect();
DisconnectWithReason(m_DemoPlayer.ErrorMessage());
if(m_DemoPlayer.ErrorMessage()[0] != '\0')
{
SWarning Warning(Localize("Error playing demo"), m_DemoPlayer.ErrorMessage());
Warning.m_AutoHide = false;
m_vWarnings.emplace_back(Warning);
}
}
}
else if(State() == IClient::STATE_ONLINE)
Expand Down Expand Up @@ -3581,27 +3590,23 @@ void CClient::DemoSlice(const char *pDstPath, CLIENTFUNC_FILTER pfnFilter, void
{
if(m_DemoPlayer.IsPlaying())
{
const char *pDemoFileName = m_DemoPlayer.GetDemoFileName();
m_DemoEditor.Slice(pDemoFileName, pDstPath, g_Config.m_ClDemoSliceBegin, g_Config.m_ClDemoSliceEnd, pfnFilter, pUser);
m_DemoEditor.Slice(m_DemoPlayer.Filename(), pDstPath, g_Config.m_ClDemoSliceBegin, g_Config.m_ClDemoSliceEnd, pfnFilter, pUser);
}
}

const char *CClient::DemoPlayer_Play(const char *pFilename, int StorageType)
{
IOHANDLE File = Storage()->OpenFile(pFilename, IOFLAG_READ, StorageType);
if(!File)
return "error opening demo file";

io_close(File);
// Don't disconnect unless the file exists (only for play command)
if(!Storage()->FileExists(pFilename, StorageType))
return "No demo with this filename exists";

Disconnect();
m_aNetClient[CONN_MAIN].ResetErrorString();

// try to start playback
m_DemoPlayer.SetListener(this);

if(m_DemoPlayer.Load(Storage(), m_pConsole, pFilename, StorageType))
return "error loading demo";
return m_DemoPlayer.ErrorMessage();

// load map
const CMapInfo *pMapInfo = m_DemoPlayer.GetMapInfo();
Expand Down Expand Up @@ -3669,8 +3674,7 @@ const char *CClient::DemoPlayer_Render(const char *pFilename, int StorageType, c
{
m_DemoPlayer.Pause();
}
//m_pConsole->Print(IConsole::OUTPUT_LEVEL_DEBUG, "demo_recorder", "demo eof");
return 0;
return nullptr;
}
#endif

Expand Down
6 changes: 3 additions & 3 deletions src/engine/demo.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ struct CMapInfo
{
char m_aName[MAX_MAP_LENGTH];
SHA256_DIGEST m_Sha256;
int m_Crc;
int m_Size;
unsigned m_Crc;
unsigned m_Size;
};

class IDemoPlayer : public IInterface
Expand Down Expand Up @@ -100,7 +100,7 @@ class IDemoPlayer : public IInterface
virtual bool IsPlaying() const = 0;
virtual const CInfo *BaseInfo() const = 0;
virtual void GetDemoName(char *pBuffer, size_t BufferSize) const = 0;
virtual bool GetDemoInfo(class IStorage *pStorage, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers, CMapInfo *pMapInfo) const = 0;
virtual bool GetDemoInfo(class IStorage *pStorage, class IConsole *pConsole, const char *pFilename, int StorageType, CDemoHeader *pDemoHeader, CTimelineMarkers *pTimelineMarkers, CMapInfo *pMapInfo, IOHANDLE *pFile = nullptr, char *pErrorMessage = nullptr, size_t ErrorMessageSize = 0) const = 0;
};

class IDemoRecorder : public IInterface
Expand Down
Loading

0 comments on commit 5987d8d

Please sign in to comment.