NetworkBehaviour.Serialize takes SyncMethod parameter to simplify callers and prepare for unreliable delta compression

This commit is contained in:
mischa 2024-07-20 11:20:18 +02:00
parent be912f7bac
commit a7639243e0
2 changed files with 9 additions and 17 deletions

View File

@ -1260,7 +1260,7 @@ internal void DeserializeObjectsDelta(NetworkReader reader)
// on other entities would be mismatched, causing the weirdest errors.
//
// reads <<len, payload, len, payload, ...>> for 100% safety.
internal void Serialize(NetworkWriter writer, bool initialState)
internal void Serialize(NetworkWriter writer, bool initialState, SyncMethod method)
{
// reserve length header to ensure the correct amount will be read.
// originally we used a 4 byte header (too bandwidth heavy).
@ -1288,8 +1288,12 @@ internal void Serialize(NetworkWriter writer, bool initialState)
// write payload
try
{
// note this may not write anything if no syncIntervals elapsed
OnSerialize(writer, initialState);
// SyncMethod support:
// Traditional: Serialize(initial) once, then Serialize(delta) all the time.
// FastPaced: Serialize(initial) all the time because we always need full state for unreliable messages
// => reusing OnSerialize(initial=true) for FastPaced allows us to keep the API clean and simple.
// this way the end user never needs to worry about SyncMethod serialization.
OnSerialize(writer, initialState || method == SyncMethod.FastPaced);
}
catch (Exception e)
{

View File

@ -997,12 +997,7 @@ internal void SerializeServer(bool initialState, SyncMethod method, NetworkWrite
// serialize into helper writer
using (NetworkWriterPooled temp = NetworkWriterPool.Get())
{
// SyncMethod support:
// Traditional: Serialize(initial) once, then Serialize(delta) all the time.
// FastPaced: Serialize(initial) all the time because we always need full state for unreliable messages
// => reusing OnSerialize(initial=true) for FastPaced allows us to keep the API clean and simple.
// this way the end user never needs to worry about SyncMethod serialization.
comp.Serialize(temp, initialState || method == SyncMethod.FastPaced);
comp.Serialize(temp, initialState, method);
ArraySegment<byte> segment = temp.ToArraySegment();
// copy to owner / observers as needed
@ -1072,14 +1067,7 @@ internal void SerializeClient(SyncMethod method, NetworkWriter writer)
{
// serialize into writer.
// server always knows initialState, we never need to send it
// SyncMethod support:
// Traditional: always Serialize(delta) on client since server knows initial state.
// FastPaced: always Serialize(initial) on client since we need full state for unreliable sync.
// => reusing OnSerialize(initial=true) for FastPaced allows us to keep the API clean and simple.
// this way the end user never needs to worry about SyncMethod serialization.
comp.Serialize(writer, method == SyncMethod.FastPaced);
comp.Serialize(writer, false, method);
// clear dirty bits for the components that we serialized.
// do not clear for _all_ components, only the ones that