]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/registry.qh
Stats: port vectors
[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                 REGISTRY_PUSH(array, fld, this); \
54                 Register_##ns##_##id##_init(this); \
55                 Register_##ns##_##id##_init_post(this); \
56         } \
57         ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id) \
58         REGISTER_INIT(ns, id)
59
60 #define REGISTRY_PUSH(array, fld, it) do { \
61         it.fld = array##_COUNT; \
62         _##array[array##_COUNT++] = it; \
63         if (!array##_first) array##_first = it; \
64         if (array##_last)   array##_last.REGISTRY_NEXT = it; \
65         array##_last = it; \
66 } while (0)
67
68 #define REGISTRY_RESERVE(registry, fld, id, suffix) do { \
69         entity e = new(registry_reserved); \
70         e.registered_id = #id #suffix; \
71         REGISTRY_PUSH(registry, fld, e); \
72 } while (0)
73
74 /** internal next pointer */
75 #define REGISTRY_NEXT enemy
76 .entity REGISTRY_NEXT;
77
78 #define REGISTRY_SORT(id, skip) \
79         void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
80         { \
81                 i += skip; j += skip; \
82                 \
83                 entity a = _##id[i], b = _##id[j]; \
84                 _##id[i] = b; \
85                 _##id[j] = a; \
86         \
87                 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
88                 a.REGISTRY_NEXT = b_next; \
89                 b.REGISTRY_NEXT = a_next; \
90         \
91                 if (i == 0) id##_first = b; \
92                 else _##id[i - 1].REGISTRY_NEXT = b; \
93         \
94                 if (j == 0) id##_first = a; \
95                 else _##id[j - 1].REGISTRY_NEXT = a; \
96         } \
97         int _REGISTRY_CMP_##id(int i, int j, entity pass) \
98         { \
99                 i += skip; j += skip; \
100                 string a = _##id[i].registered_id; \
101                 string b = _##id[j].registered_id; \
102                 return strcmp(a, b); \
103         } \
104         STATIC_INIT(Registry_sort_##id) \
105         { \
106                 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
107         }
108
109 #define REGISTRY_HASH(id) Registry_hash_##id
110
111 [[accumulate]] void Registry_check(string r, string server) { }
112 [[accumulate]] void Registry_send_all() { }
113
114 #ifdef SVQC
115 void Registry_send(string id, string hash);
116 #else
117 #define Registry_send(id, hash)
118 #endif
119
120 #define REGISTRY_CHECK(id) \
121         string REGISTRY_HASH(id); \
122         STATIC_INIT(Registry_check_##id) \
123         { \
124                 string algo = "SHA256"; \
125                 string join = ":"; \
126                 string s = ""; \
127                 FOREACH(id, true, LAMBDA(s = strcat(s, join, it.registered_id))); \
128                 s = substring(s, strlen(join), -1); \
129                 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
130                 LOG_TRACEF(#id ": %s\n[%s]\n", h, s); \
131         } \
132         [[accumulate]] void Registry_check(string r, string sv) \
133         { \
134                 if (r == #id) \
135                 { \
136                         string cl = REGISTRY_HASH(id); \
137                         if (cl != sv) \
138                         { \
139                                 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s\n", r, cl, sv); \
140                         } \
141                 } \
142         } \
143         [[accumulate]] void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \
144
145 #endif