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