Beispiel #1
0
    private static string GetName(FieldInfo fi)
    {
        FormerlySerializedAsAttribute val = fi.GetCustomAttributes(typeof(FormerlySerializedAsAttribute), false).FirstOrDefault() as FormerlySerializedAsAttribute;

        if (val == null)
        {
            return(fi.Name);
        }
        return(val.get_oldName());
    }
Beispiel #2
0
        public override void FindDependencies <T>(Dictionary <long, T> dependencies, Dictionary <long, T> objects, bool allowNulls)
        {
            base.FindDependencies(dependencies, objects, allowNulls);

            if (baseObjectData != null)
            {
                baseObjectData.FindDependencies(dependencies, objects, allowNulls);
            }

            Type type = Type.GetType(TypeName);

            if (type == null)
            {
                Debug.LogWarning(TypeName + " is not found");
                return;
            }

            if (!type.IsScript())
            {
                throw new ArgumentException(string.Format("obj of type {0} is not subclass of {1}", type, typeof(MonoBehaviour)), "obj");
            }

            do
            {
                FieldInfo[] fields = type.GetSerializableFields();
                for (int i = 0; i < fields.Length; ++i)
                {
                    FieldInfo field = fields[i];
                    string    name  = field.Name;
                    if (!this.fields.ContainsKey(field.Name))
                    {
                        FormerlySerializedAsAttribute formelySerializedAs = field.GetCustomAttributes(typeof(FormerlySerializedAsAttribute), false).FirstOrDefault() as FormerlySerializedAsAttribute;
                        if (formelySerializedAs == null)
                        {
                            continue;
                        }
                        name = formelySerializedAs.oldName;
                        if (!this.fields.ContainsKey(name))
                        {
                            continue;
                        }
                    }

                    bool isArray   = field.FieldType.IsArray;
                    Type fieldType = field.FieldType;
                    if (isArray)
                    {
                        fieldType = fieldType.GetElementType();
                    }

                    DataContract value = this.fields[name];
                    if (fieldType.IsSubclassOf(typeof(UnityObject)) || fieldType == typeof(UnityObject))
                    {
                        if (isArray)
                        {
                            if (!(value.AsPrimitive.ValueBase is long[]))
                            {
                                continue;
                            }

                            long[] instanceIds = (long[])value.AsPrimitive.ValueBase;
                            AddDependencies(instanceIds, dependencies, objects, allowNulls);
                        }
                        else
                        {
                            if (!(value.AsPrimitive.ValueBase is long))
                            {
                                continue;
                            }

                            long instanceId = (long)value.AsPrimitive.ValueBase;
                            AddDependency(instanceId, dependencies, objects, allowNulls);
                        }
                    }
                    else
                    {
                        if (value.Data != null)
                        {
                            if (field.FieldType.IsSubclassOf(typeof(UnityEventBase)))
                            {
                                PersistentUnityEventBase persistentUnityEvent = value.AsUnityEvent;
                                persistentUnityEvent.FindDependencies(dependencies, objects, allowNulls);
                            }
                        }
                        else
                        {
                            if (!field.FieldType.IsEnum())
                            {
                                object fieldValue;
                                if (field.FieldType.IsPrimitive() || field.FieldType.IsArray())
                                {
                                    PrimitiveContract primitive = value.AsPrimitive;
                                    if (primitive == null ||
                                        primitive.ValueBase == null && field.FieldType.IsValueType() ||
                                        primitive.ValueBase != null && !field.FieldType.IsAssignableFrom(primitive.ValueBase.GetType()))
                                    {
                                        continue;
                                    }
                                    fieldValue = primitive.ValueBase;
                                }
                                else
                                {
                                    fieldValue = value.Data;
                                }

                                if (fieldValue is IEnumerable)
                                {
                                    IEnumerable enumerable = (IEnumerable)fieldValue;
                                    foreach (object o in enumerable)
                                    {
                                        if (o is IRTSerializable)
                                        {
                                            IRTSerializable rtSerializable = (IRTSerializable)o;
                                            rtSerializable.FindDependencies(dependencies, objects, allowNulls);
                                        }
                                    }
                                }
                                else
                                {
                                    if (fieldValue is IRTSerializable)
                                    {
                                        IRTSerializable rtSerializable = (IRTSerializable)fieldValue;
                                        rtSerializable.FindDependencies(dependencies, objects, allowNulls);
                                    }
                                }
                            }
                        }
                    }
                }
                type = type.BaseType();
            }while (type.IsScript());
        }
