Skip to content

Commit 038b157

Browse files
update & fixes
Some adjustments to the POC Transport. Some fixes.
1 parent c31e7c2 commit 038b157

File tree

7 files changed

+50
-89
lines changed

7 files changed

+50
-89
lines changed

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,10 @@ public override bool Initialize(string defaultWorldName)
8282

8383
return true;
8484
}
85-
86-
public static void StopClient()
87-
{
88-
ClientWorld.Dispose();
89-
ClientWorlds.Remove(ClientWorld);
90-
}
91-
92-
public static void StopServer()
93-
{
94-
ServerWorld.Dispose();
95-
ServerWorlds.Remove(ServerWorld);
96-
}
9785

9886
~UnifiedBootStrap()
9987
{
88+
World = null;
10089
Instance = null;
10190
}
10291
}

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@ internal partial class UnifiedUpdateConnections : SystemBase
3131
private List<NetcodeConnection> m_TempConnections = new List<NetcodeConnection>();
3232

3333
private Dictionary<int, NetcodeConnection> m_NewConnections = new Dictionary<int, NetcodeConnection>();
34-
35-
public void MarkSync(int NetworkId)
36-
{
37-
if (m_NewConnections.TryGetValue(NetworkId, out var connection))
38-
{
39-
connection.IsSynced = true;
40-
m_NewConnections[NetworkId] = connection;
41-
}
42-
}
4334

4435
protected override void OnUpdate()
4536
{

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,11 +1204,11 @@ internal void Initialize(bool server)
12041204

12051205
// UnityTransport dependencies are then initialized
12061206
RealTimeProvider = ComponentFactory.Create<IRealTimeProvider>(this);
1207-
1207+
12081208
#if UNIFIED_NETCODE
12091209
NetworkConfig.NetworkTransport = gameObject.AddComponent<UnifiedNetcodeTransport>();
12101210
#endif
1211-
1211+
12121212
MetricsManager.Initialize(this);
12131213

12141214
{
@@ -1320,18 +1320,27 @@ private System.Collections.IEnumerator WaitForHybridPrefabRegistration(StartType
13201320
{
13211321
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] Netcode is not active but has an instance at this point.");
13221322
}
1323+
1324+
/// !! Important !!
1325+
/// Clear out any pre-existing configuration in the event this applicatioin instance has already been connected to a session.
1326+
NetCode.Netcode.Reset();
1327+
1328+
/// !! Initialize worlds here !!
1329+
/// Worlds are created here: <see cref="UnifiedBootStrap.Initialize"/>
13231330
DefaultWorldInitialization.Initialize("Default World", false);
1324-
var waitTime = new WaitForSeconds(0.016f);
1325-
// This should not be needed at this point, but here in the event something changes.
1326-
while (NetworkConfig.Prefabs.HasPendingGhostPrefabs)
1331+
1332+
// This should not be needed at this point, but this is here in the event something changes.
1333+
if (NetworkConfig.Prefabs.HasPendingGhostPrefabs)
13271334
{
1328-
if (LogLevel <= LogLevel.Developer)
1335+
NetworkLog.LogWarning($"[{nameof(WaitForHybridPrefabRegistration)}] !!!!! (Ghosts are still pending registration) !!!!!");
1336+
var waitTime = new WaitForSeconds(0.016f);
1337+
while (NetworkConfig.Prefabs.HasPendingGhostPrefabs)
13291338
{
1330-
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] Ghosts are still pending registration!");
1339+
NetworkConfig.Prefabs.RegisterGhostPrefabs(this);
1340+
yield return waitTime;
13311341
}
1332-
NetworkConfig.Prefabs.RegisterGhostPrefabs(this);
1333-
yield return waitTime;
13341342
}
1343+
13351344
if (LogLevel <= LogLevel.Developer)
13361345
{
13371346
NetworkLog.LogInfo($"[{nameof(WaitForHybridPrefabRegistration)}] All hybrid prefabs have been registered!");

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,9 +1740,11 @@ private void OnDestroy()
17401740
#if UNIFIED_NETCODE
17411741
spawnManager?.GhostsPendingSpawn.Remove(NetworkObjectId);
17421742
spawnManager?.GhostsPendingSynchronization.Remove(NetworkObjectId);
1743-
#endif
1744-
1743+
// N4E controls this on the client, allow this if there is a ghost
1744+
if (IsSpawned && !HasGhost && !networkManager.ShutdownInProgress)
1745+
#else
17451746
if (IsSpawned && !networkManager.ShutdownInProgress)
1747+
#endif
17461748
{
17471749
// An authorized destroy is when done by the authority instance or done due to a scene event and the NetworkObject
17481750
// was marked as destroy pending scene event (which means the destroy with scene property was set).

com.unity.netcode.gameobjects/Runtime/Messaging/Messages/SceneEventMessage.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
2-
using Unity.Netcode.Components;
3-
using Unity.Netcode.Unified;
4-
51
namespace Unity.Netcode
62
{
73
// Todo: Would be lovely to get this one nicely formatted with all the data it sends in the struct
@@ -30,11 +26,6 @@ public void Handle(ref NetworkContext context)
3026
{
3127
var networkManager = (NetworkManager)context.SystemOwner;
3228
networkManager.SceneManager.HandleSceneEvent(context.SenderId, m_ReceivedData);
33-
34-
#if UNIFIED_NETCODE
35-
var unifiedConnectionSystem = NetCode.Netcode.GetWorld(false).GetExistingSystemManaged<UnifiedUpdateConnections>();
36-
unifiedConnectionSystem.MarkSync((int)context.SenderId);
37-
#endif
3829
}
3930
}
4031
}

com.unity.netcode.gameobjects/Runtime/Spawning/NetworkSpawnManager.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,14 @@ internal NetworkObject GetGhostNetworkObjectForSpawn(ulong networkObjectId)
8383
return null;
8484
}
8585
var networkObject = GhostsPendingSpawn[networkObjectId];
86+
8687
GhostsPendingSpawn.Remove(networkObjectId);
87-
// TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
88-
// NOTE: We might be able to use the NetworkSceneHandle to get the associated local scene handle to which we can use to get the targeted scene.
89-
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(networkObject.gameObject, UnityEngine.SceneManagement.SceneManager.GetActiveScene());
88+
if (networkObject != null)
89+
{
90+
// TODO-UNIFIED: We need a better way to preserve any hybrid instances pending NGO spawn.
91+
// NOTE: We might be able to use the NetworkSceneHandle to get the associated local scene handle to which we can use to get the targeted scene.
92+
UnityEngine.SceneManagement.SceneManager.MoveGameObjectToScene(networkObject.gameObject, UnityEngine.SceneManagement.SceneManager.GetActiveScene());
93+
}
9094
return networkObject;
9195
}
9296

