Skip to content

Commit 4b99f27

Browse files
fix: ClientIdToTransportId fix when MULTIPLAYER_TOOLS_1_0_0_PRE_7 is defined (#3730)
* fix When MULTIPLAYER_TOOLS_1_0_0_PRE_7 is defined there is code within UnityTransport that did not take the ClientIdToTransportId into consideration. This resolves that issue. * update Updating packages. Adding multiplayer tools to assure nothing that uses an asmdef defined define breaks if changes are made. * style Fixing whitespace. * style More whitespace issues.
1 parent a064258 commit 4b99f27

File tree

7 files changed

+204
-97
lines changed

7 files changed

+204
-97
lines changed

com.unity.netcode.gameobjects/Editor/NetworkManagerEditor.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ private void DisplayNetworkManagerProperties()
285285
}
286286
}
287287
var networkPrefabs = m_NetworkManager.NetworkConfig.MigrateOldNetworkPrefabsToNetworkPrefabsList();
288+
#if UNITY_6000_2_OR_NEWER
289+
string path = Path.Combine(directory, $"NetworkPrefabs-{m_NetworkManager.GetEntityId()}.asset");
290+
#else
288291
string path = Path.Combine(directory, $"NetworkPrefabs-{m_NetworkManager.GetInstanceID()}.asset");
292+
#endif
289293
Debug.Log("Saving migrated Network Prefabs List to " + path);
290294
AssetDatabase.CreateAsset(networkPrefabs, path);
291295
EditorUtility.SetDirty(m_NetworkManager);
@@ -390,7 +394,8 @@ public override void OnInspectorGUI()
390394
#if !MULTIPLAYER_TOOLS
391395
DrawInstallMultiplayerToolsTip();
392396
#endif
393-
void SetExpanded(bool expanded) { networkManager.NetworkManagerExpanded = expanded; };
397+
void SetExpanded(bool expanded) { networkManager.NetworkManagerExpanded = expanded; }
398+
;
394399
DrawFoldOutGroup<NetworkManager>(networkManager.GetType(), DisplayNetworkManagerProperties, networkManager.NetworkManagerExpanded, SetExpanded);
395400
DisplayCallToActionButtons();
396401
base.OnInspectorGUI();

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ public void RegisterHandler(IContactEventHandler contactEventHandler, bool regis
125125
{
126126
return;
127127
}
128+
#if UNITY_6000_2_OR_NEWER
129+
var instanceId = rigidbody.GetEntityId();
130+
#else
128131
var instanceId = rigidbody.GetInstanceID();
132+
#endif
129133
if (register)
130134
{
131135
if (!m_RigidbodyMapping.ContainsKey(instanceId))

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

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,7 @@ public bool TrySetParent(NetworkObject parent, bool worldPositionStays = true)
22172217
// DANGO-TODO: Do we want to worry about ownership permissions here?
22182218
// It wouldn't make sense to not allow parenting, but keeping this note here as a reminder.
22192219
var isAuthority = HasAuthority || (AllowOwnerToParent && IsOwner);
2220+
Debug.Log($"something is broken! isAuthority={isAuthority} | HasAuthority={HasAuthority} | (AllowOwnerToParent && IsOwner)={(AllowOwnerToParent && IsOwner)}");
22202221

22212222
// If we don't have authority and we are not shutting down, then don't allow any parenting.
22222223
// If we are shutting down and don't have authority then allow it.
@@ -2376,47 +2377,29 @@ private void OnTransformParentChanged()
23762377
m_CachedWorldPositionStays = true;
23772378
}
23782379

2379-
// If we are connected to a CMB service or we are running a mock CMB service then send to the "server" identifier
2380-
if (distributedAuthority || (!distributedAuthority && AllowOwnerToParent && IsOwner && !NetworkManager.IsServer))
2380+
// If we're not the server, we should tell the server about this parent change
2381+
if (!NetworkManager.IsServer)
23812382
{
2382-
if (!NetworkManager.DAHost)
2383+
// Don't send a message in DA mode if we're the only observers of this object (we're the only authority).
2384+
if (distributedAuthority && Observers.Count <= 1)
23832385
{
2384-
NetworkManager.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, 0);
23852386
return;
23862387
}
2387-
else
2388-
{
2389-
foreach (var clientId in NetworkManager.ConnectionManager.ConnectedClientIds)
2390-
{
2391-
if (clientId == NetworkManager.ServerClientId)
2392-
{
2393-
continue;
2394-
}
2395-
NetworkManager.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, clientId);
2396-
}
2397-
}
2388+
2389+
NetworkManager.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, NetworkManager.ServerClientId);
2390+
return;
23982391
}
2399-
else
2392+
2393+
// Otherwise we are a Server (client-server or DAHost). Send to all observers
2394+
foreach (var clientId in NetworkManager.ConnectionManager.ConnectedClientIds)
24002395
{
2401-
// Otherwise we are running in client-server =or= this has to be a DAHost instance.
2402-
// Send to all connected clients.
2403-
unsafe
2396+
if (clientId == NetworkManager.ServerClientId)
24042397
{
2405-
var maxCount = NetworkManager.ConnectedClientsIds.Count;
2406-
ulong* clientIds = stackalloc ulong[maxCount];
2407-
int idx = 0;
2408-
foreach (var clientId in NetworkManager.ConnectionManager.ConnectedClientIds)
2409-
{
2410-
if (clientId == NetworkManager.ServerClientId)
2411-
{
2412-
continue;
2413-
}
2414-
if (Observers.Contains(clientId))
2415-
{
2416-
clientIds[idx++] = clientId;
2417-
}
2418-
}
2419-
NetworkManager.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, clientIds, idx);
2398+
continue;
2399+
}
2400+
if (Observers.Contains(clientId))
2401+
{
2402+
NetworkManager.ConnectionManager.SendMessage(ref message, MessageDeliveryType<ParentSyncMessage>.DefaultDelivery, clientId);
24202403
}
24212404
}
24222405
}

