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
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
using System;
using Unity.Entities;
using Unity.NetCode;
using UnityEngine;

namespace Unity.Netcode
{

#if UNIFIED_NETCODE
/// <summary>
/// TODO-UNIFIED: Needs further peer review and exploring alternate ways of handling this.
/// </summary>
Expand All @@ -15,7 +17,7 @@ namespace Unity.Netcode
public partial class NetworkObjectBridge : GhostBehaviour
{

#if UNITY_EDITOR
#if UNITY_EDITOR && !UNITY_INCLUDE_TESTS
[UnityEngine.HideInInspector]
[UnityEngine.SerializeField]
private bool m_Sorted = false;
Expand Down Expand Up @@ -46,12 +48,13 @@ private void OnValidate()
/// N4E-spawned hybrid prefab instances.
/// </summary>
internal GhostField<ulong> NetworkObjectId = new GhostField<ulong>();

public void SetNetworkObjectId(ulong value)
public void SetNetworkObjectId(ulong networkObjectId)
{
NetworkObjectId.Value = value;
NetworkObjectId.PresetValue(networkObjectId);
NetworkObjectId.Value = networkObjectId;
}
}
#endif

/// <summary>
/// TODO-UNIFIED: Would need to be reviewed for alternate ways of handling this.
Expand All @@ -63,46 +66,67 @@ internal class UnifiedBootStrap : ClientServerBootstrap
public static UnifiedBootStrap Instance { get; private set; }
public static Action OnInitialized;
public static ushort Port = 7979;
public static NetworkManager CurrentNetworkManagerForInitialization;

public static World World { get; private set; }
public static World LastCreatedWorld { get; private set; }

private static int s_WorldCounter = 0;

public override bool Initialize(string defaultWorldName)
{
var networkManager = NetworkManager.Singleton;
Instance = this;
var networkManager = CurrentNetworkManagerForInitialization;
if (networkManager == NetworkManager.Singleton)
{
Instance = this;
}

AutoConnectPort = Port;
if (base.Initialize(defaultWorldName))
{
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] Auto-bootstrap is enabled!!! This will break the POC!");
Debug.LogError($"[{nameof(UnifiedBootStrap)}] Auto-bootstrap is enabled!!! This will break the POC!");
return true;
}

World = networkManager.IsServer ? CreateSingleWorldHost("ClientAndServerWorld") : CreateClientWorld("ClientWorld");

if (World == null)
if (networkManager != null)
{
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] World is null!");
return false;
}
Debug.Log($"Starting a world for {(networkManager.IsServer ? "Host" : "Client")}");
s_WorldCounter++;
LastCreatedWorld = networkManager.IsServer ? CreateSingleWorldHost($"HostSingleWorld-{s_WorldCounter}")
: CreateClientWorld($"ClientWorld-{s_WorldCounter}");

if (!World.IsCreated)
{
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] World was not created!");
return false;
}
if (LastCreatedWorld == null)
{
s_WorldCounter--;
Debug.LogError($"[{nameof(UnifiedBootStrap)}] World is null!");
return false;
}

if (networkManager.LogLevel <= LogLevel.Developer)
{
NetworkLog.LogInfo($"[{nameof(UnifiedBootStrap)}] Created world: {World.Name}");
}
if (!LastCreatedWorld.IsCreated)
{
s_WorldCounter--;
Debug.LogError($"[{nameof(UnifiedBootStrap)}] World was not created!");
return false;
}

if (networkManager.NetworkConfig.Prefabs.HasPendingGhostPrefabs)
{
if (networkManager.LogLevel <= LogLevel.Developer)
//if (networkManager.LogLevel <= LogLevel.Developer)
{
NetworkLog.LogInfo($"[{nameof(UnifiedBootStrap)}] Registering hybrid prefabs...");
NetworkLog.LogInfo($"[{nameof(UnifiedBootStrap)}] Created world: {LastCreatedWorld.Name} / {LastCreatedWorld.SequenceNumber}");
}
networkManager.NetworkConfig.Prefabs.RegisterGhostPrefabs(networkManager);

networkManager.NetcodeWorld = (NetcodeWorld)LastCreatedWorld;
if (networkManager.NetworkConfig.Prefabs.HasPendingGhostPrefabs)
{
if (networkManager.LogLevel <= LogLevel.Developer)
{
NetworkLog.LogInfo($"[{nameof(UnifiedBootStrap)}] Registering hybrid prefabs...");
}

networkManager.NetworkConfig.Prefabs.RegisterGhostPrefabs(networkManager);
}
}
else
{
LastCreatedWorld = CreateLocalWorld("LocalWorld");
}

OnInitialized?.Invoke();
Expand All @@ -112,7 +136,7 @@ public override bool Initialize(string defaultWorldName)

