2003-11-25 03:11:50 +01:00
|
|
|
#include "Serialization.h"
|
2004-03-08 03:03:24 +01:00
|
|
|
#include <vector>
|
2003-11-25 03:11:50 +01:00
|
|
|
|
|
|
|
// If included from within the NMT Creation process, perform a pass
|
|
|
|
#ifdef CREATING_NMT
|
|
|
|
|
|
|
|
#include NMT_CREATE_HEADER_NAME
|
|
|
|
|
|
|
|
#undef START_NMTS
|
|
|
|
#undef END_NMTS
|
|
|
|
#undef START_NMT_CLASS
|
|
|
|
#undef NMT_FIELD_INT
|
2003-11-30 17:36:43 +01:00
|
|
|
#undef NMT_FIELD
|
2004-03-08 03:03:24 +01:00
|
|
|
#undef NMT_START_ARRAY
|
|
|
|
#undef NMT_END_ARRAY
|
2003-11-25 03:11:50 +01:00
|
|
|
#undef END_NMT_CLASS
|
|
|
|
|
|
|
|
#else
|
|
|
|
// If not within the creation process, and called with argument, perform the
|
|
|
|
// creation process with the header specified
|
|
|
|
#ifdef NMT_CREATE_HEADER_NAME
|
|
|
|
|
2004-03-08 03:03:24 +01:00
|
|
|
#ifndef ARRAY_STRUCT_PREFIX
|
|
|
|
#define ARRAY_STRUCT_PREFIX S_
|
|
|
|
#endif
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#define CREATING_NMT
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
// Pass 1, class definition
|
2004-01-21 03:08:09 +01:00
|
|
|
#define NMT_CREATOR_PASS_CLASSDEF
|
2003-11-25 03:11:50 +01:00
|
|
|
#define START_NMTS()
|
|
|
|
#define END_NMTS()
|
|
|
|
|
2003-11-30 17:36:43 +01:00
|
|
|
/**
|
|
|
|
* Start the definition of a network message type.
|
|
|
|
*
|
|
|
|
* @param _nm The name of the class
|
2004-03-08 03:03:24 +01:00
|
|
|
* @param _tp The NetMessageType associated with the class. It is *not* safe to
|
2003-11-30 17:36:43 +01:00
|
|
|
* have several classes with the same value of _tp in the same executable
|
|
|
|
*/
|
2003-11-25 03:11:50 +01:00
|
|
|
#define START_NMT_CLASS(_nm, _tp) \
|
2003-11-30 17:36:43 +01:00
|
|
|
CNetMessage *Deserialize##_nm(const u8 *, uint); \
|
2003-11-25 03:11:50 +01:00
|
|
|
struct _nm: public CNetMessage \
|
|
|
|
{ \
|
|
|
|
_nm(): CNetMessage(_tp) {} \
|
|
|
|
virtual uint GetSerializedLength() const; \
|
2004-03-08 03:03:24 +01:00
|
|
|
virtual u8 *Serialize(u8 *buffer) const; \
|
|
|
|
virtual const u8 *Deserialize(const u8 *pos, const u8 *end); \
|
|
|
|
virtual CStr GetString() const; \
|
2004-08-16 17:19:17 +02:00
|
|
|
virtual CNetMessage *Copy() const \
|
|
|
|
{ \
|
|
|
|
return new _nm(*this); \
|
|
|
|
} \
|
2004-03-08 03:03:24 +01:00
|
|
|
inline operator CStr () const \
|
|
|
|
{ return GetString(); }
|
2003-11-25 03:11:50 +01:00
|
|
|
|
2003-11-30 17:36:43 +01:00
|
|
|
/**
|
|
|
|
* Add an integer field to the message type.
|
|
|
|
*
|
|
|
|
* @param _nm The name of the field
|
|
|
|
* @param _hosttp The local type of the field (the data type used in the field
|
|
|
|
* definition)
|
|
|
|
* @param _netsz The number of bytes that should be serialized. If the variable
|
|
|
|
* has a value larger than the maximum value of the specified network size,
|
|
|
|
* higher order bytes will be discarded.
|
|
|
|
*/
|
2003-11-25 03:11:50 +01:00
|
|
|
#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
|
|
|
|
_hosttp _nm;
|
|
|
|
|
2003-11-30 17:36:43 +01:00
|
|
|
/**
|
|
|
|
* Add a generic field to the message type. The data type must be a class
|
|
|
|
* implementing the ISerializable interface
|
|
|
|
*
|
|
|
|
* @param _tp The local data type of the field
|
|
|
|
* @param _nm The name of the field
|
|
|
|
* @see ISerializable
|
|
|
|
*/
|
|
|
|
#define NMT_FIELD(_tp, _nm) \
|
|
|
|
_tp _nm;
|
|
|
|
|
2004-03-08 03:03:24 +01:00
|
|
|
#define NMT_START_ARRAY(_nm) \
|
|
|
|
struct ARRAY_STRUCT_PREFIX ## _nm; \
|
|
|
|
std::vector <ARRAY_STRUCT_PREFIX ## _nm> _nm; \
|
|
|
|
struct ARRAY_STRUCT_PREFIX ## _nm {
|
|
|
|
|
|
|
|
#define NMT_END_ARRAY() \
|
|
|
|
};
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#define END_NMT_CLASS() };
|
|
|
|
|
|
|
|
#include "NMTCreator.h"
|
2004-01-21 03:08:09 +01:00
|
|
|
#undef NMT_CREATOR_PASS_CLASSDEF
|
2003-11-25 03:11:50 +01:00
|
|
|
|
|
|
|
#ifdef NMT_CREATOR_IMPLEMENT
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
// Pass 2, GetSerializedLength
|
2004-01-21 03:08:09 +01:00
|
|
|
#define NMT_CREATOR_PASS_GETLENGTH
|
2003-11-25 03:11:50 +01:00
|
|
|
#define START_NMTS()
|
|
|
|
#define END_NMTS()
|
|
|
|
|
|
|
|
#define START_NMT_CLASS(_nm, _tp) \
|
|
|
|
uint _nm::GetSerializedLength() const \
|
|
|
|
{ \
|
2004-03-08 03:03:24 +01:00
|
|
|
uint ret=0; \
|
|
|
|
const _nm *thiz=this;
|
|
|
|
|
|
|
|
#define NMT_START_ARRAY(_nm) \
|
2004-07-04 17:41:17 +02:00
|
|
|
std::vector <ARRAY_STRUCT_PREFIX##_nm>::const_iterator it=_nm.begin(); \
|
2004-03-08 03:03:24 +01:00
|
|
|
while (it != _nm.end()) \
|
|
|
|
{ \
|
|
|
|
const ARRAY_STRUCT_PREFIX##_nm *thiz=&*it;
|
|
|
|
|
|
|
|
#define NMT_END_ARRAY() \
|
|
|
|
++it; \
|
|
|
|
}
|
2003-11-25 03:11:50 +01:00
|
|
|
|
|
|
|
#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
|
|
|
|
ret += _netsz;
|
|
|
|
|
2003-11-30 17:36:43 +01:00
|
|
|
#define NMT_FIELD(_tp, _nm) \
|
2004-03-08 03:03:24 +01:00
|
|
|
ret += thiz->_nm.GetSerializedLength();
|
2003-11-30 17:36:43 +01:00
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#define END_NMT_CLASS() \
|
|
|
|
return ret; \
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "NMTCreator.h"
|
2004-01-21 03:08:09 +01:00
|
|
|
#undef NMT_CREATOR_PASS_GETLENGTH
|
2003-11-25 03:11:50 +01:00
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
// Pass 3, Serialize
|
2004-01-21 03:08:09 +01:00
|
|
|
|
|
|
|
#define NMT_CREATOR_PASS_SERIALIZE
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#define START_NMTS()
|
|
|
|
#define END_NMTS()
|
|
|
|
|
|
|
|
#define START_NMT_CLASS(_nm, _tp) \
|
2004-03-08 03:03:24 +01:00
|
|
|
u8 *_nm::Serialize(u8 *buffer) const \
|
2003-11-25 03:11:50 +01:00
|
|
|
{ \
|
2003-11-30 17:36:43 +01:00
|
|
|
/*printf("In " #_nm "::Serialize()\n");*/ \
|
2003-11-25 03:11:50 +01:00
|
|
|
u8 *pos=buffer; \
|
2004-03-08 03:03:24 +01:00
|
|
|
const _nm *thiz=this;
|
|
|
|
|
|
|
|
#define NMT_START_ARRAY(_nm) \
|
2004-07-04 17:41:17 +02:00
|
|
|
std::vector <ARRAY_STRUCT_PREFIX##_nm>::const_iterator it=_nm.begin(); \
|
2004-03-08 03:03:24 +01:00
|
|
|
while (it != _nm.end()) \
|
|
|
|
{ \
|
|
|
|
const ARRAY_STRUCT_PREFIX##_nm *thiz=&*it;
|
|
|
|
|
|
|
|
#define NMT_END_ARRAY() \
|
|
|
|
++it; \
|
|
|
|
}
|
2003-11-25 03:11:50 +01:00
|
|
|
|
|
|
|
#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
|
2004-03-08 03:03:24 +01:00
|
|
|
Serialize_int_##_netsz(pos, thiz->_nm); \
|
2003-11-30 17:36:43 +01:00
|
|
|
|
|
|
|
#define NMT_FIELD(_tp, _nm) \
|
2004-03-08 03:03:24 +01:00
|
|
|
pos=thiz->_nm.Serialize(pos);
|
2003-11-25 03:11:50 +01:00
|
|
|
|
2004-03-08 03:03:24 +01:00
|
|
|
#define END_NMT_CLASS() \
|
|
|
|
return pos; \
|
|
|
|
}
|
2003-11-25 03:11:50 +01:00
|
|
|
|
|
|
|
#include "NMTCreator.h"
|
|
|
|
|
2004-01-21 03:08:09 +01:00
|
|
|
#undef NMT_CREATOR_PASS_SERIALIZE
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
// Pass 4, Deserialize
|
2004-01-21 03:08:09 +01:00
|
|
|
|
|
|
|
#define NMT_CREATOR_PASS_DESERIALIZE
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#define START_NMTS()
|
|
|
|
#define END_NMTS()
|
|
|
|
|
2004-03-08 03:03:24 +01:00
|
|
|
#define BAIL_DESERIALIZER return NULL
|
2003-11-30 17:36:43 +01:00
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#define START_NMT_CLASS(_nm, _tp) \
|
2003-11-30 17:36:43 +01:00
|
|
|
CNetMessage *Deserialize##_nm(const u8 *buffer, uint length) \
|
2003-11-25 03:11:50 +01:00
|
|
|
{ \
|
|
|
|
_nm *ret=new _nm(); \
|
2004-03-08 03:03:24 +01:00
|
|
|
if (ret->Deserialize(buffer, buffer+length)) \
|
|
|
|
return ret; \
|
|
|
|
else \
|
|
|
|
{ delete ret; return NULL; } \
|
|
|
|
} \
|
|
|
|
const u8 *_nm::Deserialize(const u8 *pos, const u8 *end) \
|
|
|
|
{ \
|
|
|
|
_nm *thiz=this; \
|
|
|
|
/*printf("In Deserialize" #_nm "\n"); */
|
|
|
|
|
|
|
|
#define NMT_START_ARRAY(_nm) \
|
|
|
|
while (pos < end) \
|
|
|
|
{ \
|
|
|
|
ARRAY_STRUCT_PREFIX##_nm *thiz=&*_nm.insert(_nm.end(), ARRAY_STRUCT_PREFIX##_nm());
|
|
|
|
|
|
|
|
#define NMT_END_ARRAY() \
|
|
|
|
}
|
2003-11-25 03:11:50 +01:00
|
|
|
|
|
|
|
#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
|
2004-03-08 03:03:24 +01:00
|
|
|
if (pos+_netsz > end) BAIL_DESERIALIZER; \
|
|
|
|
Deserialize_int_##_netsz(pos, thiz->_nm); \
|
|
|
|
/*printf("\t" #_nm " == 0x%x\n", thiz->_nm);*/
|
2003-11-30 17:36:43 +01:00
|
|
|
|
|
|
|
#define NMT_FIELD(_tp, _nm) \
|
2004-03-08 03:03:24 +01:00
|
|
|
if ((pos=thiz->_nm.Deserialize(pos, end)) == NULL) BAIL_DESERIALIZER;
|
2003-11-25 03:11:50 +01:00
|
|
|
|
|
|
|
#define END_NMT_CLASS() \
|
2004-03-08 03:03:24 +01:00
|
|
|
return pos; \
|
2003-11-25 03:11:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "NMTCreator.h"
|
|
|
|
|
2003-11-30 17:36:43 +01:00
|
|
|
#undef BAIL_DESERIALIZER
|
|
|
|
|
2004-01-21 03:08:09 +01:00
|
|
|
#undef NMT_CREATOR_PASS_DESERIALIZE
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
// Pass 5, Deserializer Registration
|
2004-01-21 03:08:09 +01:00
|
|
|
|
|
|
|
#define NMT_CREATOR_PASS_REGISTRATION
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#define START_NMTS() SNetMessageDeserializerRegistration g_DeserializerRegistrations[] = {
|
|
|
|
#define END_NMTS() { NMT_NONE, NULL } };
|
|
|
|
|
|
|
|
#define START_NMT_CLASS(_nm, _tp) \
|
|
|
|
{ _tp, Deserialize##_nm },
|
|
|
|
|
2004-03-08 03:03:24 +01:00
|
|
|
#define NMT_START_ARRAY(_nm)
|
|
|
|
|
|
|
|
#define NMT_END_ARRAY()
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#define NMT_FIELD_INT(_nm, _hosttp, _netsz)
|
|
|
|
|
2003-11-30 17:36:43 +01:00
|
|
|
#define NMT_FIELD(_tp, _nm)
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#define END_NMT_CLASS()
|
|
|
|
|
|
|
|
#include "NMTCreator.h"
|
|
|
|
|
2004-01-21 03:08:09 +01:00
|
|
|
#undef NMT_CREATOR_PASS_REGISTRATION
|
|
|
|
|
2004-03-08 03:03:24 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
// Pass 6, String Representation
|
|
|
|
#define START_NMTS()
|
|
|
|
#define END_NMTS()
|
|
|
|
|
|
|
|
#define START_NMT_CLASS(_nm, _tp) \
|
|
|
|
CStr _nm::GetString() const \
|
|
|
|
{ \
|
|
|
|
CStr ret=#_nm _T(" { "); \
|
|
|
|
const _nm *thiz=this;
|
|
|
|
|
|
|
|
#define NMT_START_ARRAY(_nm) \
|
|
|
|
ret+=#_nm _T(": { "); \
|
|
|
|
std::vector < ARRAY_STRUCT_PREFIX ## _nm >::const_iterator it=_nm.begin(); \
|
|
|
|
while (it != _nm.end()) \
|
|
|
|
{ \
|
|
|
|
ret+=_T(" { "); \
|
|
|
|
const ARRAY_STRUCT_PREFIX##_nm *thiz=&*it;
|
|
|
|
|
|
|
|
#define NMT_END_ARRAY() \
|
|
|
|
++it; \
|
|
|
|
ret=ret.GetSubstring(0, ret.Length()-2)+_T(" }, "); \
|
|
|
|
} \
|
|
|
|
ret=ret.GetSubstring(0, ret.Length()-2)+_T(" }, ");
|
|
|
|
|
|
|
|
#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
|
|
|
|
ret += #_nm _T(": "); \
|
|
|
|
ret += CStr(thiz->_nm); \
|
|
|
|
ret += _T(", ");
|
|
|
|
|
|
|
|
#define NMT_FIELD(_tp, _nm) \
|
|
|
|
ret += #_nm _T(": "); \
|
|
|
|
ret += thiz->_nm; \
|
|
|
|
ret += _T(", ");
|
|
|
|
|
|
|
|
#define END_NMT_CLASS() \
|
|
|
|
return ret.GetSubstring(0, ret.Length()-2)+_T(" }"); \
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "NMTCreator.h"
|
|
|
|
|
2003-11-25 03:11:50 +01:00
|
|
|
#endif // #ifdef NMT_CREATOR_IMPLEMENT
|
|
|
|
|
|
|
|
/*************************************************************************/
|
|
|
|
// Cleanup
|
|
|
|
#undef NMT_CREATE_HEADER_NAME
|
|
|
|
#undef NMT_CREATOR_IMPLEMENT
|
|
|
|
#undef CREATING_NMT
|
|
|
|
|
|
|
|
#endif // #ifdef NMT_CREATE_HEADER_NAME
|
|
|
|
#endif // #ifndef CREATING_NMT
|