示例#1
0
        public bool DeSerialize(Type _Type, byte[] _Bytes, out object _Object)
        {
            if (!_Type.IsNullable())
            {
                _Object = null;
                return(false);
            }

            Type var_UnderlyingType = _Type.GetNullableUnderlyingType();

            ByteReader var_Reader = new ByteReader(_Bytes);

            bool var_IsNull = var_Reader.ReadBoolean();

            if (var_IsNull)
            {
                _Object = null;
                return(true);
            }

            //
            if (PrimitiveTypeMatcher.TypeDictionary.ContainsKey(var_UnderlyingType))
            {
                _Object = var_Reader.Read(var_UnderlyingType);
            }
            else
            {
                byte[] var_Bytes = var_Reader.ReadBytesAndSize();
                _Object = Serializer.DeSerialize(var_UnderlyingType, var_Bytes);
            }

            return(true);
        }
示例#2
0
        public bool DeSerialize(Type _Type, byte[] _Bytes, out object _Object)
        {
            if (_Type.IsGenericType && _Type.GetGenericTypeDefinition() == typeof(Queue <>))
            {
                Type var_QueueType        = typeof(Queue <>);
                Type var_GenericItemType  = _Type.GetGenericArguments()[0];
                Type var_GenericQueueType = var_QueueType.MakeGenericType(var_GenericItemType);

                ByteReader var_Reader = new ByteReader(_Bytes);

                bool var_IsNull = var_Reader.ReadBoolean();
                if (var_IsNull)
                {
                    _Object = null;
                    return(true);
                }

                //

                int var_ItemCount = var_Reader.ReadUInt16();

                byte[] var_ItemBytes = var_Reader.ReadBytesAndSize();

                ByteReader var_ItemReader = new ByteReader(var_ItemBytes);

                Type var_ListType        = typeof(List <>);
                Type var_GenericListType = var_ListType.MakeGenericType(var_GenericItemType);

                IList var_Items = Activator.CreateInstance(var_GenericListType) as IList;

                if (PrimitiveTypeMatcher.TypeDictionary.ContainsKey(var_GenericItemType))
                {
                    for (int i = 0; i < var_ItemCount; i++)
                    {
                        var_Items.Add(var_ItemReader.Read(var_GenericItemType));
                    }
                }
                else
                {
                    for (int i = 0; i < var_ItemCount; i++)
                    {
                        byte[] var_Bytes = var_ItemReader.ReadBytesAndSize();
                        Object var_Item  = Serializer.DeSerialize(var_GenericItemType, var_Bytes);
                        var_Items.Add(var_Item);
                    }
                }

                _Object = Activator.CreateInstance(var_GenericQueueType, var_Items);
                return(true);
            }

            _Object = null;
            return(false);
        }
