Skip to content

Commit 164a713

Browse files
committed
Integration tests WIP
1 parent 78874ff commit 164a713

19 files changed

Lines changed: 656 additions & 235 deletions

com.unity.netcode.gameobjects/Runtime/Components/Helpers/NetworkObjectBridge.cs

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
using System;
33
using Unity.Entities;
44
using Unity.NetCode;
5+
using UnityEngine;
56

67
namespace Unity.Netcode
78
{
89

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

18-
#if UNITY_EDITOR
20+
#if UNITY_EDITOR && !UNITY_INCLUDE_TESTS
1921
[UnityEngine.HideInInspector]
2022
[UnityEngine.SerializeField]
2123
private bool m_Sorted = false;
@@ -52,6 +54,7 @@ public void SetNetworkObjectId(ulong value)
5254
NetworkObjectId.Value = value;
5355
}
5456
}
57+
#endif
5558

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

67-
public static World World { get; private set; }
71+
public static World LastCreatedWorld { get; private set; }
6872

73+
private static int WorldCounter = 0;
74+
6975
public override bool Initialize(string defaultWorldName)
7076
{
71-
var networkManager = NetworkManager.Singleton;
72-
Instance = this;
77+
var networkManager = CurrentNetworkManagerForInitialization;
78+
if (networkManager == NetworkManager.Singleton)
79+
{
80+
Instance = this;
81+
}
82+
7383
AutoConnectPort = Port;
7484
if (base.Initialize(defaultWorldName))
7585
{
7686
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] Auto-bootstrap is enabled!!! This will break the POC!");
7787
return true;
7888
}
7989

80-
World = networkManager.IsServer ? CreateSingleWorldHost("ClientAndServerWorld") : CreateClientWorld("ClientWorld");
81-
82-
if (World == null)
90+
if (networkManager != null)
8391
{
84-
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] World is null!");
85-
return false;
86-
}
92+
Debug.Log($"Starting a world for {(networkManager.IsServer ? "Host" : "Client")}");
93+
LastCreatedWorld = networkManager.IsServer
94+
? CreateSingleWorldHost($"ClientAndServerWorld {WorldCounter++}")
95+
: CreateClientWorld($"ClientWorld {WorldCounter++}");
8796

88-
if (!World.IsCreated)
89-
{
90-
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] World was not created!");
91-
return false;
92-
}
97+
if (LastCreatedWorld == null)
98+
{
99+
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] World is null!");
100+
return false;
101+
}
93102

94-
if (networkManager.LogLevel <= LogLevel.Developer)
95-
{
96-
NetworkLog.LogInfo($"[{nameof(UnifiedBootStrap)}] Created world: {World.Name}");
97-
}
103+
if (!LastCreatedWorld.IsCreated)
104+
{
105+
UnityEngine.Debug.LogError($"[{nameof(UnifiedBootStrap)}] World was not created!");
106+
return false;
107+
}
98108

99-
if (networkManager.NetworkConfig.Prefabs.HasPendingGhostPrefabs)
100-
{
101-
if (networkManager.LogLevel <= LogLevel.Developer)
109+
//if (networkManager.LogLevel <= LogLevel.Developer)
102110
{
103-
NetworkLog.LogInfo($"[{nameof(UnifiedBootStrap)}] Registering hybrid prefabs...");
111+
NetworkLog.LogInfo($"[{nameof(UnifiedBootStrap)}] Created world: {LastCreatedWorld.Name} / {LastCreatedWorld.SequenceNumber}");
104112
}
105-
networkManager.NetworkConfig.Prefabs.RegisterGhostPrefabs(networkManager);
113+
114+
networkManager.NetcodeWorld = (NetcodeWorld)LastCreatedWorld;
115+
if (networkManager.NetworkConfig.Prefabs.HasPendingGhostPrefabs)
116+
{
117+
if (networkManager.LogLevel <= LogLevel.Developer)
118+
{
119+
NetworkLog.LogInfo($"[{nameof(UnifiedBootStrap)}] Registering hybrid prefabs...");
120+
}
121+
122+
networkManager.NetworkConfig.Prefabs.RegisterGhostPrefabs(networkManager);
123+
}
124+
}
125+
else
126+
{
127+
LastCreatedWorld = CreateLocalWorld("LocalWorld");
106128
}
107129

108130
OnInitialized?.Invoke();
@@ -112,7 +134,7 @@ public override bool Initialize(string defaultWorldName)
112134

113135
~UnifiedBootStrap()
114136
{
115-
World = null;
137+
LastCreatedWorld = null;
116138
Instance = null;
117139
}
118140
}

com.unity.netcode.gameobjects/Runtime/Components/Helpers/UnifiedUpdateConnections.cs

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Unity.Collections;
44
using Unity.Entities;
55
using Unity.NetCode;
6+
using UnityEngine;
67

78
namespace Unity.Netcode.Components
89
{
@@ -36,64 +37,75 @@ protected override void OnUpdate()
3637
{
3738
var isServer = World.IsServer();
3839
var commandBuffer = new EntityCommandBuffer(Allocator.Temp);
39-
var networkManager = NetworkManager.Singleton;
40-
41-
foreach (var (networkId, connectionState, entity) in SystemAPI.Query<NetworkId, ConnectionState>().WithNone<NetworkStreamConnection>().WithEntityAccess())
42-
{
43-
commandBuffer.RemoveComponent<ConnectionState>(entity);
44-
m_TempConnections.Add(new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value });
45-
}
46-
foreach (var con in m_TempConnections)
40+
// var networkManager = NetworkManager.Singleton;
41+
foreach (var networkManager in GameObject.FindObjectsByType<NetworkManager>())
4742
{
48-
NetworkManager.OnNetCodeDisconnect?.Invoke(con);
49-
}
50-
51-
m_TempConnections.Clear();
43+
foreach (var (networkId, connectionState, entity) in SystemAPI.Query<NetworkId, ConnectionState>()
44+
.WithNone<NetworkStreamConnection>().WithEntityAccess())
45+
{
46+
commandBuffer.RemoveComponent<ConnectionState>(entity);
47+
m_TempConnections.Add(new NetcodeConnection
48+
{ World = World, Entity = entity, NetworkId = networkId.Value });
49+
}
5250

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

63-
// If we have any pending connections
64-
if (m_NewConnections.Count > 0)
65-
{
66-
foreach (var entry in m_NewConnections)
56+
m_TempConnections.Clear();
57+
58+
// TODO: We should figure out how to associate the N4E NetworkId with the NGO ClientId
59+
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithAll<NetworkStreamConnection>()
60+
.WithNone<NetworkStreamInGame>().WithEntityAccess())
6761
{
68-
// Server: always connect
69-
// Client: wait until we have synchronized before announcing we are ready to receive snapshots
70-
if (networkManager.IsServer || (!networkManager.IsServer && networkManager.IsConnectedClient))
62+
if (!m_NewConnections.ContainsKey(networkId.Value))
7163
{
72-
// Set the connection in-game
73-
commandBuffer.AddComponent<NetworkStreamInGame>(entry.Value.Entity);
74-
commandBuffer.AddComponent(entry.Value.Entity, default(ConnectionState));
75-
NetworkManager.OnNetCodeConnect?.Invoke(entry.Value);
76-
m_TempConnections.Add(entry.Value);
64+
var newConnection = new NetcodeConnection
65+
{ World = World, Entity = entity, NetworkId = networkId.Value };
66+
m_NewConnections.Add(networkId.Value, newConnection);
7767
}
7868
}
79-
// Remove any connections that have "gone in-game".
80-
foreach (var connection in m_TempConnections)
69+
70+
// If we have any pending connections
71+
if (m_NewConnections.Count > 0)
8172
{
82-
m_NewConnections.Remove(connection.NetworkId);
73+
foreach (var entry in m_NewConnections)
74+
{
75+
// Server: always connect
76+
// Client: wait until we have synchronized before announcing we are ready to receive snapshots
77+
if (networkManager.IsServer || (!networkManager.IsServer && networkManager.IsConnectedClient))
78+
{
79+
// Set the connection in-game
80+
commandBuffer.AddComponent<NetworkStreamInGame>(entry.Value.Entity);
81+
commandBuffer.AddComponent(entry.Value.Entity, default(ConnectionState));
82+
NetworkManager.OnNetCodeConnect?.Invoke(entry.Value);
83+
m_TempConnections.Add(entry.Value);
84+
}
85+
}
86+
87+
// Remove any connections that have "gone in-game".
88+
foreach (var connection in m_TempConnections)
89+
{
90+
m_NewConnections.Remove(connection.NetworkId);
91+
}
8392
}
84-
}
85-
m_TempConnections.Clear();
8693

