]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/registry.qh
Registry: network and verify checksums on connect
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / registry.qh
1 #ifndef REGISTRY_H
2 #define REGISTRY_H
3
4 #include "oo.qh"
5
6 #define REGISTER_REGISTRY(func) ACCUMULATE_FUNCTION(__static_init, func)
7
8 #define REGISTER_INIT(ns, id) [[accumulate]] void Register_##ns##_##id##_init(entity this)
9 #define REGISTER_INIT_POST(ns, id) [[accumulate]] void Register_##ns##_##id##_init_post(entity this)
10
11 #define REGISTRY(id, max) \
12         void Register##id() {} \
13         const int id##_MAX = max; \
14         noref entity _##id[id##_MAX], id##_first, id##_last; \
15         int id##_COUNT; \
16         entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _##id[i]; if (e) return e; } return null; }
17
18 /** registered item identifier */
19 .string registered_id;
20
21 /**
22  * Register a new entity with a global constructor.
23  * Must be followed by a semicolon or a function body with a `this` parameter.
24  * Wrapper macros may perform actions after user initialization like so:
25  *     #define REGISTER_FOO(id) \
26  *         REGISTER(RegisterFoos, FOO, FOOS, id, m_id, NEW(Foo)); \
27  *         REGISTER_INIT_POST(FOO, id) { \
28  *             print("Registering foo #", this.m_id + 1, "\n"); \
29  *         } \
30  *         REGISTER_INIT(FOO, id)
31  *
32  * Don't forget to forward declare `initfunc` and call `REGISTER_REGISTRY`:
33  *     void RegisterFoos();
34  *     REGISTER_REGISTRY(RegisterFoos)
35  *
36  * @param initfunc  The global constructor to accumulate into
37  * @param ns        Short for namespace, prefix for each global (ns##_##id)
38  * @param array     The array to add each entity to. Also requires `array##_first` and `array##_last` to be defined
39  * @param id        The identifier of the current entity being registered
40  * @param fld       The field to store the current count into
41  * @param inst      An expression to create a new instance, invoked for every registration
42  */
43 #define REGISTER(initfunc, ns, array, id, fld, inst) \
44         entity ns##_##id; \
45         REGISTER_INIT(ns, id) {} \
46         REGISTER_INIT_POST(ns, id) {} \
47         void Register_##ns##_##id() \
48         { \
49                 if (array##_COUNT >= array##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(array##_MAX)); \
50                 entity this = inst; \
51                 ns##_##id = this; \
52                 this.registered_id = #id; \
53                 this.fld = array##_COUNT; \
54                 _##array[array##_COUNT++] = this; \
55                 if (!array##_first) array##_first = this; \
56                 if (array##_last)   array##_last.REGISTRY_NEXT = this; \
57                 array##_last = this; \
58                 Register_##ns##_##id##_init(this); \
59                 Register_##ns##_##id##_init_post(this); \
60         } \
61         ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id) \
62         REGISTER_INIT(ns, id)
63
64 /** internal next pointer */
65 #define REGISTRY_NEXT enemy
66 .entity REGISTRY_NEXT;
67
68 #define REGISTRY_SORT(id, skip) \
69         void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
70         { \
71                 i += skip; j += skip; \
72                 \
73                 entity a = _##id[i], b = _##id[j]; \
74                 _##id[i] = b; \
75                 _##id[j] = a; \
76         \
77                 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
78                 a.REGISTRY_NEXT = b_next; \
79                 b.REGISTRY_NEXT = a_next; \
80         \
81                 if (i == 0) id##_first = b; \
82                 else _##id[i - 1].REGISTRY_NEXT = b; \
83         \
84                 if (j == 0) id##_first = a; \
85                 else _##id[j - 1].REGISTRY_NEXT = a; \
86         } \
87         int _REGISTRY_CMP_##id(int i, int j, entity pass) \
88         { \
89                 i += skip; j += skip; \
90                 string a = _##id[i].registered_id; \
91                 string b = _##id[j].registered_id; \
92                 return strcmp(a, b); \
93         } \
94         STATIC_INIT(Registry_sort_##id) \
95         { \
96                 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
97         }
98
99 #define REGISTRY_HASH(id) Registry_hash_##id
100
101 [[accumulate]] void Registry_check(string r, string server) { }
102 [[accumulate]] void Registry_send_all() { }
103
104 #ifdef SVQC
105 void Registry_send(string id, string hash);
106 #else
107 #define Registry_send(id, hash)
108 #endif
109
110 #define REGISTRY_CHECK(id) \
111         string REGISTRY_HASH(id); \
112         STATIC_INIT(Registry_check_##id) \
113         { \
114                 string algo = "SHA256"; \
115                 string join = ":"; \
116                 string s = ""; \
117                 FOREACH(id, true, LAMBDA(s = strcat(s, join, it.registered_id))); \
118                 s = substring(s, strlen(join), -1); \
119                 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
120                 LOG_TRACEF(#id ": %s\n[%s]\n", h, s); \
121         } \
122         [[accumulate]] void Registry_check(string r, string sv) \
123         { \
124                 if (r == #id) \
125                 { \
126                         string cl = REGISTRY_HASH(id); \
127                         if (cl != sv) \
128                         { \
129                                 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s\n", r, cl, sv); \
130                         } \
131                 } \
132         } \
133         [[accumulate]] void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \
134
135 #endif