Example #1
0
        private static ushort GetFreeIndex(SerializationInfo info)
        {
            HashSet <ushort> indexes = new HashSet <ushort>();

            foreach (SerializationEntry entry in info)
            {
                Match m = IDRegex.Match(entry.Name);
                if (!m.Success)
                {
                    continue;
                }

                if (ushort.TryParse(m.Groups[IndexGroup].Value, out ushort result))
                {
                    indexes.Add(result);
                }
            }

            for (ushort index = 0; index <= ushort.MaxValue; index++)
            {
                if (!indexes.Contains(index))
                {
                    return(index);
                }
            }

            throw new InvalidOperationException();
        }
Example #2
0
        private static void GetId(SerializationInfo info, out ulong id, out ushort index, out bool reference)
        {
            HashSet <ushort> indexes    = new HashSet <ushort>();
            HashSet <ushort> ref_values = new HashSet <ushort>();

            foreach (SerializationEntry entry in info)
            {
                Match m = IDRegex.Match(entry.Name);
                if (!m.Success)
                {
                    continue;
                }

                if (ushort.TryParse(m.Groups[IndexGroup].Value, out ushort result))
                {
                    if (m.Groups[RefGroup].Success)
                    {
                        ref_values.Add(result);
                    }
                    else
                    {
                        indexes.Add(result);
                    }
                }
            }

            id        = 1;
            reference = false;

            for (index = 0; index <= ushort.MaxValue; index++)
            {
                if (!indexes.Contains(index))
                {
                    break;
                }

                if (index == ushort.MaxValue)
                {
                    throw new InvalidOperationException();
                }
            }

            for (; index >= 0; index--)
            {
                try
                {
                    if (!ref_values.Contains(index))
                    {
                        if (index == 0)
                        {
                            break;
                        }
                        continue;
                    }
                    id        = info.GetUInt64(IDPrefix + index);
                    reference = info.GetBoolean(IDPrefix + index + RefSuffix);
                }
                catch (InvalidCastException)
                {
                    id = 1;
                    if (index == 0)
                    {
                        break; // Don't run third addition
                    }
                }
            }
        }