Skip to content

Commit 283493e

Browse files
EmandMPitouGames
andauthored
fix: Avoid throwing exception when using NetworkList without a NetworkManager (#3502)
<!-- Replace this block with what this PR does and why. Describe what you'd like reviewers to know, how you applied the engineering principles, and any interesting tradeoffs made. Delete bullet points below that don't apply, and update the changelog section as appropriate. --> <!-- Add short version of the JIRA ticket to the PR title (e.g. "feat: new shiny feature [MTT-123]") --> Continues #2540 Fixes #2539 ## Changelog - Added: `LocalClientCannotWrite` function to co-locate the shared functionality - Fixed: Ensure that the `NetworkManager` exists before attempting to access the `LocalClientId` ## Testing and Documentation - No tests have been added. <!-- Uncomment and mark items off with a * if this PR deprecates any API: ### Deprecated API - [ ] An `[Obsolete]` attribute was added along with a `(RemovedAfter yyyy-mm-dd)` entry. - [ ] An [api updater] was added. - [ ] Deprecation of the API is explained in the CHANGELOG. - [ ] The users can understand why this API was removed and what they should use instead. --> ## Backport <!-- If this is a backport: - Add the following to the PR title: "\[Backport\] ..." . - Link to the original PR. If this needs a backport - state this here If a backport is not needed please provide the reason why. If the "Backports" section is not present it will lead to a CI test failure. --> Up-ported in #3503 --------- Co-authored-by: PitouGames <[email protected]>
1 parent bd3e5a2 commit 283493e

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Additional documentation and release notes are available at [Multiplayer Documen
1515

1616
### Fixed
1717

18+
- Fixed `NullReferenceException` on `NetworkList` when used without a NetworkManager in scene. (#3502)
1819
- Fixed inconsistencies in the `OnSceneEvent` callback. (#3487)
1920
- Fixed issue where `NetworkClient` could persist some settings if re-using the same `NetworkManager` instance. (#3494)
2021
- Fixed issue where a pooled `NetworkObject` was not resetting the internal latest parent property when despawned. (#3494)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ public void Serialize(FastBufferWriter writer, int targetVersion)
144144
var startingSize = writer.Length;
145145
var networkVariable = NetworkBehaviour.NetworkVariableFields[i];
146146
var shouldWrite = networkVariable.IsDirty() &&
147-
networkVariable.CanClientRead(TargetClientId) &&
148-
(networkManager.IsServer || networkVariable.CanClientWrite(networkManager.LocalClientId)) &&
149-
networkVariable.CanSend();
147+
networkVariable.CanClientRead(TargetClientId) &&
148+
(networkManager.IsServer || networkVariable.CanWrite) &&
149+
networkVariable.CanSend();
150150

151151
// Prevent the server from writing to the client that owns a given NetworkVariable
152152
// Allowing the write would send an old value to the client and cause jitter

com.unity.netcode.gameobjects/Runtime/NetworkVariable/AnticipatedNetworkVariable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public void Anticipate(T value)
213213
m_LastAnticipationCounter = m_NetworkBehaviour.NetworkManager.AnticipationSystem.AnticipationCounter;
214214
m_AnticipatedValue = value;
215215
NetworkVariableSerialization<T>.Duplicate(m_AnticipatedValue, ref m_PreviousAnticipatedValue);
216-
if (CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId))
216+
if (CanWrite)
217217
{
218218
AuthoritativeValue = value;
219219
}

com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ public IEnumerator<T> GetEnumerator()
428428
public void Add(T item)
429429
{
430430
// check write permissions
431-
if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId))
431+
if (CannotWrite)
432432
{
433433
LogWritePermissionError();
434434
return;
@@ -455,7 +455,7 @@ public void Add(T item)
455455
public void Clear()
456456
{
457457
// check write permissions
458-
if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId))
458+
if (CannotWrite)
459459
{
460460
LogWritePermissionError();
461461
return;
@@ -493,7 +493,7 @@ public bool Contains(T item)
493493
public bool Remove(T item)
494494
{
495495
// check write permissions
496-
if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId))
496+
if (CannotWrite)
497497
{
498498
LogWritePermissionError();
499499
return false;
@@ -542,7 +542,7 @@ public int IndexOf(T item)
542542
public void Insert(int index, T item)
543543
{
544544
// check write permissions
545-
if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId))
545+
if (CannotWrite)
546546
{
547547
LogWritePermissionError();
548548
return;
@@ -578,7 +578,7 @@ public void Insert(int index, T item)
578578
public void RemoveAt(int index)
579579
{
580580
// check write permissions
581-
if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId))
581+
if (CannotWrite)
582582
{
583583
LogWritePermissionError();
584584
return;
@@ -611,7 +611,7 @@ public T this[int index]
611611
set
612612
{
613613
// check write permissions
614-
if (!CanClientWrite(m_NetworkBehaviour.NetworkManager.LocalClientId))
614+
if (CannotWrite)
615615
{
616616
LogWritePermissionError();
617617
return;

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariable.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public virtual T Value
128128
get => m_InternalValue;
129129
set
130130
{
131-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
131+
if (CannotWrite)
132132
{
133133
LogWritePermissionError();
134134
return;
@@ -162,7 +162,7 @@ public bool CheckDirtyState(bool forceCheck = false)
162162
var isDirty = base.IsDirty();
163163

164164
// A client without permissions invoking this method should only check to assure the current value is equal to the last known current value
165-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
165+
if (CannotWrite)
166166
{
167167
// If modifications are detected, then revert back to the last known current value
168168
if (!NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_InternalOriginalValue))
@@ -242,7 +242,7 @@ public override bool IsDirty()
242242
{
243243
// If the client does not have write permissions but the internal value is determined to be locally modified and we are applying updates, then we should revert
244244
// to the original collection value prior to applying updates (primarily for collections).
245-
if (!NetworkUpdaterCheck && m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId) && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_InternalOriginalValue))
245+
if (!NetworkUpdaterCheck && CannotWrite && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_InternalOriginalValue))
246246
{
247247
NetworkVariableSerialization<T>.Duplicate(m_InternalOriginalValue, ref m_InternalValue);
248248
return true;
@@ -318,7 +318,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
318318
{
319319
// If the client does not have write permissions but the internal value is determined to be locally modified and we are applying updates, then we should revert
320320
// to the original collection value prior to applying updates (primarily for collections).
321-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId) && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
321+
if (CannotWrite && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
322322
{
323323
NetworkVariableSerialization<T>.Duplicate(m_InternalOriginalValue, ref m_InternalValue);
324324
}
@@ -359,7 +359,7 @@ public override void ReadField(FastBufferReader reader)
359359
{
360360
// If the client does not have write permissions but the internal value is determined to be locally modified and we are applying updates, then we should revert
361361
// to the original collection value prior to applying updates (primarily for collections).
362-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId) && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
362+
if (CannotWrite && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
363363
{
364364
NetworkVariableSerialization<T>.Duplicate(m_InternalOriginalValue, ref m_InternalValue);
365365
}

com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableBase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,16 @@ public bool CanClientWrite(ulong clientId)
353353
}
354354
}
355355

356+
/// <summary>
357+
/// Returns true if the current <see cref="NetworkManager.LocalClientId"/> can write to this variable; otherwise false.
358+
/// </summary>
359+
internal bool CanWrite => m_NetworkManager && CanClientWrite(m_NetworkManager.LocalClientId);
360+
361+
/// <summary>
362+
/// Returns false if the current <see cref="NetworkManager.LocalClientId"/> can write to this variable; otherwise true.
363+
/// </summary>
364+
internal bool CannotWrite => m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId);
365+
356366
/// <summary>
357367
/// Returns the ClientId of the owning client
358368
/// </summary>

0 commit comments

Comments
 (0)