com.unity.netcode.gameobjects/Runtime/Transports/UTP/UnityTransport.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,11 @@ private void InitDriver()
428428
out m_UnreliableFragmentedPipeline,
429429
out m_UnreliableSequencedFragmentedPipeline,
430430
out m_ReliableSequencedPipeline);
431-
431+
#if UNITY_6000_2_OR_NEWER
432+
TransportInitialized?.Invoke(GetEntityId(), m_Driver);
433+
#else
432434
TransportInitialized?.Invoke(GetInstanceID(), m_Driver);
435+
#endif
433436
}
434437

435438
private void DisposeInternals()
@@ -446,7 +449,11 @@ private void DisposeInternals()
446449

447450
m_SendQueue.Clear();
448451

452+
#if UNITY_6000_2_OR_NEWER
453+
TransportDisposed?.Invoke(GetEntityId());
454+
#else
449455
TransportDisposed?.Invoke(GetInstanceID());
456+
#endif
450457
}
451458

452459
/// <summary>
@@ -496,14 +503,11 @@ public NetworkSettings GetDefaultNetworkSettings()
496503
// Latency, jitter and packet loss will be set by the network simulator in the tools
497504
// package. We just need to initialize the settings since otherwise these features will
498505
// not be enabled at all in the driver.
499-
settings.WithSimulatorStageParameters(
500-
// Assuming a maximum average latency of 50 ms, and that we're somehow able to flush
501-
// an entire reliable window every tick, then at 60 ticks per second we need to be
502-
// able to store 60 * 0.05 * 64 = 192 packets per connection in the simulator
503-
// pipeline stage. Double that since we handle both directions and round it up, and
504-
// that's how we get 400 here.
505-
maxPacketCount: 400,
506-
randomSeed: DebugSimulatorRandomSeed ?? (uint)System.Diagnostics.Stopwatch.GetTimestamp());
506+
// Assuming a maximum average latency of 50 ms, and that we're somehow able to flush an entire reliable window every tick,
507+
// then at 60 ticks per second we need to be able to store 60 * 0.05 * 64 = 192 packets per connection in the simulator
508+
// pipeline stage. Double that since we handle both directions and round it up, and
509+
// that's how we get 400 here.
510+
settings.WithSimulatorStageParameters(maxPacketCount: 400, randomSeed: DebugSimulatorRandomSeed ?? (uint)System.Diagnostics.Stopwatch.GetTimestamp());
507511
settings.WithNetworkSimulatorParameters();
508512
#endif
509513

