6 #define REGISTER_REGISTRY(func) ACCUMULATE_FUNCTION(__static_init, func)
8 #define REGISTER_INIT(ns, id) [[accumulate]] void Register_##ns##_##id##_init(entity this)
9 #define REGISTER_INIT_POST(ns, id) [[accumulate]] void Register_##ns##_##id##_init_post(entity this)
11 #define REGISTRY(id, max) \
12 void Register##id() {} \
13 const int id##_MAX = max; \
14 noref entity _##id[id##_MAX], id##_first, id##_last; \
16 entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _##id[i]; if (e) return e; } return null; }
18 /** registered item identifier */
19 .string registered_id;
22 * Register a new entity with a global constructor.
23 * Must be followed by a semicolon or a function body with a `this` parameter.
24 * Wrapper macros may perform actions after user initialization like so:
25 * #define REGISTER_FOO(id) \
26 * REGISTER(RegisterFoos, FOO, FOOS, id, m_id, NEW(Foo)); \
27 * REGISTER_INIT_POST(FOO, id) { \
28 * print("Registering foo #", this.m_id + 1, "\n"); \
30 * REGISTER_INIT(FOO, id)
32 * Don't forget to forward declare `initfunc` and call `REGISTER_REGISTRY`:
33 * void RegisterFoos();
34 * REGISTER_REGISTRY(RegisterFoos)
36 * @param initfunc The global constructor to accumulate into
37 * @param ns Short for namespace, prefix for each global (ns##_##id)
38 * @param array The array to add each entity to. Also requires `array##_first` and `array##_last` to be defined
39 * @param id The identifier of the current entity being registered
40 * @param fld The field to store the current count into
41 * @param inst An expression to create a new instance, invoked for every registration
43 #define REGISTER(initfunc, ns, array, id, fld, inst) \
45 REGISTER_INIT(ns, id) {} \
46 REGISTER_INIT_POST(ns, id) {} \
47 void Register_##ns##_##id() \
49 if (array##_COUNT >= array##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(array##_MAX)); \
52 this.registered_id = #id; \
53 this.fld = array##_COUNT; \
54 _##array[array##_COUNT++] = this; \
55 if (!array##_first) array##_first = this; \
56 if (array##_last) array##_last.REGISTRY_NEXT = this; \
57 array##_last = this; \
58 Register_##ns##_##id##_init(this); \
59 Register_##ns##_##id##_init_post(this); \
61 ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id) \
64 /** internal next pointer */
65 #define REGISTRY_NEXT enemy
66 .entity REGISTRY_NEXT;
68 #define REGISTRY_SORT(id, skip) \
69 void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
71 i += skip; j += skip; \
73 entity a = _##id[i], b = _##id[j]; \
77 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \
78 a.REGISTRY_NEXT = b_next; \
79 b.REGISTRY_NEXT = a_next; \
81 if (i == 0) id##_first = b; \
82 else _##id[i - 1].REGISTRY_NEXT = b; \
84 if (j == 0) id##_first = a; \
85 else _##id[j - 1].REGISTRY_NEXT = a; \
87 int _REGISTRY_CMP_##id(int i, int j, entity pass) \
89 i += skip; j += skip; \
90 string a = _##id[i].registered_id; \
91 string b = _##id[j].registered_id; \
92 return strcmp(a, b); \
94 STATIC_INIT(Registry_sort_##id) \
96 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
99 #define REGISTRY_HASH(id) Registry_hash_##id
101 [[accumulate]] void Registry_check(string r, string server) { }
102 [[accumulate]] void Registry_send_all() { }
105 void Registry_send(string id, string hash);
107 #define Registry_send(id, hash)
110 #define REGISTRY_CHECK(id) \
111 string REGISTRY_HASH(id); \
112 STATIC_INIT(Registry_check_##id) \
114 string algo = "SHA256"; \
117 FOREACH(id, true, LAMBDA(s = strcat(s, join, it.registered_id))); \
118 s = substring(s, strlen(join), -1); \
119 string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \
120 LOG_TRACEF(#id ": %s\n[%s]\n", h, s); \
122 [[accumulate]] void Registry_check(string r, string sv) \
126 string cl = REGISTRY_HASH(id); \
129 LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s\n", r, cl, sv); \
133 [[accumulate]] void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \