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