7 * Declare a new registry.
9 * Don't forget to call `REGISTER_REGISTRY`:
10 * REGISTER_REGISTRY(Foos)
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; \
17 entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _##id[i]; if (e) return e; } return null; }
19 REGISTRY(Registries, BITS(8))
21 /** registered item identifier */
22 .string registered_id;
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"); \
33 * REGISTER_INIT(FOO, id)
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
42 #define REGISTER(registry, ns, id, fld, inst) \
44 REGISTER_INIT(ns, id) {} \
45 REGISTER_INIT_POST(ns, id) {} \
46 void Register_##ns##_##id() \
48 if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(registry##_MAX)); \
49 entity this = ns##_##id = inst; \
50 this.registered_id = #id; \
51 this.fld = registry##_COUNT; \
52 _##registry[registry##_COUNT] = this; \
54 if (!registry##_first) registry##_first = this; \
55 if (registry##_last) registry##_last.REGISTRY_NEXT = this; \
56 registry##_last = this; \
57 Register_##ns##_##id##_init(this); \
58 Register_##ns##_##id##_init_post(this); \
60 ACCUMULATE_FUNCTION(Register##registry, Register_##ns##_##id) \
63 #define REGISTER_INIT(ns, id) [[accumulate]] void Register_##ns##_##id##_init(entity this)
64 #define REGISTER_INIT_POST(ns, id) [[accumulate]] void Register_##ns##_##id##_init_post(entity this)
66 /** internal next pointer */
67 #define REGISTRY_NEXT enemy
68 .entity REGISTRY_NEXT;
70 #define REGISTRY_SORT(id, skip) \
71 void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
73 i += skip; j += skip; \
75 entity a = _##id[i], b = _##id[j]; \
79 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
80 a.REGISTRY_NEXT = b_next; \
81 b.REGISTRY_NEXT = a_next; \
83 if (i == 0) id##_first = b; \
84 else _##id[i - 1].REGISTRY_NEXT = b; \
86 if (j == 0) id##_first = a; \
87 else _##id[j - 1].REGISTRY_NEXT = a; \
89 int _REGISTRY_CMP_##id(int i, int j, entity pass) \
91 i += skip; j += skip; \
92 string a = _##id[i].registered_id; \
93 string b = _##id[j].registered_id; \
94 return strcmp(a, b); \
96 STATIC_INIT(Registry_sort_##id) \
98 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
101 #define REGISTRY_HASH(id) Registry_hash_##id
103 [[accumulate]] void Registry_check(string r, string server) { }
104 [[accumulate]] void Registry_send_all() { }
107 void Registry_send(string id, string hash);
109 #define Registry_send(id, hash)
112 #define REGISTRY_CHECK(id) \
113 string REGISTRY_HASH(id); \
114 STATIC_INIT(Registry_check_##id) \
116 string algo = "SHA256"; \
119 FOREACH(id, true, LAMBDA(s = strcat(s, join, it.registered_id))); \
120 s = substring(s, strlen(join), -1); \
121 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
122 LOG_TRACEF(#id ": %s\n[%s]\n", h, s); \
124 void Registry_check(string r, string sv) \
128 string cl = REGISTRY_HASH(id); \
131 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s\n", r, cl, sv); \
135 void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \
137 #define REGISTER_REGISTRY(...) EVAL(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__))
138 #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id)
139 #define REGISTER_REGISTRY_2(id, str) \
140 ACCUMULATE_FUNCTION(__static_init, Register##id) \
141 CLASS(id##Registry, Object) \
142 ATTRIB(id##Registry, m_name, string, str) \
143 ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first) \
144 ENDCLASS(id##Registry) \
145 REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry));