baselineTick safety chekc moved into DeserializeServerDelta

This commit is contained in:
mischa 2024-10-16 16:04:02 +02:00
parent 6529756c91
commit 22bc5f3fbd

View File

@ -261,13 +261,23 @@ void SerializeServerDelta(NetworkWriter writer, byte baselineTick, Vector3? posi
if (syncScale) writer.WriteVector3(scale.Value);
}
void DeserializeServerDelta(NetworkReader reader, out byte baselineTick, out Vector3? position, out Quaternion? rotation, out Vector3? scale)
bool DeserializeServerDelta(NetworkReader reader, out byte baselineTick, out Vector3? position, out Quaternion? rotation, out Vector3? scale)
{
baselineTick = reader.ReadByte();
position = null;
rotation = null;
scale = null;
baselineTick = reader.ReadByte();
// unreliable messages may arrive out of order.
// ensure this is for the intended baseline.
// we don't want to put a delta onto an old baseline.
if (baselineTick != lastDeserializedBaselineTick)
{
Debug.Log($"[{name}] Client discarding unreliable delta for baseline #{baselineTick} because we already received #{lastDeserializedBaselineTick}");
return false;
}
if (syncPosition)
{
position = reader.ReadVector3();
@ -280,6 +290,8 @@ void DeserializeServerDelta(NetworkReader reader, out byte baselineTick, out Vec
{
scale = reader.ReadVector3();
}
return true;
}
// cmd /////////////////////////////////////////////////////////////////
@ -359,10 +371,12 @@ void RpcServerToClientDeltaSync(ArraySegment<byte> message)
{
using (NetworkReaderPooled reader = NetworkReaderPool.Get(message))
{
DeserializeServerDelta(reader, out byte baselineTick, out Vector3? position, out Quaternion? rotation, out Vector3? scale);
if (DeserializeServerDelta(reader, out byte baselineTick, out Vector3? position, out Quaternion? rotation, out Vector3? scale))
{
OnServerToClientDeltaSync(baselineTick, position, rotation, scale);
}
}
}
// server broadcasts sync message to all clients
protected virtual void OnServerToClientDeltaSync(byte baselineTick, Vector3? position, Quaternion? rotation, Vector3? scale)
@ -378,15 +392,6 @@ protected virtual void OnServerToClientDeltaSync(byte baselineTick, Vector3? pos
// don't apply for local player with authority
if (IsClientWithAuthority) return;
// unreliable messages may arrive out of order.
// ensure this is for the intended baseline.
// we don't want to put a delta onto an old baseline.
if (baselineTick != lastDeserializedBaselineTick)
{
Debug.Log($"[{name}] Client discarding unreliable delta for baseline #{baselineTick} because we already received #{lastDeserializedBaselineTick}");
return;
}
Debug.Log($"[{name}] Client: received delta for baseline #{baselineTick}");
// on the client, we receive rpcs for all entities.