87-
// If the local NetworkManager is shutting down or no longer connected, then
88-
// make sure we have disconnected all known connections.
89-
if (networkManager.ShutdownInProgress || !networkManager.IsListening)
90-
{
91-
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithEntityAccess())
94+
m_TempConnections.Clear();
95+
96+
// If the local NetworkManager is shutting down or no longer connected, then
97+
// make sure we have disconnected all known connections.
98+
if (networkManager.ShutdownInProgress || !networkManager.IsListening)
9299
{
93-
commandBuffer.RemoveComponent<ConnectionState>(entity);
94-
NetworkManager.OnNetCodeDisconnect?.Invoke(new NetcodeConnection { World = World, Entity = entity, NetworkId = networkId.Value });
100+
foreach (var (networkId, entity) in SystemAPI.Query<NetworkId>().WithEntityAccess())
101+
{
102+
commandBuffer.RemoveComponent<ConnectionState>(entity);
103+
NetworkManager.OnNetCodeDisconnect?.Invoke(new NetcodeConnection
104+
{ World = World, Entity = entity, NetworkId = networkId.Value });
105+
}
95106
}
96107
}
108+
97109
commandBuffer.Playback(EntityManager);
98110
}
99111

com.unity.netcode.gameobjects/Runtime/Components/NetworkTransform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3741,7 +3741,7 @@ private void ResetInterpolatedStateToCurrentAuthoritativeState()
37413741
/// The internal initialzation method to allow for internal API adjustments
37423742
/// </summary>
37433743
/// <param name="isOwnershipChange"></param>
3744-
private void InternalInitialization(bool isOwnershipChange = false)
3744+
internal virtual void InternalInitialization(bool isOwnershipChange = false)
37453745
{
37463746

37473747
#if UNIFIED_NETCODE

com.unity.netcode.gameobjects/Runtime/Configuration/NetworkPrefabs.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ internal void RegisterGhostPrefabs(NetworkManager networkManager)
362362
var networkPrefab = m_PendingGhostRegistration[i];
363363

364364
// Returns false if the single world is not available yet
365-
if (NetCode.Netcode.RegisterPrefabSingleWorld(networkPrefab.Prefab, isHost))
365+
if (NetCode.Netcode.RegisterPrefabSingleWorld(networkPrefab.Prefab, isHost, networkManager.NetcodeWorld))
366366
{
367367
Debug.Log($"[{nameof(NetworkPrefabs)}][{nameof(RegisterGhostPrefabs)}] Registered hybrid spawned object: {networkPrefab.Prefab.name}");
368368
m_PendingGhostRegistration.RemoveAt(i);
@@ -394,9 +394,9 @@ private bool AddPrefabRegistration(NetworkPrefab networkPrefab)
394394
#if UNIFIED_NETCODE
395395
if (networkPrefab.HasGhost)
396396
{
397-
HasPendingGhostPrefabs = true;
397+
//HasPendingGhostPrefabs = true;
398398
HasGhostPrefabs = true;
399-
m_PendingGhostRegistration.Add(networkPrefab);
399+
//m_PendingGhostRegistration.Add(networkPrefab);
400400
}
401401
#endif
402402

com.unity.netcode.gameobjects/Runtime/Connection/NetworkConnectionManager.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using System.Runtime.CompilerServices;
77
using Unity.Collections;
88
using Unity.Collections.LowLevel.Unsafe;
9+
#if UNIFIED_NETCODE
10+
using Unity.Netcode.Unified;
11+
#endif
912
using Unity.Profiling;
1013
using UnityEngine;
1114
using Debug = UnityEngine.Debug;

com.unity.netcode.gameobjects/Runtime/Core/NetworkManager.cs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,20 +1319,25 @@ private bool CanStart(StartType type)
13191319
return true;
13201320
}
13211321

1322+
public NetcodeWorld NetcodeWorld { get; internal set; }
1323+
13221324
#if UNIFIED_NETCODE
13231325
private System.Collections.IEnumerator WaitForHybridPrefabRegistration(StartType startType)
13241326
{
1325-
if (NetCode.Netcode.IsActive)
1327+
if (this == Singleton)
13261328
{
1327-
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] Netcode is not active but has an instance at this point.");
1329+
if (NetCode.Netcode.IsActive)
1330+
{
1331+
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] Netcode is not active but has an instance at this point.");
1332+
}
1333+
/// !! Important !!
1334+
/// Clear out any pre-existing configuration in the event this applicatioin instance has already been connected to a session.
1335+
NetCode.Netcode.Reset();
13281336
}
13291337

