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