]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/lib/registry.qh
Registry: checksum
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / registry.qh
1 #ifndef REGISTRY_H
2 #define REGISTRY_H
3
4 #include "oo.qh"
5
6 #define REGISTER_INIT(ns, id) [[accumulate]] void Register_##ns##_##id##_init(entity this)
7 #define REGISTER_INIT_POST(ns, id) [[accumulate]] void Register_##ns##_##id##_init_post(entity this)
8
9 #define REGISTRY(id, max) \
10         void Register##id() {} \
11         const int id##_MAX = max; \
12         noref entity _##id[id##_MAX], id##_first, id##_last; \
13         int id##_COUNT; \
14         entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _##id[i]; if (e) return e; } return null; } \
15         REGISTRY_CHECK(id)
16
17 /** registered item identifier */
18 .string registered_id;
19
20 /**
21  * Register a new entity with a global constructor.
22  * Must be followed by a semicolon or a function body with a `this` parameter.
23  * Wrapper macros may perform actions after user initialization like so:
24  *     #define REGISTER_FOO(id) \
25  *         REGISTER(RegisterFoos, FOO, FOOS, id, m_id, NEW(Foo)); \
26  *         REGISTER_INIT_POST(FOO, id) { \
27  *             print("Registering foo #", this.m_id + 1, "\n"); \
28  *         } \
29  *         REGISTER_INIT(FOO, id)
30  *
31  * Don't forget to forward declare `initfunc` and call `REGISTER_REGISTRY`:
32  *     void RegisterFoos();
33  *     REGISTER_REGISTRY(RegisterFoos)
34  *
35  * @param initfunc  The global constructor to accumulate into
36  * @param ns        Short for namespace, prefix for each global (ns##_##id)
37  * @param array     The array to add each entity to. Also requires `array##_first` and `array##_last` to be defined
38  * @param id        The identifier of the current entity being registered
39  * @param fld       The field to store the current count into
40  * @param inst      An expression to create a new instance, invoked for every registration
41  */
42 #define REGISTER(initfunc, ns, array, id, fld, inst)                \
43         entity ns##_##id;                                               \
44         REGISTER_INIT(ns, id) {}                                       \
45         REGISTER_INIT_POST(ns, id) {}                                  \
46         void Register_##ns##_##id() \
47         {                                   \
48                 if (array##_COUNT >= array##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(array##_MAX)); \
49                 entity this = inst;                                         \
50                 ns##_##id = this;                                           \
51                 this.registered_id = #id;                                   \
52                 this.fld = array##_COUNT;                                   \
53                 _##array[array##_COUNT++] = this;                           \
54                 if (!array##_first)    array##_first = this;                \
55                 if (array##_last)     array##_last.REGISTRY_NEXT = this;    \
56                 array##_last = this;                                        \
57                 Register_##ns##_##id##_init(this);                          \
58                 Register_##ns##_##id##_init_post(this);                     \
59         }                                                               \
60         ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id)             \
61         REGISTER_INIT(ns, id)
62
63 /** internal next pointer */
64 #define REGISTRY_NEXT enemy
65 .entity REGISTRY_NEXT;
66
67 #define REGISTRY_SORT(id, field, skip)                              \
68         void _REGISTRY_SWAP_##id(int i, int j, entity pass) \
69         {           \
70                 i += skip; j += skip;                                       \
71                                                                     \
72                 entity a = _##id[i], b = _##id[j];                          \
73                 _##id[i] = b;                                               \
74                 _##id[j] = a;                                               \
75                                                                     \
76                 entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT;  \
77                 a.REGISTRY_NEXT = b_next;                                   \
78                 b.REGISTRY_NEXT = a_next;                                   \
79                                                                     \
80                 if (i == 0) id##_first = b;                                 \
81                 else _##id[i - 1].REGISTRY_NEXT = b;                        \
82                                                                     \
83                 if (j == 0) id##_first = a;                                 \
84                 else _##id[j - 1].REGISTRY_NEXT = a;                        \
85         }                                                               \
86         float _REGISTRY_CMP_##id(int i, int j, entity pass) \
87         {           \
88                 i += skip; j += skip;                                       \
89                 string a = _##id[i].field;                                  \
90                 string b = _##id[j].field;                                  \
91                 return strcasecmp(a, b);                                    \
92         }                                                               \
93         STATIC_INIT(Registry_sort_##id) \
94         {                               \
95                 heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
96         }
97
98 #define REGISTRY_CHECK(id) \
99         STATIC_INIT_LATE(Registry_check_##id) \
100         { \
101                 string algo = "SHA256"; \
102                 string join = ":"; \
103                 string s = ""; \
104                 FOREACH(id, true, LAMBDA(s = strcat(s, join, it.registered_id))); \
105                 s = substring(s, strlen(join), -1); \
106                 LOG_TRACEF(#id ": %s\n[%s]\n", digest_hex(algo, s), s); \
107         }
108
109 #endif