1330-
/// !! Important !!
1331-
/// Clear out any pre-existing configuration in the event this applicatioin instance has already been connected to a session.
1332-
NetCode.Netcode.Reset();
1333-
13341338
/// !! Initialize worlds here !!
13351339
/// Worlds are created here: <see cref="UnifiedBootStrap.Initialize"/>
1340+
UnifiedBootStrap.CurrentNetworkManagerForInitialization = this;
13361341
DefaultWorldInitialization.Initialize("Default World", false);
13371342

13381343
// This should not be needed at this point, but this is here in the event something changes.

com.unity.netcode.gameobjects/Runtime/Core/NetworkObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3886,7 +3886,7 @@ private void Start()
38863886
private void InitGhost()
38873887
{
38883888
// All instances with Ghosts are automatically registered
3889-
if (HasGhost && NetworkObjectBridge)
3889+
if (HasGhost && NetworkObjectBridge && !GhostAdapter.IsPrefab())
38903890
{
38913891
if (NetworkManager.LogLevel == LogLevel.Developer)
38923892
{

com.unity.netcode.gameobjects/Runtime/Messaging/NetworkMessageManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ internal void HandleIncomingData(ulong clientId, ArraySegment<byte> data, float
228228
{
229229
unsafe
230230
{
231+
232+
//Debug.Log($"Receiving {data.Count} bytes: {ByteArrayToString(data.Array, data.Offset, data.Count)}");
231233
fixed (byte* dataPtr = data.Array)
232234
{
233235
var batchReader = new FastBufferReader(dataPtr + data.Offset, Allocator.None, data.Count);
@@ -394,6 +396,7 @@ public void HandleMessage(in NetworkMessageHeader header, FastBufferReader reade
394396
};
395397

396398
var type = m_ReverseTypeMap[header.MessageType];
399+
Debug.Log($"Got message of type {type}");
397400
if (!CanReceive(senderId, type, reader, ref context))
398401
{
399402
return;
@@ -870,6 +873,7 @@ internal unsafe void ProcessSendQueues()
870873

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

875879
for (var hookIdx = 0; hookIdx < m_Hooks.Count; ++hookIdx)

0 commit comments

Comments
 (0)