Xonotic
net.qh
Go to the documentation of this file.
1 #pragma once
2 
3 #include "registry.qh"
4 #include "sort.qh"
5 #include "yenc.qh"
6 
7 // netcode mismatch and not sure what caused it? developer_csqcentities 1
8 
9 .string netname;
10 .int m_id;
11 .bool(entity this, entity sender, bool isNew) m_read;
12 #define NET_HANDLE(id, param) bool Net_Handle_##id(entity this, entity sender, param)
13 
14 #define NET_GUARD(id) \
15  bool Net_Handle_##id##_guard(entity this, entity sender, bool isNew) { \
16  bool valid = false; \
17  serialize_marker(to, valid); \
18  if (!valid) LOG_FATALF("Last message not fully parsed: %s", _net_prevmsgstr); \
19  _net_prevmsgstr = #id; \
20  return Net_Handle_##id(this, sender, isNew); \
21  }
22 
23 #ifdef CSQC
24 string _net_prevmsgstr;
25  #define REGISTER_NET_TEMP(id) \
26  NET_HANDLE(id, bool); \
27  NET_GUARD(id); \
28  REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) { \
29  this.netname = #id; \
30  this.m_read = Net_Handle_##id##_guard; \
31  }
32 #else
33  #define REGISTER_NET_TEMP(id) \
34  const bool NET_##id##_istemp = true; \
35  REGISTER(TempEntities, NET, id, m_id, new_pure(net_temp_packet)) \
36  { \
37  this.netname = #id; \
38  }
39 #endif
40 #define REGISTER_NET_S2C(id) REGISTER_NET_TEMP(id)
41 
42 REGISTRY(TempEntities, BITS(8) - 80)
43 REGISTER_REGISTRY(TempEntities)
44 REGISTRY_SORT(TempEntities)
45 REGISTRY_CHECK(TempEntities)
46 
47 REGISTRY_DEFINE_GET(TempEntities, NULL)
48 STATIC_INIT(TempEntities_renumber) { FOREACH(TempEntities, true, it.m_id = 80 + i); }
49 
50 
51 
52 #ifdef CSQC
53  #define REGISTER_NET_LINKED(id) \
54  ACCUMULATE NET_HANDLE(id, bool isnew) \
55  { \
56  this = __self; \
57  this.sourceLoc = __FILE__":"STR(__LINE__); \
58  if (!this) isnew = true; \
59  } \
60  NET_GUARD(id); \
61  REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
62  { \
63  this.netname = #id; \
64  this.m_read = Net_Handle_##id##_guard; \
65  }
66 #else
67  #define REGISTER_NET_LINKED(id) \
68  const bool NET_##id##_istemp = false; \
69  REGISTER(LinkedEntities, NET, id, m_id, new_pure(net_linked_packet)) \
70  { \
71  this.netname = #id; \
72  }
73 #endif
74 
75 REGISTRY(LinkedEntities, BITS(8) - 1)
76 REGISTER_REGISTRY(LinkedEntities)
77 REGISTRY_SORT(LinkedEntities)
78 REGISTRY_CHECK(LinkedEntities)
79 
80 REGISTRY_DEFINE_GET(LinkedEntities, NULL)
81 STATIC_INIT(LinkedEntities_renumber) { FOREACH(LinkedEntities, true, it.m_id = 1 + i); }
82 
83 
84 
85 #ifdef SVQC
86  #define REGISTER_NET_C2S(id) \
87  NET_HANDLE(id, bool); \
88  REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
89  { \
90  this.netname = #id; \
91  this.m_read = Net_Handle_##id; \
92  }
93 #else
94  #define REGISTER_NET_C2S(id) \
95  const bool NET_##id##_istemp = true; \
96  REGISTER(C2S_Protocol, NET, id, m_id, new_pure(net_c2s_packet)) \
97  { \
98  this.netname = #id; \
99  }
100 #endif
101 
102 REGISTRY(C2S_Protocol, BITS(8) - 1)
103 REGISTER_REGISTRY(C2S_Protocol)
104 REGISTRY_SORT(C2S_Protocol)
105 REGISTRY_CHECK(C2S_Protocol)
106 
107 REGISTRY_DEFINE_GET(C2S_Protocol, NULL)
108 STATIC_INIT(C2S_Protocol_renumber) { FOREACH(C2S_Protocol, true, it.m_id = i); }
109 
110 #ifdef SVQC
111  const int MSG_ENTITY = 5;
112 
113  .int SendFlags;
114 
115  IntrusiveList g_uncustomizables;
116  STATIC_INIT(g_uncustomizables) { g_uncustomizables = IL_NEW(); }
117 
118  void Net_LinkEntity(entity e, bool docull, float dt, bool(entity this, entity to, int sendflags) sendfunc)
119  {
120  if (e.classname == "")
121  {
122  LOG_WARN("Net_LinkEntity called on an entity without a classname, assigning default");
123  e.classname = "net_linked";
124  }
125 
126  if (e.model == "" || e.modelindex == 0)
127  {
128  vector mi = e.mins;
129  vector ma = e.maxs;
130  _setmodel(e, "null");
131  setsize(e, mi, ma);
132  }
133 
134  setSendEntity(e, sendfunc);
135  e.SendFlags = 0xFFFFFF;
136 
137  if (!docull) e.effects |= EF_NODEPTHTEST;
138 
139  if (dt)
140  {
141  e.nextthink = time + dt;
142  setthink(e, SUB_Remove);
143  }
144  }
145 
146  void Net_UnlinkEntity(entity e)
147  {
149  }
150 
151  .void(entity this) uncustomizeentityforclient;
152  .float uncustomizeentityforclient_set;
153 
154  void SetCustomizer(entity e, bool(entity this, entity client) customizer, void(entity this) uncustomizer)
155  {
156  setcefc(e, customizer);
157  e.uncustomizeentityforclient = uncustomizer;
158  e.uncustomizeentityforclient_set = !!uncustomizer;
159  if(uncustomizer)
160  IL_PUSH(g_uncustomizables, e);
161  }
162 
163  void UncustomizeEntitiesRun()
164  {
165  IL_EACH(g_uncustomizables, it.uncustomizeentityforclient_set, it.uncustomizeentityforclient(it));
166  }
167 
168  STRING_ITERATOR(g_buf, string_null, 0);
169 
170  int ReadByte();
171 
172  void Net_ClientCommand(entity sender, string command)
173  {
174  // command matches `c2s "(.+)"`
175  string buf = substring(command, argv_start_index(1) + 1, -2);
176  if (buf == "") return;
177  STRING_ITERATOR_SET(g_buf, buf, 0);
178  for (int C2S; (C2S = ReadByte()) >= 0; )
179  {
180  entity reader = REGISTRY_GET(C2S_Protocol, C2S);
181  if (reader && reader.m_read && reader.m_read(NULL, sender, true)) continue;
182  LOG_SEVEREF("Net_ClientCommand() with malformed C2S=%d", C2S);
183  return;
184  }
185  g_buf_i--;
186  int expected = strlen(buf);
187  if (g_buf_i > expected) LOG_WARNF("Underflow: %d", g_buf_i - expected);
188  if (g_buf_i < expected) LOG_WARNF("Overrflow: %d", expected - g_buf_i);
189  }
190 
191 #endif
192 
193 #ifdef CSQC
194  const int MSG_C2S = 0;
195 
196  #define Net_Accept(classname) \
197  MACRO_BEGIN \
198  if (!this) this = new(classname); \
199  MACRO_END
200  #define Net_Reject() \
201  MACRO_BEGIN \
202  if (this) delete(this); \
203  MACRO_END
204 
205  string g_buf;
206 
207  void Net_Flush()
208  {
209  if (g_buf == "") return;
210  localcmd("\ncmd c2s \"", strreplace("$", "$$", g_buf), "\"\n");
211  strfree(g_buf);
212  }
213 #endif
214 
215 #if defined(CSQC)
216  #define WriteHeader(to, id) \
217  WriteByte(to, NET_##id.m_id)
218 #elif defined(SVQC)
219  #define WriteHeader(to, id) \
220  MACRO_BEGIN \
221  if (NET_##id##_istemp) WriteByte(to, SVC_TEMPENTITY); \
222  WriteByte(to, NET_##id.m_id); \
223  bool _net_valid = false; serialize_marker(to, _net_valid); \
224  MACRO_END
225 #endif
226 
227 // serialization: new style
228 
229 USING(Stream, int);
230 #if defined(SVQC)
231  #define stream_reading(stream) false
232  #define stream_writing(stream) true
233 #elif defined(CSQC)
234  #define stream_reading(stream) true
235  #define stream_writing(stream) false
236 #endif
237 
238 #define serialize(T, stream, ...) \
239 MACRO_BEGIN \
240  noref Stream _stream = stream; \
241  serialize_##T(_stream, __VA_ARGS__); \
242 MACRO_END
243 
244 #if defined(SVQC)
245  #define serialize_byte(stream, this) \
246  MACRO_BEGIN \
247  WriteByte(stream, this); \
248  MACRO_END
249 #elif defined(CSQC)
250  #define serialize_byte(stream, this) \
251  MACRO_BEGIN \
252  this = ReadByte(); \
253  MACRO_END
254 #endif
255 
256 #if defined(SVQC)
257  #define serialize_float(stream, this) \
258  MACRO_BEGIN \
259  WriteCoord(stream, this); \
260  MACRO_END
261 #elif defined(CSQC)
262  #define serialize_float(stream, this) \
263  MACRO_BEGIN \
264  this = ReadCoord(); \
265  MACRO_END
266 #endif
267 
268 #define serialize_vector(stream, this) \
269 MACRO_BEGIN \
270  vector _v = this; \
271  serialize_float(stream, _v.x); \
272  serialize_float(stream, _v.y); \
273  serialize_float(stream, _v.z); \
274  this = _v; \
275 MACRO_END
276 
277 #define serialize_marker(stream, this) \
278 MACRO_BEGIN \
279  if (NDEBUG) { \
280  this = true; \
281  } else { \
282  int _de = 0xDE, _ad = 0xAD, _be = 0xBE, _ef = 0xEF; \
283  serialize_byte(stream, _de); \
284  serialize_byte(stream, _ad); \
285  serialize_byte(stream, _be); \
286  serialize_byte(stream, _ef); \
287  this = (_de == 0xDE && _ad == 0xAD && _be == 0xBE && _ef == 0xEF); \
288  } \
289 MACRO_END
290 
291 // serialization: old
292 
293 #define ReadRegistered(r) REGISTRY_GET(r, Read_byte())
294 #define WriteRegistered(r, to, it) Write_byte(to, it.m_id)
295 
296 #define Read_byte() ReadByte()
297 #define Write_byte(to, f) WriteByte(to, f)
298 
299 #if defined(CSQC)
300  int ReadByte();
301  void WriteByte(int to, int b)
302  {
303  assert(to == MSG_C2S);
304  string s = string_null;
305  yenc_single(b, s);
306  string tmp = strcat(g_buf, s);
307  strcpy(g_buf, tmp);
308  }
309  void WriteShort(int to, int b)
310  {
311  WriteByte(to, (b >> 8) & 0xFF);
312  WriteByte(to, b & 0xFF);
313  }
314 #elif defined(SVQC)
315  int ReadByte()
316  {
317  int ret = -1;
318  ydec_single(g_buf, ret);
319  return ret;
320  }
321  int ReadShort()
322  {
323  return (ReadByte() << 8) | (ReadByte());
324  }
325  void WriteByte(int to, int b);
326 #endif
327 
328 #define Read_int() ReadInt24_t()
329 #define Write_int(to, f) WriteInt24_t(to, f)
330 
331 #define Read_float() ReadFloat()
332 #define Write_float(to, f) WriteFloat(to, f)
333 
334 #define Read_string() ReadString()
335 #define Write_string(to, f) WriteString(to, f)
336 
337 #ifdef GAMEQC
338  const float APPROXPASTTIME_ACCURACY_REQUIREMENT = 0.05;
339  #define APPROXPASTTIME_MAX (16384 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
340  #define APPROXPASTTIME_RANGE (64 * APPROXPASTTIME_ACCURACY_REQUIREMENT)
341 
342  #ifdef CSQC
343  float servertime;
344  entity ReadCSQCEntity()
345  {
346  int f = ReadShort();
347  if (f == 0) return NULL;
348  return findfloat(NULL, entnum, f);
349  }
350  int ReadInt24_t()
351  {
352  int v = ReadShort() << 8; // note: this is signed
353  v += ReadByte(); // note: this is unsigned
354  return v;
355  }
356  #define ReadInt48_t() vec2(ReadInt24_t(), ReadInt24_t())
357  #define ReadInt72_t() vec3(ReadInt24_t(), ReadInt24_t(), ReadInt24_t())
358 
359  noref int _ReadSByte;
360  #define ReadSByte() (_ReadSByte = ReadByte(), (_ReadSByte & BIT(7) ? -128 : 0) + (_ReadSByte & BITS(7)))
361  #define ReadFloat() ReadCoord()
362  #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
363  #define ReadVector2D() vec2(ReadFloat(), ReadFloat())
364  #define ReadAngleVector() vec3(ReadAngle(), ReadAngle(), ReadAngle())
365  #define ReadAngleVector2D() vec2(ReadAngle(), ReadAngle())
366 
367  int Readbits(int num)
368  {
369  if (num > 16) return ReadInt24_t();
370  if (num > 8) return ReadShort();
371  return ReadByte();
372  }
373 
374  float ReadApproxPastTime()
375  {
376  float dt = ReadByte();
377 
378  // map from range...PPROXPASTTIME_MAX / 256
379  dt = (APPROXPASTTIME_MAX / 256) * (dt / (256 - dt));
380 
381  return servertime - dt;
382  }
383 
384  #else
385  void WriteInt24_t(float dst, float val)
386  {
387  float v;
388  WriteShort(dst, (v = floor(val >> 8)));
389  WriteByte(dst, val - (v << 8)); // 0..255
390  }
391  void WriteInt48_t(float dst, vector val)
392  {
393  WriteInt24_t(dst, val.x);
394  WriteInt24_t(dst, val.y);
395  }
396  void WriteInt72_t(float dst, vector val)
397  {
398  WriteInt24_t(dst, val.x);
399  WriteInt24_t(dst, val.y);
400  WriteInt24_t(dst, val.z);
401  }
402 
403  #define WriteFloat(to, f) WriteCoord(to, f)
404  #define WriteVector(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); MACRO_END
405  #define WriteVector2D(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); MACRO_END
406  #define WriteAngleVector(to, v) MACRO_BEGIN WriteAngle(to, v.x); WriteAngle(to, v.y); WriteAngle(to, v.z); MACRO_END
407  #define WriteAngleVector2D(to, v) MACRO_BEGIN WriteAngle(to, v.x); WriteAngle(to, v.y); MACRO_END
408 
409  void Writebits(float dst, float val, int num)
410  {
411  if (num > 16) { WriteInt24_t(dst, val); return; }
412  if (num > 8) { WriteShort(dst, val); return; }
413  WriteByte(dst, val);
414  }
415 
416  // this will use the value:
417  // 128
418  // accuracy near zero is APPROXPASTTIME_MAX/(256*255)
419  // accuracy at x is 1/derivative, i.e.
420  // APPROXPASTTIME_MAX * (1 + 256 * (dt / APPROXPASTTIME_MAX))^2 / 65536
421  void WriteApproxPastTime(float dst, float t)
422  {
423  float dt = time - t;
424 
425  // warning: this is approximate; do not resend when you don't have to!
426  // be careful with sendflags here!
427  // we want: 0 -> 0.05, 1 -> 0.1, ..., 255 -> 12.75
428 
429  // map to range...
430  dt = 256 * (dt / ((APPROXPASTTIME_MAX / 256) + dt));
431 
432  // round...
433  dt = rint(bound(0, dt, 255));
434 
435  WriteByte(dst, dt);
436  }
437 
438  // allow writing to also pass through to spectators (like so spectators see the same centerprints as players for example)
439  #define WRITESPECTATABLE_MSG_ONE(to, statement) MACRO_BEGIN \
440  entity prev = msg_entity; \
441  entity dst = to; \
442  FOREACH_CLIENT(IS_REAL_CLIENT(it), { \
443  if (it == dst || (it.classname == STR_SPECTATOR && it.enemy == dst)) \
444  { \
445  msg_entity = it; \
446  LAMBDA(statement); \
447  } \
448  }); \
449  msg_entity = prev; \
450  MACRO_END
451  #endif
452 #endif
string netname
Definition: net.qh:9
#define assert(expr,...)
Definition: log.qh:8
#define IL_EACH(this, cond, body)
#define LOG_WARN(...)
Definition: log.qh:66
string string_null
Definition: nil.qh:9
STATIC_INIT(TempEntities_renumber)
Definition: net.qh:48
#define yenc_single(c, ret)
Definition: yenc.qh:5
#define IL_NEW()
int Stream
Definition: net.qh:229
entity() spawn
#define REGISTRY_GET(id, i)
Definition: registry.qh:43
#define REGISTRY_CHECK(id)
Definition: registry.qh:175
entity to
Definition: self.qh:96
limitations: NULL cannot be present elements can only be present once a maximum of IL_MAX lists can e...
const float EF_NODEPTHTEST
Definition: csprogsdefs.qc:304
#define ydec_single(stringiter, ret)
Definition: yenc.qh:31
#define REGISTRY_SORT(...)
Definition: registry.qh:128
#define strcpy(this, s)
Definition: string.qh:49
#define REGISTER_REGISTRY(id)
Definition: registry.qh:212
#define LOG_WARNF(...)
Definition: log.qh:67
#define REGISTRY_DEFINE_GET(id, null)
Definition: registry.qh:40
#define LOG_SEVEREF(...)
Definition: log.qh:63
ERASEABLE entity IL_PUSH(IntrusiveList this, entity it)
Push to tail.
STRING_ITERATOR(_json, string_null, 0)
#define argv_start_index
Definition: dpextensions.qh:27
spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 spree_cen s1 f1 s1 strcat(_("Level %s: "), "^BG%s\3\, _("^BGPress ^F2%s^BG to enter the game"))
#define REGISTRY(id, max)
Declare a new registry.
Definition: registry.qh:26
#define setSendEntity(e, f)
Definition: self.qh:97
#define NULL
Definition: post.qh:17
vector(float skel, float bonenum) _skel_get_boneabs_hidden
int m_id
Definition: net.qh:10
vector v
Definition: ent_cs.qc:116
#define STRING_ITERATOR_SET(this, s, i)
Definition: iter.qh:49
entity int sendflags
Definition: self.qh:96
#define setthink(e, f)
#define strfree(this)
Definition: string.qh:56
float servertime
Definition: main.qh:36
#define USING(name, T)
Definition: _all.inc:72
float time
Definition: csprogsdefs.qc:16
#define FOREACH(list, cond, body)
Definition: iter.qh:19
float entnum
Definition: csprogsdefs.qc:94
#define BITS(n)
Definition: bits.qh:9
var void func_null()