Пример #1
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);
        }