X-Git-Url: http://de.git.xonotic.org/?p=xonotic%2Fxonotic-data.pk3dir.git;a=blobdiff_plain;f=qcsrc%2Flib%2Fregistry.qh;h=9d171e031ab4391090341737d675574a0154aca2;hp=52ab23377454bb92dfe75708ace67d428eac92e5;hb=b0a563c3658ec620ddb9037b9417b743a47002a5;hpb=7f64f637f43db3555fbe00e2424c68474de7bcc0 diff --git a/qcsrc/lib/registry.qh b/qcsrc/lib/registry.qh index 52ab23377..9d171e031 100644 --- a/qcsrc/lib/registry.qh +++ b/qcsrc/lib/registry.qh @@ -1,8 +1,19 @@ -#ifndef REGISTRY_H -#define REGISTRY_H +#pragma once #include "oo.qh" +#if 1 + #define _R_MAP(r, max) ArrayList r; STATIC_INIT(r) { AL_NEW(r, max, NULL, e); } + #define _R_GET(r, i) AL_gete(r, i) + #define _R_SET(r, i, e) AL_sete(r, i, e) + #define _R_DEL(r) AL_DELETE(r) +#else + #define _R_MAP(r, max) entity r[max] + #define _R_GET(r, i) r[i] + #define _R_SET(r, i, e) r[i] = e + #define _R_DEL(r) +#endif + /** * Declare a new registry. * @@ -10,11 +21,25 @@ * REGISTER_REGISTRY(Foos) */ #define REGISTRY(id, max) \ - void Register##id() {} \ + void Register##id(); \ + [[accumulate]] REGISTRY_BEGIN(id) {} \ + [[accumulate]] REGISTRY_END(id) {} \ + void _Register##id() {} \ + void Register##id() { REGISTRY_BEGIN_(id); _Register##id(); REGISTRY_END_(id); } \ const int id##_MAX = max; \ - noref entity _##id[id##_MAX], id##_first, id##_last; \ int id##_COUNT; \ - entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _##id[i]; if (e) return e; } return null; } + noref entity id##_first, id##_last; \ + _R_MAP(_##id, id##_MAX); \ + SHUTDOWN(id) { _R_DEL(_##id); } \ + 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; } + +/** 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)) @@ -26,8 +51,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) @@ -39,58 +63,74 @@ REGISTRY(Registries, BITS(8)) * @param fld The field to store the locally unique unique entity id * @param inst An expression to create a new instance, invoked for every registration */ -#define REGISTER(registry, ns, id, fld, inst) \ - entity ns##_##id; \ - REGISTER_INIT(ns, id) {} \ - REGISTER_INIT_POST(ns, id) {} \ - void Register_##ns##_##id() \ +#define REGISTER(...) EVAL_REGISTER(OVERLOAD_(REGISTER, __VA_ARGS__)) +#define EVAL_REGISTER(...) __VA_ARGS__ +#define REGISTER_5(registry, ns, id, fld, inst) REGISTER_4(registry, ns##_##id, fld, inst) +#define REGISTER_4(registry, id, fld, inst) \ + entity id; \ + REGISTER_INIT(id) {} \ + void Register_##id() \ { \ - if (registry##_COUNT >= registry##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(registry##_MAX)); \ - entity this = ns##_##id = inst; \ - this.registered_id = #id; \ - this.fld = registry##_COUNT; \ - _##registry[registry##_COUNT] = this; \ - ++registry##_COUNT; \ - if (!registry##_first) registry##_first = this; \ - if (registry##_last) registry##_last.REGISTRY_NEXT = this; \ - registry##_last = this; \ - Register_##ns##_##id##_init(this); \ - Register_##ns##_##id##_init_post(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); \ } \ - ACCUMULATE_FUNCTION(Register##registry, Register_##ns##_##id) \ - REGISTER_INIT(ns, id) + ACCUMULATE_FUNCTION(_Register##registry, Register_##id) \ + REGISTER_INIT(id) -#define REGISTER_INIT(ns, id) [[accumulate]] void Register_##ns##_##id##_init(entity this) -#define REGISTER_INIT_POST(ns, id) [[accumulate]] void Register_##ns##_##id##_init_post(entity this) +#define REGISTRY_PUSH(registry, fld, it) MACRO_BEGIN { \ + it.fld = registry##_COUNT; \ + _R_SET(_##registry, registry##_COUNT, it); \ + ++registry##_COUNT; \ + if (!registry##_first) registry##_first = it; \ + if (registry##_last) registry##_last.REGISTRY_NEXT = it; \ + registry##_last = it; \ +} MACRO_END + +#define REGISTRY_RESERVE(registry, fld, id, suffix) MACRO_BEGIN { \ + entity e = new_pure(registry_reserved); \ + e.registered_id = #id "/" #suffix; \ + REGISTRY_PUSH(registry, fld, e); \ +} MACRO_END + +#define REGISTER_INIT(id) [[accumulate]] void Register_##id##_init(entity this) /** internal next pointer */ #define REGISTRY_NEXT enemy .entity REGISTRY_NEXT; -#define REGISTRY_SORT(id, skip) \ +#define REGISTRY_SORT(...) EVAL_REGISTRY_SORT(OVERLOAD(REGISTRY_SORT, __VA_ARGS__)) +#define EVAL_REGISTRY_SORT(...) __VA_ARGS__ +#define REGISTRY_SORT_1(id) REGISTRY_SORT_2(id, 0) +#define REGISTRY_SORT_2(id, skip) \ void _REGISTRY_SWAP_##id(int i, int j, entity pass) \ { \ i += skip; j += skip; \ \ - entity a = _##id[i], b = _##id[j]; \ - _##id[i] = b; \ - _##id[j] = a; \ + entity a = _R_GET(_##id, i), b = _R_GET(_##id, j); \ + _R_SET(_##id, i, b); \ + _R_SET(_##id, j, a); \ \ entity a_next = a.REGISTRY_NEXT, b_next = b.REGISTRY_NEXT; \ a.REGISTRY_NEXT = b_next; \ b.REGISTRY_NEXT = a_next; \ \ if (i == 0) id##_first = b; \ - else _##id[i - 1].REGISTRY_NEXT = b; \ + else _R_GET(_##id, i - 1).REGISTRY_NEXT = b; \ \ if (j == 0) id##_first = a; \ - else _##id[j - 1].REGISTRY_NEXT = a; \ + else _R_GET(_##id, j - 1).REGISTRY_NEXT = a; \ } \ int _REGISTRY_CMP_##id(int i, int j, entity pass) \ { \ i += skip; j += skip; \ - string a = _##id[i].registered_id; \ - string b = _##id[j].registered_id; \ + string a = _R_GET(_##id, i).registered_id; \ + string b = _R_GET(_##id, j).registered_id; \ return strcmp(a, b); \ } \ STATIC_INIT(Registry_sort_##id) \ @@ -113,13 +153,14 @@ void Registry_send(string id, string hash); string REGISTRY_HASH(id); \ STATIC_INIT(Registry_check_##id) \ { \ - string algo = "SHA256"; \ + /* Note: SHA256 isn't always available, use MD4 instead */ \ + 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) \ { \ @@ -128,21 +169,23 @@ 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); \ } \ } \ } \ void Registry_send_all() { Registry_send(#id, REGISTRY_HASH(id)); } \ -#define REGISTER_REGISTRY(...) EVAL(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__)) +#define REGISTER_REGISTRY(...) EVAL_REGISTER_REGISTRY(OVERLOAD(REGISTER_REGISTRY, __VA_ARGS__)) +#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) \ 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)); - - -#endif + REGISTER(Registries, REGISTRY, id, m_id, NEW(id##Registry)); \ + METHOD(id##Registry, m_reload, void()) { \ + Register##id(); \ + }