From 2e597565bf62011e8e45c35f5631e8c0d89855cc Mon Sep 17 00:00:00 2001 From: Jordan Cristiano Date: Sun, 5 Jul 2015 23:42:08 -0400 Subject: [PATCH] Changed json output to an object stream based format --- demboyz/base/jsonfile.cpp | 39 +++++++++++++++++++++++++++++++++++++++ demboyz/base/jsonfile.h | 3 +++ demboyz/io/jsonwriter.cpp | 33 +++++++++++++++++++++++---------- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/demboyz/base/jsonfile.cpp b/demboyz/base/jsonfile.cpp index 442df5f..e1523be 100644 --- a/demboyz/base/jsonfile.cpp +++ b/demboyz/base/jsonfile.cpp @@ -25,6 +25,10 @@ namespace base void JsonWriterFile::Flush() { m_fileStream.Flush(); + + void JsonWriterFile::Reset() + { + m_writer.Reset(m_fileStream); } bool JsonWriterFile::IsComplete() const @@ -62,6 +66,13 @@ namespace base m_writer.EndArray(); } + void JsonWriterFile::WriteNull(const char* name) + { + auto& writer = m_writer; + writer.String(name); + writer.Null(); + } + void JsonWriterFile::WriteBool(const char* name, bool value) { auto& writer = m_writer; @@ -76,6 +87,20 @@ namespace base writer.Int(value); } + void JsonWriterFile::WriteInt32(const char* name, std::int32_t value, bool writeCondition) + { + auto& writer = m_writer; + writer.String(name); + if (writeCondition) + { + writer.Int(value); + } + else + { + writer.Null(); + } + } + void JsonWriterFile::WriteInt64(const char* name, std::int64_t value) { auto& writer = m_writer; @@ -90,6 +115,20 @@ namespace base writer.Uint(value); } + void JsonWriterFile::WriteUInt32(const char* name, std::uint32_t value, bool writeCondition) + { + auto& writer = m_writer; + writer.String(name); + if (writeCondition) + { + writer.Uint(value); + } + else + { + writer.Null(); + } + } + void JsonWriterFile::WriteUint64(const char* name, std::uint64_t value) { auto& writer = m_writer; diff --git a/demboyz/base/jsonfile.h b/demboyz/base/jsonfile.h index 10c9aca..c258dd0 100644 --- a/demboyz/base/jsonfile.h +++ b/demboyz/base/jsonfile.h @@ -17,6 +17,7 @@ namespace base FILE* GetFp() const; void Flush(); + void Reset(); bool IsComplete() const; void StartObject(const char* name = nullptr); @@ -28,8 +29,10 @@ namespace base void WriteNull(const char* name); void WriteBool(const char* name, bool value); void WriteInt32(const char* name, std::int32_t value); + void WriteInt32(const char* name, std::int32_t value, bool writeCondition); void WriteInt64(const char* name, std::int64_t value); void WriteUInt32(const char* name, std::uint32_t value); + void WriteUInt32(const char* name, std::uint32_t value, bool writeCondition); void WriteUint64(const char* name, std::uint64_t value); void WriteString(const char* name, const char* value); void WriteString(const char* name, const char* value, std::int32_t length); diff --git a/demboyz/io/jsonwriter.cpp b/demboyz/io/jsonwriter.cpp index 8ef3102..53751a9 100644 --- a/demboyz/io/jsonwriter.cpp +++ b/demboyz/io/jsonwriter.cpp @@ -41,50 +41,63 @@ JsonWriter::JsonWriter(FILE* outputFp): void JsonWriter::StartWriting(demoheader_t& header) { auto& jsonFile = m_jsonFile; + jsonFile.Reset(); jsonFile.StartObject(); - DemoJsonWriter::WriteDemoHeader(jsonFile, header); - jsonFile.StartArray("demmessages"); + jsonFile.EndObject(); } void JsonWriter::EndWriting() { auto& jsonFile = m_jsonFile; - jsonFile.EndArray(); - jsonFile.EndObject(); + jsonFile.Flush(); assert(jsonFile.IsComplete()); } void JsonWriter::StartCommandPacket(const CommandPacket& packet) { auto& jsonFile = m_jsonFile; + jsonFile.Reset(); jsonFile.StartObject(); jsonFile.WriteInt32("tick", packet.tick); - jsonFile.StartObject(DemoCmdToString(packet.cmd)); + jsonFile.WriteString("cmd", DemoCmdToString(packet.cmd)); + jsonFile.EndObject(); + + jsonFile.Reset(); + jsonFile.StartObject(); DemHandlers::DemMsg_JsonWrite(packet.cmd, jsonFile, packet.data); + jsonFile.EndObject(); + assert(jsonFile.IsComplete()); + if (packet.cmd == dem_packet || packet.cmd == dem_signon) { m_writingNetPackets = true; - jsonFile.StartArray("netpackets"); } } void JsonWriter::EndCommandPacket(const PacketTrailingBits& trailingBits) { - auto& jsonFile = m_jsonFile; if (m_writingNetPackets) { m_writingNetPackets = false; - jsonFile.EndArray(); + auto& jsonFile = m_jsonFile; + jsonFile.Reset(); + jsonFile.StartObject(); + jsonFile.StartObject("netpackets_end"); + jsonFile.WriteInt32("numTrailingBits", trailingBits.numTrailingBits); + jsonFile.WriteInt32("trailingBitsValue", trailingBits.value, (trailingBits.numTrailingBits > 0)); + jsonFile.EndObject(); + jsonFile.EndObject(); + assert(jsonFile.IsComplete()); } - jsonFile.EndObject(); - jsonFile.EndObject(); } void JsonWriter::WriteNetPacket(NetPacket& packet, SourceGameContext& context) { auto& jsonFile = m_jsonFile; + jsonFile.Reset(); jsonFile.StartObject(); NetHandlers::NetMsg_JsonWrite(packet.type, jsonFile, context, packet.data); jsonFile.EndObject(); + assert(jsonFile.IsComplete()); }