using System; #if !NO_UNITY using UnityEngine; #endif #if !UNITY_EDITOR && UNITY_WSA // For System.Reflection.TypeExtensions using System.Reflection; #endif namespace FullSerializer { /// /// Extend this interface on your type to receive notifications about serialization/deserialization events. If you don't /// have access to the type itself, then you can write an fsObjectProcessor instead. /// public interface fsISerializationCallbacks { /// /// Called before serialization. /// void OnBeforeSerialize(Type storageType); /// /// Called after serialization. /// /// The field/property type that is storing the instance. /// The data that was serialized. void OnAfterSerialize(Type storageType, ref fsData data); /// /// Called before deserialization. /// /// The field/property type that is storing the instance. /// The data that will be used for deserialization. void OnBeforeDeserialize(Type storageType, ref fsData data); /// /// Called after deserialization. /// /// The field/property type that is storing the instance. /// The type of the instance. void OnAfterDeserialize(Type storageType); } } namespace FullSerializer.Internal { public class fsSerializationCallbackProcessor : fsObjectProcessor { public override bool CanProcess(Type type) { return typeof(fsISerializationCallbacks).IsAssignableFrom(type); } public override void OnBeforeSerialize(Type storageType, object instance) { // Don't call the callback on null instances. if(instance == null) return; ((fsISerializationCallbacks)instance).OnBeforeSerialize(storageType); } public override void OnAfterSerialize(Type storageType, object instance, ref fsData data) { // Don't call the callback on null instances. if(instance == null) return; ((fsISerializationCallbacks)instance).OnAfterSerialize(storageType, ref data); } public override void OnBeforeDeserializeAfterInstanceCreation(Type storageType, object instance, ref fsData data) { if (instance is fsISerializationCallbacks == false) { throw new InvalidCastException("Please ensure the converter for " + storageType + " actually returns an instance of it, not an instance of " + instance.GetType()); } ((fsISerializationCallbacks)instance).OnBeforeDeserialize(storageType, ref data); } public override void OnAfterDeserialize(Type storageType, object instance) { // Don't call the callback on null instances. if(instance == null) return; ((fsISerializationCallbacks)instance).OnAfterDeserialize(storageType); } } #if !NO_UNITY public class fsSerializationCallbackReceiverProcessor : fsObjectProcessor { public override bool CanProcess(Type type) { return typeof(ISerializationCallbackReceiver).IsAssignableFrom(type); } public override void OnBeforeSerialize(Type storageType, object instance) { // Don't call the callback on null instances. if(instance == null) return; ((ISerializationCallbackReceiver)instance).OnBeforeSerialize(); } public override void OnAfterDeserialize(Type storageType, object instance) { // Don't call the callback on null instances. if(instance == null) return; ((ISerializationCallbackReceiver)instance).OnAfterDeserialize(); } } #endif }