Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
NMTCreator.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "Serialization.h"
19
20// If included from within the NMT Creation process, perform a pass
21#ifdef CREATING_NMT
22
23#include NMT_CREATE_HEADER_NAME
24
25#undef START_NMTS
26#undef END_NMTS
27#undef START_NMT_CLASS
28#undef START_NMT_CLASS_DERIVED
29#undef NMT_FIELD_INT
30#undef NMT_FIELD
31#undef NMT_FIELD_SECRET
32#undef NMT_START_ARRAY
33#undef NMT_END_ARRAY
34#undef END_NMT_CLASS
35
36#else
37// If not within the creation process, and called with argument, perform the
38// creation process with the header specified
39#ifdef NMT_CREATE_HEADER_NAME
40
41#ifndef ARRAY_STRUCT_PREFIX
42#define ARRAY_STRUCT_PREFIX(_nm) S_##_nm
43#endif
44
45#define CREATING_NMT
46
47#ifndef NMT_CREATOR_IMPLEMENT
48
49#include <vector>
50
51/*************************************************************************/
52// Pass 1, class definition
53#define NMT_CREATOR_PASS_CLASSDEF
54#define START_NMTS()
55#define END_NMTS()
56
57#define START_NMT_CLASS(_nm, _tp) \
58 START_NMT_CLASS_DERIVED(CNetMessage, _nm, _tp)
59
60/**
61 * Start the definition of a network message type.
62 *
63 * @param _base The name of the base class of the message
64 * @param _nm The name of the class
65 * @param _tp The NetMessageType associated with the class. It is *not* safe to
66 * have several classes with the same value of _tp in the same executable
67 */
68#define START_NMT_CLASS_DERIVED(_base, _nm, _tp) \
69CNetMessage *Deserialize##_nm(const std::uint8_t *, size_t); \
70class _nm: public _base \
71{ \
72protected: \
73 _nm(NetMessageType type): _base(type) {}\
74 \
75 /* This one is for subclasses that want to use the base class' string */ \
76 /* converters to get SubMessage { <parent fields>, ... } */ \
77 CStr ToStringRaw() const;\
78public: \
79 _nm(): _base(_tp) {} \
80 virtual size_t GetSerializedLength() const; \
81 virtual std::uint8_t *Serialize(std::uint8_t *buffer) const; \
82 virtual const std::uint8_t *Deserialize(const std::uint8_t *pos, const std::uint8_t *end); \
83 virtual CStr ToString() const; \
84 inline operator CStr () const \
85 { return ToString(); }
86
87/**
88 * Add an integer field to the message type.
89 *
90 * @param _nm The name of the field
91 * @param _hosttp The local type of the field (the data type used in the field
92 * definition)
93 * @param _netsz The number of bytes that should be serialized. If the variable
94 * has a value larger than the maximum value of the specified network size,
95 * higher order bytes will be discarded.
96 */
97#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
98 _hosttp _nm;
99
100/**
101 * Add a generic field to the message type. The data type must be a class
102 * implementing the ISerializable interface
103 *
104 * @param _tp The local data type of the field
105 * @param _nm The name of the field
106 * @see ISerializable
107 */
108#define NMT_FIELD(_tp, _nm) \
109 _tp _nm;
110
111/**
112 * Likewise, but the string representation is hidden.
113 * NB: the data length is not hidden, so make sure to use fixed-length data if confidentiality is desirable.
114 */
115#define NMT_FIELD_SECRET(_tp, _nm) \
116 _tp _nm;
117
118#define NMT_START_ARRAY(_nm) \
119 struct ARRAY_STRUCT_PREFIX(_nm); \
120 std::vector <ARRAY_STRUCT_PREFIX(_nm)> _nm; \
121 struct ARRAY_STRUCT_PREFIX(_nm) {
122
123#define NMT_END_ARRAY() \
124 };
125
126#define END_NMT_CLASS() };
127
128#include "NMTCreator.h"
129#undef NMT_CREATOR_PASS_CLASSDEF
130
131#else // NMT_CREATOR_IMPLEMENT
132
133#include "StringConverters.h"
134
135#include <vector>
136
137/*************************************************************************/
138// Pass 2, GetSerializedLength
139#define NMT_CREATOR_PASS_GETLENGTH
140#define START_NMTS()
141#define END_NMTS()
142
143#define START_NMT_CLASS(_nm, _tp) \
144 START_NMT_CLASS_DERIVED(CNetMessage, _nm, _tp)
145#define START_NMT_CLASS_DERIVED(_base, _nm, _tp) \
146size_t _nm::GetSerializedLength() const \
147{ \
148 size_t ret=_base::GetSerializedLength(); \
149 [[maybe_unused]] const _nm *thiz=this;
150
151#define NMT_START_ARRAY(_nm) \
152 std::vector <ARRAY_STRUCT_PREFIX(_nm)>::const_iterator it=_nm.begin(); \
153 while (it != _nm.end()) \
154 { \
155 [[maybe_unused]] const ARRAY_STRUCT_PREFIX(_nm) *thiz=&*it;
156
157#define NMT_END_ARRAY() \
158 ++it; \
159 }
160
161#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
162 ret += _netsz;
163
164#define NMT_FIELD(_tp, _nm) \
165 ret += thiz->_nm.GetSerializedLength();
166
167#define NMT_FIELD_SECRET(_tp, _nm) \
168 ret += thiz->_nm.GetSerializedLength();
169
170#define END_NMT_CLASS() \
171 return ret; \
172};
173
174#include "NMTCreator.h"
175#undef NMT_CREATOR_PASS_GETLENGTH
176
177/*************************************************************************/
178// Pass 3, Serialize
179
180#define NMT_CREATOR_PASS_SERIALIZE
181
182#define START_NMTS()
183#define END_NMTS()
184
185#define START_NMT_CLASS(_nm, _tp) \
186 START_NMT_CLASS_DERIVED(CNetMessage, _nm, _tp)
187#define START_NMT_CLASS_DERIVED(_base, _nm, _tp) \
188std::uint8_t* _nm::Serialize(std::uint8_t *buffer) const \
189{ \
190 /*printf("In " #_nm "::Serialize()\n");*/ \
191 std::uint8_t *pos=_base::Serialize(buffer); \
192 [[maybe_unused]] const _nm *thiz=this;
193
194#define NMT_START_ARRAY(_nm) \
195 std::vector <ARRAY_STRUCT_PREFIX(_nm)>::const_iterator it=_nm.begin(); \
196 while (it != _nm.end()) \
197 { \
198 [[maybe_unused]] const ARRAY_STRUCT_PREFIX(_nm) *thiz=&*it;
199
200#define NMT_END_ARRAY() \
201 ++it; \
202 }
203
204#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
205 Serialize_int_##_netsz(pos, thiz->_nm); \
206
207#define NMT_FIELD(_tp, _nm) \
208 pos=thiz->_nm.Serialize(pos);
209
210#define NMT_FIELD_SECRET(_tp, _nm) \
211 pos=thiz->_nm.Serialize(pos);
212
213#define END_NMT_CLASS() \
214 return pos; \
215}
216
217#include "NMTCreator.h"
218
219#undef NMT_CREATOR_PASS_SERIALIZE
220
221/*************************************************************************/
222// Pass 4, Deserialize
223
224#define NMT_CREATOR_PASS_DESERIALIZE
225
226#define START_NMTS()
227#define END_NMTS()
228
229#define BAIL_DESERIALIZER return NULL
230
231#define START_NMT_CLASS(_nm, _tp) \
232 START_NMT_CLASS_DERIVED(CNetMessage, _nm, _tp)
233#define START_NMT_CLASS_DERIVED(_base, _nm, _tp) \
234const std::uint8_t* _nm::Deserialize(const std::uint8_t *pos, const std::uint8_t *end) \
235{ \
236 pos=_base::Deserialize(pos, end); \
237 if (pos == NULL) BAIL_DESERIALIZER;\
238 [[maybe_unused]] _nm *thiz=this; \
239 /*printf("In Deserialize" #_nm "\n"); */
240
241
242#define NMT_START_ARRAY(_nm) \
243 while (pos < end) \
244 { \
245 [[maybe_unused]] ARRAY_STRUCT_PREFIX(_nm) *thiz = \
246 &*_nm.insert(_nm.end(), ARRAY_STRUCT_PREFIX(_nm)());
247
248#define NMT_END_ARRAY() \
249 }
250
251#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
252 if (pos+_netsz > end) BAIL_DESERIALIZER; \
253 Deserialize_int_##_netsz(pos, thiz->_nm); \
254 /*printf("\t" #_nm " == 0x%x\n", thiz->_nm);*/
255
256#define NMT_FIELD(_tp, _nm) \
257 if ((pos=thiz->_nm.Deserialize(pos, end)) == NULL) BAIL_DESERIALIZER;
258
259#define NMT_FIELD_SECRET(_tp, _nm) \
260 if ((pos=thiz->_nm.Deserialize(pos, end)) == NULL) BAIL_DESERIALIZER;
261
262#define END_NMT_CLASS() \
263 return pos; \
264}
265
266#include "NMTCreator.h"
267
268#undef BAIL_DESERIALIZER
269
270#undef NMT_CREATOR_PASS_DESERIALIZE
271
272/*************************************************************************/
273// Pass 5, String Representation
274
275#define START_NMTS()
276#define END_NMTS()
277
278#define START_NMT_CLASS(_nm, _tp) \
279CStr _nm::ToString() const \
280{ \
281 CStr ret=#_nm " { "; \
282 return ret + ToStringRaw() + " }"; \
283} \
284CStr _nm::ToStringRaw() const \
285{ \
286 CStr ret; \
287 [[maybe_unused]] const _nm *thiz=this;
288
289#define START_NMT_CLASS_DERIVED(_base, _nm, _tp) \
290CStr _nm::ToString() const \
291{ \
292 CStr ret=#_nm " { "; \
293 return ret + ToStringRaw() + " }"; \
294} \
295CStr _nm::ToStringRaw() const \
296{ \
297 CStr ret=_base::ToStringRaw() + ", "; \
298 [[maybe_unused]] const _nm *thiz=this;
299
300#define NMT_START_ARRAY(_nm) \
301 ret+=#_nm ": { "; \
302 std::vector < ARRAY_STRUCT_PREFIX(_nm) >::const_iterator it=_nm.begin(); \
303 while (it != _nm.end()) \
304 { \
305 ret+=" { "; \
306 [[maybe_unused]] const ARRAY_STRUCT_PREFIX(_nm) *thiz=&*it;
307
308#define NMT_END_ARRAY() \
309 ++it; \
310 ret=ret.substr(0, ret.length()-2)+" }, "; \
311 } \
312 ret=ret.substr(0, ret.length()-2)+" }, ";
313
314#define NMT_FIELD_INT(_nm, _hosttp, _netsz) \
315 ret += #_nm ": "; \
316 ret += NetMessageStringConvert(thiz->_nm); \
317 ret += ", ";
318
319#define NMT_FIELD(_tp, _nm) \
320 ret += #_nm ": "; \
321 ret += NetMessageStringConvert(thiz->_nm); \
322 ret += ", ";
323
324#define NMT_FIELD_SECRET(_tp, _nm) \
325 ret += #_nm ": [secret], ";
326
327#define END_NMT_CLASS() \
328 return ret.substr(0, ret.length()-2); \
329}
330
331#include "NMTCreator.h"
332
333#endif // #ifdef NMT_CREATOR_IMPLEMENT
334
335/*************************************************************************/
336// Cleanup
337#undef NMT_CREATE_HEADER_NAME
338#undef NMT_CREATOR_IMPLEMENT
339#undef CREATING_NMT
340
341#endif // #ifdef NMT_CREATE_HEADER_NAME
342#endif // #ifndef CREATING_NMT