Skip to content

Commit 7bb6e7e

Browse files
committed
Refactor into shared logic
1 parent 04a6b54 commit 7bb6e7e

File tree

4 files changed

+19
-14
lines changed

4 files changed

+19
-14
lines changed

com.unity.netcode.gameobjects/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ 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. (#2539)
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)
2122
- Fixed issue where the initial client synchronization pre-serialization process was not excluding spawned `NetworkObjects` that already had pending visibility for the client being synchronized. (#3493)
2223
- Fixed issue where invoking `NetworkObject.NetworkShow` and `NetworkObject.ChangeOwnership` consecutively within the same call stack location could result in an unnecessary change in ownership error message generated on the target client side. (#3493)
2324
- Fixed issue where `NetworkVariable`s on a `NetworkBehaviour` could fail to synchronize changes if one has `NetworkVariableUpdateTraits` set and is dirty but is not ready to send. (#3465)
2425
- Fixed issue where when a client changes ownership via RPC the `NetworkBehaviour.OnOwnershipChanged` can result in identical previous and current owner identifiers. (#3434)
25-
- Fixed `NullReferenceException` on `NetworkList` when used without a NetworkManager in scene. (#2539)
2626

2727
### Changed
2828

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ public IEnumerator<T> GetEnumerator()
425425
public void Add(T item)
426426
{
427427
// check write permissions
428-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
428+
if (LocalClientCannotWrite())
429429
{
430430
LogWritePermissionError();
431431
return;
@@ -452,7 +452,7 @@ public void Add(T item)
452452
public void Clear()
453453
{
454454
// check write permissions
455-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
455+
if (LocalClientCannotWrite())
456456
{
457457
LogWritePermissionError();
458458
return;
@@ -490,7 +490,7 @@ public bool Contains(T item)
490490
public bool Remove(T item)
491491
{
492492
// check write permissions
493-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
493+
if (LocalClientCannotWrite())
494494
{
495495
LogWritePermissionError();
496496
return false;
@@ -539,7 +539,7 @@ public int IndexOf(T item)
539539
public void Insert(int index, T item)
540540
{
541541
// check write permissions
542-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
542+
if (LocalClientCannotWrite())
543543
{
544544
LogWritePermissionError();
545545
return;
@@ -575,7 +575,7 @@ public void Insert(int index, T item)
575575
public void RemoveAt(int index)
576576
{
577577
// check write permissions
578-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
578+
if (LocalClientCannotWrite())
579579
{
580580
LogWritePermissionError();
581581
return;
@@ -608,7 +608,7 @@ public T this[int index]
608608
set
609609
{
610610
// check write permissions
611-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
611+
if (LocalClientCannotWrite())
612612
{
613613
LogWritePermissionError();
614614
return;

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void Reset(T value = default)
9090
// The introduction of standard .NET collections caused an issue with permissions since there is no way to detect changes in the
9191
// collection without doing a full comparison. While this approach does consume more memory per collection instance, it is the
9292
// lowest risk approach to resolving the issue where a client with no write permissions could make changes to a collection locally
93-
// which can cause a myriad of issues.
93+
// which can cause a myriad of issues.
9494
private protected T m_InternalOriginalValue;
9595

9696
private protected T m_PreviousValue;
@@ -112,7 +112,7 @@ public virtual T Value
112112
get => m_InternalValue;
113113
set
114114
{
115-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
115+
if (LocalClientCannotWrite())
116116
{
117117
LogWritePermissionError();
118118
return;
@@ -145,7 +145,7 @@ public bool CheckDirtyState(bool forceCheck = false)
145145
var isDirty = base.IsDirty();
146146

147147
// A client without permissions invoking this method should only check to assure the current value is equal to the last known current value
148-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId))
148+
if (LocalClientCannotWrite())
149149
{
150150
// If modifications are detected, then revert back to the last known current value
151151
if (!NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_InternalOriginalValue))
@@ -221,7 +221,7 @@ public override bool IsDirty()
221221
{
222222
// 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
223223
// to the original collection value prior to applying updates (primarily for collections).
224-
if (!NetworkUpdaterCheck && m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId) && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_InternalOriginalValue))
224+
if (!NetworkUpdaterCheck && LocalClientCannotWrite() && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalValue, ref m_InternalOriginalValue))
225225
{
226226
NetworkVariableSerialization<T>.Duplicate(m_InternalOriginalValue, ref m_InternalValue);
227227
return true;
@@ -297,7 +297,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
297297
{
298298
// 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
299299
// to the original collection value prior to applying updates (primarily for collections).
300-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId) && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
300+
if (LocalCLientCannotWrite() && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
301301
{
302302
NetworkVariableSerialization<T>.Duplicate(m_InternalOriginalValue, ref m_InternalValue);
303303
}
@@ -320,7 +320,7 @@ public override void ReadDelta(FastBufferReader reader, bool keepDirtyDelta)
320320
/// This should be always invoked (client & server) to assure the previous values are set
321321
/// !! IMPORTANT !!
322322
/// When a server forwards delta updates to connected clients, it needs to preserve the previous dirty value(s)
323-
/// until it is done serializing all valid NetworkVariable field deltas (relative to each client). This is invoked
323+
/// until it is done serializing all valid NetworkVariable field deltas (relative to each client). This is invoked
324324
/// after it is done forwarding the deltas at the end of the <see cref="NetworkVariableDeltaMessage.Handle(ref NetworkContext)"/> method.
325325
/// </summary>
326326
internal override void PostDeltaRead()
@@ -338,7 +338,7 @@ public override void ReadField(FastBufferReader reader)
338338
{
339339
// 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
340340
// to the original collection value prior to applying updates (primarily for collections).
341-
if (m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId) && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
341+
if (LocalClientCannotWrite() && !NetworkVariableSerialization<T>.AreEqual(ref m_InternalOriginalValue, ref m_InternalValue))
342342
{
343343
NetworkVariableSerialization<T>.Duplicate(m_InternalOriginalValue, ref m_InternalValue);
344344
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ public bool CanClientWrite(ulong clientId)
337337
}
338338
}
339339

340+
internal bool LocalClientCannotWrite()
341+
{
342+
return m_NetworkManager && !CanClientWrite(m_NetworkManager.LocalClientId);
343+
}
344+
340345
/// <summary>
341346
/// Returns the ClientId of the owning client
342347
/// </summary>

0 commit comments

Comments
 (0)