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

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