]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/replicate.qh
Add strfree to reduce explicit use of strunzone/string_null
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / replicate.qh
1 #pragma once
2
3 #ifdef GAMEQC
4
5     /**
6      * Replicate a client cvar into a server field
7      *
8      * @param fld   The field to replicate into
9      * @param type  The field type
10      * @param cvar  The cvar name
11      */
12         #define REPLICATE(...) EVAL_REPLICATE(OVERLOAD(REPLICATE, __VA_ARGS__))
13         #define EVAL_REPLICATE(...) __VA_ARGS__
14
15         [[accumulate]] void ReplicateVars(entity this, entity store, string thisname, int i) {}
16
17         #define REPLICATE_3(fld, type, var) REPLICATE_4(fld, type, var, )
18         #define REPLICATE_4(fld, type, var, func) REPLICATE_##type(fld, var, func)
19         #define REPLICATE_string(fld, var, func) \
20                 REPLICATE_7(fld, string, var, , \
21         { strcpy(field, it); }, \
22         { strfree(field); }, \
23         { \
24                 /* also initialize to the default value of func when requesting cvars */ \
25                 string s = func(field); \
26                 if (s != field) \
27                 { \
28                     strcpy(field, s); \
29                 } \
30         })
31         #define REPLICATE_float(fld, var, func) REPLICATE_7(fld, float, var, func,  { field = stof(it); },          , )
32         #define REPLICATE_bool(fld, var, func) REPLICATE_7(fld, bool, var, func,   { field = boolean(stoi(it)); }, , )
33         #define REPLICATE_int(fld, var, func) REPLICATE_7(fld, int, var, func,    { field = stoi(it); },          , )
34
35         #if defined(SVQC)
36                 #define REPLICATE_7(fld, type, var, func, create, destroy, after) \
37                         void ReplicateVars(entity this, entity store, string thisname, int i) \
38                         { \
39                                 type field = store.fld; \
40                                 if (i < 0) { destroy } \
41                                 else \
42                                 { \
43                                         string it = func(argv(i + 1)); \
44                                         bool current = thisname == var; \
45                                         if (i > 0) \
46                                         { \
47                                                 if (current) { create } \
48                                         } \
49                                         else \
50                                         { \
51                                                 stuffcmd(this, "cl_cmd sendcvar " var "\n"); \
52                                         } \
53                                         if (current) { after } \
54                                 } \
55                                 store.fld = field; \
56                         }
57         #elif defined(CSQC)
58                 // TODO
59                 #define REPLICATE_7(fld, type, var, func, create, destroy, after)
60         #endif
61
62 #endif