Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ public void OnProcessScene(Scene scene, BuildReport report)
{
foreach (var networkObject in FindObjects.FromSceneByType<NetworkObject>(scene, true))
{
// Exclude any already spawned objects found since this executes after the NetworkSceneManager has finished
// migrating anything from the DDOL into the newly loaded scene.
if (networkObject.IsSpawned)
{
continue;
}
networkObject.InScenePlaced = true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3479,7 +3479,8 @@ internal static NetworkObject Deserialize(in SerializedObject serializedObject,
{
if (networkManager.LogLevel <= LogLevel.Normal)
{
NetworkLog.LogWarning($"[{networkObject.name}][Deserialize][{nameof(NetworkBehaviour)}Synchronization][Size mismatch] Expected: {endOfSynchronizationData} Currently At: {reader.Position}!");
var networkObjectName = networkObject != null ? networkObject.name : "null";
NetworkLog.LogWarning($"[{networkObjectName}][Deserialize][{nameof(NetworkBehaviour)}Synchronization][Size mismatch] Expected: {endOfSynchronizationData} Currently At: {reader.Position}!");
}
reader.Seek(endOfSynchronizationData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,21 @@ private void OnClientLoadedScene(uint sceneEventId, Scene scene)
/// </summary>
internal List<ulong> ClientConnectionQueue = new List<ulong>();


[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AddSceneToClientSynchronization(ref SceneEventData sceneEventData, ref Scene scene)
{
// If we are just a normal client and in distributed authority mode, then always use the known server scene handle
if (NetworkManager.DistributedAuthorityMode && NetworkManager.CMBServiceConnection)
{
sceneEventData.AddSceneToSynchronize(SceneHashFromNameOrPath(scene.path), ClientSceneHandleToServerSceneHandle[scene.handle]);
}
else
{
sceneEventData.AddSceneToSynchronize(SceneHashFromNameOrPath(scene.path), scene.handle);
}
}

/// <summary>
/// Server Side:
/// This is used for players that have just had their connection approved and will assure they are synchronized
Expand Down Expand Up @@ -2002,61 +2017,53 @@ internal void SynchronizeNetworkObjects(ulong clientId, bool synchronizingServic

// Organize how (and when) we serialize our NetworkObjects
var hasSynchronizedActive = false;
for (int i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);

// NetworkSceneManager does not synchronize scenes that are not loaded by NetworkSceneManager
// unless the scene in question is the currently active scene.
if (ExcludeSceneFromSychronization != null && !ExcludeSceneFromSychronization(scene))
// It is possible a user might not want to synchronize the active scene, so we will check to see if it is valid before adding it to the synchronization list.
// !! Important !!
// The active scene MUST always be the first scene in the synchronization list.
if (ValidateSceneBeforeLoading(activeScene.buildIndex, activeScene.name, sceneEventData.LoadSceneMode))
{
sceneEventData.SceneHash = SceneHashFromNameOrPath(activeScene.path);
if (sceneEventData.SceneHash == sceneEventData.ActiveSceneHash)
{
continue;
hasSynchronizedActive = true;
}

if (scene == DontDestroyOnLoadScene)
// If we are just a normal client, then always use the server scene handle
if (NetworkManager.DistributedAuthorityMode)
{
continue;
sceneEventData.SenderClientId = NetworkManager.LocalClientId;
sceneEventData.SceneHandle = ClientSceneHandleToServerSceneHandle[activeScene.handle];
}

// This would depend upon whether we are additive or not
// If we are the base scene, then we set the root scene index;
if (activeScene == scene)
else
{
if (!ValidateSceneBeforeLoading(scene.buildIndex, scene.name, sceneEventData.LoadSceneMode))
{
continue;
}
sceneEventData.SceneHash = SceneHashFromNameOrPath(scene.path);
if (sceneEventData.SceneHash == sceneEventData.ActiveSceneHash)
{
hasSynchronizedActive = true;
}

// If we are just a normal client, then always use the server scene handle
if (NetworkManager.DistributedAuthorityMode)
{
sceneEventData.SenderClientId = NetworkManager.LocalClientId;
sceneEventData.SceneHandle = ClientSceneHandleToServerSceneHandle[scene.handle];
}
else
{
sceneEventData.SceneHandle = scene.handle;
}
sceneEventData.SceneHandle = activeScene.handle;
}
else if (!ValidateSceneBeforeLoading(scene.buildIndex, scene.name, LoadSceneMode.Additive))
AddSceneToClientSynchronization(ref sceneEventData, ref activeScene);
}

for (int i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);
// Skip adding the active scene at this point as we are just adding all other additively loaded scenes to the synchronization list.
// Skip adding the dont destroy on load scene as that is never synchronized.
if ((scene.handle == activeScene.handle) || (scene == DontDestroyOnLoadScene))
{
continue;
}

// If we are just a normal client and in distributed authority mode, then always use the known server scene handle
if (NetworkManager.DistributedAuthorityMode && NetworkManager.CMBServiceConnection)
// NetworkSceneManager does not synchronize scenes that are not loaded by NetworkSceneManager
// unless the scene in question is the currently active scene.
if (ExcludeSceneFromSychronization != null && !ExcludeSceneFromSychronization(scene))
{
sceneEventData.AddSceneToSynchronize(SceneHashFromNameOrPath(scene.path), ClientSceneHandleToServerSceneHandle[scene.handle]);
continue;
}
else

if (!ValidateSceneBeforeLoading(scene.buildIndex, scene.name, LoadSceneMode.Additive))
{
sceneEventData.AddSceneToSynchronize(SceneHashFromNameOrPath(scene.path), scene.handle);
continue;
}
AddSceneToClientSynchronization(ref sceneEventData, ref scene);
}

if (!hasSynchronizedActive && NetworkManager.CMBServiceConnection && synchronizingService)
Expand Down Expand Up @@ -2109,9 +2116,12 @@ private void OnClientBeginSync(uint sceneEventId)
var sceneHash = sceneEventData.GetNextSceneSynchronizationHash();
var sceneHandle = sceneEventData.GetNextSceneSynchronizationHandle();
var sceneName = SceneNameFromHash(sceneHash);
var activeSceneName = SceneNameFromHash(sceneEventData.ActiveSceneHash);
var activeScene = SceneManager.GetActiveScene();

var loadSceneMode = sceneHash == sceneEventData.SceneHash ? sceneEventData.LoadSceneMode : LoadSceneMode.Additive;
var activeSceneLoaded = activeSceneName == activeScene.name;

var loadSceneMode = sceneHash == sceneEventData.SceneHash && !activeSceneLoaded ? sceneEventData.LoadSceneMode : LoadSceneMode.Additive;

// Store the sceneHandle and hash
sceneEventData.NetworkSceneHandle = sceneHandle;
Expand Down