static BaseFormatter() { if (!Serializer.SerializeUnityEngineObjectReferences && !Serializer.DeserializeUnityEngineObjectReferences && typeof(T).ImplementsOrInherits(typeof(UnityEngine.Object))) { DefaultLoggers.DefaultLogger.LogWarning("A formatter has been created for the UnityEngine.Object type " + typeof(T).Name + " - this is *strongly* discouraged. Unity should be allowed to handle serialization and deserialization of its own weird objects. Remember to serialize with a UnityReferenceResolver as the external index reference resolver in the serialization context.\n\n Stacktrace: " + new System.Diagnostics.StackTrace().ToString()); } var methods = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); Func <MethodInfo, SerializationCallback> selector; selector = (info) => { var parameters = info.GetParameters(); if (parameters.Length == 0) { var action = EmitUtilities.CreateInstanceRefMethodCaller <T>(info); return((ref T value, StreamingContext context) => action(ref value)); } else if (parameters.Length == 1 && parameters[0].ParameterType == typeof(StreamingContext) && parameters[0].ParameterType.IsByRef == false) { var action = EmitUtilities.CreateInstanceRefMethodCaller <T, StreamingContext>(info); return((ref T value, StreamingContext context) => action(ref value, context)); } else { DefaultLoggers.DefaultLogger.LogWarning("The method " + info.GetNiceName() + " has an invalid signature and will be ignored by the serialization system."); return(null); } }; OnSerializingCallbacks = methods.Where(n => n.IsDefined(typeof(OnSerializingAttribute), true)) .Select(selector) .Where(n => n != null) .ToArray(); OnSerializedCallbacks = methods.Where(n => n.IsDefined(typeof(OnSerializedAttribute), true)) .Select(selector) .Where(n => n != null) .ToArray(); OnDeserializingCallbacks = methods.Where(n => n.IsDefined(typeof(OnDeserializingAttribute), true)) .Select(selector) .Where(n => n != null) .ToArray(); OnDeserializedCallbacks = methods.Where(n => n.IsDefined(typeof(OnDeserializedAttribute), true)) .Select(selector) .Where(n => n != null) .ToArray(); }
private static SerializationCallback CreateCallback(MethodInfo info) { var parameters = info.GetParameters(); if (parameters.Length == 0) { #if CAN_EMIT var action = EmitUtilities.CreateInstanceRefMethodCaller <T>(info); return((ref T value, StreamingContext context) => action(ref value)); #else return((ref T value, StreamingContext context) => { object obj = value; info.Invoke(obj, null); value = (T)obj; }); #endif } else if (parameters.Length == 1 && parameters[0].ParameterType == typeof(StreamingContext) && parameters[0].ParameterType.IsByRef == false) { #if CAN_EMIT var action = EmitUtilities.CreateInstanceRefMethodCaller <T, StreamingContext>(info); return((ref T value, StreamingContext context) => action(ref value, context)); #else return((ref T value, StreamingContext context) => { object obj = value; info.Invoke(obj, new object[] { context }); value = (T)obj; }); #endif } else { DefaultLoggers.DefaultLogger.LogWarning("The method " + info.GetNiceName() + " has an invalid signature and will be ignored by the serialization system."); return(null); } }