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. // on other entities would be mismatched, causing the weirdest errors.
// //
// reads <<len, payload, len, payload, ...>> for 100% safety. // 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. // reserve length header to ensure the correct amount will be read.
// originally we used a 4 byte header (too bandwidth heavy). // originally we used a 4 byte header (too bandwidth heavy).
@ -1288,8 +1288,12 @@ internal void Serialize(NetworkWriter writer, bool initialState)
// write payload // write payload
try try
{ {
// note this may not write anything if no syncIntervals elapsed // SyncMethod support:
OnSerialize(writer, initialState); // 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) catch (Exception e)
{ {

View File

@ -997,12 +997,7 @@ internal void SerializeServer(bool initialState, SyncMethod method, NetworkWrite
// serialize into helper writer // serialize into helper writer
using (NetworkWriterPooled temp = NetworkWriterPool.Get()) using (NetworkWriterPooled temp = NetworkWriterPool.Get())
{ {
// SyncMethod support: comp.Serialize(temp, initialState, method);
// 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);
ArraySegment<byte> segment = temp.ToArraySegment(); ArraySegment<byte> segment = temp.ToArraySegment();
// copy to owner / observers as needed // copy to owner / observers as needed
@ -1072,14 +1067,7 @@ internal void SerializeClient(SyncMethod method, NetworkWriter writer)
{ {
// serialize into writer. // serialize into writer.
// server always knows initialState, we never need to send it // server always knows initialState, we never need to send it
comp.Serialize(writer, false, method);
// 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);
// clear dirty bits for the components that we serialized. // clear dirty bits for the components that we serialized.
// do not clear for _all_ components, only the ones that // do not clear for _all_ components, only the ones that