An ISerializationOperator that uses a list for reference storage and targets fiPersistentObjects instead of regular UnityObject references.
Inheritance: ISerializationOperator
コード例 #1
0
        public fiDeserializedObject(fiSerializedObject serializedState)
        {
            Type targetType = serializedState.Target.Target.GetType();

            var serializationOperator = new fiSerializationOperator()
            {
                SerializedObjects = serializedState.ObjectReferences
            };

            // Fetch the serializer that the target uses
            Type serializerType = BehaviorTypeToSerializerTypeMap.GetSerializerType(targetType);
            var  serializer     = (BaseSerializer)fiSingletons.Get(serializerType);

            var inspectedType = InspectedType.Get(targetType);

            Members = new List <fiDeserializedMember>();

            foreach (fiSerializedMember member in serializedState.Members)
            {
                InspectedProperty property = inspectedType.GetPropertyByName(member.Name);
                if (property != null)
                {
                    object deserialized = serializer.Deserialize(
                        fsPortableReflection.AsMemberInfo(property.StorageType), member.Value, serializationOperator);
                    Members.Add(new fiDeserializedMember()
                    {
                        InspectedProperty = property,
                        Value             = deserialized,
                        ShouldRestore     = member.ShouldRestore
                    });
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Restores a backup that was previously created.
        /// </summary>
        public static void RestoreBackup(fiSerializedObject serializedState)
        {
            Type targetType = serializedState.Target.Target.GetType();
            var inspectedType = InspectedType.Get(targetType);

            var serializationOperator = new fiSerializationOperator() {
                SerializedObjects = serializedState.ObjectReferences
            };

            // Fetch the serializer that the target uses
            Type serializerType = BehaviorTypeToSerializerTypeMap.GetSerializerType(targetType);
            var serializer = (BaseSerializer)fiSingletons.Get(serializerType);

            foreach (fiSerializedMember member in serializedState.Members) {
                // user requested a skip for restoring this property
                if (member.ShouldRestore.Enabled == false) {
                    continue;
                }

                InspectedProperty property = inspectedType.GetPropertyByName(member.Name);
                if (property != null) {
                    Type storageType = property.StorageType;
                    object restoredValue = serializer.Deserialize(storageType, member.Value, serializationOperator);
                    property.Write(serializedState.Target.Target, restoredValue);
                }
            }

            if (serializedState.Target.Target is ISerializedObject) {
                var serializedObj = ((ISerializedObject)serializedState.Target.Target);
                serializedObj.SaveState();
                serializedObj.RestoreState();
            }
        }
コード例 #3
0
        public fiDeserializedObject(fiSerializedObject serializedState)
        {
            Type targetType = serializedState.Target.Target.GetType();

            var serializationOperator = new fiSerializationOperator() {
                SerializedObjects = serializedState.ObjectReferences
            };

            // Fetch the serializer that the target uses
            Type serializerType = BehaviorTypeToSerializerTypeMap.GetSerializerType(targetType);
            var serializer = (BaseSerializer)fiSingletons.Get(serializerType);

            var inspectedType = InspectedType.Get(targetType);

            Members = new List<fiDeserializedMember>();

            foreach (fiSerializedMember member in serializedState.Members) {
                InspectedProperty property = inspectedType.GetPropertyByName(member.Name);
                if (property != null) {
                    object deserialized = serializer.Deserialize(
                        fsPortableReflection.AsMemberInfo(property.StorageType), member.Value, serializationOperator);
                    Members.Add(new fiDeserializedMember() {
                        InspectedProperty = property,
                        Value = deserialized,
                        ShouldRestore = member.ShouldRestore
                    });
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Creates a serialized object from the given component.
        /// </summary>
        private static fiSerializedObject Serialize(Component target)
        {
            // Fetch the serializer that the target uses
            Type serializerType = BehaviorTypeToSerializerTypeMap.GetSerializerType(target.GetType());

            if (serializerType == typeof(NullSerializer))
            {
                Debug.LogError("Backups are not supported for NullSerializer types");
                return(null);
            }

            var serializer = (BaseSerializer)fiSingletons.Get(serializerType);

            // Save the current state
            if (target is ISerializedObject)
            {
                ((ISerializedObject)target).SaveState();
            }

            // Create our backup storage
            fiSerializedObject state = new fiSerializedObject()
            {
                Target  = new fiUnityObjectReference(target),
                SavedAt = DateTime.Now.ToString()
            };

            var serializationOperator = new fiSerializationOperator()
            {
                SerializedObjects = state.ObjectReferences
            };

            // note: we use InspectedProperties, *not* SerializedProperties, because we want to
            //       backup every field
            var properties = InspectedType.Get(target.GetType()).GetProperties(InspectedMemberFilters.InspectableMembers);

            foreach (InspectedProperty property in properties)
            {
                if (ShouldIgnoreForPersist(property))
                {
                    continue;
                }

                Type   storageType  = property.StorageType;
                object currentValue = property.Read(target);

                string serialized = serializer.Serialize(storageType, currentValue, serializationOperator);

                state.Members.Add(new fiSerializedMember()
                {
                    Name  = property.Name,
                    Value = serialized
                });
            }

            return(state);
        }
コード例 #5
0
        /// <summary>
        /// Restores a backup that was previously created.
        /// </summary>
        public static void RestoreBackup(fiSerializedObject serializedState)
        {
            Type targetType    = serializedState.Target.Target.GetType();
            var  inspectedType = InspectedType.Get(targetType);

            var serializationOperator = new fiSerializationOperator()
            {
                SerializedObjects = serializedState.ObjectReferences
            };

            // Fetch the serializer that the target uses
            Type serializerType = BehaviorTypeToSerializerTypeMap.GetSerializerType(targetType);
            var  serializer     = (BaseSerializer)fiSingletons.Get(serializerType);

            foreach (fiSerializedMember member in serializedState.Members)
            {
                // user requested a skip for restoring this property
                if (member.ShouldRestore.Enabled == false)
                {
                    continue;
                }

                InspectedProperty property = inspectedType.GetPropertyByName(member.Name);
                if (property != null)
                {
                    Type   storageType   = property.StorageType;
                    object restoredValue = serializer.Deserialize(storageType, member.Value, serializationOperator);
                    property.Write(serializedState.Target.Target, restoredValue);
                }
            }

            if (serializedState.Target.Target is ISerializedObject)
            {
                var serializedObj = ((ISerializedObject)serializedState.Target.Target);
                serializedObj.SaveState();
                serializedObj.RestoreState();
            }
        }
コード例 #6
0
        /// <summary>
        /// Creates a serialized object from the given component.
        /// </summary>
        private static fiSerializedObject Serialize(Component target)
        {
            // Fetch the serializer that the target uses
            Type serializerType = BehaviorTypeToSerializerTypeMap.GetSerializerType(target.GetType());
            var serializer = (BaseSerializer)fiSingletons.Get(serializerType);

            // Save the current state
            if (target is ISerializedObject) {
                ((ISerializedObject)target).SaveState();
            }

            // Create our backup storage
            fiSerializedObject state = new fiSerializedObject() {
                Target = new fiUnityObjectReference(target),
                SavedAt = DateTime.Now.ToString()
            };

            var serializationOperator = new fiSerializationOperator() {
                SerializedObjects = state.ObjectReferences
            };

            // note: we use InspectedProperties, *not* SerializedProperties, because we want to
            //       backup every field
            var properties = InspectedType.Get(target.GetType()).GetProperties(InspectedMemberFilters.InspectableMembers);
            foreach (InspectedProperty property in properties) {
                if (ShouldIgnoreForPersist(property)) continue;

                Type storageType = property.StorageType;
                object currentValue = property.Read(target);

                string serialized = serializer.Serialize(storageType, currentValue, serializationOperator);

                state.Members.Add(new fiSerializedMember() {
                    Name = property.Name,
                    Value = serialized
                });
            }

            return state;
        }