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> /// 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(); } }
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> /// Removes the given backup instance. /// </summary> public static void RemoveBackup(fiSerializedObject serializedObj) { fiSceneManager.Storage.Objects.Remove(serializedObj); if (fiPrefabManager.Storage.Objects.Remove(serializedObj)) { fiPrefabManager.SetDirty(); } }
/// <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> /// Attempts to migrate prefab storage into scene storage. /// </summary> public static void MigrateStorage() { // We cannot migrate data while playing -- scene storage will not be // persisted. if (Application.isPlaying) { return; } // Nothing to migrate. if (fiPrefabManager.Storage.Objects.Count == 0) { return; } // Migrate everything except prefabs into storage. int i = 0; while (i < fiPrefabManager.Storage.Objects.Count) { fiSerializedObject obj = fiPrefabManager.Storage.Objects[i]; // If the target object is persistent (ie, not from the scene or // a temporary object), then we want to keep it in the prefab // storage. if (AssetDatabase.Contains(obj.Target.Target)) { ++i; } // This appears to be a scene object, so migrate it to scene // storage. else { fiSceneManager.Storage.Objects.Add(obj); fiPrefabManager.Storage.Objects.RemoveAt(i); fiPrefabManager.SetDirty(); } } }
/// <summary> /// Creates a new backup of the given component. Only guaranteed to work for types that /// derive from CommonBaseBehavior, but there is a good chance it'll work for most/all /// types derived from Component. /// </summary> public static void CreateBackup(Component behavior) { fiSerializedObject serialized = Serialize(behavior); // serialization failed if (serialized == null) { return; } if (PrefabUtility.GetPrefabType(behavior) == PrefabType.Prefab) { fiPrefabManager.Storage.Objects.Add(serialized); EditorGUIUtility.PingObject(fiPrefabManager.Storage); } else { fiStorageManager.PersistentStorage.Objects.Add(serialized); EditorGUIUtility.PingObject(fiStorageManager.PersistentStorage); } fiPrefabManager.SetDirty(); }
/// <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; }