Exemplo n.º 1
0
        private object BytesToDBType(DBType type, byte[] data)
        {
            FloatByteMap floatMap = new FloatByteMap();

            switch (type)
            {
            case DBType.String:
                return(Encoding.UTF8.GetString(data));

            case DBType.Blob:
            case DBType.ByteArray:
                return(new List <byte>(data));

            case DBType.UShortArray:
                List <ushort> uShortList = new List <ushort>(data.Length / 2);
                if (data.Length > 1)
                {
                    for (int i = 0; i < data.Length; i += 2)
                    {
                        uShortList.Add(UShortFromBufferLE(ref data, i));
                    }
                }
                return(uShortList);

            case DBType.UIntArray:
                List <uint> uIntList = new List <uint>(data.Length / 4);
                if (data.Length > 1)
                {
                    for (int i = 0; i < data.Length; i += 4)
                    {
                        uIntList.Add(UIntFromBufferLE(ref data, i));
                    }
                }
                return(uIntList);

            case DBType.Vector2Array:
                List <Vector2> vector2List = new List <Vector2>(data.Length / 8);
                if (data.Length > 1)
                {
                    for (int i = 0; i < data.Length; i += 8)
                    {
                        Vector2 v2 = new Vector2();
                        v2.x = FloatFromBufferLE(ref data, ref floatMap, i);
                        v2.y = FloatFromBufferLE(ref data, ref floatMap, i + 4);
                        vector2List.Add(v2);
                    }
                }
                return(vector2List);

            case DBType.Vector3Array:
                List <Vector3> vector3List = new List <Vector3>(data.Length / 12);
                if (data.Length > 1)
                {
                    for (int i = 0; i < data.Length; i += 12)
                    {
                        Vector3 v3 = new Vector3();
                        v3.x = FloatFromBufferLE(ref data, ref floatMap, i);
                        v3.y = FloatFromBufferLE(ref data, ref floatMap, i + 4);
                        v3.z = FloatFromBufferLE(ref data, ref floatMap, i + 8);
                        vector3List.Add(v3);
                    }
                }
                return(vector3List);

            case DBType.Vector4Array:
                List <Vector4> vector4List = new List <Vector4>(data.Length / 16);
                if (data.Length > 1)
                {
                    for (int i = 0; i < data.Length; i += 16)
                    {
                        Vector4 v4 = new Vector4();
                        v4.x = FloatFromBufferLE(ref data, ref floatMap, i);
                        v4.y = FloatFromBufferLE(ref data, ref floatMap, i + 4);
                        v4.z = FloatFromBufferLE(ref data, ref floatMap, i + 8);
                        v4.w = FloatFromBufferLE(ref data, ref floatMap, i + 12);
                        vector4List.Add(v4);
                    }
                }
                return(vector4List);
            }
            return(null);
        }
Exemplo n.º 2
0
        private byte[] DBTypeToBytes(DBType type, object data)
        {
            FloatByteMap floatMap = new FloatByteMap();

            byte[] bytes = null;

            switch (type)
            {
            case DBType.String:
                bytes = Encoding.UTF8.GetBytes((string)data);
                break;

            case DBType.Blob:
            case DBType.ByteArray:
                bytes = ((List <byte>)data).ToArray();
                break;

            case DBType.UShortArray:
                List <ushort> uShortList = (List <ushort>)data;
                bytes = new byte[uShortList.Count * 2];
                for (int i = 0; i < uShortList.Count; i++)
                {
                    WriteToBufferLE(ref bytes, uShortList[i], i * 2);
                }
                break;

            case DBType.UIntArray:
                List <uint> uIntList = (List <uint>)data;
                bytes = new byte[uIntList.Count * 4];
                for (int i = 0; i < uIntList.Count; i++)
                {
                    WriteToBufferLE(ref bytes, uIntList[i], i * 4);
                }
                break;

            case DBType.Vector2Array:
                List <Vector2> vector2List = (List <Vector2>)data;

                bytes = new byte[vector2List.Count * 8];
                for (int i = 0, x = 0; i < vector2List.Count; i++, x += 8)
                {
                    WriteToBufferLE(ref bytes, ref floatMap, vector2List[i].x, x);
                    WriteToBufferLE(ref bytes, ref floatMap, vector2List[i].y, x + 4);
                }
                break;

            case DBType.Vector3Array:
                List <Vector3> vector3List = (List <Vector3>)data;
                bytes = new byte[vector3List.Count * 12];
                for (int i = 0, x = 0; i < vector3List.Count; i++, x += 12)
                {
                    WriteToBufferLE(ref bytes, ref floatMap, vector3List[i].x, x);
                    WriteToBufferLE(ref bytes, ref floatMap, vector3List[i].y, x + 4);
                    WriteToBufferLE(ref bytes, ref floatMap, vector3List[i].z, x + 8);
                }
                break;

            case DBType.Vector4Array:
                List <Vector4> vector4List = (List <Vector4>)data;
                bytes = new byte[vector4List.Count * 16];
                for (int i = 0, x = 0; i < vector4List.Count; i++, x += 16)
                {
                    WriteToBufferLE(ref bytes, ref floatMap, vector4List[i].x, x);
                    WriteToBufferLE(ref bytes, ref floatMap, vector4List[i].y, x + 4);
                    WriteToBufferLE(ref bytes, ref floatMap, vector4List[i].z, x + 8);
                    WriteToBufferLE(ref bytes, ref floatMap, vector4List[i].w, x + 12);
                }
                break;
            }
            return(bytes);
        }