@@ -1100,15 +1104,18 @@ private void ExtractNetworkMetrics()
11001104
{
11011105
if (m_NetworkManager.IsServer)
11021106
{
1103-
for (int i=0; i<m_NetworkManager.ConnectedClientsIds.Count; ++i)
1107+
for (int i = 0; i < m_NetworkManager.ConnectedClientsIds.Count; ++i)
11041108
{
1105-
var ngoConnectionId = m_NetworkManager.ConnectedClientsIds[i];
1109+
var ngoConnectionId = m_NetworkManager.ConnectedClientsIds[i];
11061110
if (ngoConnectionId == 0 && m_NetworkManager.IsHost)
11071111
{
11081112
continue;
11091113
}
1110-
var transportClientId = m_NetworkManager.ConnectionManager.ClientIdToTransportId(ngoConnectionId);
1111-
ExtractNetworkMetricsForClient(transportClientId);
1114+
var transportClientIdReturn = m_NetworkManager.ConnectionManager.ClientIdToTransportId(ngoConnectionId);
1115+
if (transportClientIdReturn.Item2)
1116+
{
1117+
ExtractNetworkMetricsForClient(transportClientIdReturn.Item1);
1118+
}
11121119
}
11131120
}
11141121
else

com.unity.netcode.gameobjects/Tests/Runtime/NetworkTransform/NetworkTransformOrderOfOperations.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ private bool TransformsMatch(StringBuilder errorLog)
111111

112112
foreach (var networkManager in m_NetworkManagers)
113113
{
114+
if (!networkManager.SpawnManager.SpawnedObjects.ContainsKey(m_AuthoritySeqControllerInstance.NetworkObjectId))
115+
{
116+
hasErrors = true;
117+
continue;
118+
}
114119
var nonAuthorityInstance = networkManager.SpawnManager.SpawnedObjects[m_AuthoritySeqControllerInstance.NetworkObjectId].GetComponent<SpawnSequenceController>();
115120
var nonAuthorityEulerRotation = nonAuthorityInstance.GetSpaceRelativeRotation().eulerAngles;
116121

@@ -173,7 +178,7 @@ public IEnumerator OrderOfOperations()
173178
var parent1 = m_AuthorityParentInstances[0];
174179
var parent2 = m_AuthorityParentInstances[1];
175180

176-
ConfigureSequencesTest1(parent1);
181+
ConfigureSequencesTest1_ClientServerOnly(parent1);
177182
yield return RunTestSequences();
178183

179184
ConfigureSequencesTest2(parent1);
@@ -288,12 +293,19 @@ private IEnumerator __RunTestSequences(bool spawnWithObservers = true, bool spaw
288293

289294
#region Test Sequence Configurations
290295
/// <summary>
296+
/// Client server only - under da mode only the authority can parent
291297
/// Test-1:
292298
/// Authority-> Spawn, change ownership, (wait), parent
293299
/// </summary>
294-
private void ConfigureSequencesTest1(NetworkObject parent)
300+
private void ConfigureSequencesTest1_ClientServerOnly(NetworkObject parent)
295301
{
296-
SpawnSequenceController.CurrentTest = "Test1";
302+
SpawnSequenceController.CurrentTest = "Test1 (Client-Server Only)";
303+
if (m_AuthorityNetworkManager.DistributedAuthorityMode)
304+
{
305+
SpawnSequenceController.ClientServerOnly = true;
306+
return;
307+
}
308+
297309
var changeOwnershipSequence = new ChangeOwnershipSequence()
298310
{
299311
Stage = SpawnSequence.SpawnStage.AfterSpawn,
@@ -305,6 +317,7 @@ private void ConfigureSequencesTest1(NetworkObject parent)
305317
TimeDelayInMS = 200,
306318
Stage = SpawnSequence.SpawnStage.AfterSpawn,
307319
TargetParent = parent,
320+
InvokeOnlyOnClientId = GetAuthorityNetworkManager().LocalClientId,
308321
};
309322

310323
SpawnSequenceController.AddAction(changeOwnershipSequence);
@@ -855,6 +868,10 @@ protected override bool OnShouldInvoke(SpawnStage stage)
855868
protected override void OnAction()
856869
{
857870
m_NetworkObject.ChangeOwnership(TargetOwnerClientId);
871+
if (TargetOwnerClientId != m_NetworkObject.OwnerClientId)
872+
{
873+
Debug.LogError($"[{m_NetworkObject.name}] Failed to change ownership!");
874+
}
858875
base.OnAction();
859876
}
860877
}
@@ -866,7 +883,20 @@ internal class ParentSequence : SpawnSequence
866883

867884
protected override bool OnShouldInvoke(SpawnStage stage)
868885
{
869-
return base.OnShouldInvoke(stage) && (m_NetworkObject.HasAuthority || (m_NetworkObject.IsOwner && m_NetworkObject.AllowOwnerToParent));
886+
// Don't invoke if the base says no
887+
if (!base.OnShouldInvoke(stage))
888+
{
889+
return false;
890+
}
891+
892+
// If sequence is configured to specifically invoke on this client
893+
if (InvokeOnlyOnClientId.HasValue && m_NetworkObject.NetworkManager.LocalClientId == InvokeOnlyOnClientId.Value)
894+
{
895+
return true;
896+
}
897+
898+
// Otherwise we should invoke if we have the authority to invoke
899+
return m_NetworkObject.HasAuthority || (m_NetworkObject.IsOwner && m_NetworkObject.AllowOwnerToParent);
870900
}
871901

872902
protected override void OnAction()

testproject/Packages/manifest.json

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
{
22
"disableProjectUpdate": false,
33
"dependencies": {
4-
"com.unity.addressables": "2.2.2",
5-
"com.unity.ai.navigation": "2.0.5",
6-
"com.unity.collab-proxy": "2.6.0",
7-
"com.unity.ide.rider": "3.0.31",
8-
"com.unity.ide.visualstudio": "2.0.22",
9-
"com.unity.mathematics": "1.3.2",
4+
"com.unity.addressables": "2.7.4",
5+
"com.unity.ai.navigation": "2.0.9",
6+
"com.unity.collab-proxy": "2.9.3",
7+
"com.unity.ide.rider": "3.0.38",
8+
"com.unity.ide.visualstudio": "2.0.25",
9+
"com.unity.mathematics": "1.3.3",
10+
"com.unity.multiplayer.tools": "2.2.6",
1011
"com.unity.netcode.gameobjects": "file:../../com.unity.netcode.gameobjects",
1112
"com.unity.package-validation-suite": "0.49.0-preview",
12-
"com.unity.services.authentication": "3.4.0",
13-
"com.unity.services.core": "1.14.0",
14-
"com.unity.test-framework": "1.4.6",
15-
"com.unity.test-framework.performance": "3.0.3",
16-
"com.unity.timeline": "1.8.7",
13+
"com.unity.services.authentication": "3.5.2",
14+
"com.unity.services.multiplayer": "1.1.8",
15+
"com.unity.test-framework": "1.6.0",
16+
"com.unity.test-framework.performance": "3.1.0",
17+
"com.unity.timeline": "1.8.9",
1718
"com.unity.ugui": "2.0.0",
1819
"com.unity.modules.accessibility": "1.0.0",
1920
"com.unity.modules.ai": "1.0.0",

0 commit comments

Comments
 (0)