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 }); } } }
/// <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); }
/// <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(); } }