com.unity.netcode.gameobjects/Runtime/Transports/Unified/UnifiedNetcodeTransport.cs

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
#if UNIFIED_NETCODE
22
using System;
33
using System.Collections.Generic;
4-
using System.Text;
54
using Unity.Burst;
65
using Unity.Burst.Intrinsics;
76
using Unity.Collections;
8-
using Unity.Collections.LowLevel.Unsafe;
97
using Unity.Entities;
108
using Unity.NetCode;
119
using Unity.Netcode.Transports.UTP;
1210
using Unity.Networking.Transport;
13-
using UnityEngine;
1411

1512
namespace Unity.Netcode.Unified
1613
{
14+
[BurstCompile]
1715
internal struct TransportRpc : IRpcCommand, IRpcCommandSerializer<TransportRpc>
1816
{
1917
public FixedList4096Bytes<byte> Buffer;
@@ -31,8 +29,10 @@ public unsafe void Deserialize(ref DataStreamReader reader, in RpcDeserializerSt
3129
{
3230
data.Order = reader.ReadULong();
3331
var length = reader.ReadInt();
34-
data.Buffer = new FixedList4096Bytes<byte>();
35-
data.Buffer.Length = length;
32+
data.Buffer = new FixedList4096Bytes<byte>()
33+
{
34+
Length = length
35+
};
3636
var span = new Span<byte>(data.Buffer.GetUnsafePtr(), length);
3737
reader.ReadBytes(span);
3838
}
@@ -43,29 +43,29 @@ private static void InvokeExecute(ref RpcExecutor.Parameters parameters)
4343
RpcExecutor.ExecuteCreateRequestComponent<TransportRpc, TransportRpc>(ref parameters);
4444
}
4545

46-
static readonly PortableFunctionPointer<RpcExecutor.ExecuteDelegate> InvokeExecuteFunctionPointer = new PortableFunctionPointer<RpcExecutor.ExecuteDelegate>(InvokeExecute);
46+
private static readonly PortableFunctionPointer<RpcExecutor.ExecuteDelegate> k_InvokeExecuteFunctionPointer = new PortableFunctionPointer<RpcExecutor.ExecuteDelegate>(InvokeExecute);
4747

4848
public PortableFunctionPointer<RpcExecutor.ExecuteDelegate> CompileExecute()
4949
{
50-
return InvokeExecuteFunctionPointer;
50+
return k_InvokeExecuteFunctionPointer;
5151
}
5252
}
5353

5454
[UpdateInGroup(typeof(RpcCommandRequestSystemGroup))]
5555
[CreateAfter(typeof(RpcSystem))]
5656
[BurstCompile]
57-
partial struct TransportRpcCommandRequestSystem : ISystem
57+
internal partial struct TransportRpcCommandRequestSystem : ISystem
5858
{
5959
private RpcCommandRequest<TransportRpc, TransportRpc> m_Request;
6060

6161
[BurstCompile]
62-
struct SendRpc : IJobChunk
62+
internal struct SendRpc : IJobChunk
6363
{
64-
public RpcCommandRequest<TransportRpc, TransportRpc>.SendRpcData data;
64+
public RpcCommandRequest<TransportRpc, TransportRpc>.SendRpcData Data;
6565

6666
public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex, bool useEnabledMask, in v128 chunkEnabledMask)
6767
{
68-
data.Execute(chunk, unfilteredChunkIndex);
68+
Data.Execute(chunk, unfilteredChunkIndex);
6969
}
7070
}
7171

@@ -77,7 +77,7 @@ public void OnCreate(ref SystemState state)
7777
[BurstCompile]
7878
public void OnUpdate(ref SystemState state)
7979
{
80-
var sendJob = new SendRpc { data = m_Request.InitJobData(ref state) };
80+
var sendJob = new SendRpc { Data = m_Request.InitJobData(ref state) };
8181
state.Dependency = sendJob.Schedule(m_Request.Query, state.Dependency);
8282
}
8383
}
@@ -128,10 +128,6 @@ internal class UnifiedNetcodeTransport : NetworkTransport
128128
private int m_ServerClientId = -1;
129129
public override ulong ServerClientId => (ulong)m_ServerClientId;
130130

