]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/registry.qh
Merge branch 'master' into terencehill/translate_colors_2
[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  *         REGISTER_INIT_POST(FOO, id) { \
56  *             print("Registering foo #", this.m_id + 1, "\n"); \
57  *         } \
58  *         REGISTER_INIT(FOO, id)
59  *
60  *
61  * @param registry  The registry to add each entity to.
62  * @param ns        Short for namespace, prefix for each global (ns##_##id)
63  * @param id        The identifier of the current entity being registered
64  * @param fld       The field to store the locally unique unique entity id
65  * @param inst      An expression to create a new instance, invoked for every registration
66  */
67 #define REGISTER(...) EVAL_REGISTER(OVERLOAD_(REGISTER, __VA_ARGS__))
68 #define EVAL_REGISTER(...) __VA_ARGS__
69 #define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
70 #define REGISTER_4(registry, id, fld, inst) \
71         entity id; \
72         REGISTER_INIT(id) {} \
73         REGISTER_INIT_POST(id) {} \
74         void Register_##id() \
75         { \
76                 if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%d)", registry##_MAX); \
77                 entity this = id = inst; \
78                 this.registered_id = #id; \
79                 REGISTRY_PUSH(registry, fld, this); \
80                 Register_##id##_init(this); \
81                 Register_##id##_init_post(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 #define REGISTER_INIT_POST(id) [[accumulate]] void Register_##id##_init_post(entity this)
103
104 /** internal next pointer */
105 #define REGISTRY_NEXT enemy
106 .entity REGISTRY_NEXT;
107
108 #define REGISTRY_SORT(...) EVAL_REGISTRY_SORT(OVERLOAD(REGISTRY_SORT, __VA_ARGS__))
109 #define EVAL_REGISTRY_SORT(...) __VA_ARGS__
110 #define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0)
111 #define REGISTRY_SORT_2(id, skip) \
112         void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
113         { \
114                 i += skip; j += skip; \
115                 \
116                 entity a = _R_GET(_##id, i), b = _R_GET(_##id, j); \
117                 _R_SET(_##id, i, b); \
118                 _R_SET(_##id, j, a); \
119         \
120                 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
121                 a.REGISTRY_NEXT = b_next; \
122                 b.REGISTRY_NEXT = a_next; \
123         \
124                 if (i == 0) id##_first = b; \
125                 else _R_GET(_##id, i - 1).REGISTRY_NEXT = b; \
126         \
127                 if (j == 0) id##_first = a; \
128                 else _R_GET(_##id, j - 1).REGISTRY_NEXT = a; \
129         } \
130         int _REGISTRY_CMP_##id(int i, int j, entity pass) \
131         { \
132                 i += skip; j += skip; \
133                 string a = _R_GET(_##id, i).registered_id; \
134                 string b = _R_GET(_##id, j).registered_id; \
135                 return strcmp(a, b); \
136         } \
137         STATIC_INIT(Registry_sort_##id) \
138         { \
139                 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
140         }
141
142 #define REGISTRY_HASH(id) Registry_hash_##id
143
144 [[accumulate]] void Registry_check(string r, string server) { }
145 [[accumulate]] void Registry_send_all() { }
146
147 #ifdef SVQC
148 void Registry_send(string id, string hash);
149 #else
150 #define Registry_send(id, hash)
151 #endif
152
153 #define REGISTRY_CHECK(id) \
154         string REGISTRY_HASH(id); \
155         STATIC_INIT(Registry_check_##id) \
156         { \
157                 /* Note: SHA256 isn't always available, use MD4 instead */ \
158                 string algo = "MD4"; \
159                 string join = ":"; \
160                 string s = ""; \
161                 FOREACH(id, true, s = strcat(s, join, it.registered_id)); \
162                 s = substring(s, strlen(join), -1); \
163                 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
164                 LOG_TRACEF(#id ": %s\n[%s]\n", h, s); \
165         } \
166         void Registry_check(string r, string sv) \
167         { \
168                 if (r == #id) \
169                 { \
170                         string cl = REGISTRY_HASH(id); \
171                         if (cl != sv) \
172                         { \
173                                 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s\n", r, cl, sv); \
174                         } \
175                 } \
176         } \
177         void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \
178
179 #define REGISTER_REGISTRY(...) EVAL_REGISTER_REGISTRY(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__))
180 #define EVAL_REGISTER_REGISTRY(...) __VA_ARGS__
181 #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id)
182 #define REGISTER_REGISTRY_2(id, str) \
183         ACCUMULATE_FUNCTION(__static_init, Register##id) \
184         CLASS(id##Registry, Object) \
185                 ATTRIB(id##Registry, m_name, string, str) \
186                 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first) \
187         ENDCLASS(id##Registry) \
188         REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry));