Implemented JsonWriter io class

This commit is contained in:
Jordan Cristiano 2015-06-26 01:11:36 -04:00
parent 45da2a9b2d
commit 1d3656b24b
3 changed files with 75 additions and 2 deletions

View File

@ -3,12 +3,20 @@
#include <cstdint> #include <cstdint>
namespace base
{
class JsonWriterFile;
}
class DemoFileReader; class DemoFileReader;
class DemoFileWriter; class DemoFileWriter;
class DemoFileReader2;
class DemoFileWriter2;
using FileRead = DemoFileReader; using FileRead = DemoFileReader;
using FileWrite = DemoFileWriter; using FileWrite = DemoFileWriter;
class JsonRead; class JsonRead;
class JsonWrite; using JsonWrite = base::JsonWriterFile;
#define DECLARE_DEM_HANDLERS(msgname) \ #define DECLARE_DEM_HANDLERS(msgname) \
namespace DemHandlers \ namespace DemHandlers \

View File

@ -61,6 +61,27 @@ enum
dem_lastcmd = dem_stringtables dem_lastcmd = dem_stringtables
}; };
inline const char* DemoCmdToString(unsigned int cmd)
{
static const char* cmdNames[] =
{
"dem_unknown",
"dem_signon",
"dem_packet",
"dem_synctick",
"dem_consolecmd",
"dem_usercmd",
"dem_datatables",
"dem_stop",
"dem_stringtables"
};
if (cmd > (sizeof(cmdNames) / sizeof(const char*)))
{
return "";
}
return cmdNames[cmd];
}
struct demoheader_t struct demoheader_t
{ {
char demofilestamp[ 8 ]; // Should be HL2DEMO char demofilestamp[ 8 ]; // Should be HL2DEMO

View File

@ -1,6 +1,12 @@
#include "idemowriter.h" #include "idemowriter.h"
#include "demofile/demojson.h"
#include "demofile/demotypes.h"
#include "base/jsonfile.h"
#include "demmessages/demhandlers.h"
#include "netmessages/nethandlers.h"
#include <cstdio> #include <cstdio>
#include <cassert>
class JsonWriter: public IDemoWriter class JsonWriter: public IDemoWriter
{ {
@ -14,6 +20,11 @@ public:
virtual void EndCommandPacket(const PacketTrailingBits& trailingBits) override final; virtual void EndCommandPacket(const PacketTrailingBits& trailingBits) override final;
virtual void WriteNetPacket(NetPacket& packet, SourceGameContext& context) override final; virtual void WriteNetPacket(NetPacket& packet, SourceGameContext& context) override final;
private:
base::JsonWriterFile m_jsonFile;
bool m_writingNetPackets;
char m_buffer[4096];
}; };
IDemoWriter* IDemoWriter::CreateJsonWriter(void* outputFp) IDemoWriter* IDemoWriter::CreateJsonWriter(void* outputFp)
@ -21,26 +32,59 @@ IDemoWriter* IDemoWriter::CreateJsonWriter(void* outputFp)
return new JsonWriter(reinterpret_cast<FILE*>(outputFp)); return new JsonWriter(reinterpret_cast<FILE*>(outputFp));
} }
JsonWriter::JsonWriter(FILE* outputFp) JsonWriter::JsonWriter(FILE* outputFp):
m_jsonFile(outputFp, m_buffer, sizeof(m_buffer)),
m_writingNetPackets(false)
{ {
} }
void JsonWriter::StartWriting(demoheader_t& header) void JsonWriter::StartWriting(demoheader_t& header)
{ {
auto& jsonFile = m_jsonFile;
jsonFile.StartObject();
DemoJsonWriter::WriteDemoHeader(jsonFile, header);
jsonFile.StartArray("demmessages");
} }
void JsonWriter::EndWriting() void JsonWriter::EndWriting()
{ {
auto& jsonFile = m_jsonFile;
jsonFile.EndArray();
jsonFile.EndObject();
assert(jsonFile.IsComplete());
} }
void JsonWriter::StartCommandPacket(const CommandPacket& packet) void JsonWriter::StartCommandPacket(const CommandPacket& packet)
{ {
auto& jsonFile = m_jsonFile;
jsonFile.StartObject();
jsonFile.WriteInt32("tick", packet.tick);
jsonFile.StartObject(DemoCmdToString(packet.cmd));
DemHandlers::DemMsg_JsonWrite(packet.cmd, jsonFile, packet.data);
if (packet.cmd == dem_packet || packet.cmd == dem_signon)
{
m_writingNetPackets = true;
jsonFile.StartArray("netpackets");
}
} }
void JsonWriter::EndCommandPacket(const PacketTrailingBits& trailingBits) void JsonWriter::EndCommandPacket(const PacketTrailingBits& trailingBits)
{ {
auto& jsonFile = m_jsonFile;
if (m_writingNetPackets)
{
m_writingNetPackets = false;
jsonFile.EndArray();
}
jsonFile.EndObject();
jsonFile.EndObject();
} }
void JsonWriter::WriteNetPacket(NetPacket& packet, SourceGameContext& context) void JsonWriter::WriteNetPacket(NetPacket& packet, SourceGameContext& context)
{ {
auto& jsonFile = m_jsonFile;
jsonFile.StartObject();
NetHandlers::NetMsg_JsonWrite(packet.type, jsonFile, context, packet.data);
jsonFile.EndObject();
} }