diff --git a/Assets/Scenes/PacMan.unity b/Assets/Scenes/PacMan.unity
index ba728fd..957a588 100644
--- a/Assets/Scenes/PacMan.unity
+++ b/Assets/Scenes/PacMan.unity
@@ -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: []
diff --git a/Assets/Scripts/BonusFruit.cs b/Assets/Scripts/BonusFruit.cs
index 0a1f1dc..f907f2e 100644
--- a/Assets/Scripts/BonusFruit.cs
+++ b/Assets/Scripts/BonusFruit.cs
@@ -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;
}
diff --git a/Assets/Scripts/ByteUtils.cs b/Assets/Scripts/ByteUtils.cs
new file mode 100644
index 0000000..7f16099
--- /dev/null
+++ b/Assets/Scripts/ByteUtils.cs
@@ -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;
+ }
+ }
+}
\ No newline at end of file
diff --git a/Assets/Scripts/ByteUtils.cs.meta b/Assets/Scripts/ByteUtils.cs.meta
new file mode 100644
index 0000000..19def50
--- /dev/null
+++ b/Assets/Scripts/ByteUtils.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 7d2126e683a87e241ad0399cefcda807
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs
index 01c9aea..667f5bc 100644
--- a/Assets/Scripts/GameManager.cs
+++ b/Assets/Scripts/GameManager.cs
@@ -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)
{
diff --git a/Assets/Scripts/GhostManager.cs b/Assets/Scripts/GhostManager.cs
index 2c7adb0..5e8a951 100644
--- a/Assets/Scripts/GhostManager.cs
+++ b/Assets/Scripts/GhostManager.cs
@@ -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;
}
diff --git a/Assets/Scripts/GridMover.cs b/Assets/Scripts/GridMover.cs
index 25a04be..da2611a 100644
--- a/Assets/Scripts/GridMover.cs
+++ b/Assets/Scripts/GridMover.cs
@@ -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;
}
diff --git a/Assets/Scripts/Intermission2Pole.cs b/Assets/Scripts/Intermission2Pole.cs
index 6dccbb3..03008cb 100644
--- a/Assets/Scripts/Intermission2Pole.cs
+++ b/Assets/Scripts/Intermission2Pole.cs
@@ -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;
}
diff --git a/Assets/Scripts/NetworkManager.cs b/Assets/Scripts/NetworkManager.cs
index 5daed89..0a11f26 100644
--- a/Assets/Scripts/NetworkManager.cs
+++ b/Assets/Scripts/NetworkManager.cs
@@ -64,6 +64,10 @@ namespace Marro.PacManUdon
/// The maximum amount of events in the buffer.
///
private const int BufferMaxTotalEvents = 255;
+ ///
+ /// The maximum amount of events in the buffer.
+ ///
+ private const int MaxEventSize = ushort.MaxValue;
///
/// 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)
{
diff --git a/Assets/Scripts/PacMan.cs b/Assets/Scripts/PacMan.cs
index e04ca27..09331f0 100644
--- a/Assets/Scripts/PacMan.cs
+++ b/Assets/Scripts/PacMan.cs
@@ -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);
}
}
}
\ No newline at end of file
diff --git a/Assets/Scripts/PelletManager.cs b/Assets/Scripts/PelletManager.cs
index 27ebff5..a0c51bf 100644
--- a/Assets/Scripts/PelletManager.cs
+++ b/Assets/Scripts/PelletManager.cs
@@ -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;
}
diff --git a/Assets/Scripts/ScoreBonusDisplay.cs b/Assets/Scripts/ScoreBonusDisplay.cs
index 25ae69b..ef1d436 100644
--- a/Assets/Scripts/ScoreBonusDisplay.cs
+++ b/Assets/Scripts/ScoreBonusDisplay.cs
@@ -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;
}
diff --git a/Assets/Scripts/SoundManager.cs b/Assets/Scripts/SoundManager.cs
index 2112355..4418350 100644
--- a/Assets/Scripts/SoundManager.cs
+++ b/Assets/Scripts/SoundManager.cs
@@ -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;
}
diff --git a/Assets/Scripts/StatusDisplay.cs b/Assets/Scripts/StatusDisplay.cs
index 4f4d084..4d683a9 100644
--- a/Assets/Scripts/StatusDisplay.cs
+++ b/Assets/Scripts/StatusDisplay.cs
@@ -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;
}
diff --git a/Assets/Scripts/SyncedObject.cs b/Assets/Scripts/SyncedObject.cs
index 0b07bea..f3c26ca 100644
--- a/Assets/Scripts/SyncedObject.cs
+++ b/Assets/Scripts/SyncedObject.cs
@@ -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);
}
}
\ No newline at end of file
diff --git a/Assets/Test stuff/TestBall.cs b/Assets/Test stuff/TestBall.cs
index 3eeec3a..c0bbdea 100644
--- a/Assets/Test stuff/TestBall.cs
+++ b/Assets/Test stuff/TestBall.cs
@@ -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
{