コード例 #1
0
        public static GameObject[] Unpack(byte[] bytes, UABConfig config = null, List <ISerializer> serializers = null)
        {
            var unzipped         = Zipper.UnzipString(bytes);
            var dataDeserialized = UABSerializer.DeserializeValueType <UABPackage>(unzipped);

            return(Builder.Unpack(dataDeserialized, config, serializers));
        }
コード例 #2
0
        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);
        }