Added demo message handling functions

This commit is contained in:
Jordan Cristiano 2015-06-14 17:23:09 -04:00
parent cc7066ac86
commit b1830dccae
19 changed files with 572 additions and 0 deletions

View File

@ -0,0 +1,31 @@
#include "dem_consolecmd.h"
#include "demofile/demofile.h"
namespace DemHandlers
{
bool Dem_ConsoleCmd_FileRead_Internal(DemoFileReader& demofile, DemMsg::Dem_ConsoleCmd* data)
{
char command[DemMsg::Dem_ConsoleCmd::COMMAND_MAX_LENGTH];
demofile.ReadRawData(reinterpret_cast<uint8_t*>(command), sizeof(command));
data->command.assign(command);
return demofile.IsOk();
}
bool Dem_ConsoleCmd_FileWrite_Internal(DemoFileWriter& demofile, DemMsg::Dem_ConsoleCmd* data)
{
const uint8_t* command = reinterpret_cast<const uint8_t*>(data->command.data());
demofile.WriteRawData(command, data->command.length() + 1);
return demofile.IsOk();
}
bool Dem_ConsoleCmd_JsonRead_Internal(JsonRead& jsonbuf, DemMsg::Dem_ConsoleCmd* data)
{
return true;
}
bool Dem_ConsoleCmd_JsonWrite_Internal(JsonWrite& jsonbuf, DemMsg::Dem_ConsoleCmd* data)
{
return true;
}
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "demhandlers.h"
#include <string>
namespace DemMsg
{
struct Dem_ConsoleCmd
{
static const int COMMAND_MAX_LENGTH = 1024;
std::string command;
};
}
DECLARE_DEM_HANDLERS(Dem_ConsoleCmd);

View File

@ -0,0 +1,28 @@
#include "dem_datatables.h"
#include "demofile/demofile.h"
namespace DemHandlers
{
bool Dem_DataTables_FileRead_Internal(DemoFileReader& demofile, DemMsg::Dem_DataTables* data)
{
data->data = demofile.ReadRawData(DemMsg::Dem_DataTables::DATA_MAX_LENGTH);
return demofile.IsOk();
}
bool Dem_DataTables_FileWrite_Internal(DemoFileWriter& demofile, DemMsg::Dem_DataTables* data)
{
demofile.WriteRawData(data->data.begin(), data->data.length());
return demofile.IsOk();
}
bool Dem_DataTables_JsonRead_Internal(JsonRead& jsonbuf, DemMsg::Dem_DataTables* data)
{
return true;
}
bool Dem_DataTables_JsonWrite_Internal(JsonWrite& jsonbuf, DemMsg::Dem_DataTables* data)
{
return true;
}
}

View File

@ -0,0 +1,16 @@
#pragma once
#include "demhandlers.h"
#include "base/array.h"
namespace DemMsg
{
struct Dem_DataTables
{
static const int DATA_MAX_LENGTH = 256 * 1024;
Array<uint8_t> data;
};
}
DECLARE_DEM_HANDLERS(Dem_DataTables);

View File

@ -0,0 +1,32 @@
#include "dem_packet.h"
#include "demofile/demofile.h"
namespace DemHandlers
{
bool Dem_Packet_FileRead_Internal(DemoFileReader& demofile, DemMsg::Dem_Packet* data)
{
demofile.ReadCmdInfo(data->cmdInfo);
demofile.ReadSequenceInfo(data->sequenceNum1, data->sequenceNum2);
//data->dataLengthInBytes = demofile.ReadRawData(data->data, sizeof(data->data));
return demofile.IsOk();
}
bool Dem_Packet_FileWrite_Internal(DemoFileWriter& demofile, DemMsg::Dem_Packet* data)
{
demofile.WriteCmdInfo(data->cmdInfo);
demofile.WriteSequenceInfo(data->sequenceNum1, data->sequenceNum2);
//demofile.WriteRawData(data->data, data->dataLengthInBytes);
return demofile.IsOk();
}
bool Dem_Packet_JsonRead_Internal(JsonRead& jsonbuf, DemMsg::Dem_Packet* data)
{
return true;
}
bool Dem_Packet_JsonWrite_Internal(JsonWrite& jsonbuf, DemMsg::Dem_Packet* data)
{
return true;
}
}

View File

@ -0,0 +1,20 @@
#pragma once
#include "demhandlers.h"
#include "demofile/demotypes.h"
#include "netmessages/netcontants.h"
namespace DemMsg
{
struct Dem_Packet
{
democmdinfo_t cmdInfo;
int32_t sequenceNum1;
int32_t sequenceNum2;
//uint32_t dataLengthInBytes;
//uint8_t data[NET_MAX_PAYLOAD];
};
}
DECLARE_DEM_HANDLERS(Dem_Packet);

View File

@ -0,0 +1,25 @@
#include "dem_stop.h"
namespace DemHandlers
{
bool Dem_Stop_FileRead_Internal(DemoFileReader& demofile, DemMsg::Dem_Stop* data)
{
return true;
}
bool Dem_Stop_FileWrite_Internal(DemoFileWriter& demofile, DemMsg::Dem_Stop* data)
{
return true;
}
bool Dem_Stop_JsonRead_Internal(JsonRead& jsonbuf, DemMsg::Dem_Stop* data)
{
return true;
}
bool Dem_Stop_JsonWrite_Internal(JsonWrite& jsonbuf, DemMsg::Dem_Stop* data)
{
return true;
}
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "demhandlers.h"
namespace DemMsg
{
struct Dem_Stop
{
};
}
DECLARE_DEM_HANDLERS(Dem_Stop);

View File

@ -0,0 +1,136 @@
#include "dem_stringtables.h"
#include "demofile/demofile.h"
#include "netmessages/netcontants.h"
#include "sourcesdk/bitbuf.h"
#include <memory>
static void StringTableEntry_BitRead(bf_read& bitbuf, DemMsg::Dem_StringTables::StringTableEntry* data)
{
using StringTableEntry = DemMsg::Dem_StringTables::StringTableEntry;
{
char entryName[StringTableEntry::ENTRYNAME_MAX_LENGTH];
bitbuf.ReadString(entryName, sizeof(entryName));
data->entryName.assign(entryName);
}
if (bitbuf.ReadOneBit() != 0)
{
const int32_t numBytes = bitbuf.ReadWord();
data->data.reset(numBytes);
bitbuf.ReadBytes(data->data.begin(), numBytes);
}
else
{
data->data.reset(0);
}
}
static void StringTableEntry_BitWrite(bf_write& bitbuf, DemMsg::Dem_StringTables::StringTableEntry* data)
{
bitbuf.WriteString(data->entryName.c_str());
const int32_t numDataBytes = data->data.length();
bitbuf.WriteOneBit(numDataBytes > 0);
if (numDataBytes > 0)
{
bitbuf.WriteWord(numDataBytes);
bitbuf.WriteBytes(data->data.begin(), numDataBytes);
}
}
static void StringTable_BitRead(bf_read& bitbuf, DemMsg::Dem_StringTables::StringTable* data)
{
using StringTable = DemMsg::Dem_StringTables::StringTable;
using StringTableEntry = DemMsg::Dem_StringTables::StringTableEntry;
{
char tableName[StringTable::TABLENAME_MAX_LENGTH];
bitbuf.ReadString(tableName, sizeof(tableName));
data->tableName.assign(tableName);
}
data->entries1.reset(bitbuf.ReadWord());
for (StringTableEntry& entry : data->entries1)
{
StringTableEntry_BitRead(bitbuf, &entry);
}
if (bitbuf.ReadOneBit() != 0)
{
data->entries2.reset(bitbuf.ReadWord());
for (StringTableEntry& entry : data->entries2)
{
StringTableEntry_BitRead(bitbuf, &entry);
}
}
else
{
data->entries2.reset(0);
}
}
static void StringTable_BitWrite(bf_write& bitbuf, DemMsg::Dem_StringTables::StringTable* data)
{
using StringTableEntry = DemMsg::Dem_StringTables::StringTableEntry;
bitbuf.WriteString(data->tableName.c_str());
bitbuf.WriteWord(data->entries1.length());
for (StringTableEntry& entry : data->entries1)
{
StringTableEntry_BitWrite(bitbuf, &entry);
}
const int32_t entries2Count = data->entries2.length();
bitbuf.WriteOneBit(entries2Count > 0);
if (entries2Count > 0)
{
bitbuf.WriteWord(entries2Count);
for (StringTableEntry& entry : data->entries2)
{
StringTableEntry_BitWrite(bitbuf, &entry);
}
}
}
namespace DemHandlers
{
bool Dem_StringTables_FileRead_Internal(DemoFileReader& demofile, DemMsg::Dem_StringTables* data)
{
using StringTable = DemMsg::Dem_StringTables::StringTable;
Array<uint8_t> buffer = demofile.ReadRawData(MAX_STRINGTABLE_DATA);
bf_read bitbuf(buffer.begin(), buffer.length());
data->stringtables.reset(bitbuf.ReadByte());
for (StringTable& table : data->stringtables)
{
StringTable_BitRead(bitbuf, &table);
}
return !bitbuf.IsOverflowed();
}
bool Dem_StringTables_FileWrite_Internal(DemoFileWriter& demofile, DemMsg::Dem_StringTables* data)
{
using StringTable = DemMsg::Dem_StringTables::StringTable;
std::unique_ptr<uint8_t[]> buffer(new uint8_t[MAX_STRINGTABLE_DATA]);
bf_write bitbuf(buffer.get(), MAX_STRINGTABLE_DATA);
bitbuf.WriteByte(data->stringtables.length());
for (StringTable& table : data->stringtables)
{
StringTable_BitWrite(bitbuf, &table);
}
demofile.WriteRawData(bitbuf.GetBasePointer(), bitbuf.GetNumBytesWritten());
return !bitbuf.IsOverflowed();
}
bool Dem_StringTables_JsonRead_Internal(JsonRead& jsonbuf, DemMsg::Dem_StringTables* data)
{
return true;
}
bool Dem_StringTables_JsonWrite_Internal(JsonWrite& jsonbuf, DemMsg::Dem_StringTables* data)
{
return true;
}
}

View File

@ -0,0 +1,33 @@
#pragma once
#include "demhandlers.h"
#include <string>
#include "base/array.h"
namespace DemMsg
{
struct Dem_StringTables
{
struct StringTableEntry
{
static const int ENTRYNAME_MAX_LENGTH = 4096;
std::string entryName;
Array<uint8_t, int16_t> data;
};
struct StringTable
{
static const int TABLENAME_MAX_LENGTH = 256;
std::string tableName;
Array<StringTableEntry> entries1;
Array<StringTableEntry> entries2;
};
Array<StringTable> stringtables;
};
}
DECLARE_DEM_HANDLERS(Dem_StringTables);

View File

@ -0,0 +1,25 @@
#include "dem_synctick.h"
namespace DemHandlers
{
bool Dem_SyncTick_FileRead_Internal(DemoFileReader& demofile, DemMsg::Dem_SyncTick* data)
{
return true;
}
bool Dem_SyncTick_FileWrite_Internal(DemoFileWriter& demofile, DemMsg::Dem_SyncTick* data)
{
return true;
}
bool Dem_SyncTick_JsonRead_Internal(JsonRead& jsonbuf, DemMsg::Dem_SyncTick* data)
{
return true;
}
bool Dem_SyncTick_JsonWrite_Internal(JsonWrite& jsonbuf, DemMsg::Dem_SyncTick* data)
{
return true;
}
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "demhandlers.h"
namespace DemMsg
{
struct Dem_SyncTick
{
};
}
DECLARE_DEM_HANDLERS(Dem_SyncTick);

View File

@ -0,0 +1,25 @@
#include "dem_unknown.h"
namespace DemHandlers
{
bool Dem_Unknown_FileRead_Internal(DemoFileReader& demofile, DemMsg::Dem_Unknown* data)
{
return true;
}
bool Dem_Unknown_FileWrite_Internal(DemoFileWriter& demofile, DemMsg::Dem_Unknown* data)
{
return true;
}
bool Dem_Unknown_JsonRead_Internal(JsonRead& jsonbuf, DemMsg::Dem_Unknown* data)
{
return true;
}
bool Dem_Unknown_JsonWrite_Internal(JsonWrite& jsonbuf, DemMsg::Dem_Unknown* data)
{
return true;
}
}

View File

@ -0,0 +1,13 @@
#pragma once
#include "demhandlers.h"
namespace DemMsg
{
struct Dem_Unknown
{
};
}
DECLARE_DEM_HANDLERS(Dem_Unknown);

View File

@ -0,0 +1,28 @@
#include "dem_usercmd.h"
#include "demofile/demofile.h"
namespace DemHandlers
{
bool Dem_UserCmd_FileRead_Internal(DemoFileReader& demofile, DemMsg::Dem_UserCmd* data)
{
data->commandData = demofile.ReadUserCmd(data->commandNum, DemMsg::Dem_UserCmd::COMMANDDATA_MAX_LENGTH);
return demofile.IsOk();
}
bool Dem_UserCmd_FileWrite_Internal(DemoFileWriter& demofile, DemMsg::Dem_UserCmd* data)
{
demofile.WriteUserCmd(data->commandNum, data->commandData.begin(), data->commandData.length());
return demofile.IsOk();
}
bool Dem_UserCmd_JsonRead_Internal(JsonRead& jsonbuf, DemMsg::Dem_UserCmd* data)
{
return true;
}
bool Dem_UserCmd_JsonWrite_Internal(JsonWrite& jsonbuf, DemMsg::Dem_UserCmd* data)
{
return true;
}
}

View File

@ -0,0 +1,17 @@
#pragma once
#include "demhandlers.h"
#include "base/array.h"
namespace DemMsg
{
struct Dem_UserCmd
{
static const int COMMANDDATA_MAX_LENGTH = 256;
int32_t commandNum;
Array<uint8_t> commandData;
};
}
DECLARE_DEM_HANDLERS(Dem_UserCmd);

View File

@ -0,0 +1,45 @@
#include "demhandlers.h"
#include "demmessages.h"
#define DECLARE_DEM_HANDLER_ARRAY(funcname) \
{ \
&DemHandlers::Dem_Unknown_##funcname, \
&DemHandlers::Dem_Packet_##funcname, \
&DemHandlers::Dem_Packet_##funcname, \
&DemHandlers::Dem_SyncTick_##funcname, \
&DemHandlers::Dem_ConsoleCmd_##funcname, \
&DemHandlers::Dem_UserCmd_##funcname, \
&DemHandlers::Dem_DataTables_##funcname, \
&DemHandlers::Dem_Stop_##funcname, \
&DemHandlers::Dem_StringTables_##funcname \
}
typedef bool (*DemMsgFileReadFn)(DemoFileReader& demofile, void* data);
typedef bool (*DemMsgFileWriteFn)(DemoFileWriter& demofile, void* data);
typedef bool (*DemMsgJsonReadFn)(JsonRead& jsonbuf, void* data);
typedef bool (*DemMsgJsonWriteFn)(JsonWrite& jsonbuf, void* data);
bool DemHandlers::DemMsg_FileRead(uint32_t type, DemoFileReader& demofile, void* data)
{
static const DemMsgFileReadFn demHandlers[] = DECLARE_DEM_HANDLER_ARRAY(FileRead);
return demHandlers[type](demofile, data);
}
bool DemHandlers::DemMsg_FileWrite(uint32_t type, DemoFileWriter& demofile, void* data)
{
static const DemMsgFileWriteFn demHandlers[] = DECLARE_DEM_HANDLER_ARRAY(FileWrite);
return demHandlers[type](demofile, data);
}
bool DemHandlers::DemMsg_JsonRead(uint32_t type, JsonRead& jsonbuf, void* data)
{
static const DemMsgJsonReadFn demHandlers[] = DECLARE_DEM_HANDLER_ARRAY(JsonRead);
return demHandlers[type](jsonbuf, data);
}
bool DemHandlers::DemMsg_JsonWrite(uint32_t type, JsonWrite& jsonbuf, void* data)
{
static const DemMsgJsonWriteFn demHandlers[] = DECLARE_DEM_HANDLER_ARRAY(JsonWrite);
return demHandlers[type](jsonbuf, data);
}

View File

@ -0,0 +1,42 @@
#pragma once
#include <cstdint>
class DemoFileReader;
class DemoFileWriter;
class JsonRead;
class JsonWrite;
#define DECLARE_DEM_HANDLERS(msgname) \
namespace DemHandlers \
{ \
bool msgname##_FileRead_Internal(DemoFileReader& demofile, DemMsg::msgname* data); \
bool msgname##_FileWrite_Internal(DemoFileWriter& demofile, DemMsg::msgname* data); \
bool msgname##_JsonRead_Internal(JsonRead& jsonbuf, DemMsg::msgname* data); \
bool msgname##_JsonWrite_Internal(JsonWrite& jsonbuf, DemMsg::msgname* data); \
inline bool msgname##_FileRead(DemoFileReader& demofile, void* data) \
{ \
return msgname##_FileRead_Internal(demofile, reinterpret_cast<DemMsg::msgname*>(data)); \
} \
inline bool msgname##_FileWrite(DemoFileWriter& demofile, void* data) \
{ \
return msgname##_FileWrite_Internal(demofile, reinterpret_cast<DemMsg::msgname*>(data)); \
} \
inline bool msgname##_JsonRead(JsonRead& jsonbuf, void* data) \
{ \
return msgname##_JsonRead_Internal(jsonbuf, reinterpret_cast<DemMsg::msgname*>(data)); \
} \
inline bool msgname##_JsonWrite(JsonWrite& jsonbuf, void* data) \
{ \
return msgname##_JsonWrite_Internal(jsonbuf, reinterpret_cast<DemMsg::msgname*>(data)); \
} \
}
namespace DemHandlers
{
bool DemMsg_FileRead(uint32_t type, DemoFileReader& demofile, void* data);
bool DemMsg_FileWrite(uint32_t type, DemoFileWriter& demofile, void* data);
bool DemMsg_JsonRead(uint32_t type, JsonRead& jsonbuf, void* data);
bool DemMsg_JsonWrite(uint32_t type, JsonWrite& jsonbuf, void* data);
}

View File

@ -0,0 +1,14 @@
#pragma once
#include <cstdint>
#include "demofile/demotypes.h"
#include "dem_unknown.h"
#include "dem_packet.h"
#include "dem_synctick.h"
#include "dem_consolecmd.h"
#include "dem_usercmd.h"
#include "dem_datatables.h"
#include "dem_stop.h"
#include "dem_stringtables.h"