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