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
17 #define REGISTRY_MAX(id) id##_MAX
18 #define REGISTRY_COUNT(id) id##_COUNT
20 * Declare a new registry.
22 * Don't forget to call `REGISTER_REGISTRY`:
23 * REGISTER_REGISTRY(Foos)
25 #define REGISTRY(id, max) \
26 void Register##id(); \
27 ACCUMULATE void REGISTRY_DEPENDS_(id) {} \
28 REGISTRY_BEGIN(id) {} \
30 void _Register##id() {} \
32 void Register##id() { if (id##_state) return; id##_state = 1; REGISTRY_DEPENDS_(id); REGISTRY_BEGIN_(id); _Register##id(); id##_state = 2; REGISTRY_END_(id); } \
33 const int id##_MAX = max; \
35 noref entity id##_first, id##_last; \
36 _R_MAP(_##id, id##_MAX); \
37 SHUTDOWN(id) { _R_DEL(_##id); } \
38 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; }
40 /** Add registry dependencies to a registry */
41 #define REGISTRY_DEPENDS(id, dep) void Register##dep(); void REGISTRY_DEPENDS_(id) { Register##dep(); }
42 #define REGISTRY_DEPENDS_(id) Register##id##_Depends()
44 /** Called before initializing a registry. */
45 #define REGISTRY_BEGIN(id) ACCUMULATE void REGISTRY_BEGIN_(id) { noref void() f = Register##id; } void REGISTRY_BEGIN_(id)
46 #define REGISTRY_BEGIN_(id) Register##id##_First()
48 /** Called after initializing a registry. */
49 #define REGISTRY_END(id) ACCUMULATE void REGISTRY_END_(id) { noref void() f = Register##id; } void REGISTRY_END_(id)
50 #define REGISTRY_END_(id) Register##id##_Done()
52 REGISTRY(Registries, BITS(8))
54 /** registered item identifier */
55 .string registered_id;
57 void _regCheck(int i, int _max)
59 // this is inside a function to avoid expanding it on compilation everytime
60 // (this very long line would be repeated literally thousands times!)
62 LOG_FATALF("Registry capacity exceeded (%d)", _max);
66 * Register a new entity with a registry.
67 * Must be followed by a semicolon or a function body with a `this` parameter.
68 * Wrapper macros may perform actions after user initialization like so:
69 * #define REGISTER_FOO(id) \
70 * REGISTER(Foos, FOO, id, m_id, NEW(Foo)) { \
71 * print("Registering foo #", this.m_id + 1, "\n"); \
73 * REGISTER_INIT(FOO, id)
76 * @param registry The registry to add each entity to.
77 * @param ns Short for namespace, prefix for each global (ns##_##id)
78 * @param id The identifier of the current entity being registered
79 * @param fld The field to store the locally unique unique entity id
80 * @param inst An expression to create a new instance, invoked for every registration
82 #define REGISTER(...) EVAL_REGISTER(OVERLOAD_(REGISTER, __VA_ARGS__))
83 #define EVAL_REGISTER(...) __VA_ARGS__
84 #define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst)
85 #define REGISTER_4(registry, id, fld, inst) \
87 REGISTER_INIT(id) {} \
88 void Register_##id() \
92 _regCheck(registry##_COUNT, registry##_MAX); \
94 this.registered_id = #id; \
95 REGISTRY_PUSH(registry, fld, this); \
97 Register_##id##_init(this); \
99 ACCUMULATE_FUNCTION(_Register##registry, Register_##id) \
102 #define REGISTRY_PUSH(registry, fld, it) MACRO_BEGIN \
103 it.fld = registry##_COUNT; \
104 _R_SET(_##registry, registry##_COUNT, it); \
105 ++registry##_COUNT; \
106 if (!registry##_first) registry##_first = it; \
107 if (registry##_last) registry##_last.REGISTRY_NEXT = it; \
108 registry##_last = it; \
111 #define REGISTRY_RESERVE(registry, fld, id, suffix) MACRO_BEGIN \
112 entity e = new_pure(registry_reserved); \
113 e.registered_id = #id "/" #suffix; \
114 REGISTRY_PUSH(registry, fld, e); \
117 #define REGISTER_INIT(id) ACCUMULATE void Register_##id##_init(entity this)
119 /** internal next pointer */
120 #define REGISTRY_NEXT enemy
121 .entity REGISTRY_NEXT;
123 #define REGISTRY_SORT(...) EVAL_REGISTRY_SORT(OVERLOAD(REGISTRY_SORT, __VA_ARGS__))
124 #define EVAL_REGISTRY_SORT(...) __VA_ARGS__
125 #define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0)
126 #define REGISTRY_SORT_2(id, skip) \
127 void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
129 i += skip; j += skip; \
131 entity a = _R_GET(_##id, i), b = _R_GET(_##id, j); \
132 _R_SET(_##id, i, b); \
133 _R_SET(_##id, j, a); \
135 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
136 a.REGISTRY_NEXT = b_next; \
137 b.REGISTRY_NEXT = a_next; \
139 if (i == 0) id##_first = b; \
140 else _R_GET(_##id, i - 1).REGISTRY_NEXT = b; \
142 if (j == 0) id##_first = a; \
143 else _R_GET(_##id, j - 1).REGISTRY_NEXT = a; \
145 int _REGISTRY_CMP_##id(int i, int j, entity pass) \
147 i += skip; j += skip; \
148 string a = _R_GET(_##id, i).registered_id; \
149 string b = _R_GET(_##id, j).registered_id; \
150 return strcmp(a, b); \
152 STATIC_INIT(Registry_sort_##id) \
154 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
157 #define REGISTRY_HASH(id) Registry_hash_##id
160 ACCUMULATE void Registry_check(string r, string sv) { }
162 ACCUMULATE void Registry_send_all() { }
165 void Registry_send(string id, string hash);
167 #define Registry_send(id, hash)
170 #define REGISTRY_CHECK(id) \
171 string REGISTRY_HASH(id); \
172 STATIC_INIT(Registry_check_##id) \
174 /* Note: SHA256 isn't always available, use MD4 instead */ \
176 FOREACH(id, true, s = strcat(s, ":", it.registered_id)); \
177 s = substring(s, 1, -1); /* remove initial ":" */ \
178 string h = REGISTRY_HASH(id) = strzone(digest_hex("MD4", s)); \
179 LOG_DEBUGF(#id ": %s\n[%s]", h, s); \
181 void Registry_check(string r, string sv) \
185 string cl = REGISTRY_HASH(id); \
188 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s", r, cl, sv); \
192 void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \
194 #define REGISTER_REGISTRY(...) EVAL_REGISTER_REGISTRY(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__))
195 #define EVAL_REGISTER_REGISTRY(...) __VA_ARGS__
196 #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id)
197 #define REGISTER_REGISTRY_2(id, str) \
198 ACCUMULATE_FUNCTION(__static_init_1, Register##id) \
199 CLASS(id##Registry, Object) \
200 ATTRIB(id##Registry, m_name, string, str); \
201 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first); \
202 METHOD(id##Registry, m_reload, void()); \
203 ENDCLASS(id##Registry) \
204 REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry)); \
205 METHOD(id##Registry, m_reload, void()) { \