~UnifiedBootStrap()
{
World = null;
LastCreatedWorld = null;
Instance = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using UnityEngine;

namespace Unity.Netcode.Components
{
Expand Down Expand Up @@ -36,64 +37,75 @@ protected override void OnUpdate()
{
var isServer = World.IsServer();
var commandBuffer = new EntityCommandBuffer(Allocator.Temp);
var networkManager = NetworkManager.Singleton;

foreach (var (networkId, connectionState, entity) in SystemAPI.Query<NetworkId, ConnectionState>().WithNone<NetworkStreamConnection>().WithEntityAccess())
{
commandBuffer.RemoveComponent<ConnectionState>(entity);
m_TempConnections.Add(new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value });
}
foreach (var con in m_TempConnections)
// var networkManager = NetworkManager.Singleton;
foreach (var networkManager in GameObject.FindObjectsByType<NetworkManager>())
{
NetworkManager.OnNetCodeDisconnect?.Invoke(con);
}

m_TempConnections.Clear();
foreach (var (networkId, connectionState, entity) in SystemAPI.Query<NetworkId, ConnectionState>()
.WithNone<NetworkStreamConnection>().WithEntityAccess())
{
commandBuffer.RemoveComponent<ConnectionState>(entity);
m_TempConnections.Add(new NetcodeConnection
{ World = World, Entity = entity, NetworkId = networkId.Value });
}

// TODO: We should figure out how to associate the N4E NetworkId with the NGO ClientId
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithAll<NetworkStreamConnection>().WithNone<NetworkStreamInGame>().WithEntityAccess())
{
if (!m_NewConnections.ContainsKey(networkId.Value))
foreach (var con in m_TempConnections)
{
var newConnection = new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value };
m_NewConnections.Add(networkId.Value, newConnection);
NetworkManager.OnNetCodeDisconnect?.Invoke(con);
}
}

// If we have any pending connections
if (m_NewConnections.Count > 0)
{
foreach (var entry in m_NewConnections)
m_TempConnections.Clear();

// TODO: We should figure out how to associate the N4E NetworkId with the NGO ClientId
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithAll<NetworkStreamConnection>()
.WithNone<NetworkStreamInGame>().WithEntityAccess())
{
// Server: always connect
// Client: wait until we have synchronized before announcing we are ready to receive snapshots
if (networkManager.IsServer || (!networkManager.IsServer && networkManager.IsConnectedClient))
if (!m_NewConnections.ContainsKey(networkId.Value))
{
// Set the connection in-game
commandBuffer.AddComponent<NetworkStreamInGame>(entry.Value.Entity);
commandBuffer.AddComponent(entry.Value.Entity, default(ConnectionState));
NetworkManager.OnNetCodeConnect?.Invoke(entry.Value);
m_TempConnections.Add(entry.Value);
var newConnection = new NetcodeConnection
{ World = World, Entity = entity, NetworkId = networkId.Value };
m_NewConnections.Add(networkId.Value, newConnection);
}
}
// Remove any connections that have "gone in-game".
foreach (var connection in m_TempConnections)

// If we have any pending connections
if (m_NewConnections.Count > 0)
{
m_NewConnections.Remove(connection.NetworkId);
foreach (var entry in m_NewConnections)
{
// Server: always connect
// Client: wait until we have synchronized before announcing we are ready to receive snapshots
if (networkManager.IsServer || (!networkManager.IsServer && networkManager.IsConnectedClient))
{
// Set the connection in-game
commandBuffer.AddComponent<NetworkStreamInGame>(entry.Value.Entity);
commandBuffer.AddComponent(entry.Value.Entity, default(ConnectionState));
NetworkManager.OnNetCodeConnect?.Invoke(entry.Value);
m_TempConnections.Add(entry.Value);
}
}

// Remove any connections that have "gone in-game".
foreach (var connection in m_TempConnections)
{
m_NewConnections.Remove(connection.NetworkId);
}
}
}
m_TempConnections.Clear();

// If the local NetworkManager is shutting down or no longer connected, then
// make sure we have disconnected all known connections.
if (networkManager.ShutdownInProgress || !networkManager.IsListening)
{
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithEntityAccess())
m_TempConnections.Clear();

// If the local NetworkManager is shutting down or no longer connected, then
// make sure we have disconnected all known connections.
if (networkManager.ShutdownInProgress || !networkManager.IsListening)
{
commandBuffer.RemoveComponent<ConnectionState>(entity);
NetworkManager.OnNetCodeDisconnect?.Invoke(new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value });
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithEntityAccess())
{
commandBuffer.RemoveComponent<ConnectionState>(entity);
NetworkManager.OnNetCodeDisconnect?.Invoke(new NetcodeConnection
{ World = World, Entity = entity, NetworkId = networkId.Value });
}
}
}

commandBuffer.Playback(EntityManager);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3741,7 +3741,7 @@ private void ResetInterpolatedStateToCurrentAuthoritativeState()
/// The internal initialzation method to allow for internal API adjustments
/// </summary>
/// <param name="isOwnershipChange"></param>
private void InternalInitialization(bool isOwnershipChange = false)
internal virtual void InternalInitialization(bool isOwnershipChange = false)
{

#if UNIFIED_NETCODE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ internal void RegisterGhostPrefabs(NetworkManager networkManager)
var networkPrefab = m_PendingGhostRegistration[i];

// Returns false if the single world is not available yet
if (NetCode.Netcode.RegisterPrefabSingleWorld(networkPrefab.Prefab, isHost))
if (NetCode.Netcode.RegisterPrefabSingleWorld(networkPrefab.Prefab, isHost, networkManager.NetcodeWorld))
{
Debug.Log($"[{nameof(NetworkPrefabs)}][{nameof(RegisterGhostPrefabs)}] Registered hybrid spawned object: {networkPrefab.Prefab.name}");
m_PendingGhostRegistration.RemoveAt(i);
Expand Down Expand Up @@ -394,9 +394,9 @@ private bool AddPrefabRegistration(NetworkPrefab networkPrefab)
#if UNIFIED_NETCODE
if (networkPrefab.HasGhost)
{
HasPendingGhostPrefabs = true;
//HasPendingGhostPrefabs = true;
HasGhostPrefabs = true;
m_PendingGhostRegistration.Add(networkPrefab);
//m_PendingGhostRegistration.Add(networkPrefab);
}
#endif

Expand Down
Loading