示例#3
0
        public bool DeSerialize(Type _Type, byte[] _Bytes, out object _Object)
        {
            if (_Type.IsGenericType && _Type.GetGenericTypeDefinition() == typeof(List <>))
            {
                if (_Type.GetGenericArguments().Length == 1)
                {
                    //TODO: Kann man nicht _Type direkt nehemn :D
                    Type var_ListType        = typeof(List <>);
                    Type var_GenericItemType = _Type.GetGenericArguments()[0];
                    Type var_GenericListType = var_ListType.MakeGenericType(var_GenericItemType);

                    IList var_List = Activator.CreateInstance(var_GenericListType) as IList;

                    ByteReader var_Reader = new ByteReader(_Bytes);

                    bool var_IsNull = var_Reader.ReadBoolean();
                    if (var_IsNull)
                    {
                        _Object = null;
                        return(true);
                    }

                    //
                    int var_Count = var_Reader.ReadUInt16();

                    if (PrimitiveTypeMatcher.TypeHashSet.Contains(_Type))
                    {
                        for (int i = 0; i < var_Count; i++)
                        {
                            var_List.Add(var_Reader.Read(var_GenericItemType));
                        }
                    }
                    else
                    {
                        for (int i = 0; i < var_Count; i++)
                        {
                            byte[] var_Bytes = var_Reader.ReadBytesAndSize();
                            Object var_Item  = Serializer.DeSerialize(var_GenericItemType, var_Bytes);
                            var_List.Add(var_Item);
                        }
                    }

                    _Object = var_List;
                    return(true);
                }
            }

            _Object = null;
            return(false);
        }
        public bool DeSerialize(Type _Type, byte[] _Bytes, out object _Object)
        {
            //Check if is a Dictionary.
            if (_Type.IsGenericType && _Type.GetGenericTypeDefinition() == typeof(Dictionary <,>))
            {
                //Setup Types
                Type var_DictionaryType        = typeof(Dictionary <,>);
                Type var_GenericItemType_Key   = _Type.GetGenericArguments()[0];
                Type var_GenericItemType_Value = _Type.GetGenericArguments()[1];
                Type var_GenericDictionaryType = var_DictionaryType.MakeGenericType(var_GenericItemType_Key, var_GenericItemType_Value);

                ByteReader var_Reader = new ByteReader(_Bytes);

                bool var_IsNull = var_Reader.ReadBoolean();
                if (var_IsNull)
                {
                    _Object = null;
                    return(true);
                }

                //Read Length
                int var_ItemCount = var_Reader.ReadInt32();

                //Read Arrays
                byte[] var_KeyByteArray   = var_Reader.ReadBytesAndSize();
                byte[] var_ValueByteArray = var_Reader.ReadBytesAndSize();

                Array var_KeyArray   = Serializer.DeSerialize(var_GenericItemType_Key.MakeArrayType(), var_KeyByteArray) as Array;
                Array var_ValueArray = Serializer.DeSerialize(var_GenericItemType_Value.MakeArrayType(), var_ValueByteArray) as Array;

                //Create Dictionary
                IDictionary var_Dictionary = Activator.CreateInstance(var_GenericDictionaryType) as IDictionary;

                //Set Dictionary Keys and Values
                for (int i = 0; i < var_ItemCount; i++)
                {
                    var_Dictionary.Add(var_KeyArray.GetValue(i), var_ValueArray.GetValue(i));
                }

                _Object = var_Dictionary;
                return(true);
            }

            _Object = null;
            return(false);
        }
