]> git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blobdiff - qcsrc/lib/registry.qh
Registry: fix incorrect sorting
[xonotic/xonotic-data.pk3dir.git] / qcsrc / lib / registry.qh
index 7ff94fbaae4acafa030259db706e0f07948dede1..eb8372f7638c980791a48179988f8f4f4f6deb3a 100644 (file)
@@ -3,6 +3,8 @@
 
 #include "oo.qh"
 
+#define REGISTER_REGISTRY(func) ACCUMULATE_FUNCTION(__static_init, func)
+
 #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)
 
@@ -11,8 +13,7 @@
        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; } \
-       REGISTRY_CHECK(id)
+       entity _##id##_from(int i, entity null) { if (i >= 0 && i < id##_COUNT) { entity e = _##id[i]; if (e) return e; } return null; }
 
 /** registered item identifier */
 .string registered_id;
  * @param fld       The field to store the current count into
  * @param inst      An expression to create a new instance, invoked for every registration
  */
-#define REGISTER(initfunc, ns, array, id, fld, inst)                \
-       entity ns##_##id;                                               \
-       REGISTER_INIT(ns, id) {}                                       \
-       REGISTER_INIT_POST(ns, id) {}                                  \
+#define REGISTER(initfunc, ns, array, id, fld, inst) \
+       entity ns##_##id; \
+       REGISTER_INIT(ns, id) {} \
+       REGISTER_INIT_POST(ns, id) {} \
        void Register_##ns##_##id() \
-       {                                   \
+       { \
                if (array##_COUNT >= array##_MAX) LOG_FATALF("Registry capacity exceeded (%s)", ftos(array##_MAX)); \
-               entity this = inst;                                         \
-               ns##_##id = this;                                           \
-               this.registered_id = #id;                                   \
-               this.fld = array##_COUNT;                                   \
-               _##array[array##_COUNT++] = this;                           \
-               if (!array##_first)    array##_first = this;                \
-               if (array##_last)     array##_last.REGISTRY_NEXT = this;    \
-               array##_last = this;                                        \
-               Register_##ns##_##id##_init(this);                          \
-               Register_##ns##_##id##_init_post(this);                     \
-       }                                                               \
-       ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id)             \
+               entity this = inst; \
+               ns##_##id = this; \
+               this.registered_id = #id; \
+               this.fld = array##_COUNT; \
+               _##array[array##_COUNT++] = this; \
+               if (!array##_first) array##_first = this; \
+               if (array##_last)   array##_last.REGISTRY_NEXT = this; \
+               array##_last = this; \
+               Register_##ns##_##id##_init(this); \
+               Register_##ns##_##id##_init_post(this); \
+       } \
+       ACCUMULATE_FUNCTION(initfunc, Register_##ns##_##id) \
        REGISTER_INIT(ns, id)
 
 /** internal next pointer */
 #define REGISTRY_NEXT enemy
 .entity REGISTRY_NEXT;
 
-#define REGISTRY_SORT(id, field, skip)                              \
+#define REGISTRY_SORT(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_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;                        \
-                                                                    \
-               if (j == 0) id##_first = a;                                 \
-               else _##id[j - 1].REGISTRY_NEXT = a;                        \
-       }                                                               \
-       float _REGISTRY_CMP_##id(int i, int j, entity pass) \
-       {           \
-               i += skip; j += skip;                                       \
-               string a = _##id[i].field;                                  \
-               string b = _##id[j].field;                                  \
-               return strcasecmp(a, b);                                    \
-       }                                                               \
+       { \
+               i += skip; j += skip; \
+               \
+               entity a = _##id[i], b = _##id[j]; \
+               _##id[i] = b; \
+               _##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; \
+        \
+               if (j == 0) id##_first = a; \
+               else _##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; \
+               return strcmp(a, b); \
+       } \
        STATIC_INIT(Registry_sort_##id) \
-       {                               \
+       { \
                heapsort(id##_COUNT - (skip), _REGISTRY_SWAP_##id, _REGISTRY_CMP_##id, NULL); \
-       }
+       } \
+       REGISTRY_CHECK(id)
 
 #define REGISTRY_CHECK(id) \
-       STATIC_INIT_LATE(Registry_check_##id) \
+       STATIC_INIT(Registry_check_##id) \
        { \
                string algo = "SHA256"; \
                string join = ":"; \