2019-03-27 【c#】【轉(zhuǎn)】字節(jié)流序列化存儲

安凱哥哥的代碼,安凱哥哥牛逼!
用到了移位運(yùn)算,強(qiáng)轉(zhuǎn)截取精度方式將數(shù)值序列化byte二進(jìn)制。
據(jù)安凱哥哥說效率比自帶的方法高。
還有秋雨說的scriptable。
等真正使用的時(shí)候再來看。

// ==========================================
// 描述: 
// 作者: HAK
// 時(shí)間: 2018-11-13 08:38:23
// 版本: V 1.0
// ==========================================
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

/// <summary>
/// 提供了一種基于字節(jié)流的協(xié)議
public class ProtocolBytes
{
    private byte[] bytes;     //傳輸?shù)淖止?jié)流
    private List<byte> byteList;
    private int index;

    public ProtocolBytes()
    {
        index = 0;
        byteList = new List<byte>();
    }

    public ProtocolBytes(byte[] _bytes)
    {
        index = 0;
        bytes = _bytes;
        byteList = new List<byte>(_bytes);
    }

    /// <summary>
    /// 編碼器
    /// </summary>
    /// <returns></returns>
    public byte[] Encode()
    {
        return byteList.ToArray();
    }

    /// <summary>
    /// 協(xié)議內(nèi)容 提取每一個字節(jié)并組成字符串 用于查看消息
    /// </summary>
    /// <returns></returns>
    public string GetDesc()
    {
        string str = "";
        if (bytes == null) return str;
        for (int i = 0; i < bytes.Length; i++)
        {
            int b = (int)bytes[i];
            str += b.ToString() + " ";
        }
        return str;
    }

    #region 添加和獲取字符串

    /// <summary>
    /// 將字符轉(zhuǎn)轉(zhuǎn)為字節(jié)數(shù)組加入字節(jié)流
    /// </summary>
    /// <param name="str">要添加的字符串</param>
    public void AddString(string str)
    {
        Int32 len = str.Length;
        byte[] lenBytes = BitConverter.GetBytes(len);
        byte[] strBytes = Encoding.UTF8.GetBytes(str);
        byteList.AddRange(lenBytes);
        byteList.AddRange(strBytes);
    }

    /// <summary>
    /// 將字節(jié)數(shù)組轉(zhuǎn)化為字符串
    /// </summary>
    /// <param name="index">索引起點(diǎn)</param>
    /// <param name="end">為下一個轉(zhuǎn)換提供索引起點(diǎn)</param>
    /// <returns></returns>
    public string GetString()
    {
        if (bytes == null)
            return "";
        if (bytes.Length < index + sizeof(int))
            return "";
        int strLen = BitConverter.ToInt32(bytes, index);
        if (bytes.Length < index + sizeof(int) + strLen)
            return "";
        string str = Encoding.UTF8.GetString(bytes, index + sizeof(int), strLen);
        index = index + sizeof(int) + strLen;
        return str;
    }

    #endregion

    #region 添加獲取整數(shù)

    /// <summary>
    /// 將Int32轉(zhuǎn)化成字節(jié)數(shù)組加入字節(jié)流
    /// </summary>
    /// <param name="num">要轉(zhuǎn)化的Int32</param>
    public void AddInt32(int num)
    {
        byteList.Add((byte)num);
        byteList.Add((byte)(num >> 8));
        byteList.Add((byte)(num >> 16));
        byteList.Add((byte)(num >> 24));
    }

    /// <summary>
    /// 將字節(jié)數(shù)組轉(zhuǎn)化成Int32
    /// </summary>
    public int GetInt32()
    {
        if (bytes == null)
            return 0;
        if (bytes.Length < index + 4)
            return 0;

        return (int)(bytes[index++] | bytes[index++] << 8 | bytes[index++] << 16 | bytes[index++] << 24);
    }

    #endregion

    #region 添加獲取浮點(diǎn)數(shù)

    /// <summary>
    /// 將float轉(zhuǎn)化成字節(jié)數(shù)組加入字節(jié)流
    /// </summary>
    /// <param name="num">要轉(zhuǎn)化的float</param>
    public unsafe void AddFloat(float num)
    {
        uint temp = *(uint*)&num;
        byteList.Add((byte)temp);
        byteList.Add((byte)(temp >> 8));
        byteList.Add((byte)(temp >> 16));
        byteList.Add((byte)(temp >> 24));
    }

    /// <summary>
    /// 將字節(jié)數(shù)組轉(zhuǎn)化成float
    /// </summary>
    public unsafe float GetFloat()
    {
        if (bytes == null)
            return -1;
        if (bytes.Length < index + sizeof(float))
            return -1;
        uint temp = (uint)(bytes[index++] | bytes[index++] << 8 | bytes[index++] << 16 | bytes[index++] << 24);
        return *((float*)&temp);
    }

    #endregion

    #region 添加獲取布爾值

    public void AddBoolen(bool value)
    {
        byteList.Add((byte)(value ? 1 : 0));
    }

    public bool GetBoolen()
    {
        return (bytes[index++] == 1);
    }

    #endregion

    #region 添加獲取Vector3

