common serialize funcs for delta

This commit is contained in:
mischa 2024-10-16 14:33:14 +02:00
parent e4375e4f62
commit c3d383f206

View File

@ -204,9 +204,14 @@ void CmdClientToServerSync(Vector3? position, Quaternion? rotation, Vector3? sca
if (syncDirection == SyncDirection.ClientToServer &&
connectionToClient != null && // CUSTOM CHANGE: for the drop thing..
!disableSendingThisToClients) // CUSTOM CHANGE: see comment at definition
{
using (NetworkWriterPooled writer = NetworkWriterPool.Get())
{
Debug.LogWarning($"[{name}] CmdClientToServerSync: TODO which baseline to pass in Rpc?");
RpcServerToClientDeltaSync(0xFF, position, rotation, scale);
SerializeServerDelta(writer, 0xFF, position, rotation, scale);
RpcServerToClientDeltaSync(writer);
}
}
}
@ -302,6 +307,36 @@ void DeserializeServerBaseline(NetworkReader reader)
}
}
void SerializeServerDelta(NetworkWriter writer, byte baselineTick, Vector3? position, Quaternion? rotation, Vector3? scale)
{
writer.WriteByte(baselineTick);
if (syncPosition) writer.WriteVector3(position.Value);
if (syncRotation) writer.WriteQuaternion(rotation.Value);
if (syncScale) writer.WriteVector3(scale.Value);
}
void DeserializeServerDelta(NetworkReader reader, out byte baselineTick, out Vector3? position, out Quaternion? rotation, out Vector3? scale)
{
baselineTick = reader.ReadByte();
position = null;
rotation = null;
scale = null;
if (syncPosition)
{
position = reader.ReadVector3();
}
if (syncRotation)
{
rotation = reader.ReadQuaternion();
}
if (syncScale)
{
scale = reader.ReadVector3();
}
}
// rpc /////////////////////////////////////////////////////////////////
[ClientRpc(channel = Channels.Reliable)]
void RpcServerToClientBaselineSync(ArraySegment<byte> message)
@ -315,8 +350,14 @@ void RpcServerToClientBaselineSync(ArraySegment<byte> message)
// only unreliable. see comment above of this file.
[ClientRpc(channel = Channels.Unreliable)]
void RpcServerToClientDeltaSync(byte baselineTick, Vector3? position, Quaternion? rotation, Vector3? scale) =>
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);
OnServerToClientDeltaSync(baselineTick, position, rotation, scale);
}
}
// server broadcasts sync message to all clients
protected virtual void OnServerToClientDeltaSync(byte baselineTick, Vector3? position, Quaternion? rotation, Vector3? scale)
@ -444,15 +485,19 @@ void UpdateServerDelta()
#endif
#if onlySyncOnChange_BANDWIDTH_SAVING
RpcServerToClientDeltaSync(
using (NetworkWriterPooled writer = NetworkWriterPool.Get())
{
SerializeServerDelta(
writer,
// include the last reliable baseline tick#.
// the unreliable delta is meant to go on top of it that, and no older one.
lastSerializedBaselineTick,
// only sync what the user wants to sync
syncPosition && positionChanged ? snapshot.position : default(Vector3?),
syncRotation && rotationChanged ? snapshot.rotation : default(Quaternion?),
syncScale && scaleChanged ? snapshot.scale : default(Vector3?)
syncPosition ? snapshot.position : default(Vector3?),
syncRotation ? snapshot.rotation : default(Quaternion?),
syncScale ? snapshot.scale : default(Vector3?)
);
RpcServerToClientDeltaSync(writer);
}
#else
RpcServerToClientSync(
// only sync what the user wants to sync