MS-LIMA / PhotonSerializer

Custom Type Serializer For Photon(PUN2), Unity.
MIT License
18 stars 1 forks source link

Custom serialization for byte[] #2

Closed uurha closed 2 years ago

uurha commented 2 years ago

Hello, thanks for creating this serializer. It's very helpful, could you please help me with extending your serializer. I want to add serializing for byte[], but it gives me error about lenght of byte array, but I done this same as int[].

public static void Serialize(byte value, ref byte[] bytes)
{
    byte[] _bytes = BitConverter.GetBytes(value);
    if (BitConverter.IsLittleEndian)
        Array.Reverse(_bytes);

    JoinBytes(ref bytes, _bytes);
}

public static void Serialize(byte[] values, ref byte[] bytes)
{
    int length = values == null ? 0 : values.Length;
    if (length > 0)
    {
        Serialize(length, ref bytes);
        for (int i = 0; i < values.Length; i++)
        {
            Serialize(values[i], ref bytes);
        }
    }
    else
    {
        Serialize(0, ref bytes);
    }
}

public static byte DeserializeByte(byte[] bytes, ref int offset)
{
    byte[] _bytes = new byte[4];
    Array.Copy(bytes, offset, _bytes, 0, 4);

    if (BitConverter.IsLittleEndian)
        Array.Reverse(_bytes);

    offset += 4;
    return (byte)BitConverter.ToInt16(_bytes, 0);

    // int, float, bool vecot3, ve2 ,qua, string
}

public static byte[] DeserializeByteArray(byte[] bytes, ref int offset)
{
    int length = DeserializeInt(bytes, ref offset);
    if (length > 0)
    {
        byte[] array = new byte[length];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = DeserializeByte(bytes, ref offset);
        }

        return array;
    }

    return new byte[0];
}
MS-LIMA commented 2 years ago

Hi, thank you for your interest in my archive.

In fact, BitConverter serializes other data types into bytes, therefore, using BitConverter is not necessary to serialize bytes!

Try this below.

public static void Serialize(byte value, ref byte[] bytes)
        {
            byte[] _bytes = new byte[] {value};
            bytes = bytes.Concat(_bytes).ToArray();
        }

public static byte DeserializeByte(byte[] bytes, ref int offset)
{
    byte _byte = bytes[offset];

    offset += 1;
    return _byte;
}

public static byte[] DeserializeByteArray(byte[] bytes, ref int offset)
{
    int length = DeserializeInt(bytes, ref offset);
    if (length > 0)
    {
        byte[] array = new byte[length];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = DeserializeByte(bytes, ref offset);
        }

        return array;
    }

    return new byte[0];
}
uurha commented 2 years ago

Thanks. I'll come then I'll finish tests)

uurha commented 2 years ago

Only one change for Serialize(byte[])

        public static void Serialize(byte[] value, ref byte[] bytes)
        {
            int length = value?.Length ?? 0;
            if (length > 0)
            {
                Serialize(length, ref bytes);
                JoinBytes(ref bytes, value);
            }
            else
            {
                Serialize(0, ref bytes);
            }
        }

Well tested and working good, thanks for your help) Should I close the issue?

MS-LIMA commented 2 years ago

Yes, please. Thanks for your advice, I'll update it to my serializer later on.

uurha commented 2 years ago

Yes, please. Thanks for your advice, I'll update it to my serializer later on.

I can make pull request from fork. Is that will be good?)

MS-LIMA commented 2 years ago

I can make pull request from fork. Is that will be good?)

Sure, :)