コード例 #1
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);
        }
コード例 #2
0
ファイル: BinarySerializer.cs プロジェクト: voledyhil/MiniEcs
 public static byte[] Serialize(object obj)
 {
     using (BinaryDataWriter writer = new BinaryDataWriter())
     {
         GetSerializer(obj.GetType()).Serialize(obj, writer);
         return(writer.GetData());
     }
 }
コード例 #3
0
ファイル: BinarySerializer.cs プロジェクト: voledyhil/MiniEcs
        public static byte[] Serialize(object obj, Baseline <byte> baseline)
        {
            CompositeBinarySerializer serializer = GetSerializer(obj.GetType());

            if (!baseline.HasValues)
            {
                baseline.CreateValues(serializer.Count);
            }

            using (BinaryDataWriter writer = new BinaryDataWriter())
            {
                serializer.Serialize(obj, writer, baseline);
                return(writer.GetData());
            }
        }
コード例 #4
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);
        }