@@ -2876,6 +2876,7 @@ public struct TransformData : INetworkSerializeByMemcpy
28762876
28772877 public int NetworkSceneHandle ;
28782878
2879+ internal int SynchronizationDataSize ;
28792880
28802881 public void Serialize ( FastBufferWriter writer )
28812882 {
@@ -2937,15 +2938,29 @@ public void Serialize(FastBufferWriter writer)
29372938 writer . WriteValue ( OwnerObject . GetSceneOriginHandle ( ) ) ;
29382939 }
29392940
2941+ // write placeholder for serialized data size.
2942+ // Can't be bitpacked because we don't know the value until we calculate it later
2943+ var positionBeforeSynchronizing = writer . Position ;
2944+ writer . WriteValueSafe ( 0 ) ;
2945+ var sizeToSkipCalculationPosition = writer . Position ;
2946+
29402947 if ( HasInstantiationData )
29412948 {
2942- BytePacker . WriteValuePacked ( writer , OwnerObject . InstantiationData . Length ) ;
2943- writer . WriteBytesSafe ( OwnerObject . InstantiationData ) ;
2949+ writer . WriteValueSafe ( OwnerObject . InstantiationData ) ;
29442950 }
29452951
29462952 // Synchronize NetworkVariables and NetworkBehaviours
29472953 var bufferSerializer = new BufferSerializer < BufferSerializerWriter > ( new BufferSerializerWriter ( writer ) ) ;
29482954 OwnerObject . SynchronizeNetworkBehaviours ( ref bufferSerializer , TargetClientId ) ;
2955+
2956+ var currentPosition = writer . Position ;
2957+ // Write the total number of bytes written for synchronization data.
2958+ writer . Seek ( positionBeforeSynchronizing ) ;
2959+ // We want the size of everything after our size to skip calculation position
2960+ var size = currentPosition - sizeToSkipCalculationPosition ;
2961+ writer . WriteValueSafe ( size ) ;
2962+ // seek back to the head of the writer.
2963+ writer . Seek ( currentPosition ) ;
29492964 }
29502965
29512966 public void Deserialize ( FastBufferReader reader )
@@ -3001,6 +3016,10 @@ public void Deserialize(FastBufferReader reader)
30013016 // The NetworkSceneHandle is the server-side relative
30023017 // scene handle that the NetworkObject resides in.
30033018 reader . ReadValue ( out NetworkSceneHandle ) ;
3019+
3020+ // Read the size of the remaining synchronization data
3021+ // This data will be read in AddSceneObject()
3022+ reader . ReadValueSafe ( out SynchronizationDataSize ) ;
30043023 }
30053024 }
30063025
@@ -3015,12 +3034,7 @@ internal void SynchronizeNetworkBehaviours<T>(ref BufferSerializer<T> serializer
30153034 {
30163035 if ( serializer . IsWriter )
30173036 {
3018- // write placeholder int.
3019- // Can't be bitpacked because we don't know the value until we calculate it later
30203037 var writer = serializer . GetFastBufferWriter ( ) ;
3021- var positionBeforeSynchronizing = writer . Position ;
3022- writer . WriteValueSafe ( 0 ) ;
3023- var sizeToSkipCalculationPosition = writer . Position ;
30243038
30253039 // Synchronize NetworkVariables
30263040 foreach ( var behavior in ChildNetworkBehaviours )
@@ -3046,12 +3060,6 @@ internal void SynchronizeNetworkBehaviours<T>(ref BufferSerializer<T> serializer
30463060 }
30473061
30483062 var currentPosition = writer . Position ;
3049- // Write the total number of bytes written for NetworkVariable and NetworkBehaviour
3050- // synchronization.
3051- writer . Seek ( positionBeforeSynchronizing ) ;
3052- // We want the size of everything after our size to skip calculation position
3053- var size = currentPosition - sizeToSkipCalculationPosition ;
3054- writer . WriteValueSafe ( size ) ;
30553063 // Write the number of NetworkBehaviours synchronized
30563064 writer . Seek ( networkBehaviourCountPosition ) ;
30573065 writer . WriteValueSafe ( synchronizationCount ) ;
@@ -3061,41 +3069,26 @@ internal void SynchronizeNetworkBehaviours<T>(ref BufferSerializer<T> serializer
30613069 }
30623070 else
30633071 {
3064- var seekToEndOfSynchData = 0 ;
30653072 var reader = serializer . GetFastBufferReader ( ) ;
3066- try
3067- {
3068- reader . ReadValueSafe ( out int sizeOfSynchronizationData ) ;
3069- seekToEndOfSynchData = reader . Position + sizeOfSynchronizationData ;
30703073
3071- // Apply the network variable synchronization data
3072- foreach ( var behaviour in ChildNetworkBehaviours )
3073- {
3074- behaviour . InitializeVariables ( ) ;
3075- behaviour . SetNetworkVariableData ( reader , targetClientId ) ;
3076- }
3074+ // Apply the network variable synchronization data
3075+ foreach ( var behaviour in ChildNetworkBehaviours )
3076+ {
3077+ behaviour . InitializeVariables ( ) ;
3078+ behaviour . SetNetworkVariableData ( reader , targetClientId ) ;
3079+ }
30773080
3078- // Read the number of NetworkBehaviours to synchronize
3079- reader . ReadValueSafe ( out byte numberSynchronized ) ;
3081+ // Read the number of NetworkBehaviours to synchronize
3082+ reader . ReadValueSafe ( out byte numberSynchronized ) ;
30803083
3081- // If a NetworkBehaviour writes synchronization data, it will first
3082- // write its NetworkBehaviourId so when deserializing the client-side
3083- // can find the right NetworkBehaviour to deserialize the synchronization data.
3084- for ( int i = 0 ; i < numberSynchronized ; i ++ )
3085- {
3086- reader . ReadValueSafe ( out ushort networkBehaviourId ) ;
3087- var networkBehaviour = GetNetworkBehaviourAtOrderIndex ( networkBehaviourId ) ;
3088- networkBehaviour . Synchronize ( ref serializer , targetClientId ) ;
3089- }
3090-
3091- if ( seekToEndOfSynchData != reader . Position )
3092- {
3093- Debug . LogWarning ( $ "[Size mismatch] Expected: { seekToEndOfSynchData } Currently At: { reader . Position } !") ;
3094- }
3095- }
3096- catch
3084+ // If a NetworkBehaviour writes synchronization data, it will first
3085+ // write its NetworkBehaviourId so when deserializing the client-side
3086+ // can find the right NetworkBehaviour to deserialize the synchronization data.
3087+ for ( int i = 0 ; i < numberSynchronized ; i ++ )
30973088 {
3098- reader . Seek ( seekToEndOfSynchData ) ;
3089+ reader . ReadValueSafe ( out ushort networkBehaviourId ) ;
3090+ var networkBehaviour = GetNetworkBehaviourAtOrderIndex ( networkBehaviourId ) ;
3091+ networkBehaviour . Synchronize ( ref serializer , targetClientId ) ;
30993092 }
31003093 }
31013094 }
@@ -3185,15 +3178,18 @@ internal SceneObject GetMessageSceneObject(ulong targetClientId = NetworkManager
31853178 /// <returns>The deserialized NetworkObject or null if deserialization failed</returns>
31863179 internal static NetworkObject AddSceneObject ( in SceneObject sceneObject , FastBufferReader reader , NetworkManager networkManager , bool invokedByMessage = false )
31873180 {
3188- var bufferSerializer = new BufferSerializer < BufferSerializerReader > ( new BufferSerializerReader ( reader ) ) ;
3181+ var endOfSynchronizationData = reader . Position + sceneObject . SynchronizationDataSize ;
3182+
3183+ byte [ ] instantiationData = null ;
3184+ if ( sceneObject . HasInstantiationData )
3185+ {
3186+ reader . ReadValueSafe ( out instantiationData ) ;
3187+ }
31893188
3190- //Synchronize the instantiation data if needed
3191- FastBufferReader instantiationDataReader = sceneObject . HasInstantiationData ? networkManager . PrefabHandler . GetInstantiationDataReader ( sceneObject . Hash , reader ) : default ;
31923189
3193- //Attempt to create a local NetworkObject
3194- var networkObject = networkManager . SpawnManager . CreateLocalNetworkObject ( sceneObject , instantiationDataReader ) ;
3190+ // Attempt to create a local NetworkObject
3191+ var networkObject = networkManager . SpawnManager . CreateLocalNetworkObject ( sceneObject , instantiationData ) ;
31953192
3196- instantiationDataReader . Dispose ( ) ;
31973193
31983194 if ( networkObject == null )
31993195 {
@@ -3206,8 +3202,7 @@ internal static NetworkObject AddSceneObject(in SceneObject sceneObject, FastBuf
32063202 try
32073203 {
32083204 // If we failed to load this NetworkObject, then skip past the Network Variable and (if any) synchronization data
3209- reader . ReadValueSafe ( out int networkBehaviourSynchronizationDataLength ) ;
3210- reader . Seek ( reader . Position + networkBehaviourSynchronizationDataLength ) ;
3205+ reader . Seek ( endOfSynchronizationData ) ;
32113206 }
32123207 catch ( Exception ex )
32133208 {
@@ -3225,8 +3220,23 @@ internal static NetworkObject AddSceneObject(in SceneObject sceneObject, FastBuf
32253220 // Special Case: Invoke NetworkBehaviour.OnPreSpawn methods here before SynchronizeNetworkBehaviours
32263221 networkObject . InvokeBehaviourNetworkPreSpawn ( ) ;
32273222
3228- // Synchronize NetworkBehaviours
3229- networkObject . SynchronizeNetworkBehaviours ( ref bufferSerializer , networkManager . LocalClientId ) ;
3223+ // Process the remaining synchronization data from the buffer
3224+ try
3225+ {
3226+ // Synchronize NetworkBehaviours
3227+ var bufferSerializer = new BufferSerializer < BufferSerializerReader > ( new BufferSerializerReader ( reader ) ) ;
3228+ networkObject . SynchronizeNetworkBehaviours ( ref bufferSerializer , networkManager . LocalClientId ) ;
3229+
3230+ if ( reader . Position != endOfSynchronizationData )
3231+ {
3232+ Debug . LogWarning ( $ "[Size mismatch] Expected: { endOfSynchronizationData } Currently At: { reader . Position } !") ;
3233+ reader . Seek ( endOfSynchronizationData ) ;
3234+ }
3235+ }
3236+ catch
3237+ {
3238+ reader . Seek ( endOfSynchronizationData ) ;
3239+ }
32303240
32313241 // If we are an in-scene placed NetworkObject and we originally had a parent but when synchronized we are
32323242 // being told we do not have a parent, then we want to clear the latest parent so it is not automatically
0 commit comments