Ejemplo n.º 1
0
        public void Serialize(object obj, BinaryDataWriter writer, IBaseline <TKey> baseline)
        {
            IBinaryObjectCollection <TKey> collections = (IBinaryObjectCollection <TKey>)obj;

            List <TKey> baseKeys = new List <TKey>(baseline.BaselineKeys);

            foreach (TKey key in collections.Keys)
            {
                BinaryDataWriter itemWriter   = writer.TryWriteNode(_keySize);
                Baseline <byte>  itemBaseline = baseline.GetOrCreateBaseline <Baseline <byte> >(key, _itemSerializer.Count, out bool isNew);

                _itemSerializer.Serialize(collections[key], itemWriter, itemBaseline);
                if (itemWriter.Length > 0 || isNew)
                {
                    _writer.Write(writer, key);
                    itemWriter.PushNode();
                }

                baseKeys.Remove(key);
            }

            if (baseKeys.Count <= 0)
            {
                return;
            }

            _writer.Write(writer, _reservedKey);

            foreach (TKey key in baseKeys)
            {
                _writer.Write(writer, key);
                baseline.DestroyBaseline(key);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Serializes all entities matching the specified filter
        /// </summary>
        /// <param name="filter">Filter</param>
        public byte[] Serialize(EcsFilter filter)
        {
            IEcsGroup group = Filter(filter);

            byte[] data;
            using (BinaryDataWriter writer = new BinaryDataWriter())
            {
                foreach (IEcsArchetype archetype in group)
                {
                    byte[] indices = archetype.Indices;

                    for (int i = 0; i < archetype.EntitiesCount; i++)
                    {
                        IEcsEntity entity = archetype[i];

                        BinaryDataWriter entityWriter = writer.TryWriteNode(sizeof(uint));
                        foreach (byte index in indices)
                        {
                            CompositeBinarySerializer ser             = Serializer.GetSerializer(EcsTypeManager.Types[index]);
                            BinaryDataWriter          componentWriter = entityWriter.TryWriteNode(sizeof(byte));
                            ser.Serialize(archetype.GetComponentPool(index).Get(i), componentWriter);
                            entityWriter.WriteByte(index);
                            componentWriter.PushNode();
                        }

                        writer.WriteUInt(entity.Id);
                        entityWriter.PushNode();
                    }
                }

                data = writer.GetData();
            }

            return(data);
        }
Ejemplo n.º 3
0
        public void Serialize(object obj, BinaryDataWriter writer)
        {
            BinaryDataWriter childWriter = writer.TryWriteNode(sizeof(byte));

            _serializer.Serialize(_getter(obj), childWriter);

            if (childWriter.Length <= 0)
            {
                return;
            }

            WriteKey(_index, writer);
            childWriter.PushNode();
        }
Ejemplo n.º 4
0
        public void Serialize(object obj, BinaryDataWriter writer, IBaseline <TKey> baseline)
        {
            BinaryDataWriter     childWriter  = writer.TryWriteNode(sizeof(byte));
            Baseline <TChildKey> itemBaseline = baseline.GetOrCreateBaseline <Baseline <TChildKey> >(_index, _valuesCount, out bool isNew);

            _serializer.Serialize(_getter(obj), childWriter, itemBaseline);

            if (childWriter.Length <= 0 && !isNew)
            {
                return;
            }

            WriteKey(_index, writer);
            childWriter.PushNode();
        }
Ejemplo n.º 5
0
        public void Serialize(object obj, BinaryDataWriter writer)
        {
            IBinaryObjectCollection <TKey> collection = (IBinaryObjectCollection <TKey>)obj;

            foreach (TKey key in collection.Keys)
            {
                if (_reservedKey.Equals(key))
                {
                    throw new ArgumentException();
                }

                BinaryDataWriter itemWriter = writer.TryWriteNode(_keySize);
                _itemSerializer.Serialize(collection[key], itemWriter);
                _writer.Write(writer, key);
                itemWriter.PushNode();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Serializes all objects matching the specified filter. Regarding baseline
        /// </summary>
        /// <param name="filter">Filter</param>
        /// <param name="baseline">Baseline</param>
        public byte[] Serialize(EcsFilter filter, Baseline <uint> baseline)
        {
            IEcsGroup group = Filter(filter);

            byte[] data;
            using (BinaryDataWriter writer = new BinaryDataWriter())
            {
                List <uint> entitiesBaseKeys = new List <uint>(baseline.BaselineKeys);
                foreach (IEcsArchetype archetype in group)
                {
                    byte[] indices = archetype.Indices;
                    for (int i = 0; i < archetype.EntitiesCount; i++)
                    {
                        IEcsEntity entity = archetype[i];

                        uint entityId = entity.Id;

                        BinaryDataWriter entityWriter   = writer.TryWriteNode(sizeof(uint));
                        Baseline <byte>  entityBaseline = baseline.GetOrCreateBaseline <Baseline <byte> >(entityId, 0, out bool entIsNew);
                        List <byte>      entityBaseKeys = new List <byte>(entityBaseline.BaselineKeys);

                        foreach (byte index in indices)
                        {
                            CompositeBinarySerializer ser          = Serializer.GetSerializer(EcsTypeManager.Types[index]);
                            BinaryDataWriter          compWriter   = entityWriter.TryWriteNode(sizeof(byte));
                            Baseline <byte>           compBaseline = entityBaseline.GetOrCreateBaseline <Baseline <byte> >(index, ser.Count, out bool compIsNew);
                            ser.Serialize(archetype.GetComponentPool(index).Get(i), compWriter, compBaseline);

                            if (compWriter.Length > 0 || compIsNew)
                            {
                                entityWriter.WriteByte(index);
                                compWriter.PushNode();
                            }

                            entityBaseKeys.Remove(index);
                        }

                        if (entityBaseKeys.Count > 0)
                        {
                            entityWriter.WriteByte(byte.MaxValue);

                            foreach (byte key in entityBaseKeys)
                            {
                                entityWriter.WriteByte(key);
                                entityBaseline.DestroyBaseline(key);
                            }
                        }

                        if (entityWriter.Length > 0 || entIsNew)
                        {
                            writer.WriteUInt(entityId);
                            entityWriter.PushNode();
                        }

                        entitiesBaseKeys.Remove(entityId);
                    }
                }

                if (entitiesBaseKeys.Count > 0)
                {
                    writer.WriteUInt(uint.MaxValue);

                    foreach (uint key in entitiesBaseKeys)
                    {
                        writer.WriteUInt(key);
                        baseline.DestroyBaseline(key);
                    }
                }

                data = writer.GetData();
            }

            return(data);
        }