From 8c178bf9ac5a74cfb520b72dfb87f47188e4f203 Mon Sep 17 00:00:00 2001 From: Jordan Cristiano Date: Thu, 14 May 2015 23:38:53 -0400 Subject: [PATCH] Added nethandler namespace api, added conlog writer --- demboyz/conlogwriter.cpp | 53 ++++++++++++++++++++ demboyz/demboyz.cpp | 26 ++++++++-- demboyz/demreader.cpp | 6 +-- demboyz/idemowriter.h | 1 + demboyz/netmessages/nethandlers.cpp | 77 +++++++++++++++++++++++++++++ demboyz/netmessages/nethandlers.h | 43 +++------------- 6 files changed, 163 insertions(+), 43 deletions(-) create mode 100644 demboyz/conlogwriter.cpp create mode 100644 demboyz/netmessages/nethandlers.cpp diff --git a/demboyz/conlogwriter.cpp b/demboyz/conlogwriter.cpp new file mode 100644 index 0000000..2dbbc6b --- /dev/null +++ b/demboyz/conlogwriter.cpp @@ -0,0 +1,53 @@ + +#pragma once + +#include "idemowriter.h" +#include "netmessages/nethandlers.h" +#include +#include + +class ConLogWriter: public IDemoWriter +{ +public: + ConLogWriter(FILE* outputFp); + + virtual void StartWriting(demoheader_t& header) override final; + virtual void EndWriting() override final; + + virtual void StartCommandPacket(CommandPacket& packet) override final; + virtual void EndCommandPacket() override final; + + virtual void WriteNetPacket(NetPacket& packet) override final; +}; + +IDemoWriter* IDemoWriter::CreateConLogWriter(void* outputFp) +{ + return new ConLogWriter(reinterpret_cast(outputFp)); +} + +ConLogWriter::ConLogWriter(FILE* outputFp) +{ +} + +void ConLogWriter::StartWriting(demoheader_t& header) +{ +} + +void ConLogWriter::EndWriting() +{ +} + +void ConLogWriter::StartCommandPacket(CommandPacket& packet) +{ +} + +void ConLogWriter::EndCommandPacket() +{ +} + +void ConLogWriter::WriteNetPacket(NetPacket& packet) +{ + std::ostringstream ss; + NetHandlers::NetMsg_ToString(packet.type, ss, packet.data); + std::cout << ss.str() << std::endl; +} diff --git a/demboyz/demboyz.cpp b/demboyz/demboyz.cpp index 1e6e0bc..a7fc973 100644 --- a/demboyz/demboyz.cpp +++ b/demboyz/demboyz.cpp @@ -3,6 +3,7 @@ #include "demoreader.h" #include #include +#include std::string GetExtension(const std::string& filename) { @@ -18,7 +19,8 @@ enum class FileType { None, Dem, - Json + Json, + ConLog }; FileType GetFileType(const std::string& filename) @@ -32,6 +34,10 @@ FileType GetFileType(const std::string& filename) { return FileType::Json; } + if (ext == "con") + { + return FileType::ConLog; + } return FileType::None; } @@ -74,24 +80,36 @@ int main(const int argc, const char* argv[]) return -1; } - IDemoWriter* writer; + IDemoWriter* writer = nullptr; if (outputType == FileType::Dem) { writer = IDemoWriter::CreateDemoWriter(outputFp); } - else + else if (outputType == FileType::Json) { writer = IDemoWriter::CreateJsonWriter(outputFp); } + else if (outputType == FileType::ConLog) + { + writer = IDemoWriter::CreateConLogWriter(outputFp); + } + else + { + assert(false); + } if (inputType == FileType::Dem) { DemoReader::ProcessDem(inputFp, writer); } - else + else if (inputType == FileType::Json) { DemoReader::ProcessJson(inputFp, writer); } + else + { + assert(false); + } fclose(inputFp); fclose(outputFp); diff --git a/demboyz/demreader.cpp b/demboyz/demreader.cpp index d7734cd..7bcb049 100644 --- a/demboyz/demreader.cpp +++ b/demboyz/demreader.cpp @@ -10,9 +10,6 @@ #include #include -typedef bool (*NetMsgReadFn)(bf_read& bitbuf, SourceGameContext& context, void* data); - -static const NetMsgReadFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(BitRead); static void* netDataStructs[32]; static void CreateNetMsgStructs() @@ -96,7 +93,8 @@ void ParsePacket(uint8_t* packet, size_t length, SourceGameContext& context, IDe { netPacket.type = bitbuf.ReadUBitLong(NETMSG_TYPE_BITS); netPacket.data = netDataStructs[netPacket.type]; - netHandlers[netPacket.type](bitbuf, context, netPacket.data); + NetHandlers::NetMsg_BitRead(netPacket.type, bitbuf, context, netPacket.data); + writer->WriteNetPacket(netPacket); } } diff --git a/demboyz/idemowriter.h b/demboyz/idemowriter.h index 414c8fa..1df85d6 100644 --- a/demboyz/idemowriter.h +++ b/demboyz/idemowriter.h @@ -40,6 +40,7 @@ public: public: static IDemoWriter* CreateJsonWriter(void* outputFp); static IDemoWriter* CreateDemoWriter(void* outputFp); + static IDemoWriter* CreateConLogWriter(void* outputFp); static void FreeDemoWriter(IDemoWriter* writer) { diff --git a/demboyz/netmessages/nethandlers.cpp b/demboyz/netmessages/nethandlers.cpp new file mode 100644 index 0000000..60b9e8c --- /dev/null +++ b/demboyz/netmessages/nethandlers.cpp @@ -0,0 +1,77 @@ + +#pragma once + +#include "nethandlers.h" +#include "netmessages.h" + +#define DECLARE_NET_HANDLER_ARRAY(funcname) \ + { \ + &NetHandlers::Net_NOP_##funcname, \ + &NetHandlers::Net_Disconnect_##funcname, \ + &NetHandlers::Net_File_##funcname, \ + &NetHandlers::Net_Tick_##funcname, \ + &NetHandlers::Net_StringCmd_##funcname, \ + &NetHandlers::Net_SetConVar_##funcname, \ + &NetHandlers::Net_SignonState_##funcname, \ + &NetHandlers::SVC_Print_##funcname, \ + &NetHandlers::SVC_ServerInfo_##funcname, \ + &NetHandlers::SVC_SendTable_##funcname, \ + &NetHandlers::SVC_ClassInfo_##funcname, \ + &NetHandlers::SVC_SetPause_##funcname, \ + &NetHandlers::SVC_CreateStringTable_##funcname, \ + &NetHandlers::SVC_UpdateStringTable_##funcname, \ + &NetHandlers::SVC_VoiceInit_##funcname, \ + &NetHandlers::SVC_VoiceData_##funcname, \ + &NetHandlers::SVC_HLTV_##funcname, \ + &NetHandlers::SVC_Sounds_##funcname, \ + &NetHandlers::SVC_SetView_##funcname, \ + &NetHandlers::SVC_FixAngle_##funcname, \ + &NetHandlers::SVC_CrosshairAngle_##funcname, \ + &NetHandlers::SVC_BSPDecal_##funcname, \ + &NetHandlers::SVC_TerrainMod_##funcname, \ + &NetHandlers::SVC_UserMessage_##funcname, \ + &NetHandlers::SVC_EntityMessage_##funcname, \ + &NetHandlers::SVC_GameEvent_##funcname, \ + &NetHandlers::SVC_PacketEntities_##funcname, \ + &NetHandlers::SVC_TempEntities_##funcname, \ + &NetHandlers::SVC_Prefetch_##funcname, \ + &NetHandlers::SVC_Menu_##funcname, \ + &NetHandlers::SVC_GameEventList_##funcname, \ + &NetHandlers::SVC_GetCvarValue_##funcname \ + } + +typedef bool (*NetMsgBitReadFn)(bf_read& bitbuf, SourceGameContext& context, void* data); +typedef bool (*NetMsgBitWriteFn)(bf_write& bitbuf, SourceGameContext& context, void* data); +typedef bool (*NetMsgJsonReadFn)(JsonRead& bitbuf, SourceGameContext& context, void* data); +typedef bool (*NetMsgJsonWriteFn)(JsonWrite& bitbuf, SourceGameContext& context, void* data); +typedef void (*NetMsgToStringFn)(std::ostringstream& out, void* data); + +bool NetHandlers::NetMsg_BitRead(uint32_t type, bf_read& bitbuf, SourceGameContext& context, void* data) +{ + static const NetMsgBitReadFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(BitRead); + return netHandlers[type](bitbuf, context, data); +} + +bool NetHandlers::NetMsg_BitWrite(uint32_t type, bf_write& bitbuf, SourceGameContext& context, void* data) +{ + static const NetMsgBitWriteFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(BitWrite); + return netHandlers[type](bitbuf, context, data); +} + +bool NetHandlers::NetMsg_JsonRead(uint32_t type, JsonRead& jsonbuf, SourceGameContext& context, void* data) +{ + static const NetMsgJsonReadFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(JsonRead); + return netHandlers[type](jsonbuf, context, data); +} + +bool NetHandlers::NetMsg_JsonWrite(uint32_t type, JsonWrite& jsonbuf, SourceGameContext& context, void* data) +{ + static const NetMsgJsonWriteFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(JsonWrite); + return netHandlers[type](jsonbuf, context, data); +} + +void NetHandlers::NetMsg_ToString(uint32_t type, std::ostringstream& out, void* data) +{ + static const NetMsgToStringFn netHandlers[] = DECLARE_NET_HANDLER_ARRAY(ToString); + netHandlers[type](out, data); +} diff --git a/demboyz/netmessages/nethandlers.h b/demboyz/netmessages/nethandlers.h index b1842b2..5cbf5bf 100644 --- a/demboyz/netmessages/nethandlers.h +++ b/demboyz/netmessages/nethandlers.h @@ -48,38 +48,11 @@ struct SourceGameContext } \ } -#define DECLARE_NET_HANDLER_ARRAY(funcname) \ - { \ - &NetHandlers::Net_NOP_##funcname, \ - &NetHandlers::Net_Disconnect_##funcname, \ - &NetHandlers::Net_File_##funcname, \ - &NetHandlers::Net_Tick_##funcname, \ - &NetHandlers::Net_StringCmd_##funcname, \ - &NetHandlers::Net_SetConVar_##funcname, \ - &NetHandlers::Net_SignonState_##funcname, \ - &NetHandlers::SVC_Print_##funcname, \ - &NetHandlers::SVC_ServerInfo_##funcname, \ - &NetHandlers::SVC_SendTable_##funcname, \ - &NetHandlers::SVC_ClassInfo_##funcname, \ - &NetHandlers::SVC_SetPause_##funcname, \ - &NetHandlers::SVC_CreateStringTable_##funcname, \ - &NetHandlers::SVC_UpdateStringTable_##funcname, \ - &NetHandlers::SVC_VoiceInit_##funcname, \ - &NetHandlers::SVC_VoiceData_##funcname, \ - &NetHandlers::SVC_HLTV_##funcname, \ - &NetHandlers::SVC_Sounds_##funcname, \ - &NetHandlers::SVC_SetView_##funcname, \ - &NetHandlers::SVC_FixAngle_##funcname, \ - &NetHandlers::SVC_CrosshairAngle_##funcname, \ - &NetHandlers::SVC_BSPDecal_##funcname, \ - &NetHandlers::SVC_TerrainMod_##funcname, \ - &NetHandlers::SVC_UserMessage_##funcname, \ - &NetHandlers::SVC_EntityMessage_##funcname, \ - &NetHandlers::SVC_GameEvent_##funcname, \ - &NetHandlers::SVC_PacketEntities_##funcname, \ - &NetHandlers::SVC_TempEntities_##funcname, \ - &NetHandlers::SVC_Prefetch_##funcname, \ - &NetHandlers::SVC_Menu_##funcname, \ - &NetHandlers::SVC_GameEventList_##funcname, \ - &NetHandlers::SVC_GetCvarValue_##funcname \ - } +namespace NetHandlers +{ + bool NetMsg_BitRead(uint32_t type, bf_read& bitbuf, SourceGameContext& context, void* data); + bool NetMsg_BitWrite(uint32_t type, bf_write& bitbuf, SourceGameContext& context, void* data); + bool NetMsg_JsonRead(uint32_t type, JsonRead& jsonbuf, SourceGameContext& context, void* data); + bool NetMsg_JsonWrite(uint32_t type, JsonWrite& jsonbuf, SourceGameContext& context, void* data); + void NetMsg_ToString(uint32_t type, std::ostringstream& out, void* data); +}