ByteUtils to extension methods

This commit is contained in:
2026-01-18 16:45:47 +01:00
parent b68b3d1c25
commit 305a0ec3a1
4 changed files with 52 additions and 52 deletions

View File

@@ -5,139 +5,139 @@ namespace Marro.PacManUdon
{ {
public static class ByteUtils 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}"); Debug.Log($"ByteUtils Append value.Length: {value.Length}, array.Length : {target.Length}, index: {index}");
Array.Copy(value, 0, target, index, value.Length); Array.Copy(value, 0, target, index, value.Length);
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; 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; 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; target[index++] = value ? (byte)1 : (byte)1;
public static void Append(ushort value, byte[] target, ref int index) => public static void Append(this byte[] target, ushort value, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index); target.Append(BitConverter.GetBytes(value), ref index);
public static void Append(short value, byte[] target, ref int index) => public static void Append(this byte[] target, short value, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index); target.Append(BitConverter.GetBytes(value), ref index);
public static void Append(uint value, byte[] target, ref int index) => public static void Append(this byte[] target, uint value, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index); target.Append(BitConverter.GetBytes(value), ref index);
public static void Append(int value, byte[] target, ref int index) => public static void Append(this byte[] target, int value, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index); target.Append(BitConverter.GetBytes(value), ref index);
public static void Append(ulong value, byte[] target, ref int index) => public static void Append(this byte[] target, ulong value, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index); target.Append(BitConverter.GetBytes(value), ref index);
public static void Append(long value, byte[] target, ref int index) => public static void Append(this byte[] target, long value, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index); target.Append(BitConverter.GetBytes(value), ref index);
public static void Append(float value, byte[] target, ref int index) => public static void Append(this byte[] target, float value, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index); target.Append(BitConverter.GetBytes(value), ref index);
public static void Append(double value, byte[] target, ref int index) => public static void Append(this byte[] target, double value, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref index); target.Append(BitConverter.GetBytes(value), ref index);
public static void Append(char value, byte[] target, ref int index) => public static void Append(this byte[] target, char value, ref int index) =>
Append(BitConverter.GetBytes(value), target, ref 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); target.Append(target, ref index);
Append(value.y, 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); target.Append(value.x, ref index);
Append(value.y, target, ref index); target.Append(value.y, ref index);
Append(value.z, target, 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++]; source[index++];
public static bool ReadBool(byte[] source, ref int index) => public static bool ReadBool(this byte[] source, ref int index) =>
source[index++] > 0; 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); var value = BitConverter.ToUInt16(source, index);
index += 2; index += 2;
return value; 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); var value = BitConverter.ToInt16(source, (int)index);
index += 2; index += 2;
return value; 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); var value = BitConverter.ToUInt32(source, index);
index += 4; index += 4;
return value; 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); var value = BitConverter.ToInt32(source, index);
index += 4; index += 4;
return value; 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); var value = BitConverter.ToUInt64(source, index);
index += 8; index += 8;
return value; 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); var value = BitConverter.ToInt64(source, index);
index += 8; index += 8;
return value; 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); var value = BitConverter.ToSingle(source, index);
index += 4; index += 4;
return value; 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); var value = BitConverter.ToDouble(source, index);
index += 8; index += 8;
return value; 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); var value = BitConverter.ToChar(source, index);
index += 2; index += 2;
return value; 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)); var value = new Vector2(BitConverter.ToSingle(source, index), BitConverter.ToSingle(source, index + 4));
index += 8; index += 8;
return value; 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)); var value = new Vector3(BitConverter.ToSingle(source, index), BitConverter.ToSingle(source, index + 4), BitConverter.ToSingle(source, index + 8));
index += 12; index += 12;

View File

@@ -37,8 +37,8 @@ namespace Marro.PacManUdon
return; return;
} }
ByteUtils.Append(GetPosition(), data, ref index); data.Append(GetPosition(), ref index);
ByteUtils.Append(GetDirection(), data, ref index); data.Append(GetDirection(), ref index);
} }
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType) public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
@@ -48,8 +48,8 @@ namespace Marro.PacManUdon
return true; return true;
} }
SetPosition(ByteUtils.ReadVector2(data, ref index)); SetPosition(data.ReadVector2(ref index));
SetDirection(ByteUtils.ReadVector2(data, ref index)); SetDirection(data.ReadVector2(ref index));
return true; return true;
} }

View File

@@ -343,7 +343,7 @@ namespace Marro.PacManUdon
return; return;
} }
ByteUtils.Append(targetDirection, data, ref index); data.Append(targetDirection, ref index);
base.CollectSyncedData(data, ref index, eventType); base.CollectSyncedData(data, ref index, eventType);
} }
@@ -355,7 +355,7 @@ namespace Marro.PacManUdon
return true; return true;
} }
SetTargetDirection(ByteUtils.ReadVector2(data, ref index)); SetTargetDirection(data.ReadVector2(ref index));
return base.WriteSyncedData(data, ref index, eventType); return base.WriteSyncedData(data, ref index, eventType);
} }

View File

@@ -134,8 +134,8 @@ public class TestBall : SyncedObject
|| eventType == NetworkEventType.FullSyncForced) || eventType == NetworkEventType.FullSyncForced)
{ {
Debug.Log($"({nameof(TestBall)}) Sending sync data at progress {GetProgress()} and amountUp {amountUp}."); Debug.Log($"({nameof(TestBall)}) Sending sync data at progress {GetProgress()} and amountUp {amountUp}.");
ByteUtils.Append(amountUp, data, ref index); data.Append(amountUp, ref index);
ByteUtils.Append(GetProgress(), data, ref index); data.Append(GetProgress(), ref index);
} }
} }
@@ -144,8 +144,8 @@ public class TestBall : SyncedObject
if (eventType == NetworkEventType.FullSync if (eventType == NetworkEventType.FullSync
|| eventType == NetworkEventType.FullSyncForced) || eventType == NetworkEventType.FullSyncForced)
{ {
amountUp = ByteUtils.ReadFloat(data, ref index); amountUp = data.ReadFloat(ref index);
SetProgress(ByteUtils.ReadFloat(data, ref index)); SetProgress(data.ReadFloat(ref index));
//Debug.Log($"({nameof(TestBall)}) Received sync event, synced to progress {GetProgress()} and amountUp {amountUp}."); //Debug.Log($"({nameof(TestBall)}) Received sync event, synced to progress {GetProgress()} and amountUp {amountUp}.");
} }
else else