    public void AddVector3(Vector3 v)
    {
        AddFloat(v.x);
        AddFloat(v.y);
        AddFloat(v.z);
    }

    public Vector3 GetVector3()
    {
        float x = GetFloat();
        float y = GetFloat();
        float z = GetFloat();
        return new Vector3(x, y, z);
    }

    #endregion

    #region 添加獲取數(shù)組

    public void AddFloatArray1(float[] array)
    {
        AddInt32(array.GetLength(0));

        for (int i = 0, length_0 = array.GetLength(0); i < length_0; i++)
        {
            AddFloat(array[i]);
        }
    }

    public void AddFloatArray2(float[,] array)
    {
        AddInt32(array.GetLength(0));
        AddInt32(array.GetLength(1));

        for (int i = 0, length_0 = array.GetLength(0); i < length_0; i++)
        {
            for (int j = 0, length_1 = array.GetLength(1); j < length_1; j++)
            {
                AddFloat(array[i, j]);
            }
        }
    }

    public void AddFloatArray3(float[,,] array)
    {
        AddInt32(array.GetLength(0));
        AddInt32(array.GetLength(1));
        AddInt32(array.GetLength(2));

        for (int i = 0, length_0 = array.GetLength(0); i < length_0; i++)
        {
            for (int j = 0, length_1 = array.GetLength(1); j < length_1; j++)
            {
                for (int k = 0, length_2 = array.GetLength(2); k < length_2; k++)
                {
                    AddFloat(array[i, j, k]);
                }
            }
        }
    }


    public float[] GetFloatArray1()
    {
        int length_0 = GetInt32();

        float[] array = new float[length_0];
        for (int i = 0; i < length_0; i++)
        {
            array[i] = GetFloat();
        }

        return array;
    }

    public float[,] GetFloatArray2()
    {
        int length_0 = GetInt32();
        int length_1 = GetInt32();

        float[,] array = new float[length_0, length_1];
        for (int i = 0; i < length_0; i++)
        {
            for (int j = 0; j < length_1; j++)
            {
                array[i, j] = GetFloat();
            }
        }

        return array;
    }

    public float[,,] GetFloatArray3()
    {
        int length_0 = GetInt32();
        int length_1 = GetInt32();
        int length_2 = GetInt32();

        float[,,] array = new float[length_0, length_1, length_2];
        for (int i = 0; i < length_0; i++)
        {
            for (int j = 0; j < length_1; j++)
            {
                for (int k = 0; k < length_2; k++)
                {
                    array[i, j, k] = GetFloat();
                }
            }
        }

        return array;
    }


    public void AddIntArray1(int[] array)
    {
        AddInt32(array.GetLength(0));

        for (int i = 0, length_0 = array.GetLength(0); i < length_0; i++)
        {
            AddInt32(array[i]);
        }
    }

    public void AddIntArray2(int[,] array)
    {
        AddInt32(array.GetLength(0));
        AddInt32(array.GetLength(1));

        for (int i = 0, length_0 = array.GetLength(0); i < length_0; i++)
        {
            for (int j = 0, length_1 = array.GetLength(1); j < length_1; j++)
            {
                AddInt32(array[i, j]);
            }
        }
    }

    public void AddIntArray3(int[,,] array)
    {
        AddInt32(array.GetLength(0));
        AddInt32(array.GetLength(1));
        AddInt32(array.GetLength(2));

        for (int i = 0, length_0 = array.GetLength(0); i < length_0; i++)
        {
            for (int j = 0, length_1 = array.GetLength(1); j < length_1; j++)
            {
                for (int k = 0, length_2 = array.GetLength(2); k < length_2; k++)
                {
                    AddInt32(array[i, j, k]);
                }
            }
        }
    }


    public int[] GetIntArray1()
    {
        int length_0 = GetInt32();

        int[] array = new int[length_0];
        for (int i = 0; i < length_0; i++)
        {
            array[i] = GetInt32();
        }

        return array;
    }

    public int[,] GetIntArray2()
    {
        int length_0 = GetInt32();
        int length_1 = GetInt32();

        int[,] array = new int[length_0, length_1];
        for (int i = 0; i < length_0; i++)
        {
            for (int j = 0; j < length_1; j++)
            {
                array[i, j] = GetInt32();
            }
        }

        return array;
    }

    public int[,,] GetIntArray3()
    {
        int length_0 = GetInt32();
        int length_1 = GetInt32();
        int length_2 = GetInt32();

        int[,,] array = new int[length_0, length_1, length_2];
        for (int i = 0; i < length_0; i++)
        {
            for (int j = 0; j < length_1; j++)
            {
                for (int k = 0; k < length_2; k++)
                {
                    array[i, j, k] = GetInt32();
                }
            }
        }

        return array;
    }

    public void AddVectorArray1(float[] array)
    {
        AddInt32(array.GetLength(0));

        for (int i = 0, length_0 = array.GetLength(0); i < length_0; i++)
        {
            AddFloat(array[i]);
        }
    }

    #endregion


    /// <summary>
    /// 添加幀同步
    /// </summary>
    public void AddFrameSynInfo(int id, Transform tran)
    {
        AddInt32(id);
        AddVector3(tran.position);
        AddVector3(tran.localEulerAngles);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容