Added nethandler namespace api, added conlog writer

This commit is contained in:
Jordan Cristiano 2015-05-14 23:38:53 -04:00
parent 3f9bd6e3f2
commit 8c178bf9ac
6 changed files with 163 additions and 43 deletions

53
demboyz/conlogwriter.cpp Normal file
View File

@ -0,0 +1,53 @@
#pragma once
#include "idemowriter.h"
#include "netmessages/nethandlers.h"
#include <cstdio>
#include <iostream>
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<FILE*>(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;
}

View File

@ -3,6 +3,7 @@
#include "demoreader.h" #include "demoreader.h"
#include <cstdio> #include <cstdio>
#include <string> #include <string>
#include <cassert>
std::string GetExtension(const std::string& filename) std::string GetExtension(const std::string& filename)
{ {
@ -18,7 +19,8 @@ enum class FileType
{ {
None, None,
Dem, Dem,
Json Json,
ConLog
}; };
FileType GetFileType(const std::string& filename) FileType GetFileType(const std::string& filename)
@ -32,6 +34,10 @@ FileType GetFileType(const std::string& filename)
{ {
return FileType::Json; return FileType::Json;
} }
if (ext == "con")
{
return FileType::ConLog;
}
return FileType::None; return FileType::None;
} }
@ -74,24 +80,36 @@ int main(const int argc, const char* argv[])
return -1; return -1;
} }
IDemoWriter* writer; IDemoWriter* writer = nullptr;
if (outputType == FileType::Dem) if (outputType == FileType::Dem)
{ {
writer = IDemoWriter::CreateDemoWriter(outputFp); writer = IDemoWriter::CreateDemoWriter(outputFp);
} }
else else if (outputType == FileType::Json)
{ {
writer = IDemoWriter::CreateJsonWriter(outputFp); writer = IDemoWriter::CreateJsonWriter(outputFp);
} }
else if (outputType == FileType::ConLog)
{
writer = IDemoWriter::CreateConLogWriter(outputFp);
}
else
{
assert(false);
}
if (inputType == FileType::Dem) if (inputType == FileType::Dem)
{ {
DemoReader::ProcessDem(inputFp, writer); DemoReader::ProcessDem(inputFp, writer);
} }
else else if (inputType == FileType::Json)
{ {
DemoReader::ProcessJson(inputFp, writer); DemoReader::ProcessJson(inputFp, writer);
} }
else
{
assert(false);
}
fclose(inputFp); fclose(inputFp);
fclose(outputFp); fclose(outputFp);

View File

@ -10,9 +10,6 @@
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
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* netDataStructs[32];
static void CreateNetMsgStructs() 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.type = bitbuf.ReadUBitLong(NETMSG_TYPE_BITS);
netPacket.data = netDataStructs[netPacket.type]; netPacket.data = netDataStructs[netPacket.type];
netHandlers[netPacket.type](bitbuf, context, netPacket.data); NetHandlers::NetMsg_BitRead(netPacket.type, bitbuf, context, netPacket.data);
writer->WriteNetPacket(netPacket);
} }
} }

View File

@ -40,6 +40,7 @@ public:
public: public:
static IDemoWriter* CreateJsonWriter(void* outputFp); static IDemoWriter* CreateJsonWriter(void* outputFp);
static IDemoWriter* CreateDemoWriter(void* outputFp); static IDemoWriter* CreateDemoWriter(void* outputFp);
static IDemoWriter* CreateConLogWriter(void* outputFp);
static void FreeDemoWriter(IDemoWriter* writer) static void FreeDemoWriter(IDemoWriter* writer)
{ {

View File

@ -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);
}

View File

@ -48,38 +48,11 @@ struct SourceGameContext
} \ } \
} }
#define DECLARE_NET_HANDLER_ARRAY(funcname) \ namespace NetHandlers
{ \ {
&NetHandlers::Net_NOP_##funcname, \ bool NetMsg_BitRead(uint32_t type, bf_read& bitbuf, SourceGameContext& context, void* data);
&NetHandlers::Net_Disconnect_##funcname, \ bool NetMsg_BitWrite(uint32_t type, bf_write& bitbuf, SourceGameContext& context, void* data);
&NetHandlers::Net_File_##funcname, \ bool NetMsg_JsonRead(uint32_t type, JsonRead& jsonbuf, SourceGameContext& context, void* data);
&NetHandlers::Net_Tick_##funcname, \ bool NetMsg_JsonWrite(uint32_t type, JsonWrite& jsonbuf, SourceGameContext& context, void* data);
&NetHandlers::Net_StringCmd_##funcname, \ void NetMsg_ToString(uint32_t type, std::ostringstream& out, void* data);
&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 \
}