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)
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
18 * Declare a new registry.
20 * Don't forget to call `REGISTER_REGISTRY`:
21 * REGISTER_REGISTRY(Foos)
23 #define REGISTRY(id, max) \
24 void Register##id(); \
25 ACCUMULATE void REGISTRY_DEPENDS_(id) {} \
26 REGISTRY_BEGIN(id) {} \
28 void _Register##id() {} \
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; \
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; }
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()
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()
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()
50 REGISTRY(Registries, BITS(8))
52 /** registered item identifier */
53 .string registered_id;
55 void _regCheck(int i, int _max)
57 // this is inside a function to avoid expanding it on compilation everytime
58 // (this very long line would be repeated literally thousands times!)
60 LOG_FATALF("Registry capacity exceeded (%d)", _max);
64 * Register a new entity with a registry.
65 * Must be followed by a semicolon or a function body with a `this` parameter.
66 * Wrapper macros may perform actions after user initialization like so:
67 * #define REGISTER_FOO(id) \
68 * REGISTER(Foos, FOO, id, m_id, NEW(Foo)) { \
69 * print("Registering foo #", this.m_id + 1, "\n"); \
71 * REGISTER_INIT(FOO, id)
74 * @param registry The registry to add each entity to.
75 * @param ns Short for namespace, prefix for each global (ns##_##id)
76 * @param id The identifier of the current entity being registered
77 * @param fld The field to store the locally unique unique entity id
78 * @param inst An expression to create a new instance, invoked for every registration
80 #define REGISTER(...) EVAL_REGISTER(OVERLOAD_(REGISTER, __VA_ARGS__))
81 #define EVAL_REGISTER(...) __VA_ARGS__
82 #define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
83 #define REGISTER_4(registry, id, fld, inst) \
85 REGISTER_INIT(id) {} \
86 void Register_##id() \
90 _regCheck(registry##_COUNT, registry##_MAX); \
92 this.registered_id = #id; \
93 REGISTRY_PUSH(registry, fld, this); \
95 Register_##id##_init(this); \
97 ACCUMULATE_FUNCTION(_Register##registry, Register_##id) \
100 #define REGISTRY_PUSH(registry, fld, it) MACRO_BEGIN \
101 it.fld = registry##_COUNT; \
102 _R_SET(_##registry, registry##_COUNT, it); \
103 ++registry##_COUNT; \
104 if (!registry##_first) registry##_first = it; \
105 if (registry##_last) registry##_last.REGISTRY_NEXT = it; \
106 registry##_last = it; \
109 #define REGISTRY_RESERVE(registry, fld, id, suffix) MACRO_BEGIN \
110 entity e = new_pure(registry_reserved); \
111 e.registered_id = #id "/" #suffix; \
112 REGISTRY_PUSH(registry, fld, e); \
115 #define REGISTER_INIT(id) ACCUMULATE void Register_##id##_init(entity this)
117 /** internal next pointer */
118 #define REGISTRY_NEXT enemy
119 .entity REGISTRY_NEXT;
121 #define REGISTRY_SORT(...) EVAL_REGISTRY_SORT(OVERLOAD(REGISTRY_SORT, __VA_ARGS__))
122 #define EVAL_REGISTRY_SORT(...) __VA_ARGS__
123 #define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0)
124 #define REGISTRY_SORT_2(id, skip) \
125 void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
127 i += skip; j += skip; \
129 entity a = _R_GET(_##id, i), b = _R_GET(_##id, j); \
130 _R_SET(_##id, i, b); \
131 _R_SET(_##id, j, a); \
133 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
134 a.REGISTRY_NEXT = b_next; \
135 b.REGISTRY_NEXT = a_next; \
137 if (i == 0) id##_first = b; \
138 else _R_GET(_##id, i - 1).REGISTRY_NEXT = b; \
140 if (j == 0) id##_first = a; \
141 else _R_GET(_##id, j - 1).REGISTRY_NEXT = a; \
143 int _REGISTRY_CMP_##id(int i, int j, entity pass) \
145 i += skip; j += skip; \
146 string a = _R_GET(_##id, i).registered_id; \
147 string b = _R_GET(_##id, j).registered_id; \
148 return strcmp(a, b); \
150 STATIC_INIT(Registry_sort_##id) \
152 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
155 #define REGISTRY_HASH(id) Registry_hash_##id
158 ACCUMULATE void Registry_check(string r, string server) { }
160 ACCUMULATE void Registry_send_all() { }
163 void Registry_send(string id, string hash);
165 #define Registry_send(id, hash)
168 #define REGISTRY_CHECK(id) \
169 string REGISTRY_HASH(id); \
170 STATIC_INIT(Registry_check_##id) \
172 /* Note: SHA256 isn't always available, use MD4 instead */ \
173 string s = "", join = ":"; \
174 FOREACH(id, true, s = strcat(s, join, it.registered_id)); \
175 s = substring(s, strlen(join), -1); \
176 string h = REGISTRY_HASH(id) = strzone(digest_hex("MD4", s)); \
177 LOG_DEBUGF(#id ": %s\n[%s]", h, s); \
179 void Registry_check(string r, string sv) \
183 string cl = REGISTRY_HASH(id); \
186 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s", r, cl, sv); \
190 void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \
192 #define REGISTER_REGISTRY(...) EVAL_REGISTER_REGISTRY(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__))
193 #define EVAL_REGISTER_REGISTRY(...) __VA_ARGS__
194 #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id)
195 #define REGISTER_REGISTRY_2(id, str) \
196 ACCUMULATE_FUNCTION(__static_init_1, Register##id) \
197 CLASS(id##Registry, Object) \
198 ATTRIB(id##Registry, m_name, string, str); \
199 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first); \
200 METHOD(id##Registry, m_reload, void()); \
201 ENDCLASS(id##Registry) \
202 REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry)); \
203 METHOD(id##Registry, m_reload, void()) { \