From 9bb4aa46e8b461f1176d12f85c00eddcdf20fd7d Mon Sep 17 00:00:00 2001 From: Jordan Cristiano Date: Wed, 29 Oct 2014 01:35:43 -0400 Subject: [PATCH] Added packet parsing logic and assertions for frame and tick count --- demboyz/demboyz.cpp | 30 ++++++++++++++++++++++++++++++ demboyz/demofile.cpp | 26 ++++++++++++++++++++++++++ demboyz/demofile.h | 2 ++ 3 files changed, 58 insertions(+) diff --git a/demboyz/demboyz.cpp b/demboyz/demboyz.cpp index 6644166..e0d5c47 100644 --- a/demboyz/demboyz.cpp +++ b/demboyz/demboyz.cpp @@ -1,6 +1,7 @@ #include "demofile.h" #include +#include int main(const int argc, const char* argv[]) { @@ -19,9 +20,38 @@ int main(const int argc, const char* argv[]) unsigned char cmd; int32 tick; + int32 sequenceInfo1; + int32 sequenceInfo2; + democmdinfo_t cmdInfo; + std::string packetBuf; demoFile.ReadCmdHeader(cmd, tick); assert(cmd == dem_synctick && tick == 0); + + const int numFrames = demoHeader->playback_frames; + const int numTicks = demoHeader->playback_ticks; + for (int i = 0; i <= numFrames; ++i) + { + demoFile.ReadCmdHeader(cmd, tick); + printf("tick: %i\n", tick); + switch (cmd) + { + case dem_packet: + demoFile.ReadCmdInfo(cmdInfo); + demoFile.ReadSequenceInfo(sequenceInfo1, sequenceInfo2); + demoFile.ReadRawData(packetBuf); + break; + case dem_stop: + assert(i == numFrames && tick == numTicks); + break; + case dem_synctick: + case dem_consolecmd: + case dem_usercmd: + case dem_datatables: + default: + break; + } + } demoFile.Close(); diff --git a/demboyz/demofile.cpp b/demboyz/demofile.cpp index 7e1f74e..3e83e07 100644 --- a/demboyz/demofile.cpp +++ b/demboyz/demofile.cpp @@ -124,6 +124,32 @@ int32 CDemoFile::ReadRawData( char *buffer, int32 length ) return size; } +bool CDemoFile::ReadRawData(std::string& buf) +{ + if (!m_fileBuffer.size()) + { + return false; + } + + // read length of data block + int32 size = *(int32 *)(&m_fileBuffer[m_fileBufferPos]); + m_fileBufferPos += sizeof(int32); + + if (size < 0) + { + fprintf(stderr, "CDemoFile::ReadRawData: invalid size (%i).\n", size); + return false; + } + + buf.resize(size); + + // read data into buffer + memcpy(&buf[0], &m_fileBuffer[m_fileBufferPos], size); + m_fileBufferPos += size; + + return true; +} + bool CDemoFile::Open( const char *name ) { Close(); diff --git a/demboyz/demofile.h b/demboyz/demofile.h index 7c6c226..4886c19 100644 --- a/demboyz/demofile.h +++ b/demboyz/demofile.h @@ -229,6 +229,8 @@ public: int32 ReadRawData( char *buffer, int32 length ); + bool ReadRawData(std::string& buf); + void ReadSequenceInfo( int32 &nSeqNrIn, int32 &nSeqNrOutAck ); void ReadCmdInfo( democmdinfo_t& info );