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()); }
public override void ReadFrom(object obj) { base.ReadFrom(obj); if (baseObjectData != null) { baseObjectData.ReadFrom(obj); } 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"); } TypeName = type.AssemblyQualifiedName; do { FieldInfo[] fields = type.GetSerializableFields(); for (int i = 0; i < fields.Length; ++i) { FieldInfo field = fields[i]; if (this.fields.ContainsKey(field.Name)) { Debug.LogErrorFormat("Fields with same names are not supported. Field name {0}", field.Name); continue; } bool isArray = field.FieldType.IsArray; Type fieldType = field.FieldType; if (isArray) { fieldType = fieldType.GetElementType(); } if (fieldType.IsSubclassOf(typeof(UnityObject)) || fieldType == typeof(UnityObject)) { if (isArray) { UnityObject[] unityObjects = (UnityObject[])field.GetValue(obj); if (unityObjects != null) { long[] ids = new long[unityObjects.Length]; for (int j = 0; j < ids.Length; ++j) { ids[j] = unityObjects[j].GetMappedInstanceID(); } this.fields.Add(field.Name, new DataContract(PrimitiveContract.Create(ids))); } else { this.fields.Add(field.Name, new DataContract(PrimitiveContract.Create(new long[0]))); } } else { UnityObject unityObject = (UnityObject)field.GetValue(obj); this.fields.Add(field.Name, new DataContract(PrimitiveContract.Create(unityObject.GetMappedInstanceID()))); } } else { if (fieldType.IsSubclassOf(typeof(UnityEventBase))) { UnityEventBase unityEvent = (UnityEventBase)field.GetValue(obj); if (unityEvent != null) { PersistentUnityEventBase persistentUnityEvent = new PersistentUnityEventBase(); persistentUnityEvent.ReadFrom(unityEvent); this.fields.Add(field.Name, new DataContract(persistentUnityEvent)); } } else { if (!field.FieldType.IsEnum()) { object fieldValue = field.GetValue(obj); if (typeof(IEnumerable).IsAssignableFrom(field.FieldType)) { IEnumerable enumerable = (IEnumerable)fieldValue; foreach (object o in enumerable) { if (o is IRTSerializable) { IRTSerializable rtSerializable = (IRTSerializable)o; rtSerializable.Serialize(); } } } else { if (fieldValue is IRTSerializable) { IRTSerializable rtSerializable = (IRTSerializable)fieldValue; rtSerializable.Serialize(); } } if (field.FieldType.IsPrimitive() || field.FieldType.IsArray()) { PrimitiveContract primitive = PrimitiveContract.Create(field.FieldType); primitive.ValueBase = fieldValue; this.fields.Add(field.Name, new DataContract(primitive)); } else { this.fields.Add(field.Name, new DataContract { Data = fieldValue }); } } else { //Debug.Log("Want to create primitive contract for " + field.FieldType); PrimitiveContract primitive = PrimitiveContract.Create(typeof(uint)); primitive.ValueBase = (uint)Convert.ChangeType(field.GetValue(obj), typeof(uint)); this.fields.Add(field.Name, new DataContract(primitive)); } } } } type = type.BaseType(); }while (type.IsScript()); }
protected override void GetDependencies(Dictionary <long, UnityObject> dependencies, object obj) { base.GetDependencies(dependencies, obj); base.ReadFrom(obj); //? if (baseObjectData != null) { baseObjectData.GetDependencies(obj, dependencies); } 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]; bool isArray = field.FieldType.IsArray; Type fieldType = field.FieldType; if (isArray) { fieldType = fieldType.GetElementType(); } if (fieldType.IsSubclassOf(typeof(UnityObject)) || fieldType == typeof(UnityObject)) { if (isArray) { UnityObject[] unityObjects = (UnityObject[])field.GetValue(obj); if (unityObjects != null) { AddDependencies(unityObjects, dependencies); } } else { UnityObject unityObject = (UnityObject)field.GetValue(obj); AddDependency(unityObject, dependencies); } } else { if (fieldType.IsSubclassOf(typeof(UnityEventBase))) { UnityEventBase unityEvent = (UnityEventBase)field.GetValue(obj); if (unityEvent != null) { PersistentUnityEventBase persistentUnityEvent = new PersistentUnityEventBase(); persistentUnityEvent.GetDependencies(unityEvent, dependencies); } } else { if (!field.FieldType.IsEnum()) { object fieldValue = field.GetValue(obj); if (typeof(IEnumerable).IsAssignableFrom(field.FieldType)) { IEnumerable enumerable = (IEnumerable)fieldValue; foreach (object o in enumerable) { if (o is IRTSerializable) { IRTSerializable rtSerializable = (IRTSerializable)o; rtSerializable.GetDependencies(dependencies); } } } else { if (fieldValue is IRTSerializable) { IRTSerializable rtSerializable = (IRTSerializable)fieldValue; rtSerializable.GetDependencies(dependencies); } } } } } } type = type.BaseType(); }while (type.IsScript()); }
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); }