X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=qcsrc%2Flib%2Fregistry.qh;h=2d41e5d431b3ba9c7b63ccbb87268a4b96615267;hb=05776c4204bf73c8ebc84e615ad088af45ebe5ed;hp=490ffd30316c38784c2ae3cce0f8d4e4c836527f;hpb=7bcb3a89b3271e018da4d92437dc5ba125ea8698;p=xonotic%2Fxonotic-data.pk3dir.git diff --git a/qcsrc/lib/registry.qh b/qcsrc/lib/registry.qh index 490ffd303..2d41e5d43 100644 --- a/qcsrc/lib/registry.qh +++ b/qcsrc/lib/registry.qh @@ -21,14 +21,32 @@ * REGISTER_REGISTRY(Foos) */ #define REGISTRY(id, max) \ - void Register##id() {} \ + void Register##id(); \ + [[accumulate]] void REGISTRY_DEPENDS_(id) {} \ + [[accumulate]] REGISTRY_BEGIN(id) {} \ + [[accumulate]] REGISTRY_END(id) {} \ + void _Register##id() {} \ + int id##_state = 0; \ + void Register##id() { if (id##_state) return; id##_state = 1; REGISTRY_DEPENDS_(id); REGISTRY_BEGIN_(id); _Register##id(); id##_state = 2; REGISTRY_END_(id); } \ const int id##_MAX = max; \ + int id##_COUNT; \ noref entity id##_first, id##_last; \ _R_MAP(_##id, id##_MAX); \ SHUTDOWN(id) { _R_DEL(_##id); } \ - int id##_COUNT; \ 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; } +/** Add registry dependencies to a registry */ +#define REGISTRY_DEPENDS(id, dep) void Register##dep(); void REGISTRY_DEPENDS_(id) { Register##dep(); } +#define REGISTRY_DEPENDS_(id) Register##id##_Depends() + +/** Called before initializing a registry. */ +#define REGISTRY_BEGIN(id) [[accumulate]] void REGISTRY_BEGIN_(id) { noref void() f = Register##id; } void REGISTRY_BEGIN_(id) +#define REGISTRY_BEGIN_(id) Register##id##_First() + +/** Called after initializing a registry. */ +#define REGISTRY_END(id) [[accumulate]] void REGISTRY_END_(id) { noref void() f = Register##id; } void REGISTRY_END_(id) +#define REGISTRY_END_(id) Register##id##_Done() + REGISTRY(Registries, BITS(8)) /** registered item identifier */ @@ -39,8 +57,7 @@ REGISTRY(Registries, BITS(8)) * Must be followed by a semicolon or a function body with a `this` parameter. * Wrapper macros may perform actions after user initialization like so: * #define REGISTER_FOO(id) \ - * REGISTER(Foos, FOO, id, m_id, NEW(Foo)); \ - * REGISTER_INIT_POST(FOO, id) { \ + * REGISTER(Foos, FOO, id, m_id, NEW(Foo)) { \ * print("Registering foo #", this.m_id + 1, "\n"); \ * } \ * REGISTER_INIT(FOO, id) @@ -58,17 +75,18 @@ REGISTRY(Registries, BITS(8)) #define REGISTER_4(registry, id, fld, inst) \ entity id; \ REGISTER_INIT(id) {} \ - REGISTER_INIT_POST(id) {} \ void Register_##id() \ { \ - if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%d)", registry##_MAX); \ - entity this = id = inst; \ - this.registered_id = #id; \ - REGISTRY_PUSH(registry, fld, this); \ + entity this = id; \ + if (this == NULL) { \ + if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%d)", registry##_MAX); \ + this = id = inst; \ + this.registered_id = #id; \ + REGISTRY_PUSH(registry, fld, this); \ + } \ Register_##id##_init(this); \ - Register_##id##_init_post(this); \ } \ - ACCUMULATE_FUNCTION(Register##registry, Register_##id) \ + ACCUMULATE_FUNCTION(_Register##registry, Register_##id) \ REGISTER_INIT(id) #define REGISTRY_PUSH(registry, fld, it) MACRO_BEGIN { \ @@ -87,7 +105,6 @@ REGISTRY(Registries, BITS(8)) } MACRO_END #define REGISTER_INIT(id) [[accumulate]] void Register_##id##_init(entity this) -#define REGISTER_INIT_POST(id) [[accumulate]] void Register_##id##_init_post(entity this) /** internal next pointer */ #define REGISTRY_NEXT enemy @@ -129,7 +146,9 @@ REGISTRY(Registries, BITS(8)) #define REGISTRY_HASH(id) Registry_hash_##id +ERASEABLE [[accumulate]] void Registry_check(string r, string server) { } +ERASEABLE [[accumulate]] void Registry_send_all() { } #ifdef SVQC @@ -146,10 +165,10 @@ void Registry_send(string id, string hash); string algo = "MD4"; \ string join = ":"; \ string s = ""; \ - FOREACH(id, true, LAMBDA(s = strcat(s, join, it.registered_id))); \ + FOREACH(id, true, s = strcat(s, join, it.registered_id)); \ s = substring(s, strlen(join), -1); \ string h = REGISTRY_HASH(id) = strzone(digest_hex(algo, s)); \ - LOG_TRACEF(#id ": %s\n[%s]\n", h, s); \ + LOG_DEBUGF(#id ": %s\n[%s]", h, s); \ } \ void Registry_check(string r, string sv) \ { \ @@ -158,7 +177,7 @@ void Registry_send(string id, string hash); string cl = REGISTRY_HASH(id); \ if (cl != sv) \ { \ - LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s\n", r, cl, sv); \ + LOG_FATALF("client/server mismatch (%s).\nCL: %s\nSV: %s", r, cl, sv); \ } \ } \ } \ @@ -168,9 +187,14 @@ void Registry_send(string id, string hash); #define EVAL_REGISTER_REGISTRY(...) __VA_ARGS__ #define REGISTER_REGISTRY_1(id) REGISTER_REGISTRY_2(id, #id) #define REGISTER_REGISTRY_2(id, str) \ - ACCUMULATE_FUNCTION(__static_init, Register##id) \ + ACCUMULATE_FUNCTION(__static_init_1, Register##id) \ CLASS(id##Registry, Object) \ - ATTRIB(id##Registry, m_name, string, str) \ - ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first) \ + ATTRIB(id##Registry, m_name, string, str); \ + ATTRIB(id##Registry, REGISTRY_NEXT, entity, id##_first); \ + METHOD(id##Registry, m_reload, void()); \ ENDCLASS(id##Registry) \ - REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry)); + REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry)); \ + METHOD(id##Registry, m_reload, void()) { \ + id##_state = 0; \ + Register##id(); \ + }