예제 #1
0
        public void OnDeserializeAll(NetworkReader reader)
        {
            // This list can now only be modified by synchronization
            IsReadOnly = true;

            // if init,  write the full list content
            int count = (int)reader.ReadUInt32();

            objects.Clear();
            changes.Clear();

            for (int i = 0; i < count; i++)
            {
                T obj = reader.Read <T>();
                objects.Add(obj);
            }

            // We will need to skip all these changes
            // the next time the list is synchronized
            // because they have already been applied
            changesAhead = (int)reader.ReadUInt32();
        }
예제 #2
0
        public void OnDeserializeDelta(NetworkReader reader)
        {
            // This list can now only be modified by synchronization
            IsReadOnly = true;

            int changesCount = (int)reader.ReadPackedUInt32();

            for (int i = 0; i < changesCount; i++)
            {
                Operation operation = (Operation)reader.ReadByte();

                // apply the operation only if it is a new change
                // that we have not applied yet
                bool apply   = changesAhead == 0;
                int  index   = 0;
                T    oldItem = default;
                T    newItem = default;

                switch (operation)
                {
                case Operation.OP_ADD:
                    newItem = reader.Read <T>();
                    if (apply)
                    {
                        index = objects.Count;
                        objects.Add(newItem);
                    }
                    break;

                case Operation.OP_CLEAR:
                    if (apply)
                    {
                        objects.Clear();
                    }
                    break;

                case Operation.OP_INSERT:
                    index   = (int)reader.ReadPackedUInt32();
                    newItem = reader.Read <T>();
                    if (apply)
                    {
                        objects.Insert(index, newItem);
                    }
                    break;

                case Operation.OP_REMOVEAT:
                    index = (int)reader.ReadPackedUInt32();
                    if (apply)
                    {
                        oldItem = objects[index];
                        objects.RemoveAt(index);
                    }
                    break;

                case Operation.OP_SET:
                    index   = (int)reader.ReadPackedUInt32();
                    newItem = reader.Read <T>();
                    if (apply)
                    {
                        oldItem        = objects[index];
                        objects[index] = newItem;
                    }
                    break;
                }

                if (apply)
                {
                    Callback?.Invoke(operation, index, oldItem, newItem);
                }
                // we just skipped this change
                else
                {
                    changesAhead--;
                }
            }
        }
예제 #3
0
            void Callback(NetworkReader reader)
            {
                T result = reader.Read <T>();

                completionSource.TrySetResult(result);
            }
예제 #4
0
        public void OnDeserializeDelta(NetworkReader reader)
        {
            // This list can now only be modified by synchronization
            IsReadOnly = true;
            bool raiseOnChange = false;

            int changesCount = (int)reader.ReadPackedUInt32();

            for (int i = 0; i < changesCount; i++)
            {
                var operation = (Operation)reader.ReadByte();

                // apply the operation only if it is a new change
                // that we have not applied yet
                bool   apply   = changesAhead == 0;
                TKey   key     = default;
                TValue item    = default;
                TValue oldItem = default;

                switch (operation)
                {
                case Operation.OP_ADD:
                    key  = reader.Read <TKey>();
                    item = reader.Read <TValue>();
                    if (apply)
                    {
                        objects[key] = item;
                    }
                    break;

                case Operation.OP_SET:
                    key  = reader.Read <TKey>();
                    item = reader.Read <TValue>();
                    if (apply)
                    {
                        oldItem      = objects[key];
                        objects[key] = item;
                    }
                    break;

                case Operation.OP_CLEAR:
                    if (apply)
                    {
                        objects.Clear();
                    }
                    break;

                case Operation.OP_REMOVE:
                    key  = reader.Read <TKey>();
                    item = reader.Read <TValue>();
                    if (apply)
                    {
                        objects.Remove(key);
                    }
                    break;
                }

                if (apply)
                {
                    RaiseEvents(operation, key, item, oldItem);
                    raiseOnChange = true;
                }
                // we just skipped this change
                else
                {
                    changesAhead--;
                }
            }

            if (raiseOnChange)
            {
                OnChange?.Invoke();
            }
        }