Skip to content
Open
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
71 changes: 22 additions & 49 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,15 @@ public void NetworkUpdate(NetworkUpdateStage updateStage)

// This should be invoked just prior to the MessageManager processes its outbound queue.
SceneManager.CheckForAndSendNetworkObjectSceneChanged();

// Process outbound messages
MessageManager.ProcessSendQueues();
#if UNIFIED_NETCODE
if (!NetworkConfig.Prefabs.HasGhostPrefabs)
{
#endif
// Process outbound messages
MessageManager.ProcessSendQueues();
#if UNIFIED_NETCODE
}
#endif

// Metrics update needs to be driven by NetworkConnectionManager's update to assure metrics are dispatched after the send queue is processed.
MetricsManager.UpdateMetrics();
Expand Down Expand Up @@ -1354,13 +1360,18 @@ private bool CanStart(StartType type)
/// The world instance assigned to this NetworkManager instance.
/// </summary>
public NetcodeWorld NetcodeWorld { get; internal set; }
private System.Collections.IEnumerator WaitForHybridPrefabRegistration(StartType startType)
internal void InitializeNetcodeWorld()
{
if (NetcodeWorld != null)
{
return;
}

if (this == Singleton)
{
if (NetCode.Netcode.IsActive)
{
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] Netcode is not active but has an instance at this point.");
NetworkLog.LogInfo($"[{nameof(InitializeNetcodeWorld)}] Netcode is not active but has an instance at this point.");
}
/// !! Important !!
/// Clear out any pre-existing configuration in the event this applicatioin instance has already been connected to a session.
Expand All @@ -1371,42 +1382,6 @@ private System.Collections.IEnumerator WaitForHybridPrefabRegistration(StartType
/// Worlds are created here: <see cref="UnifiedBootStrap.Initialize"/>
UnifiedBootstrap.CurrentNetworkManagerForInitialization = this;
DefaultWorldInitialization.Initialize("Default World", false);

// This should not be needed at this point, but this is here in the event something changes.
if (NetworkConfig.Prefabs.HasPendingGhostPrefabs)
{
NetworkLog.LogWarning($"[{nameof(WaitForHybridPrefabRegistration)}] !!!!! (Ghosts are still pending registration) !!!!!");
var waitTime = new WaitForSeconds(0.016f);
while (NetworkConfig.Prefabs.HasPendingGhostPrefabs)
{
NetworkConfig.Prefabs.RegisterGhostPrefabs(this);
yield return waitTime;
}
}

if (LogLevel <= LogLevel.Developer)
{
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] All hybrid prefabs have been registered!");
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] Finalizing NetworkManager start...");
}
switch (startType)
{
case StartType.Server:
{
InternalStartServer();
break;
}
case StartType.Host:
{
InternalStartHost();
break;
}
case StartType.Client:
{
InternalStartClient();
break;
}
}
}

