public void RegisterBinaryPack(UABField uField, object value) { this.tempBinariesPack.Add(new TempBinaryPack() { uField = uField, value = value }); }
public void RegisterReferencePack(UABField uField, object value) { this.tempReferencesPack.Add(new TempReferencePack() { uField = uField, value = value }); }
private UABComponent Pack(Component component, List <ISerializer> serializers) { var uComponent = new UABComponent(); uComponent.instanceId = component.GetInstanceID(); uComponent.type = Builder.GetTypeString(component.GetType()); var found = false; for (int j = 0; j < serializers.Count; ++j) { if (serializers[j].IsValid(component) == true) { var field = new UABField(); var componentObj = (object)component; serializers[j].Serialize(this, field, ref componentObj, serializers); uComponent.fields = field.fields; found = true; break; } } if (found == false) { var uFields = this.Serialize(component, serializers); uComponent.fields = uFields; } return(uComponent); }
public void Unpack(Component component, UABComponent uComponent, List <ISerializer> serializers) { var found = false; for (int j = 0; j < serializers.Count; ++j) { if (serializers[j].IsValid(component) == true) { var field = new UABField(); field.fields = uComponent.fields; var componentObj = (object)component; serializers[j].DeserializeBeforeRef(this, field, ref componentObj, serializers); found = true; break; } } if (found == false) { this.Deserialize(component, uComponent.fields, serializers); } }
public object Unpack(object container, System.Type fieldType, UABField fieldData, List <ISerializer> serializers) { for (int i = 0; i < serializers.Count; ++i) { var s = serializers[i]; if ((s is IBinarySerializer) == false && s.IsValid(fieldData.serializatorId) == true) { object obj = container; s.DeserializeBeforeRef(this, fieldData, ref obj, serializers); return(obj); } } object result = null; if (fieldData.fieldType == FieldType.ArrayType) { if (fieldData.fields == null) { fieldData.fields = new UABField[0]; } var elementType = fieldType.GetEnumerableType(); if (fieldType.IsGenericType == true) { IList list = (IList)System.Activator.CreateInstance(typeof(List <>).MakeGenericType(elementType)); for (int i = 0; i < fieldData.fields.Length; ++i) { var item = fieldData.fields[i]; if (item != null) { list.Add(this.Unpack(null, elementType, item, serializers)); } else { list.Add(null); } } result = list; } else if (fieldType.IsArray == true) { var filledArray = System.Array.CreateInstance(elementType, fieldData.fields.Length); for (int i = 0; i < fieldData.fields.Length; ++i) { var item = fieldData.fields[i]; if (item != null) { filledArray.SetValue(this.Unpack(null, elementType, item, serializers), i); } else { filledArray.SetValue(null, i); } } result = filledArray; } } else if (fieldData.fieldType == FieldType.NestedType) { var type = fieldType; object instance = null; try { instance = System.Activator.CreateInstance(type); } catch (System.Exception) { instance = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(type); } //fieldInfo.SetValue(obj, instance); result = instance; this.Deserialize(instance, fieldData.fields, serializers); } else if (fieldData.fieldType == FieldType.ValueType) { //if (UnityEngine.UI.Windows.Constants.LOGS_ENABLED == true) UnityEngine.Debug.Log(obj + " :: " + fieldInfo.Name + " = " + fieldData.data + " << " + fieldData.serializatorId); object value = null; var found = false; for (int i = 0; i < serializers.Count; ++i) { if (serializers[i].IsValid(fieldData.serializatorId) == true) { object obj = container; serializers[i].DeserializeBeforeRef(this, fieldData, ref obj, serializers); value = obj; found = true; break; } } if (found == false) { // no custom serializator was found - use default //fieldInfo.SetValue(obj, UABSerializer.DeserializeValueType(fieldData.data)); try { if (fieldType.IsEnum == true) { value = System.Enum.ToObject(fieldType, int.Parse(fieldData.data)); } else { value = System.Convert.ChangeType(fieldData.data, fieldType); } } catch (System.Exception) { //if (UnityEngine.UI.Windows.Constants.LOGS_ENABLED == true) UnityEngine.Debug.LogException(ex); value = UABSerializer.DeserializeValueType(fieldData.data, fieldType); } } //fieldInfo.SetValue(obj, value); result = value; } else if (fieldData.fieldType == FieldType.ReferenceType) { // Unpack ref type //if (UnityEngine.UI.Windows.Constants.LOGS_ENABLED == true) UnityEngine.Debug.Log("Unpack ref type: " + fieldData.data); //UAB.Builder.RegisterReferenceUnpack(fieldData, fieldInfo, obj, contextCallback); var data = UABSerializer.DeserializeValueType(fieldData.data, typeof(UABReference)) as UABReference; if (data != null) { if (data.isLocal == false) { var rf = UnityEngine.UI.Windows.WindowSystemResources.Load <ObjectReference>(data.instanceId); if (rf != null) { //if (UnityEngine.UI.Windows.Constants.LOGS_ENABLED == true) UnityEngine.Debug.LogWarning("Resources.Load: " + rf + " :: " + data.instanceId + " :: " + container); //fieldInfo.SetValue(container, rf.reference); result = rf.reference; } else { if (UnityEngine.UI.Windows.Constants.LOGS_ENABLED == true) { UnityEngine.Debug.LogWarning(string.Format("Resource Reference was not found `{0}`.", data.instanceId)); } } } else { if (data.instanceId != "0") { object obj = null; Object value; if (this.GetTempByInstanceID(data.instanceId, out value) == true) { if (data.isGameObject == true) { obj = (object)((value as Transform).gameObject); } else { obj = (object)value; } result = obj; } else { throw new UnityException(string.Format("Package malformed. Local reference `{0}` was not found.", data.instanceId)); } } } } else { throw new UnityException("Package malformed. UABReference deserialization failed."); } } else if (fieldData.fieldType == FieldType.BinaryType) { var data = UABSerializer.DeserializeValueType(fieldData.data, typeof(UABBinary)) as UABBinary; if (data != null) { var binHeader = this.GetBinaryHeader(data.instanceId); if (binHeader != null) { var found = false; for (int i = 0; i < serializers.Count; ++i) { if (serializers[i].IsValid(binHeader.field.serializatorId) == true) { object obj = null; var binData = this.GetBinaryData(binHeader.binDataInstanceId); if (binData != null) { var field = new UABField(); field.data = binData.data; field.fields = binHeader.field.fields; serializers[i].DeserializeBeforeRef(this, field, ref obj, serializers); result = obj; found = true; } break; } } if (found == false) { throw new UnityException(string.Format("Package malformed. Serializer was not found by id `{0}`.", binHeader.field.serializatorId)); } } else { throw new UnityException(string.Format("Package malformed. Binary Header was not found by id `{0}`.", data.instanceId)); } } else { throw new UnityException("Package malformed. UABBinary deserialization failed."); } } return(result); }
public UABField Pack(string fieldName, System.Type fieldType, object value, List <ISerializer> serializers) { var uField = new UABField(); uField.name = fieldName; uField.fieldType = FieldType.ValueType; if (value == null || (value is Object && (value as Object) == null)) { uField.data = null; return(uField); } if ((value is Object) == false) { for (int i = 0; i < serializers.Count; ++i) { var s = serializers[i]; if ((s is IBinarySerializer) == false && s.IsValid(value) == true) { s.Serialize(this, uField, ref value, serializers); return(uField); } } } var type = fieldType; if (value.IsArray() == true) { // array uField.fieldType = FieldType.ArrayType; } else if ( (type.IsClass == true || type.IsValueType == true) && type.IsSerializable == true && type.IsSimpleType() == false && type.IsEnum == false) { // nested uField.fieldType = FieldType.NestedType; uField.fields = this.Serialize(value, serializers); } else if ( type.IsSimpleType() == true || type.IsValueType == true || type.IsEnum == true) { // value type uField.fieldType = FieldType.ValueType; } else { // ref type uField.fieldType = FieldType.ReferenceType; if (UAB.Builder.IsBinary(this.currentBundleName, value as Object) == true) { // binary type uField.fieldType = FieldType.BinaryType; } } if (uField.fieldType == FieldType.ValueType) { var found = false; for (int i = 0; i < serializers.Count; ++i) { if (serializers[i].IsValid(value) == true) { serializers[i].Serialize(this, uField, ref value, serializers); found = true; break; } } if (found == false) { // no custom serializator was found - use default uField.serializatorId = "-1"; if (value is string) { uField.data = (string)value; } else { uField.data = UABSerializer.SerializeValueType(value); } } } else if (uField.fieldType == FieldType.ReferenceType) { //Debug.Log("Pack ref type: " + value); this.RegisterReferencePack(uField, value); } else if (uField.fieldType == FieldType.BinaryType) { this.RegisterBinaryPack(uField, value); } else if (uField.fieldType == FieldType.ArrayType) { var count = 0; var enumerator = ((IEnumerable)value).GetEnumerator(); while (enumerator.MoveNext() == true) { ++count; } //Debug.Log("Pack arr type: " + count); if (count > 0) { enumerator.Reset(); uField.fields = new UABField[count]; count = 0; while (enumerator.MoveNext() == true) { var element = enumerator.Current; uField.fields[count] = (element == null ? null : this.Pack(null, element.GetType(), element, serializers)); ++count; } } } return(uField); }
public void BuildBinaries(ref UABBinaryHeader[] headers, ref UABBinaryData[] data, List <ISerializer> serializers) { var binData = ListPool <UABBinaryData> .Get(); var binHeaders = ListPool <UABBinaryHeader> .Get(); var list = this.tempBinariesPack; for (int i = 0; i < list.Count; ++i) { var uField = list[i].uField; var value = list[i].value; var headerId = string.Empty; var found = false; #if UNITY_EDITOR var instanceId = UnityEditor.AssetDatabase.AssetPathToGUID(UnityEditor.AssetDatabase.GetAssetPath(value as Object)); for (int j = 0; j < serializers.Count; ++j) { if (serializers[j].IsValid(value) == true) { if (binData.Any(x => x.instanceId == instanceId) == false) { var f = new UABField(); serializers[j].Serialize(null, f, ref value, serializers); binData.Add(new UABBinaryData() { instanceId = instanceId, data = f.data }); } var field = new UABField(); serializers[j].Serialize(this, field, ref value, serializers); var header = new UABBinaryHeader() { binDataInstanceId = instanceId }; header.instanceId = header.GetHashCode().ToString(); header.field = field; headerId = header.instanceId; if (binHeaders.Any(x => x.instanceId == header.instanceId) == false) { binHeaders.Add(header); } found = true; break; } } #endif uField.serializatorId = "-3"; uField.data = UABSerializer.SerializeValueType(new UABBinary() { instanceId = headerId }); if (found == false) { throw new UnityException(string.Format("Binary Serializer was not found for type `{0}`. Package cannot be processed.", value.GetType().ToString())); } } list.Clear(); data = binData.ToArray(); ListPool <UABBinaryData> .Release(binData); headers = binHeaders.ToArray(); ListPool <UABBinaryHeader> .Release(binHeaders); }