Switched to 1d array CollectSyncedData

This commit is contained in:
2026-01-17 19:25:45 +01:00
parent 3642006bb2
commit b68b3d1c25
16 changed files with 216 additions and 78 deletions

View File

@@ -54317,11 +54317,11 @@ MonoBehaviour:
DynamicPrefabs: []
DynamicMaterials:
- {fileID: 10754, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 2100000, guid: fc67956adbc95584ba3ae3b18d1e430d, type: 2}
- {fileID: 2541243872366204002, guid: 7168d13094fcae94b81e672d1f4ab73f, type: 2}
- {fileID: 10758, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0}
- {fileID: 2100000, guid: a3869014ba4893d409f150224856180a, type: 2}
- {fileID: 2100000, guid: fc67956adbc95584ba3ae3b18d1e430d, type: 2}
- {fileID: 2100000, guid: ae0c7562e66fca1469f7ee603e462581, type: 2}
- {fileID: 2100000, guid: db2b6d76bdd134244912a5c1c9971804, type: 2}
LightMapsNear: []

View File

@@ -82,12 +82,12 @@ namespace Marro.PacManUdon
this.active = active;
}
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
}
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
return true;
}

147
Assets/Scripts/ByteUtils.cs Normal file
View File

@@ -0,0 +1,147 @@
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;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7d2126e683a87e241ad0399cefcda807
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -403,7 +403,7 @@ namespace Marro.PacManUdon
}
}
public override void AppendSyncedData(byte[][] data, ref int offset, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int offset, NetworkEventType eventType)
{
//data[offset++] = new byte[] { NetworkManager.Int32ToByte((int)gameState) };
//data[offset++] = BitConverter.GetBytes(currentlyInTimeSequence);
@@ -411,7 +411,7 @@ namespace Marro.PacManUdon
//data[offset++] = BitConverter.GetBytes(timeSequenceSecondsPassed);
}
public override bool SetSyncedData(byte[] data, ref int offset, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int offset, NetworkEventType eventType)
{
if (eventType == NetworkEventType.StartGameButtonPressed)
{

View File

@@ -470,12 +470,12 @@ namespace Marro.PacManUdon
}
}
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
}
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
return true;
}

View File

@@ -30,32 +30,26 @@ namespace Marro.PacManUdon
protected abstract void UpdateAnimator();
public override void AppendSyncedData(byte[][] data, ref int offset, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.PacManTurn)
{
return;
}
var position = GetPosition();
data[offset++] = BitConverter.GetBytes(position.x);
data[offset++] = BitConverter.GetBytes(position.y);
var direction = GetDirection();
data[offset++] = BitConverter.GetBytes(direction.x);
data[offset++] = BitConverter.GetBytes(direction.y);
ByteUtils.Append(GetPosition(), data, ref index);
ByteUtils.Append(GetDirection(), data, ref index);
}
public override bool SetSyncedData(byte[] data, ref int offset, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.PacManTurn)
{
return true;
}
SetPosition(new Vector2(BitConverter.ToSingle(data, offset), BitConverter.ToSingle(data, offset + 4)));
SetDirection(new Vector2(BitConverter.ToSingle(data, offset + 8), BitConverter.ToSingle(data, offset + 12)));
offset += 16;
SetPosition(ByteUtils.ReadVector2(data, ref index));
SetDirection(ByteUtils.ReadVector2(data, ref index));
return true;
}

View File

@@ -144,12 +144,12 @@ namespace Marro.PacManUdon
return (Vector2)transform.localPosition;
}
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
}
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
return true;
}

View File

