]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/registry.qh
take3: format 903 files
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / registry.qh
1 #pragma once
2
3 #include "oo.qh"
4
5 #if 1
6 #define _R_MAP(r, max) \
7         ArrayList r; STATIC_INIT(r) \
8         { \
9                 AL_NEW(r, max, NULL, e); \
10         }
11 #define _R_GET(r, i) AL_gete(r, i)
12 #define _R_SET(r, i, e) AL_sete(r, i, e)
13 #define _R_DEL(r) AL_DELETE(r)
14 #else
15 #define _R_MAP(r, max) entity r[max]
16 #define _R_GET(r, i) r[i]
17 #define _R_SET(r, i, e) r[i] = e
18 #define _R_DEL(r)
19 #endif
20
21 /**
22  * Declare a new registry.
23  *
24  * Don't forget to call `REGISTER_REGISTRY`:
25  *     REGISTER_REGISTRY(Foos)
26  */
27 #define REGISTRY(id, max) \
28         void Register##id(); \
29         [[accumulate]] void REGISTRY_DEPENDS_(id) {} \
30         [[accumulate]] REGISTRY_BEGIN(id) {} \
31         [[accumulate]] REGISTRY_END(id) {} \
32         void _Register##id() {} \
33         int id##_state = 0; \
34         void Register##id() \
35         { \
36                 if (id##_state) { \
37                         return; \
38                 } \
39                 id##_state = 1; REGISTRY_DEPENDS_(id); REGISTRY_BEGIN_(id); _Register##id(); id##_state = 2; REGISTRY_END_(id); \
40         } \
41         const int id##_MAX = max; \
42         int id##_COUNT; \
43         noref entity id##_first, id##_last; \
44         _R_MAP(_##id, id##_MAX); \
45         SHUTDOWN(id) \
46         { \
47                 _R_DEL(_##id); \
48         } \
49         entity _##id##_from(int i, entity null) \
50         { \
51                 if (i >= 0 && i < id##_COUNT) { \
52                         entity e = _R_GET(_##id, i); if (e) { \
53                                 return e; \
54                         } \
55                 } \
56                 return null; \
57         }
58
59 /** Add registry dependencies to a registry */
60 #define REGISTRY_DEPENDS(id, dep) \
61         void Register##dep(); void REGISTRY_DEPENDS_(id) \
62         { \
63                 Register##dep(); \
64         }
65 #define REGISTRY_DEPENDS_(id) Register##id##_Depends()
66
67 /** Called before initializing a registry. */
68 #define REGISTRY_BEGIN(id) \
69         [[accumulate]] void REGISTRY_BEGIN_(id) \
70         { \
71                 noref void() f = Register##id; \
72         } void REGISTRY_BEGIN_(id)
73 #define REGISTRY_BEGIN_(id) Register##id##_First()
74
75 /** Called after initializing a registry. */
76 #define REGISTRY_END(id) \
77         [[accumulate]] void REGISTRY_END_(id) \
78         { \
79                 noref void() f = Register##id; \
80         } void REGISTRY_END_(id)
81 #define REGISTRY_END_(id) Register##id##_Done()
82
83 REGISTRY(Registries, BITS(8))
84
85 /** registered item identifier */
86 .string registered_id;
87
88 /**
89  * Register a new entity with a registry.
90  * Must be followed by a semicolon or a function body with a `this` parameter.
91  * Wrapper macros may perform actions after user initialization like so:
92  *     #define REGISTER_FOO(id) \
93  *         REGISTER(Foos, FOO, id, m_id, NEW(Foo)) { \
94  *             print("Registering foo #", this.m_id + 1, "\n"); \
95  *         } \
96  *         REGISTER_INIT(FOO, id)
97  *
98  *
99  * @param registry  The registry to add each entity to.
100  * @param ns        Short for namespace, prefix for each global (ns##_##id)
101  * @param id        The identifier of the current entity being registered
102  * @param fld       The field to store the locally unique unique entity id
103  * @param inst      An expression to create a new instance, invoked for every registration
104  */
105 #define REGISTER(...) EVAL_REGISTER(OVERLOAD_(REGISTER, __VA_ARGS__))
106 #define EVAL_REGISTER(...) __VA_ARGS__
107 #define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
108 #define REGISTER_4(registry, id, fld, inst) \
109         entity id; \
110         REGISTER_INIT(id) {} \
111         void Register_##id() \
112         { \
113                 entity this = id; \
114                 if (this == NULL) { \
115                         if (registry##_COUNT >= registry##_MAX) { LOG_FATALF("Registry capacity exceeded (%d)", registry##_MAX); } \
116                         this = id = inst; \
117                         this.registered_id = #id; \
118                         REGISTRY_PUSH(registry, fld, this); \
119                 } \
120                 Register_##id##_init(this); \
121         } \
122         ACCUMULATE_FUNCTION(_Register##registry, Register_##id) \
123         REGISTER_INIT(id)
124
125 #define REGISTRY_PUSH(registry, fld, it) \
126         MACRO_BEGIN \
127                 it.fld = registry##_COUNT; \
128                 _R_SET(_##registry, registry##_COUNT, it); \
129                 ++registry##_COUNT; \
130                 if (!registry##_first) { registry##_first = it; } \
131                 if (registry##_last) { registry##_last.REGISTRY_NEXT = it; } \
132                 registry##_last = it; \
133         MACRO_END
134
135 #define REGISTRY_RESERVE(registry, fld, id, suffix) \
136         MACRO_BEGIN \
137                 entity e = new_pure(registry_reserved); \
138                 e.registered_id = #id "/" #suffix; \
139                 REGISTRY_PUSH(registry, fld, e); \
140         MACRO_END
141
142 #define REGISTER_INIT(id) [[accumulate]] void Register_##id##_init(entity this)
143
144 /** internal next pointer */
145 #define REGISTRY_NEXT enemy
146 .entity REGISTRY_NEXT;
147
148 #define REGISTRY_SORT(...) EVAL_REGISTRY_SORT(OVERLOAD(REGISTRY_SORT, __VA_ARGS__))
149 #define EVAL_REGISTRY_SORT(...) __VA_ARGS__
150 #define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0)
151 #define REGISTRY_SORT_2(id, skip) \
152         void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
153         { \
154                 i += skip; j += skip; \
155         \
156                 entity a = _R_GET(_##id, i), b = _R_GET(_##id, j); \
157                 _R_SET(_##id, i, b); \
158                 _R_SET(_##id, j, a); \
159         \
160                 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
161                 a.REGISTRY_NEXT = b_next; \
162                 b.REGISTRY_NEXT = a_next; \
163         \
164                 if (i == 0) { id##_first = b; } else { _R_GET(_##id, i - 1).REGISTRY_NEXT = b; } \
165         \
166                 if (j == 0) { id##_first = a; } else { _R_GET(_##id, j - 1).REGISTRY_NEXT = a; } \
167         } \
168         int _REGISTRY_CMP_##id(int i, int j, entity pass) \
169         { \
170                 i += skip; j += skip; \
171                 string a = _R_GET(_##id, i).registered_id; \
172                 string b = _R_GET(_##id, j).registered_id; \
173                 return strcmp(a, b); \
174         } \
175         STATIC_INIT(Registry_sort_##id) \
176         { \
177                 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
178         }
179
180 #define REGISTRY_HASH(id) Registry_hash_##id
181
182 ERASEABLE
183 [[accumulate]] void Registry_check(string r, string server) {}
184 ERASEABLE
185 [[accumulate]] void Registry_send_all() {}
186
187 #ifdef SVQC
188 void Registry_send(string id, string hash);
189 #else
190 #define Registry_send(id, hash)
191 #endif
192
193 #define REGISTRY_CHECK(id) \
194         string REGISTRY_HASH(id); \
195         STATIC_INIT(Registry_check_##id) \
196         { \
197                 /* Note: SHA256 isn't always available, use MD4 instead */ \
198                 string algo = "MD4"; \
199                 string join = ":"; \
200                 string s = ""; \
201                 FOREACH(id, true, s = strcat(s, join, it.registered_id)); \
202                 s = substring(s, strlen(join), -1); \
203                 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
204                 LOG_DEBUGF( #id ": %s\n[%s]", h, s); \
205         } \
206         void Registry_check(string r, string sv) \
207         { \
208                 if (r == #id) { \
209                         string cl = REGISTRY_HASH(id); \
210                         if (cl != sv) { \
211                                 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s", r, cl, sv); \
212                         } \
213                 } \
214         } \
215         void Registry_send_all() \
216         { \
217                 Registry_send( #id, REGISTRY_HASH(id)); \
218         } \
219
220 #define REGISTER_REGISTRY(...) EVAL_REGISTER_REGISTRY(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__))
221 #define EVAL_REGISTER_REGISTRY(...) __VA_ARGS__
222 #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id)
223 #define REGISTER_REGISTRY_2(id, str) \
224         ACCUMULATE_FUNCTION(__static_init_1, Register##id) \
225         CLASS(id##Registry, Object) \
226                 ATTRIB(id##Registry, m_name, string, str); \
227                 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first); \
228                 METHOD(id##Registry, m_reload, void()); \
229         ENDCLASS(id##Registry) \
230         REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry)); \
231         METHOD(id##Registry, m_reload, void()) \
232         { \
233                 id##_state = 0; \
234                 Register##id(); \
235         }