private bool UnifiedIsConfiguredCorrectly()
Expand Down Expand Up @@ -1470,8 +1445,8 @@ public bool StartServer()
{
Debug.Log("Creating world: Default world");
}
StartCoroutine(WaitForHybridPrefabRegistration(StartType.Server));
return true;
InitializeNetcodeWorld();
return InternalStartServer();
}
else
{
Expand Down Expand Up @@ -1555,9 +1530,8 @@ public bool StartClient()
{
Debug.Log("Creating world: Default world");
}
StartCoroutine(WaitForHybridPrefabRegistration(StartType.Client));
// TODO-UNIFIED: Need a way to signal everything completed.
return true;
InitializeNetcodeWorld();
return InternalStartClient();
}
else
{
Expand Down Expand Up @@ -1640,9 +1614,8 @@ public bool StartHost()
{
Debug.Log("Creating world: Default world");
}
StartCoroutine(WaitForHybridPrefabRegistration(StartType.Host));
// TODO-UNIFIED: Need a way to signal everything completed.
return true;
InitializeNetcodeWorld();
return InternalStartHost();
}
else
{
Expand Down
6 changes: 6 additions & 0 deletions com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,6 +2072,12 @@ public void SpawnAsPlayerObject(ulong clientId, bool destroyWithScene = false)
/// <param name="destroy">(true) the <see cref="GameObject"/> will be destroyed (false) the <see cref="GameObject"/> will persist after being despawned</param>
public void Despawn(bool destroy = true)
{
#if UNIFIED_NETCODE
if (HasGhost && destroy == false)
{
throw new NotSupportedException("Despawn without destroy is not supported for hybrid objects.");
}
#endif
if (!IsSpawned)
{
if (NetworkManager.LogLevel <= LogLevel.Error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ protected virtual void PurgeTrigger(IDeferredNetworkMessageManager.TriggerType t
triggerInfo.TriggerData.Dispose();
}

public bool HasAnyOfTrigger(IDeferredNetworkMessageManager.TriggerType trigger)
{
if (m_Triggers.TryGetValue(trigger, out var triggers))
{
return triggers.Count != 0;
}

return false;
}

public virtual void ProcessTriggers(IDeferredNetworkMessageManager.TriggerType trigger, ulong key)
{
if (m_Triggers.TryGetValue(trigger, out var triggers))
Expand All @@ -143,6 +153,11 @@ public virtual void ProcessTriggers(IDeferredNetworkMessageManager.TriggerType t
triggerInfo.TriggerData.Dispose();
}
}

if (trigger != IDeferredNetworkMessageManager.TriggerType.OnOtherTriggerFinishedProcessing)
{
ProcessTriggers(IDeferredNetworkMessageManager.TriggerType.OnOtherTriggerFinishedProcessing, (ulong)trigger);
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal enum TriggerType
#if UNIFIED_NETCODE
OnGhostSpawned,
#endif
OnOtherTriggerFinishedProcessing,
}

/// <summary>
Expand All @@ -31,6 +32,8 @@ internal enum TriggerType

public void ProcessTriggers(TriggerType trigger, ulong key);

public bool HasAnyOfTrigger(IDeferredNetworkMessageManager.TriggerType trigger);

/// <summary>
/// Cleans up any trigger that's existed for more than a second.
/// These triggers were probably for situations where a request was received after a despawn rather than before a spawn.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal struct SceneEventMessage : INetworkMessage

public SceneEventData EventData;

private const string k_Name = "SceneEventMessage";

private FastBufferReader m_ReceivedData;

Expand All @@ -18,6 +19,15 @@ public void Serialize(FastBufferWriter writer, int targetVersion)

public bool Deserialize(FastBufferReader reader, ref NetworkContext context, int receivedMessageVersion)
{
var networkManager = (NetworkManager)context.SystemOwner;
#if UNIFIED_NETCODE
if (networkManager.DeferredMessageManager.HasAnyOfTrigger(IDeferredNetworkMessageManager.TriggerType
.OnGhostSpawned))
{
networkManager.DeferredMessageManager.DeferMessage(IDeferredNetworkMessageManager.TriggerType.OnOtherTriggerFinishedProcessing, (ulong)IDeferredNetworkMessageManager.TriggerType.OnGhostSpawned, reader, ref context, k_Name);
return false;
}
#endif
m_ReceivedData = reader;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,6 @@ internal void HandleIncomingData(ulong clientId, ArraySegment<byte> data, float
{
unsafe
{

//Debug.Log($"Receiving {data.Count} bytes: {ByteArrayToString(data.Array, data.Offset, data.Count)}");
fixed (byte* dataPtr = data.Array)
{
var batchReader = new FastBufferReader(dataPtr + data.Offset, Allocator.None, data.Count);
Expand Down Expand Up @@ -872,7 +870,6 @@ internal unsafe void ProcessSendQueues()

try
{
//Debug.Log($"Sending {queueItem.Writer.Length} bytes: {ByteArrayToString(queueItem.Writer.ToArray(), 0, queueItem.Writer.Length)}");
m_Sender.Send(clientId, queueItem.NetworkDelivery, queueItem.Writer);

for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
#if UNIFIED_NETCODE
using Unity.NetCode;
#endif
using UnityEngine;

namespace Unity.Netcode
Expand Down Expand Up @@ -418,6 +421,19 @@ public void AddNetworkPrefab(GameObject prefab)
{
m_NetworkManager.DeferredMessageManager.ProcessTriggers(IDeferredNetworkMessageManager.TriggerType.OnAddPrefab, networkObject.GlobalObjectIdHash);
}

#if UNIFIED_NETCODE
if (m_NetworkManager.IsListening)
{
var ghost = prefab.GetComponent<GhostAdapter>();
if (ghost)
{
m_NetworkManager.InitializeNetcodeWorld();
NetCode.Netcode.RegisterPrefabSingleWorld(prefab, m_NetworkManager.IsHost,
m_NetworkManager.NetcodeWorld);
}
}
#endif
}

/// <summary>
Expand Down
Loading