@@ -64,6 +64,10 @@ namespace Marro.PacManUdon
/// The maximum amount of events in the buffer.
/// </summary>
private const int BufferMaxTotalEvents = 255;
/// <summary>
/// The maximum amount of events in the buffer.
/// </summary>
private const int MaxEventSize = ushort.MaxValue;
/// <summary>
/// The index in an event where the event size is stored.
@@ -396,36 +400,27 @@ namespace Marro.PacManUdon
var eventId = GetNextEventId(lastEventId);
InitializeEvent(eventType, timestamp, eventId, BufferMaxTotalEvents, out byte[][] data, out var index);
InitializeEvent(eventType, timestamp, eventId, out byte[] data, out var index);
foreach (var obj in syncedObjects)
{
obj.AppendSyncedData(data, ref index, eventType);
obj.CollectSyncedData(data, ref index, eventType);
}
var result = Flatten(data, 0, index);
// Validate and fill in event size
var eventSize = result.Length;
if (eventSize > ushort.MaxValue || eventSize < 0)
{
Debug.LogError($"({nameof(PacManUdon)} {nameof(NetworkManager)}) New event is too large or negative! Size is {eventSize}, maximum allowed is {ushort.MaxValue}");
HandleError(false);
return;
}
var eventSizeBytes = BitConverter.GetBytes((ushort)eventSize);
Array.Copy(eventSizeBytes, 0, result, HeaderEventSizeIndex, eventSizeBytes.Length);
var eventSizeBytes = BitConverter.GetBytes((ushort)index);
Array.Copy(eventSizeBytes, 0, data, HeaderEventSizeIndex, eventSizeBytes.Length);
if (IsFullSync(eventType))
{
hasFullSyncReady = true;
}
QueueEventInBuffer(result);
data = GetArrayPart(data, 0, index);
Debug.Log($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Prepared event of type {eventType} with {eventSize} bytes, timestamp {timestamp} and id {eventId} for serialization.");
QueueEventInBuffer(data);
Debug.Log($"({nameof(PacManUdon)} {nameof(NetworkManager)}) Prepared event of type {eventType} with {data.Length} bytes, timestamp {timestamp} and id {eventId} for serialization.");
RequestSerializationForEvents();
@@ -434,22 +429,16 @@ namespace Marro.PacManUdon
retriesWithoutSuccess = 0; // We had success!
}
private static void InitializeEvent(NetworkEventType eventType, float timestamp, byte eventId, int maxSize, out byte[][] data, out int index)
private static void InitializeEvent(NetworkEventType eventType, float timestamp, byte eventId, out byte[] data, out int index)
{
// Create header
data = new byte[MaxEventSize];
// Create header (note: event size is added later)
var timestampBytes = BitConverter.GetBytes(timestamp);
var header = new byte[HeaderLength];
// Event size is added later
Array.Copy(timestampBytes, 0, header, HeaderTimestampIndex, timestampBytes.Length);
header[HeaderEventIdIndex] = eventId;
header[HeaderEventTypeIndex] = Int32ToByte((int)eventType);
// Initialize event container
data = new byte[maxSize][];
data[0] = header;
index = 1;
Array.Copy(timestampBytes, 0, data, HeaderTimestampIndex, timestampBytes.Length);
data[HeaderEventIdIndex] = eventId;
data[HeaderEventTypeIndex] = Int32ToByte((int)eventType);
index = HeaderLength;
}
private void RequestSerializationForEvents()
@@ -696,7 +685,7 @@ namespace Marro.PacManUdon
foreach (var obj in syncedObjects)
{
var success = obj.SetSyncedData(@event, ref index, eventType);
var success = obj.WriteSyncedData(@event, ref index, eventType);
if (!success)
{

View File

@@ -336,30 +336,28 @@ namespace Marro.PacManUdon
}
}
public override void AppendSyncedData(byte[][] data, ref int offset, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.PacManTurn || kinematic || frozen || !enabled)
{
return;
}
data[offset++] = BitConverter.GetBytes(targetDirection.x);
data[offset++] = BitConverter.GetBytes(targetDirection.y);
ByteUtils.Append(targetDirection, data, ref index);
base.AppendSyncedData(data, ref offset, eventType);
base.CollectSyncedData(data, ref index, eventType);
}
public override bool SetSyncedData(byte[] data, ref int offset, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType != NetworkEventType.PacManTurn || kinematic || frozen || !enabled)
{
return true;
}
SetTargetDirection(new Vector2(BitConverter.ToSingle(data, offset), BitConverter.ToSingle(data, offset + 4)));
offset += 8;
SetTargetDirection(ByteUtils.ReadVector2(data, ref index));
return base.SetSyncedData(data, ref offset, eventType);
return base.WriteSyncedData(data, ref index, eventType);
}
}
}

View File

@@ -84,12 +84,12 @@ namespace Marro.PacManUdon
}
}
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
}
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
return true;
}

View File

@@ -47,12 +47,12 @@ namespace Marro.PacManUdon
gameObject.SetActive(false);
}
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
}
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
return true;
}

View File

@@ -254,12 +254,12 @@ public class SoundManager : SyncedObject
};
}
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
}
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
return true;
}

View File

@@ -167,12 +167,12 @@ namespace Marro.PacManUdon
}
}
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
}
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
return true;
}

View File

@@ -9,7 +9,7 @@ namespace Marro.PacManUdon
public NetworkManager networkManager;
public virtual void SyncedUpdate() { }
public abstract void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType);
public abstract bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType);
public abstract void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType);
public abstract bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType);
}
}

View File

@@ -128,26 +128,25 @@ public class TestBall : SyncedObject
Debug.Log($"({nameof(TestBall)}) Up button pressed, jumped up at {GetProgress()} to {amountUp}.");
}
public override void AppendSyncedData(byte[][] data, ref int index, NetworkEventType eventType)
public override void CollectSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType == NetworkEventType.FullSync
|| eventType == NetworkEventType.FullSyncForced)
{
Debug.Log($"({nameof(TestBall)}) Sending sync data at progress {GetProgress()} and amountUp {amountUp}.");
data[index++] = BitConverter.GetBytes(amountUp);
data[index++] = BitConverter.GetBytes(GetProgress());
ByteUtils.Append(amountUp, data, ref index);
ByteUtils.Append(GetProgress(), data, ref index);
}
}
public override bool SetSyncedData(byte[] data, ref int index, NetworkEventType eventType)
public override bool WriteSyncedData(byte[] data, ref int index, NetworkEventType eventType)
{
if (eventType == NetworkEventType.FullSync
|| eventType == NetworkEventType.FullSyncForced)
{
amountUp = BitConverter.ToSingle(data, index);
SetProgress(BitConverter.ToSingle(data, index + 4));
amountUp = ByteUtils.ReadFloat(data, ref index);
SetProgress(ByteUtils.ReadFloat(data, ref index));
//Debug.Log($"({nameof(TestBall)}) Received sync event, synced to progress {GetProgress()} and amountUp {amountUp}.");
index += 8;
}
else
{