131-
private bool m_IsClient;
132-
private bool m_IsServer;
133-
private bool m_StartedServerWorld = false;
134-
private bool m_StartedClientWorld = false;
135131
private NetworkManager m_NetworkManager;
136132

137133
private IRealTimeProvider m_RealTimeProvider;
@@ -265,12 +261,6 @@ private void OnServerClientDisconnected(Connection connection, NetCodeConnection
265261

266262
public override bool StartClient()
267263
{
268-
if (!UnifiedBootStrap.HasClientWorlds)
269-
{
270-
UnifiedBootStrap.CreateClientWorld("ClientWorld");
271-
m_StartedClientWorld = true;
272-
}
273-
274264
NetCode.Netcode.Client.OnConnect = OnClientConnectedToServer;
275265
NetCode.Netcode.Client.OnDisconnect = OnClientDisconnectFromServer;
276266
var updateSystem = NetCode.Netcode.GetWorld(false).GetExistingSystemManaged<UnifiedNetcodeUpdateSystem>();
@@ -283,17 +273,9 @@ public override bool StartClient()
283273

284274
public override bool StartServer()
285275
{
286-
if (!UnifiedBootStrap.HasServerWorld)
276+
foreach (var connection in NetCode.Netcode.Server.Connections)
287277
{
288-
UnifiedBootStrap.CreateServerWorld("ServerWorld");
289-
m_StartedClientWorld = true;
290-
}
291-
else
292-
{
293-
foreach (var connection in NetCode.Netcode.Server.Connections)
294-
{
295-
OnServerNewClientConnection(connection, default);
296-
}
278+
OnServerNewClientConnection(connection, default);
297279
}
298280

299281
NetCode.Netcode.Server.OnConnect = OnServerNewClientConnection;
@@ -326,24 +308,17 @@ public override ulong GetCurrentRtt(ulong clientId)
326308
return (ulong)m_Connections[(int)transportId].Connection.RTT;
327309
}
328310

329-
public override void Shutdown()
330-
{
331-
if (m_StartedClientWorld)
332-
{
333-
UnifiedBootStrap.StopClient();
334-
}
335-
if (m_StartedServerWorld)
336-
{
337-
UnifiedBootStrap.StopServer();
338-
}
339-
}
340-
341311
public override void Initialize(NetworkManager networkManager = null)
342312
{
343313
m_Connections = new Dictionary<int, ConnectionInfo>();
344314
m_RealTimeProvider = networkManager.RealTimeProvider;
345315
m_NetworkManager = networkManager;
346316
}
317+
318+
public override void Shutdown()
319+
{
320+
321+
}
347322
}
348323
}
349-
#endif
324+
#endif

0 commit comments

Comments
 (0)