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