Beispiel #3
0
 public void UpdateData <T>()
 {
     // Check if context type has change
     if (!string.Equals(typeof(T).AssemblyQualifiedName, _declaringTypeName))
     {
         _declaringTypeName = typeof(T).AssemblyQualifiedName;
         foreach (KeyValuePair <string, SerializableInfo> pair in InfoDictionary)
         {
             pair.Value.DeclaringTypeName = _declaringTypeName;
         }
     }
     // Handle fields
     FieldInfo[] fieldInfos = typeof(T).GetFields(SerializableInfo.DefaultBindingFlags);
     foreach (FieldInfo fieldInfo in fieldInfos)
     {
         if (PortDictionary.ContainsKey(fieldInfo.Name))
         {
             continue;
         }
         SerializableInfo serializableFieldInfo = new SerializableInfo(fieldInfo);
         PortDictionary.Add(fieldInfo.Name, serializableFieldInfo.PortName);
         NodePort newPort = AddDynamicOutput(serializableFieldInfo.Type, ConnectionType.Multiple, TypeConstraint.None, serializableFieldInfo.PortName);
         InfoDictionary.Add(serializableFieldInfo.PortName, serializableFieldInfo);
         // Redirect port using FormerlySerializedAsAttribute
         FormerlySerializedAsAttribute attribute = fieldInfo.GetCustomAttribute <FormerlySerializedAsAttribute>();
         if (attribute != null && PortDictionary.ContainsKey(attribute.oldName))
         {
             GetPort(PortDictionary[attribute.oldName]).SwapConnections(newPort);
         }
     }
     // Handle properties
     PropertyInfo[] propertyInfos = typeof(T).GetProperties(SerializableInfo.DefaultBindingFlags);
     foreach (PropertyInfo propertyInfo in propertyInfos)
     {
         if (PortDictionary.ContainsKey(propertyInfo.Name))
         {
             continue;
         }
         SerializableInfo serializablePropertyInfo = new SerializableInfo(propertyInfo);
         PortDictionary.Add(propertyInfo.Name, serializablePropertyInfo.PortName);
         NodePort newPort = AddDynamicOutput(serializablePropertyInfo.Type, ConnectionType.Multiple, TypeConstraint.None, serializablePropertyInfo.PortName);
         InfoDictionary.Add(serializablePropertyInfo.PortName, serializablePropertyInfo);
         // Redirect port using FormerlySerializedAsAttribute
         FormerlySerializedAsAttribute attribute = propertyInfo.GetCustomAttribute <FormerlySerializedAsAttribute>();
         if (attribute != null && PortDictionary.ContainsKey(attribute.oldName))
         {
             GetPort(PortDictionary[attribute.oldName]).SwapConnections(newPort);
         }
     }
     // Remove old ports
     for (int i = PortDictionary.Count - 1; i >= 0; i--)
     {
         if (fieldInfos.Any(info => info.Name == PortDictionary.ElementAt(i).Key))
         {
             continue;
         }
         if (propertyInfos.Any(info => info.Name == PortDictionary.ElementAt(i).Key))
         {
             continue;
         }
         RemoveDynamicPort(PortDictionary.ElementAt(i).Value);
         InfoDictionary.Remove(PortDictionary.ElementAt(i).Value);
         PortDictionary.Remove(PortDictionary.ElementAt(i).Key);
     }
 }
