Xonotic QuakeC
The free, fast arena FPS with crisp movement and a wide array of weapons
registry.qh
Go to the documentation of this file.
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#define REGISTRY_MAX(id) id##_MAX
18#define REGISTRY_COUNT(id) id##_COUNT
26#define REGISTRY(id, max) \
27 void Register##id(); \
28 ACCUMULATE void REGISTRY_DEPENDS_(id) {} \
29 REGISTRY_BEGIN(id) {} \
30 REGISTRY_END(id) {} \
31 void _Register##id() {} \
32 int id##_state = 0; \
33 void Register##id() { if (id##_state) return; id##_state = 1; REGISTRY_DEPENDS_(id); REGISTRY_BEGIN_(id); _Register##id(); id##_state = 2; REGISTRY_END_(id); } \
34 const int id##_MAX = max; \
35 int id##_COUNT; \
36 noref entity id##_first, id##_last; \
37 _R_MAP(_##id, id##_MAX); \
38 SHUTDOWN(id) { _R_DEL(_##id); } \
39
40#define REGISTRY_DEFINE_GET(id, null) \
41 entity id##_from(int i) { if (i >= 0 && i < id##_COUNT) { entity e = _R_GET(_##id, i); if (e) return e; } return null; }
42
43#define REGISTRY_GET(id, i) id##_from(i)
44
46#define REGISTRY_DEPENDS(id, dep) void Register##dep(); void REGISTRY_DEPENDS_(id) { Register##dep(); }
47#define REGISTRY_DEPENDS_(id) Register##id##_Depends()
48
50#define REGISTRY_BEGIN(id) ACCUMULATE void REGISTRY_BEGIN_(id) { noref void() f = Register##id; } void REGISTRY_BEGIN_(id)
51#define REGISTRY_BEGIN_(id) Register##id##_First()
52
54#define REGISTRY_END(id) ACCUMULATE void REGISTRY_END_(id) { noref void() f = Register##id; } void REGISTRY_END_(id)
55#define REGISTRY_END_(id) Register##id##_Done()
56
57REGISTRY(Registries, BITS(8))
58
59
61
62void _regCheck(int i, int _max)
63{
64 // this is inside a function to avoid expanding it on compilation everytime
65 // (this very long line would be repeated literally thousands times!)
66 if (i >= _max)
67 LOG_FATALF("Registry capacity exceeded (%d)", _max);
68}
69
87#define REGISTER(...) EVAL_REGISTER(OVERLOAD_(REGISTER, __VA_ARGS__))
88#define EVAL_REGISTER(...) __VA_ARGS__
89#define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
90#define REGISTER_4(registry, id, fld, inst) \
91 entity id; \
92 REGISTER_INIT(id) {} \
93 void Register_##id() \
94 { \
95 entity this = id; \
96 if (this == NULL) { \
97 _regCheck(registry##_COUNT, registry##_MAX); \
98 this = id = inst; \
99 this.registered_id = #id; \
100 REGISTRY_PUSH(registry, fld, this); \
101 } \
102 Register_##id##_init(this); \
103 } \
104 ACCUMULATE_FUNCTION(_Register##registry, Register_##id) \
105 REGISTER_INIT(id)
106
107#define REGISTRY_PUSH(registry, fld, it) MACRO_BEGIN \
108 it.fld = registry##_COUNT; \
109 _R_SET(_##registry, registry##_COUNT, it); \
110 ++registry##_COUNT; \
111 if (!registry##_first) registry##_first = it; \
112 if (registry##_last) registry##_last.REGISTRY_NEXT = it; \
113 registry##_last = it; \
114MACRO_END
115
116#define REGISTRY_RESERVE(registry, fld, id, suffix) MACRO_BEGIN \
117 entity e = new_pure(registry_reserved); \
118 e.registered_id = #id "/" #suffix; \
119 REGISTRY_PUSH(registry, fld, e); \
120MACRO_END
121
122#define REGISTER_INIT(id) ACCUMULATE void Register_##id##_init(entity this)
123
125#define REGISTRY_NEXT enemy
127
128#define REGISTRY_SORT(...) EVAL_REGISTRY_SORT(OVERLOAD(REGISTRY_SORT, __VA_ARGS__))
129#define EVAL_REGISTRY_SORT(...) __VA_ARGS__
130#define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0)
131#define REGISTRY_SORT_2(id, skip) \
132 void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
133 { \
134 i += skip; j += skip; \
135 \
136 entity a = _R_GET(_##id, i), b = _R_GET(_##id, j); \
137 _R_SET(_##id, i, b); \
138 _R_SET(_##id, j, a); \
139 \
140 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
141 a.REGISTRY_NEXT = b_next; \
142 b.REGISTRY_NEXT = a_next; \
143 \
144 if (i == 0) id##_first = b; \
145 else _R_GET(_##id, i - 1).REGISTRY_NEXT = b; \
146 \
147 if (j == 0) id##_first = a; \
148 else _R_GET(_##id, j - 1).REGISTRY_NEXT = a; \
149 } \
150 int _REGISTRY_CMP_##id(int i, int j, entity pass) \
151 { \
152 i += skip; j += skip; \
153 string a = _R_GET(_##id, i).registered_id; \
154 string b = _R_GET(_##id, j).registered_id; \
155 return strcmp(a, b); \
156 } \
157 STATIC_INIT(Registry_sort_##id) \
158 { \
159 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
160 }
161
162#define REGISTRY_HASH(id) Registry_hash_##id
163
165ACCUMULATE void Registry_check(string r, string sv) { }
168
169#ifdef SVQC
170void Registry_send(string id, string hash);
171#else
172#define Registry_send(id, hash)
173#endif
174
175#define REGISTRY_CHECK(id) \
176 string REGISTRY_HASH(id); \
177 STATIC_INIT(Registry_check_##id) \
178 { \
179 /* Note: SHA256 isn't always available, use MD4 instead */ \
180 string s = "", group = ""; \
181 int str_len = 0, digests_len = 0, group_idx = 0; \
182 FOREACH(id, true, { \
183 group = strcat(group, ":", it.registered_id); \
184 if (++group_idx < 50) /* this is to reduce strlen calls */ \
185 continue; \
186 int group_len = strlen(group); \
187 if (str_len + 1 + group_len >= VM_TEMPSTRING_MAXSIZE) \
188 { \
189 /* keep previous digests and replace current string with its digest */ \
190 s = strcat(substring(s, 0, digests_len), ":", digest_hex("MD4", s)); \
191 digests_len = str_len = strlen(s); \
192 } \
193 s = strcat(s, group); \
194 str_len += group_len; \
195 group = ""; \
196 group_idx = 0; \
197 }); \
198 s = strcat(s, group); \
199 s = substring(s, 1, -1); /* remove initial ":" */ \
200 string h = REGISTRY_HASH(id) = strzone(digest_hex("MD4", s)); \
201 LOG_DEBUGF(#id ": %s\n[%s]", h, s); \
202 } \
203 void Registry_check(string r, string sv) /* called by CSQC */ \
204 { \
205 if (r == #id) \
206 { \
207 string cl = REGISTRY_HASH(id); \
208 if (cl != sv) \
209 { \
210 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s", r, cl, sv); \
211 } \
212 } \
213 } \
214 void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } /* called by SVQC */ \
215
216#define _REGISTER_REGISTRY(id, str) \
217 ACCUMULATE_FUNCTION(__static_init_1, Register##id) \
218 CLASS(id##Registry, Object) \
219 ATTRIB(id##Registry, m_name, string, str); \
220 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first); \
221 METHOD(id##Registry, m_reload, void()); \
222 ENDCLASS(id##Registry) \
223 REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry)); \
224 METHOD(id##Registry, m_reload, void()) { \
225 id##_state = 0; \
226 Register##id(); \
227 }
228
229#define REGISTER_REGISTRY(id) _REGISTER_REGISTRY(id, #id)
#define BITS(n)
Definition bits.qh:9
#define ERASEABLE
Definition _all.inc:37
#define ACCUMULATE
Definition _all.inc:31
#define LOG_FATALF(...)
Definition log.qh:54
#define REGISTRY_NEXT
internal next pointer
Definition registry.qh:125
ERASEABLE ACCUMULATE void Registry_send_all()
Definition registry.qh:167
void Registry_send(string id, string hash)
string registered_id
registered item identifier
Definition registry.qh:60
#define REGISTRY(id, max)
Declare a new registry.
Definition registry.qh:26
void _regCheck(int i, int _max)
Definition registry.qh:62
ERASEABLE ACCUMULATE void Registry_check(string r, string sv)
Definition registry.qh:165