egametang/ET · error · SerializationException

Serialization_MismatchedCount

Error message

Serialization_MismatchedCount

What it means

Thrown at the end of OnDeserialization after all items are re-Added: the live count does not match CountName from the stream. Because SortedSet deduplicates via Add, a mismatch means duplicate items were stored (impossible for a normal set) or the stream's count is wrong. Surfaces as a SerializationException (Serialization_MismatchedCount).

Source

Thrown at Packages/cn.etetet.core/Scripts/Core/Share/Collection/SortedSet.cs:1735

            if (savedCount != 0)
            {
                T[] items = (T[])siInfo.GetValue(ItemsName, typeof(T[]));

                if (items == null)
                {
                    throw new SerializationException(SR.Serialization_MissingValues);
                }

                for (int i = 0; i < items.Length; i++)
                {
                    Add(items[i]);
                }
            }

            version = siInfo.GetInt32(VersionName);
            if (count != savedCount)
            {
                throw new SerializationException(SR.Serialization_MismatchedCount);
            }

            siInfo = null;
        }

        #endregion

        #region Helper classes

        public sealed class Node
        {
            public Node(T item, NodeColor color)
            {
                Item = item;
                Color = color;
            }

            private Node()

View on GitHub (pinned to 5cab01f7a8)

Solutions

  1. Regenerate the persisted data from a canonical SortedSet so Count and Items stay consistent.
  2. Verify the comparer used at deserialize time matches the one used at serialize time.
  3. If you must accept untrusted streams, deserialize into a List and rebuild the set rather than trusting the stored count.
Defensive patterns

Strategy: validation

Validate before calling

// When authoring serialization, ensure Items has no duplicates and Count == distinct count:
T[] arr = set.ToArray();
info.AddValue(CountName, arr.Length);
info.AddValue(ItemsName, arr);

Try / catch

try { /* deserialize */ } catch (SerializationException ex) when (ex.Message.Contains("Serialization_MismatchedCount")) { /* rebuild set from raw items, ignoring stored count */ }

Prevention

When it happens

Trigger: Serialized Items array contained duplicates and the comparer collapsed them (so count shrank); serialized Count was hand-set wrong; a non-strict comparer that treats distinct references as equal during rehydration.

Common situations: Data written by a buggy serializer that included duplicates; switching to an IEqualityComparer/IComparer that merges previously-distinct keys; partial re-serialization that updated Items but not Count.

Related errors


AI-assisted analysis of egametang/ET@5cab01f7a8 (2026-08-13). Data as JSON: /api/errors/f21091b0bdb21b89. Report an issue: GitHub.