コード例 #1
0
        public override void Serialize(ref T entityComponent, ArchiveMode mode, SerializationStream stream)
        {
            if (mode == ArchiveMode.Serialize)
            {
                stream.Write(entityComponent.Entity);
                stream.Write(CloneEntityComponentData.GenerateEntityComponentData(entityComponent));
            }
            else if (mode == ArchiveMode.Deserialize)
            {
                var entity = stream.Read <Entity>();

                var data = stream.Read <CloneEntityComponentData>();
                CloneEntityComponentData.RestoreEntityComponentData(entityComponent, data);
            }
        }
コード例 #2
0
        public static CloneEntityComponentData GenerateEntityComponentData(EntityComponent entityComponent)
        {
            var data = new CloneEntityComponentData {
                Properties = new List <EntityComponentProperty>()
            };

            foreach (var field in entityComponent.GetType().GetTypeInfo().DeclaredFields)
            {
                //if (!field.GetCustomAttributes(typeof(DataMemberConvertAttribute), true).Any())
                //    continue;

                data.Properties.Add(new EntityComponentProperty(EntityComponentPropertyType.Field, field.Name, field.GetValue(entityComponent)));
            }

            foreach (var property in entityComponent.GetType().GetTypeInfo().DeclaredProperties)
            {
                //if (!property.GetCustomAttributes(typeof(DataMemberConvertAttribute), true).Any())
                //    continue;

                data.Properties.Add(new EntityComponentProperty(EntityComponentPropertyType.Property, property.Name, property.GetValue(entityComponent, null)));
            }
            return(data);
        }
コード例 #3
0
        //public List<EntityComponentProperty> Properties;

        public static void RestoreEntityComponentData(EntityComponent entityComponent, CloneEntityComponentData data)
        {
            foreach (var componentProperty in data.Properties)
            {
                switch (componentProperty.Type)
                {
                case EntityComponentPropertyType.Field:
                {
                    var field = entityComponent.GetType().GetTypeInfo().GetDeclaredField(componentProperty.Name);
                    if (field == null)         // Field disappeared? should we issue a warning?
                    {
                        continue;
                    }
                    var result = MergeObject(field.GetValue(entityComponent), componentProperty.Value);
                    field.SetValue(entityComponent, result);
                }
                break;

                case EntityComponentPropertyType.Property:
                {
                    var property = entityComponent.GetType().GetTypeInfo().GetDeclaredProperty(componentProperty.Name);
                    if (property == null)         // Property disappeared? should we issue a warning?
                    {
                        continue;
                    }
                    var result = MergeObject(property.GetValue(entityComponent, null), componentProperty.Value);
                    if (property.CanWrite)
                    {
                        property.SetValue(entityComponent, result, null);
                    }
                }
                break;

                default:
                    throw new NotImplementedException();
                }
            }
        }