]> git.rm.cloudns.org Git - xonotic/xonotic-data.pk3dir.git/commitdiff
Inventory: check at runtime if the Inventory_groups_* costants are correct, automatic...
authorterencehill <piuntn@gmail.com>
Fri, 29 May 2020 19:58:31 +0000 (21:58 +0200)
committerterencehill <piuntn@gmail.com>
Fri, 29 May 2020 19:58:31 +0000 (21:58 +0200)
qcsrc/common/items/inventory.qh
qcsrc/lib/net.qh

index 5109628e8ff4e930353748e418f758e832e76b39..ca53f718d1ae2462f780dfd262f1d26b347e2989 100644 (file)
@@ -15,9 +15,23 @@ ENDCLASS(Inventory)
 
 REGISTER_NET_LINKED(ENT_CLIENT_INVENTORY)
 
-const int Inventory_groups_minor = 8; // exactly 1 byte
-const int Inventory_groups_major = 4; // ceil(REGISTRY_MAX(Items) / Inventory_groups_minor)
+const int Inventory_groups_minor = 8; // must be a multiple of 8 (one byte) to optimize bandwidth usage
+const int Inventory_groups_major = 4; // must be >= ceil(REGISTRY_COUNT(Items) / Inventory_groups_minor)
+#endif
 
+// no need to perform these checks on both server and client
+#ifdef CSQC
+STATIC_INIT(Inventory)
+{
+       if (Inventory_groups_minor / 8 != floor(Inventory_groups_minor / 8))
+               error("Inventory_groups_minor is not a multiple of 8.");
+       int min_major_value = ceil(REGISTRY_COUNT(Items) / Inventory_groups_minor);
+       if (Inventory_groups_major < min_major_value)
+               error(sprintf("Inventory_groups_major can not be < %d.", min_major_value));
+}
+#endif
+
+#ifdef SVQC
 #define G_MAJOR(id) (floor((id) / Inventory_groups_minor))
 #define G_MINOR(id) ((id) % Inventory_groups_minor)
 #endif
@@ -28,12 +42,12 @@ NET_HANDLE(ENT_CLIENT_INVENTORY, bool isnew)
 {
     make_pure(this);
     g_inventory = this;
-    const int majorBits = ReadByte();
+    const int majorBits = Readbits(Inventory_groups_major);
     for (int i = 0; i < Inventory_groups_major; ++i) {
         if (!(majorBits & BIT(i))) {
             continue;
         }
-        const int minorBits = ReadByte();
+        const int minorBits = Readbits(Inventory_groups_minor);
         for (int j = 0; j < Inventory_groups_minor; ++j) {
             if (!(minorBits & BIT(j))) {
                 continue;
@@ -72,15 +86,15 @@ void Inventory_Write(Inventory data)
                        minorBitsArr[maj] = BITSET(minorBitsArr[maj], BIT(G_MINOR(it.m_id)), true);
         }
     });
-    WriteByte(MSG_ENTITY, majorBits);
 
+       Writebits(MSG_ENTITY, majorBits, Inventory_groups_major);
        for (int i = 0; i < Inventory_groups_major; ++i)
        {
                if (!(majorBits & BIT(i)))
                        continue;
 
                const int minorBits = minorBitsArr[i];
-               WriteByte(MSG_ENTITY, minorBits);
+               Writebits(MSG_ENTITY, minorBits, Inventory_groups_minor);
                for (int j = 0; j < Inventory_groups_minor; ++j)
                {
                        if (!(minorBits & BIT(j)))
index 71950fb73cb67120d632a4d70ed0fd4bb60f276a..3cec9fd9ba8734a9ed1b9ff52014a05a5787e821 100644 (file)
@@ -358,6 +358,13 @@ MACRO_END
                #define ReadVector() vec3(ReadFloat(), ReadFloat(), ReadFloat())
                #define ReadVector2D() vec2(ReadFloat(), ReadFloat())
 
+               int Readbits(int num)
+               {
+                       if (num > 16) return ReadInt24_t();
+                       if (num > 8) return ReadShort();
+                       return ReadByte();
+               }
+
                float ReadApproxPastTime()
                {
                        float dt = ReadByte();
@@ -391,6 +398,13 @@ MACRO_END
                #define WriteVector(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); WriteFloat(to, v.z); MACRO_END
                #define WriteVector2D(to, v) MACRO_BEGIN WriteFloat(to, v.x); WriteFloat(to, v.y); MACRO_END
 
+               void Writebits(float dst, float val, int num)
+               {
+                       if (num > 16) { WriteInt24_t(dst, val); return; }
+                       if (num > 8) { WriteShort(dst, val); return; }
+                       WriteByte(dst, val);
+               }
+
                // this will use the value:
                //   128
                // accuracy near zero is APPROXPASTTIME_MAX/(256*255)