Beispiel #4
0
        public override object WriteTo(object obj, Dictionary <long, UnityObject> objects)
        {
            obj = base.WriteTo(obj, objects);

            if (baseObjectData != null)
            {
                //prevent name to be overriden with name stored m_baseObjectData.name
                PersistentObjects.PersistentObject persistentObj = baseObjectData.AsPersistentObject;
                if (persistentObj != null)
                {
                    persistentObj.name = name;
                }
                baseObjectData.WriteTo(obj, objects);
            }

            Type type = obj.GetType();

            if (!type.IsScript())
            {
                throw new ArgumentException(string.Format("obj of type {0} is not subclass of {1}", type, typeof(MonoBehaviour)), "obj");
            }

            do
            {
                FieldInfo[] fields = type.GetSerializableFields();
                for (int i = 0; i < fields.Length; ++i)
                {
                    FieldInfo field = fields[i];
                    string    name  = field.Name;
                    if (!this.fields.ContainsKey(field.Name))
                    {
                        FormerlySerializedAsAttribute formelySerializedAs = field.GetCustomAttributes(typeof(FormerlySerializedAsAttribute), false).FirstOrDefault() as FormerlySerializedAsAttribute;
                        if (formelySerializedAs == null)
                        {
                            continue;
                        }
                        name = formelySerializedAs.oldName;
                        if (!this.fields.ContainsKey(name))
                        {
                            continue;
                        }
                    }

                    bool isArray   = field.FieldType.IsArray;
                    Type fieldType = field.FieldType;
                    if (isArray)
                    {
                        fieldType = fieldType.GetElementType();
                    }

                    DataContract value = this.fields[name];
                    if (fieldType.IsSubclassOf(typeof(UnityObject)) || fieldType == typeof(UnityObject))
                    {
                        if (isArray)
                        {
                            if (value.AsPrimitive == null)
                            {
                                continue;
                            }

                            if (!(value.AsPrimitive.ValueBase is long[]))
                            {
                                continue;
                            }

                            long[] instanceIds = (long[])value.AsPrimitive.ValueBase;
                            Array  objectsFoundByInstanceId = Array.CreateInstance(fieldType, instanceIds.Length);
                            for (int j = 0; j < instanceIds.Length; ++j)
                            {
                                object o = objects.Get(instanceIds[j]);
                                objectsFoundByInstanceId.SetValue(o, j);
                            }
                            field.SetValue(obj, objectsFoundByInstanceId);
                        }
                        else
                        {
                            if (value.AsPrimitive == null)
                            {
                                continue;
                            }
                            if (!(value.AsPrimitive.ValueBase is long))
                            {
                                continue;
                            }

                            long instanceId = (long)value.AsPrimitive.ValueBase;
                            if (!objects.ContainsKey(instanceId))
                            {
                                continue;
                            }

                            object objectFoundByInstanceId = objects[instanceId];
                            try
                            {
                                field.SetValue(obj, objectFoundByInstanceId);
                            }
                            catch
                            {
                                Debug.LogError(instanceId);
                                throw;
                            }
                        }
                    }
                    else
                    {
                        if (value == null)
                        {
                            if (field.FieldType.IsValueType())
                            {
                                continue;
                            }
                            field.SetValue(obj, value);
                        }
                        else
                        {
                            if (field.FieldType.IsSubclassOf(typeof(UnityEventBase)))
                            {
                                if (value.AsUnityEvent == null)
                                {
                                    continue;
                                }
                                PersistentUnityEventBase persistentUnityEvent = value.AsUnityEvent;
                                UnityEventBase           unityEvent           = (UnityEventBase)Activator.CreateInstance(field.FieldType);
                                persistentUnityEvent.WriteTo(unityEvent, objects);
                                field.SetValue(obj, unityEvent);
                            }
                            else
                            {
                                if (!field.FieldType.IsEnum())
                                {
                                    object fieldValue;
                                    if (field.FieldType.IsPrimitive() || field.FieldType.IsArray())
                                    {
                                        PrimitiveContract primitive = value.AsPrimitive;
                                        if (primitive == null ||
                                            primitive.ValueBase == null && field.FieldType.IsValueType() ||
                                            primitive.ValueBase != null && !field.FieldType.IsAssignableFrom(primitive.ValueBase.GetType()))
                                        {
                                            continue;
                                        }
                                        fieldValue = primitive.ValueBase;
                                    }
                                    else
                                    {
                                        fieldValue = value.Data;
                                    }

                                    field.SetValue(obj, fieldValue);
                                    if (fieldValue is IEnumerable)
                                    {
                                        IEnumerable enumerable = (IEnumerable)fieldValue;
                                        foreach (object o in enumerable)
                                        {
                                            if (o is IRTSerializable)
                                            {
                                                IRTSerializable rtSerializable = (IRTSerializable)o;
                                                rtSerializable.Deserialize(objects);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (fieldValue is IRTSerializable)
                                        {
                                            IRTSerializable rtSerializable = (IRTSerializable)fieldValue;
                                            rtSerializable.Deserialize(objects);
                                        }
                                    }
                                }
                                else// if (field.FieldType.IsEnum)
                                {
                                    PrimitiveContract primitive = value.AsPrimitive;
                                    if (primitive == null ||
                                        primitive.ValueBase == null && field.FieldType.IsValueType() ||
                                        primitive.ValueBase != null && primitive.ValueBase.GetType() != typeof(uint))
                                    {
                                        continue;
                                    }

                                    var val = Enum.ToObject(field.FieldType, primitive.ValueBase);
                                    field.SetValue(obj, val);
                                }
                            }
                        }
                    }
                }
                type = type.BaseType();
            }while (type.IsScript());

            return(obj);
        }