Files
PacManUdon/Assets/Scripts/ByteUtils.cs

147 lines
5.0 KiB
C#

using System;
using UnityEngine;
namespace Marro.PacManUdon
{
public static class ByteUtils
{
private static void Append(byte[] value, byte[] target, ref int index)
{
Debug.Log($"ByteUtils Append value.Length: {value.Length}, array.Length : {target.Length}, index: {index}");
Array.Copy(value, 0, target, index, value.Length);
index += value.Length;
}
public static void Append(byte value, byte[] target, ref int index) =>
target[index++] = value;
public static void Append(sbyte value, byte[] target, ref int index) =>
target[index++] = (byte)value;
public static void Append(bool value, byte[] target, ref int index) =>
target[index++] = value ? (byte)1 : (byte)1;
public static void Append(ushort value, byte[] target, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index);
public static void Append(short value, byte[] target, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index);
public static void Append(uint value, byte[] target, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index);
public static void Append(int value, byte[] target, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index);
public static void Append(ulong value, byte[] target, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index);
public static void Append(long value, byte[] target, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index);
public static void Append(float value, byte[] target, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index);
public static void Append(double value, byte[] target, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index);
public static void Append(char value, byte[] target, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index);
public static void Append(Vector2 value, byte[] target, ref int index)
{
Append(value.x, target, ref index);
Append(value.y, target, ref index);
}
public static void Append(Vector3 value, byte[] target, ref int index)
{
Append(value.x, target, ref index);
Append(value.y, target, ref index);
Append(value.z, target, ref index);
}
public static byte ReadByte(byte[] source, ref int index) =>
source[index++];
public static bool ReadBool(byte[] source, ref int index) =>
source[index++] > 0;
public static ushort ReadUShort(byte[] source, ref int index)
{
var value = BitConverter.ToUInt16(source, index);
index += 2;
return value;
}
public static short ReadShort(byte[] source, ref int index)
{
var value = BitConverter.ToInt16(source, (int)index);
index += 2;
return value;
}
public static uint ReadUInt(byte[] source, ref int index)
{
var value = BitConverter.ToUInt32(source, index);
index += 4;
return value;
}
public static int ReadInt(byte[] source, ref int index)
{
var value = BitConverter.ToInt32(source, index);
index += 4;
return value;
}
public static ulong ReadULong(byte[] source, ref int index)
{
var value = BitConverter.ToUInt64(source, index);
index += 8;
return value;
}
public static long ReadLong(byte[] source, ref int index)
{
var value = BitConverter.ToInt64(source, index);
index += 8;
return value;
}
public static float ReadFloat(byte[] source, ref int index)
{
var value = BitConverter.ToSingle(source, index);
index += 4;
return value;
}
public static double ReadDouble(byte[] source, ref int index)
{
var value = BitConverter.ToDouble(source, index);
index += 8;
return value;
}
public static char ReadChar(byte[] source, ref int index)
{
var value = BitConverter.ToChar(source, index);
index += 2;
return value;
}
public static Vector2 ReadVector2(byte[] source, ref int index)
{
var value = new Vector2(BitConverter.ToSingle(source, index), BitConverter.ToSingle(source, index + 4));
index += 8;
return value;
}
public static Vector3 ReadVector3(byte[] source, ref int index)
{
var value = new Vector3(BitConverter.ToSingle(source, index), BitConverter.ToSingle(source, index + 4), BitConverter.ToSingle(source, index + 8));
index += 12;
return value;
}
}
}