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