diff --git a/Assets/Scripts/ByteUtils.cs b/Assets/Scripts/ByteUtils.cs index 7f16099..c648d82 100644 --- a/Assets/Scripts/ByteUtils.cs +++ b/Assets/Scripts/ByteUtils.cs @@ -5,139 +5,139 @@ namespace Marro.PacManUdon { public static class ByteUtils { - private static void Append(byte[] value, byte[] target, ref int index) + public static void Append(this byte[] target, byte[] value, 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) => + public static void Append(this byte[] target, byte value, ref int index) => target[index++] = value; - public static void Append(sbyte value, byte[] target, ref int index) => + public static void Append(this byte[] target, sbyte value, ref int index) => target[index++] = (byte)value; - public static void Append(bool value, byte[] target, ref int index) => + public static void Append(this byte[] target, bool value, 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(this byte[] target, ushort value, ref int index) => + target.Append(BitConverter.GetBytes(value), ref index); - public static void Append(short value, byte[] target, ref int index) => - Append(BitConverter.GetBytes(value), target, ref index); + public static void Append(this byte[] target, short value, ref int index) => + target.Append(BitConverter.GetBytes(value), ref index); - public static void Append(uint value, byte[] target, ref int index) => - Append(BitConverter.GetBytes(value), target, ref index); + public static void Append(this byte[] target, uint value, ref int index) => + target.Append(BitConverter.GetBytes(value), ref index); - public static void Append(int value, byte[] target, ref int index) => - Append(BitConverter.GetBytes(value), target, ref index); + public static void Append(this byte[] target, int value, ref int index) => + target.Append(BitConverter.GetBytes(value), ref index); - public static void Append(ulong value, byte[] target, ref int index) => - Append(BitConverter.GetBytes(value), target, ref index); + public static void Append(this byte[] target, ulong value, ref int index) => + target.Append(BitConverter.GetBytes(value), ref index); - public static void Append(long value, byte[] target, ref int index) => - Append(BitConverter.GetBytes(value), target, ref index); + public static void Append(this byte[] target, long value, ref int index) => + target.Append(BitConverter.GetBytes(value), ref index); - public static void Append(float value, byte[] target, ref int index) => - Append(BitConverter.GetBytes(value), target, ref index); + public static void Append(this byte[] target, float value, ref int index) => + target.Append(BitConverter.GetBytes(value), ref index); - public static void Append(double value, byte[] target, ref int index) => - Append(BitConverter.GetBytes(value), target, ref index); + public static void Append(this byte[] target, double value, ref int index) => + target.Append(BitConverter.GetBytes(value), ref index); - public static void Append(char value, byte[] target, ref int index) => - Append(BitConverter.GetBytes(value), target, ref index); + public static void Append(this byte[] target, char value, ref int index) => + target.Append(BitConverter.GetBytes(value), ref index); - public static void Append(Vector2 value, byte[] target, ref int index) + public static void Append(this byte[] target, Vector2 value, ref int index) { - Append(value.x, target, ref index); - Append(value.y, target, ref index); + target.Append(target, ref index); + target.Append(value.y, ref index); } - public static void Append(Vector3 value, byte[] target, ref int index) + public static void Append(this byte[] target, Vector3 value, ref int index) { - Append(value.x, target, ref index); - Append(value.y, target, ref index); - Append(value.z, target, ref index); + target.Append(value.x, ref index); + target.Append(value.y, ref index); + target.Append(value.z, ref index); } - public static byte ReadByte(byte[] source, ref int index) => + public static byte ReadByte(this byte[] source, ref int index) => source[index++]; - public static bool ReadBool(byte[] source, ref int index) => + public static bool ReadBool(this byte[] source, ref int index) => source[index++] > 0; - public static ushort ReadUShort(byte[] source, ref int index) + public static ushort ReadUShort(this byte[] source, ref int index) { var value = BitConverter.ToUInt16(source, index); index += 2; return value; } - public static short ReadShort(byte[] source, ref int index) + public static short ReadShort(this 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) + public static uint ReadUInt(this byte[] source, ref int index) { var value = BitConverter.ToUInt32(source, index); index += 4; return value; } - public static int ReadInt(byte[] source, ref int index) + public static int ReadInt(this byte[] source, ref int index) { var value = BitConverter.ToInt32(source, index); index += 4; return value; } - public static ulong ReadULong(byte[] source, ref int index) + public static ulong ReadULong(this byte[] source, ref int index) { var value = BitConverter.ToUInt64(source, index); index += 8; return value; } - public static long ReadLong(byte[] source, ref int index) + public static long ReadLong(this byte[] source, ref int index) { var value = BitConverter.ToInt64(source, index); index += 8; return value; } - public static float ReadFloat(byte[] source, ref int index) + public static float ReadFloat(this byte[] source, ref int index) { var value = BitConverter.ToSingle(source, index); index += 4; return value; } - public static double ReadDouble(byte[] source, ref int index) + public static double ReadDouble(this byte[] source, ref int index) { var value = BitConverter.ToDouble(source, index); index += 8; return value; } - public static char ReadChar(byte[] source, ref int index) + public static char ReadChar(this byte[] source, ref int index) { var value = BitConverter.ToChar(source, index); index += 2; return value; } - public static Vector2 ReadVector2(byte[] source, ref int index) + public static Vector2 ReadVector2(this 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) + public static Vector3 ReadVector3(this 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; diff --git a/Assets/Scripts/GridMover.cs b/Assets/Scripts/GridMover.cs index da2611a..08402fb 100644 --- a/Assets/Scripts/GridMover.cs +++ b/Assets/Scripts/GridMover.cs @@ -37,8 +37,8 @@ namespace Marro.PacManUdon return; } - ByteUtils.Append(GetPosition(), data, ref index); - ByteUtils.Append(GetDirection(), data, ref index); + data.Append(GetPosition(), ref index); + data.Append(GetDirection(), ref index); } public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType) @@ -48,8 +48,8 @@ namespace Marro.PacManUdon return true; } - SetPosition(ByteUtils.ReadVector2(data, ref index)); - SetDirection(ByteUtils.ReadVector2(data, ref index)); + SetPosition(data.ReadVector2(ref index)); + SetDirection(data.ReadVector2(ref index)); return true; } diff --git a/Assets/Scripts/PacMan.cs b/Assets/Scripts/PacMan.cs index 09331f0..0a962b0 100644 --- a/Assets/Scripts/PacMan.cs +++ b/Assets/Scripts/PacMan.cs @@ -343,7 +343,7 @@ namespace Marro.PacManUdon return; } - ByteUtils.Append(targetDirection, data, ref index); + data.Append(targetDirection, ref index); base.CollectSyncedData(data, ref index, eventType); } @@ -355,7 +355,7 @@ namespace Marro.PacManUdon return true; } - SetTargetDirection(ByteUtils.ReadVector2(data, ref index)); + SetTargetDirection(data.ReadVector2(ref index)); return base.WriteSyncedData(data, ref index, eventType); } diff --git a/Assets/Test stuff/TestBall.cs b/Assets/Test stuff/TestBall.cs index c0bbdea..c3b80f1 100644 --- a/Assets/Test stuff/TestBall.cs +++ b/Assets/Test stuff/TestBall.cs @@ -134,8 +134,8 @@ public class TestBall : SyncedObject || eventType == NetworkEventType.FullSyncForced) { Debug.Log($"({nameof(TestBall)}) Sending sync data at progress {GetProgress()} and amountUp {amountUp}."); - ByteUtils.Append(amountUp, data, ref index); - ByteUtils.Append(GetProgress(), data, ref index); + data.Append(amountUp, ref index); + data.Append(GetProgress(), ref index); } } @@ -144,8 +144,8 @@ public class TestBall : SyncedObject if (eventType == NetworkEventType.FullSync || eventType == NetworkEventType.FullSyncForced) { - amountUp = ByteUtils.ReadFloat(data, ref index); - SetProgress(ByteUtils.ReadFloat(data, ref index)); + amountUp = data.ReadFloat(ref index); + SetProgress(data.ReadFloat(ref index)); //Debug.Log($"({nameof(TestBall)}) Received sync event, synced to progress {GetProgress()} and amountUp {amountUp}."); } else