Skip to content

Commit

Permalink
Check for all file errors in demo player, show demo error popup
Browse files Browse the repository at this point in the history
Handle the return values of all uses of the `io_read`, `io_skip` and `io_tell` functions in `CDemoPlayer`, to handle truncated demo files and other unexpected file reading and seeking errors during demo playback.

Show more detailed error message popups when demos cannot be loaded due to errors and when demo playback is stopped unexpectedly due to errors. Previously, only a generic message "Error loading demo" was shown when loading failed and no error message was shown when demo playback is stopped due to errors.
  • Loading branch information
Robyt3 committed Oct 19, 2023
1 parent 92e2e17 commit 0e4f174
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 154 deletions.
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 0e4f174

Please sign in to comment.