Switched to 1d array CollectSyncedData
This commit is contained in:
@@ -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
147
Assets/Scripts/ByteUtils.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/ByteUtils.cs.meta
Normal file
11
Assets/Scripts/ByteUtils.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d2126e683a87e241ad0399cefcda807
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user