]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/registry.qh
Merge branch 'terencehill/g_lms_extra_lives' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / registry.qh
1 #ifndef REGISTRY_H
2 #define REGISTRY_H
3
4 #include "oo.qh"
5
6 /**
7  * Declare a new registry.
8  *
9  * Don't forget to call `REGISTER_REGISTRY`:
10  *     REGISTER_REGISTRY(Foos)
11  */
12 #define REGISTRY(id, max) \
13         void Register##id() {} \
14         const int id##_MAX = max; \
15         noref entity _##id[id##_MAX], id##_first, id##_last; \
16         int id##_COUNT; \
17         entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _##id[i]; if (e) return e; } return null; }
18
19 REGISTRY(Registries, BITS(8))
20
21 /** registered item identifier */
22 .string registered_id;
23
24 /**
25  * Register a new entity with a registry.
26  * Must be followed by a semicolon or a function body with a `this` parameter.
27  * Wrapper macros may perform actions after user initialization like so:
28  *     #define REGISTER_FOO(id) \
29  *         REGISTER(Foos, FOO, id, m_id, NEW(Foo)); \
30  *         REGISTER_INIT_POST(FOO, id) { \
31  *             print("Registering foo #", this.m_id + 1, "\n"); \
32  *         } \
33  *         REGISTER_INIT(FOO, id)
34  *
35  *
36  * @param registry  The registry to add each entity to.
37  * @param ns        Short for namespace, prefix for each global (ns##_##id)
38  * @param id        The identifier of the current entity being registered
39  * @param fld       The field to store the locally unique unique entity id
40  * @param inst      An expression to create a new instance, invoked for every registration
41  */
42 #define REGISTER(...) EVAL(OVERLOAD(REGISTER, __VA_ARGS__))
43 #define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
44 #define REGISTER_4(registry, id, fld, inst) \
45         entity id; \
46         REGISTER_INIT(id) {} \
47         REGISTER_INIT_POST(id) {} \
48         void Register_##id() \
49         { \
50                 if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(registry##_MAX)); \
51                 entity this = id = inst; \
52                 this.registered_id = #id; \
53                 this.fld = registry##_COUNT; \
54                 _##registry[registry##_COUNT] = this; \
55                 ++registry##_COUNT; \
56                 if (!registry##_first) registry##_first = this; \
57                 if (registry##_last)   registry##_last.REGISTRY_NEXT = this; \
58                 registry##_last = this; \
59                 Register_##id##_init(this); \
60                 Register_##id##_init_post(this); \
61         } \
62         ACCUMULATE_FUNCTION(Register##registry, Register_##id) \
63         REGISTER_INIT(id)
64
65 #define REGISTER_INIT(id) [[accumulate]] void Register_##id##_init(entity this)
66 #define REGISTER_INIT_POST(id) [[accumulate]] void Register_##id##_init_post(entity this)
67
68 /** internal next pointer */
69 #define REGISTRY_NEXT enemy
70 .entity REGISTRY_NEXT;
71
72 #define REGISTRY_SORT(...) EVAL(OVERLOAD(REGISTRY_SORT, __VA_ARGS__))
73 #define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0)
74 #define REGISTRY_SORT_2(id, skip) \
75         void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
76         { \
77                 i += skip; j += skip; \
78                 \
79                 entity a = _##id[i], b = _##id[j]; \
80                 _##id[i] = b; \
81                 _##id[j] = a; \
82         \
83                 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
84                 a.REGISTRY_NEXT = b_next; \
85                 b.REGISTRY_NEXT = a_next; \
86         \
87                 if (i == 0) id##_first = b; \
88                 else _##id[i - 1].REGISTRY_NEXT = b; \
89         \
90                 if (j == 0) id##_first = a; \
91                 else _##id[j - 1].REGISTRY_NEXT = a; \
92         } \
93         int _REGISTRY_CMP_##id(int i, int j, entity pass) \
94         { \
95                 i += skip; j += skip; \
96                 string a = _##id[i].registered_id; \
97                 string b = _##id[j].registered_id; \
98                 return strcmp(a, b); \
99         } \
100         STATIC_INIT(Registry_sort_##id) \
101         { \
102                 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
103         }
104
105 #define REGISTRY_HASH(id) Registry_hash_##id
106
107 [[accumulate]] void Registry_check(string r, string server) { }
108 [[accumulate]] void Registry_send_all() { }
109
110 #ifdef SVQC
111 void Registry_send(string id, string hash);
112 #else
113 #define Registry_send(id, hash)
114 #endif
115
116 #define REGISTRY_CHECK(id) \
117         string REGISTRY_HASH(id); \
118         STATIC_INIT(Registry_check_##id) \
119         { \
120                 string algo = "SHA256"; \
121                 string join = ":"; \
122                 string s = ""; \
123                 FOREACH(id, true, LAMBDA(s = strcat(s, join, it.registered_id))); \
124                 s = substring(s, strlen(join), -1); \
125                 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
126                 LOG_TRACEF(#id ": %s\n[%s]\n", h, s); \
127         } \
128         void Registry_check(string r, string sv) \
129         { \
130                 if (r == #id) \
131                 { \
132                         string cl = REGISTRY_HASH(id); \
133                         if (cl != sv) \
134                         { \
135                                 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s\n", r, cl, sv); \
136                         } \
137                 } \
138         } \
139         void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \
140
141 #define REGISTER_REGISTRY(...) EVAL(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__))
142 #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id)
143 #define REGISTER_REGISTRY_2(id, str) \
144         ACCUMULATE_FUNCTION(__static_init, Register##id) \
145         CLASS(id##Registry, Object) \
146                 ATTRIB(id##Registry, m_name, string, str) \
147                 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first) \
148         ENDCLASS(id##Registry) \
149         REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry));
150
151
152 #endif