示例#5
0
        public bool DeSerialize(Type _Type, byte[] _Bytes, out object _Object)
        {
            if (!_Type.IsArray)
            {
                _Object = null;
                return(false);
            }

            ByteReader var_Reader = new ByteReader(_Bytes);

            bool var_IsNull = var_Reader.ReadBoolean();

            if (var_IsNull)
            {
                _Object = null;
                return(true);
            }

            //

            Type var_ElementType = _Type.GetElementType();

            //Read rank
            int var_Rank = var_Reader.ReadInt32();

            //Go through each dimension and read the length!
            int[] var_ArrayLength = new int[var_Rank];
            for (int d = 0; d < var_Rank; d++)
            {
                var_ArrayLength[d] = var_Reader.ReadInt32();
            }

            //Go through each dimension and read the lowerbound!
            int[] var_ArrayLowerBound = new int[var_Rank];
            for (int d = 0; d < var_Rank; d++)
            {
                var_ArrayLowerBound[d] = var_Reader.ReadInt32();
            }

            var var_Array = Array.CreateInstance(var_ElementType, var_ArrayLength);

            if (var_Rank == 1)
            {
                ArrayIndexer_D_1 arrayIndexer = new ArrayIndexer_D_1(var_ArrayLength[0], var_ArrayLowerBound[0]);

                if (PrimitiveTypeMatcher.TypeHashSet.Contains(var_ElementType))
                {
                    int[] var_IndexArray = new int[1];
                    while (arrayIndexer.MoveNext())
                    {
                        var_IndexArray[0] = arrayIndexer.Current_0;

                        var_Array.SetValue(var_Reader.Read(var_ElementType), var_IndexArray);
                    }
                }
                else
                {
                    int[] var_IndexArray = new int[1];
                    while (arrayIndexer.MoveNext())
                    {
                        var_IndexArray[0] = arrayIndexer.Current_0;

                        byte[] var_Bytes = var_Reader.ReadBytesAndSize();
                        Object var_Item  = Serializer.DeSerialize(_Type.GetElementType(), var_Bytes);
                        var_Array.SetValue(var_Item, var_IndexArray);
                    }
                }
            }
            else if (var_Rank == 2)
            {
                ArrayIndexer_D_2 arrayIndexer = new ArrayIndexer_D_2(var_ArrayLength[0], var_ArrayLength[1], var_ArrayLowerBound[0], var_ArrayLowerBound[1]);

                if (PrimitiveTypeMatcher.TypeHashSet.Contains(var_ElementType))
                {
                    int[] var_IndexArray = new int[2];
                    while (arrayIndexer.MoveNext())
                    {
                        var_IndexArray[0] = arrayIndexer.Current_0;
                        var_IndexArray[1] = arrayIndexer.Current_1;

                        var_Array.SetValue(var_Reader.Read(var_ElementType), var_IndexArray);
                    }
                }
                else
                {
                    int[] var_IndexArray = new int[2];
                    while (arrayIndexer.MoveNext())
                    {
                        var_IndexArray[0] = arrayIndexer.Current_0;
                        var_IndexArray[1] = arrayIndexer.Current_1;

                        byte[] var_Bytes = var_Reader.ReadBytesAndSize();
                        Object var_Item  = Serializer.DeSerialize(_Type.GetElementType(), var_Bytes);
                        var_Array.SetValue(var_Item, var_IndexArray);
                    }
                }
            }
            else
            {
                ArrayIndexer_D_N arrayIndexer = new ArrayIndexer_D_N(var_ArrayLength, var_ArrayLowerBound);

                if (PrimitiveTypeMatcher.TypeHashSet.Contains(var_ElementType))
                {
                    while (arrayIndexer.MoveNext())
                    {
                        var_Array.SetValue(var_Reader.Read(var_ElementType), arrayIndexer.Current);
                    }
                }
                else
                {
                    while (arrayIndexer.MoveNext())
                    {
                        byte[] var_Bytes = var_Reader.ReadBytesAndSize();
                        Object var_Item  = Serializer.DeSerialize(_Type.GetElementType(), var_Bytes);
                        var_Array.SetValue(var_Item, arrayIndexer.Current);
                    }
                }
            }

            _Object = var_Array;
            return(true);
        }
示例#6
0
        public bool DeSerialize(Type _Type, byte[] _Bytes, out object _Object)
        {
            if (_Type != typeof(Mesh))
            {
                _Object = null;
                return(false);
            }

            ByteReader var_Reader = new ByteReader(_Bytes);

            bool var_IsNull = var_Reader.ReadBoolean();

            if (var_IsNull)
            {
                _Object = null;
                return(true);
            }

            //

            Mesh var_Mesh = new Mesh();

            //Bindposes
            byte[] var_Bytes = var_Reader.ReadBytesAndSize();
            var_Mesh.bindposes = Serializer.DeSerialize <Matrix4x4[]>(var_Bytes);

            //BoneWeights
            var_Bytes            = var_Reader.ReadBytesAndSize();
            var_Mesh.boneWeights = Serializer.DeSerialize <BoneWeight[]>(var_Bytes);

            //Bound
            var_Bytes       = var_Reader.ReadBytesAndSize();
            var_Mesh.bounds = Serializer.DeSerialize <Bounds>(var_Bytes);

            //Vertices
            var_Bytes = var_Reader.ReadBytesAndSize();
            var_Mesh.SetVertices(Serializer.DeSerialize <Vector3[]>(var_Bytes).ToList());

            //Normales
            var_Bytes = var_Reader.ReadBytesAndSize();
            var_Mesh.SetNormals(Serializer.DeSerialize <Vector3[]>(var_Bytes).ToList());

            //Color
            var_Bytes = var_Reader.ReadBytesAndSize();
            var_Mesh.SetColors(Serializer.DeSerialize <Color[]>(var_Bytes).ToList());

            //Color32
            var_Bytes = var_Reader.ReadBytesAndSize();
            var_Mesh.SetColors(Serializer.DeSerialize <Color32[]>(var_Bytes).ToList());

            //UV
            var_Bytes = var_Reader.ReadBytesAndSize();
            var_Mesh.SetUVs(0, Serializer.DeSerialize <Vector2[]>(var_Bytes).ToList());
            var_Bytes = var_Reader.ReadBytesAndSize();
            var_Mesh.SetUVs(1, Serializer.DeSerialize <Vector2[]>(var_Bytes).ToList());
            var_Bytes = var_Reader.ReadBytesAndSize();
            var_Mesh.SetUVs(2, Serializer.DeSerialize <Vector2[]>(var_Bytes).ToList());
            var_Bytes = var_Reader.ReadBytesAndSize();
            var_Mesh.SetUVs(3, Serializer.DeSerialize <Vector2[]>(var_Bytes).ToList());

            //SubMeshes
            int var_SubMeshCount = var_Reader.ReadInt32();

            var_Mesh.subMeshCount = var_SubMeshCount;
            for (int s = 0; s < var_SubMeshCount; s++)
            {
                var_Bytes = var_Reader.ReadBytesAndSize();
                int[] var_Indices = Serializer.DeSerialize <int[]>(var_Bytes);

                MeshTopology var_MeshTopology = (MeshTopology)var_Reader.ReadByte();

                var_Mesh.SetIndices(var_Indices, var_MeshTopology, s);

                var_Bytes = var_Reader.ReadBytesAndSize();
                var_Mesh.SetTriangles(Serializer.DeSerialize <int[]>(var_Bytes), s);
            }

            //Tangents
            var_Bytes = var_Reader.ReadBytesAndSize();
            var_Mesh.SetTangents(Serializer.DeSerialize <Vector4[]>(var_Bytes).ToList());

            _Object = var_Mesh;
            return(true);
        }
示例#7
0
        public bool DeSerialize(Type _Type, byte[] _Bytes, out object _Object)
        {
            if (!_Type.IsArray)
            {
                _Object = null;
                return(false);
            }

            ByteReader var_Reader = new ByteReader(_Bytes);

            bool var_IsNull = var_Reader.ReadBoolean();

            if (var_IsNull)
            {
                _Object = null;
                return(true);
            }

            //

            Type var_ElementType = _Type.GetElementType();

            //Read dimensions
            int var_Dimensions = var_Reader.ReadInt32();

            //Go through each dimension and read the length!
            int[] var_ArrayLength = new int[var_Dimensions];
            for (int d = 0; d < var_Dimensions; d++)
            {
                var_ArrayLength[d] = var_Reader.ReadInt32();
            }

            //Go through each dimension and read the lowerbound!
            int[] var_ArrayLowerBound = new int[var_Dimensions];
            for (int d = 0; d < var_Dimensions; d++)
            {
                var_ArrayLowerBound[d] = var_Reader.ReadInt32();
            }

            var var_Array = Array.CreateInstance(var_ElementType, var_ArrayLength);

            ArrayIndexer arrayIndexer = new ArrayIndexer(var_ArrayLength, var_ArrayLowerBound);

            if (PrimitiveTypeMatcher.TypeDictionary.ContainsKey(var_ElementType))
            {
                while (arrayIndexer.MoveNext())
                {
                    var_Array.SetValue(var_Reader.Read(var_ElementType), arrayIndexer.Current);
                }
            }
            else
            {
                while (arrayIndexer.MoveNext())
                {
                    byte[] var_Bytes = var_Reader.ReadBytesAndSize();
                    Object var_Item  = Serializer.DeSerialize(_Type.GetElementType(), var_Bytes);
                    var_Array.SetValue(var_Item, arrayIndexer.Current);
                }
            }

            _Object = var_Array;
            return(true);
        }
        private void InternDeSerialize(Object _Object, ByteReader _Reader, List <Type> _SerializeAbleBaseList, int _CurrentIndex)
        {
            Type var_CurrentType = _SerializeAbleBaseList[_CurrentIndex];

            Dictionary <int, FieldInfo> var_FoundFieldDictionary = new Dictionary <int, FieldInfo>();

            FieldInfo[] var_FoundFields;
            int[]       var_FoundFieldsId;
            bool[]      var_FoundFieldsOptional;

            FieldContext.LoadFields(var_CurrentType, out var_FoundFields, out var_FoundFieldsId, out var_FoundFieldsOptional);

            for (int i = 0; i < var_FoundFields.Length; i++)
            {
                var_FoundFieldDictionary.Add(var_FoundFieldsId[i], var_FoundFields[i]);
            }

            int var_FieldLength = _Reader.ReadByte();

            for (int i = 0; i < var_FieldLength; i++)
            {
                int var_FieldId = _Reader.ReadByte();

                FieldInfo var_FieldInfo;
                if (var_FoundFieldDictionary.TryGetValue(var_FieldId, out var_FieldInfo))
                {
                    var var_FieldType = var_FieldInfo.FieldType;

                    //
                    if (PrimitiveTypeMatcher.TypeHashSet.Contains(var_FieldType))
                    {
                        var_FieldInfo.SetValue(_Object, _Reader.Read(var_FieldType));
                    }
                    else
                    {
                        byte[] var_Bytes = _Reader.ReadBytesAndSize();
                        Object var_Value = Serializer.DeSerialize(var_FieldType, var_Bytes);
                        var_FieldInfo.SetValue(_Object, var_Value);
                    }

                    //
                    var_FoundFieldDictionary.Remove(var_FieldId);
                }
                else
                {
                    Console.WriteLine(var_FieldId + " not found!");
                }
            }

            foreach (var var_Pair in var_FoundFieldDictionary)
            {
                if (var_FoundFieldsOptional[var_Pair.Key])
                {
                    Console.WriteLine(var_Pair.Key + " is optional!");
                }
                else
                {
                    Console.WriteLine(var_Pair.Key + " is not optional and not set!");
                }
            }

            //Inheritance
            if (_Reader.Position != _Reader.Length)
            {
                this.InternDeSerialize(_Object, _Reader, _SerializeAbleBaseList, _CurrentIndex + 1);
            }
        }
        public bool DeSerialize(Type _Type, byte[] _Bytes, out object _Object)
        {
            if (_Type != typeof(Collider2D))
            {
                _Object = null;
                return(false);
            }

            ByteReader var_Reader = new ByteReader(_Bytes);

            bool var_IsNull = var_Reader.ReadBoolean();

            if (var_IsNull)
            {
                _Object = null;
                return(true);
            }

            //

            Collider2D var_Collider = new Collider2D();

            //Shared
            var_Collider.enabled   = var_Reader.ReadBoolean();
            var_Collider.density   = var_Reader.ReadSingle();
            var_Collider.isTrigger = var_Reader.ReadBoolean();
            var_Collider.offset    = var_Reader.ReadVector2();

            if (var_Collider is BoxCollider2D)
            {
                ((BoxCollider2D)var_Collider).autoTiling = var_Reader.ReadBoolean();
                ((BoxCollider2D)var_Collider).edgeRadius = var_Reader.ReadSingle();
                ((BoxCollider2D)var_Collider).size       = var_Reader.ReadVector2();
            }
            if (var_Collider is EdgeCollider2D)
            {
                ((EdgeCollider2D)var_Collider).edgeRadius = var_Reader.ReadSingle();

                byte[] var_Bytes = var_Reader.ReadBytesAndSize();
                ((EdgeCollider2D)var_Collider).points = (Vector2[])Serializer.DeSerialize(typeof(Vector2[]), var_Bytes);
            }
            else if (var_Collider is CapsuleCollider2D)
            {
                ((CapsuleCollider2D)var_Collider).direction = (CapsuleDirection2D)var_Reader.ReadInt32();
                ((CapsuleCollider2D)var_Collider).size      = var_Reader.ReadVector2();
            }
            else if (var_Collider is CircleCollider2D)
            {
                ((CircleCollider2D)var_Collider).radius = var_Reader.ReadSingle();
            }
            else if (var_Collider is PolygonCollider2D)
            {
                ((PolygonCollider2D)var_Collider).autoTiling = var_Reader.ReadBoolean();
                ((PolygonCollider2D)var_Collider).pathCount  = var_Reader.ReadInt32();

                byte[] var_Bytes = var_Reader.ReadBytesAndSize();
                ((PolygonCollider2D)var_Collider).points = (Vector2[])Serializer.DeSerialize(typeof(Vector2[]), var_Bytes);
            }

            _Object = var_Collider;
            return(true);
        }