diff --git a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs index 9c25263d21..aa1e25338b 100644 --- a/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs +++ b/com.unity.netcode.gameobjects/Runtime/Core/NetworkBehaviour.cs @@ -639,7 +639,7 @@ internal void HandleNetworkVariableDeltas(Stream stream, ulong clientId) } } - if (NetworkManager.IsServer && !NetworkVariableFields[i].CanClientWrite(clientId)) + if (NetworkManager.IsServer) { // we are choosing not to fire an exception here, because otherwise a malicious client could use this to crash the server if (NetworkManager.NetworkConfig.EnsureNetworkVariableLengthSafety) @@ -824,5 +824,24 @@ protected NetworkObject GetNetworkObject(ulong networkId) { return NetworkManager.SpawnManager.SpawnedObjects.TryGetValue(networkId, out NetworkObject networkObject) ? networkObject : null; } + + public void OnDestroy() + { + // this seems odd to do here, but in fact especially in tests we can find ourselves + // here without having called InitializedVariables, which causes problems if any + // of those variables use native containers (e.g. NetworkList) as they won't be + // registered here and therefore won't be cleaned up. + // + // we should study to understand the initialization patterns + if (!m_VarInit) + { + InitializeVariables(); + } + + for (int i = 0; i < NetworkVariableFields.Count; i++) + { + NetworkVariableFields[i].Dispose(); + } + } } } diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkDictionary.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkDictionary.cs deleted file mode 100644 index 18b2360436..0000000000 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkDictionary.cs +++ /dev/null @@ -1,502 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using System.IO; - -namespace Unity.Netcode -{ - /// - /// Event based NetworkVariable container for syncing Dictionaries - /// - /// The type for the dictionary keys - /// The type for the dictionary values - public class NetworkDictionary : NetworkVariableBase, IDictionary where TKey : unmanaged where TValue : unmanaged - { - private readonly IDictionary m_Dictionary = new Dictionary(); - private readonly List> m_DirtyEvents = new List>(); - - /// - /// Delegate type for dictionary changed event - /// - /// Struct containing information about the change event - public delegate void OnDictionaryChangedDelegate(NetworkDictionaryEvent changeEvent); - - /// - /// The callback to be invoked when the dictionary gets changed - /// - public event OnDictionaryChangedDelegate OnDictionaryChanged; - - /// - /// Creates a NetworkDictionary with the default value and settings - /// - public NetworkDictionary() { } - - /// - /// Creates a NetworkDictionary with the default value and custom settings - /// - /// The read permission to use for this NetworkDictionary - public NetworkDictionary(NetworkVariableReadPermission readPerm) : base(readPerm) { } - - /// - /// Creates a NetworkDictionary with a custom value and custom settings - /// - /// The read permission to use for this NetworkDictionary - /// The initial value to use for the NetworkDictionary - public NetworkDictionary(NetworkVariableReadPermission readPerm, IDictionary value) : base(readPerm) - { - m_Dictionary = value; - } - - /// - /// Creates a NetworkDictionary with a custom value and the default settings - /// - /// The initial value to use for the NetworkDictionary - public NetworkDictionary(IDictionary value) - { - m_Dictionary = value; - } - - /// - public override void ResetDirty() - { - base.ResetDirty(); - m_DirtyEvents.Clear(); - } - - /// - public override void ReadDelta(Stream stream, bool keepDirtyDelta) - { - using var reader = PooledNetworkReader.Get(stream); - ushort deltaCount = reader.ReadUInt16Packed(); - for (int i = 0; i < deltaCount; i++) - { - var eventType = (NetworkDictionaryEvent.EventType)reader.ReadBits(3); - switch (eventType) - { - case NetworkDictionaryEvent.EventType.Add: - { - var key = (TKey)reader.ReadObjectPacked(typeof(TKey)); - var value = (TValue)reader.ReadObjectPacked(typeof(TValue)); - m_Dictionary.Add(key, value); - - if (OnDictionaryChanged != null) - { - OnDictionaryChanged(new NetworkDictionaryEvent - { - Type = eventType, - Key = key, - Value = value - }); - } - - if (keepDirtyDelta) - { - m_DirtyEvents.Add(new NetworkDictionaryEvent() - { - Type = eventType, - Key = key, - Value = value - }); - } - } - break; - case NetworkDictionaryEvent.EventType.Remove: - { - var key = (TKey)reader.ReadObjectPacked(typeof(TKey)); - TValue value; - m_Dictionary.TryGetValue(key, out value); - m_Dictionary.Remove(key); - - if (OnDictionaryChanged != null) - { - OnDictionaryChanged(new NetworkDictionaryEvent - { - Type = eventType, - Key = key, - Value = value - }); - } - - if (keepDirtyDelta) - { - m_DirtyEvents.Add(new NetworkDictionaryEvent() - { - Type = eventType, - Key = key, - Value = value - }); - } - } - break; - case NetworkDictionaryEvent.EventType.RemovePair: - { - var key = (TKey)reader.ReadObjectPacked(typeof(TKey)); - var value = (TValue)reader.ReadObjectPacked(typeof(TValue)); - m_Dictionary.Remove(new KeyValuePair(key, value)); - - if (OnDictionaryChanged != null) - { - OnDictionaryChanged(new NetworkDictionaryEvent - { - Type = eventType, - Key = key, - Value = value - }); - } - - if (keepDirtyDelta) - { - m_DirtyEvents.Add(new NetworkDictionaryEvent() - { - Type = eventType, - Key = key, - Value = value - }); - } - } - break; - case NetworkDictionaryEvent.EventType.Clear: - { - //read nothing - m_Dictionary.Clear(); - - if (OnDictionaryChanged != null) - { - OnDictionaryChanged(new NetworkDictionaryEvent - { - Type = eventType - }); - } - - if (keepDirtyDelta) - { - m_DirtyEvents.Add(new NetworkDictionaryEvent - { - Type = eventType - }); - } - } - break; - case NetworkDictionaryEvent.EventType.Value: - { - var key = (TKey)reader.ReadObjectPacked(typeof(TKey)); - var value = (TValue)reader.ReadObjectPacked(typeof(TValue)); - - m_Dictionary[key] = value; - - if (OnDictionaryChanged != null) - { - OnDictionaryChanged(new NetworkDictionaryEvent - { - Type = eventType, - Key = key, - Value = value - }); - } - - if (keepDirtyDelta) - { - m_DirtyEvents.Add(new NetworkDictionaryEvent() - { - Type = eventType, - Key = key, - Value = value - }); - } - } - break; - } - } - } - - /// - public override void ReadField(Stream stream) - { - using var reader = PooledNetworkReader.Get(stream); - m_Dictionary.Clear(); - ushort entryCount = reader.ReadUInt16Packed(); - for (int i = 0; i < entryCount; i++) - { - var key = (TKey)reader.ReadObjectPacked(typeof(TKey)); - var value = (TValue)reader.ReadObjectPacked(typeof(TValue)); - m_Dictionary.Add(key, value); - } - } - - /// - public bool TryGetValue(TKey key, out TValue value) - { - return m_Dictionary.TryGetValue(key, out value); - } - - /// - public override void WriteDelta(Stream stream) - { - using var writer = PooledNetworkWriter.Get(stream); - writer.WriteUInt16Packed((ushort)m_DirtyEvents.Count); - for (int i = 0; i < m_DirtyEvents.Count; i++) - { - writer.WriteBits((byte)m_DirtyEvents[i].Type, 3); - switch (m_DirtyEvents[i].Type) - { - case NetworkDictionaryEvent.EventType.Add: - { - writer.WriteObjectPacked(m_DirtyEvents[i].Key); - writer.WriteObjectPacked(m_DirtyEvents[i].Value); - } - break; - case NetworkDictionaryEvent.EventType.Remove: - { - writer.WriteObjectPacked(m_DirtyEvents[i].Key); - } - break; - case NetworkDictionaryEvent.EventType.RemovePair: - { - writer.WriteObjectPacked(m_DirtyEvents[i].Key); - writer.WriteObjectPacked(m_DirtyEvents[i].Value); - } - break; - case NetworkDictionaryEvent.EventType.Clear: - { - //write nothing - } - break; - case NetworkDictionaryEvent.EventType.Value: - { - writer.WriteObjectPacked(m_DirtyEvents[i].Key); - writer.WriteObjectPacked(m_DirtyEvents[i].Value); - } - break; - } - } - } - - /// - public override void WriteField(Stream stream) - { - using var writer = PooledNetworkWriter.Get(stream); - writer.WriteUInt16Packed((ushort)m_Dictionary.Count); - foreach (KeyValuePair pair in m_Dictionary) - { - writer.WriteObjectPacked(pair.Key); - writer.WriteObjectPacked(pair.Value); - } - } - - /// - public override bool IsDirty() - { - return base.IsDirty() || m_DirtyEvents.Count > 0; - } - - /// - public TValue this[TKey key] - { - get => m_Dictionary[key]; - set - { - m_Dictionary[key] = value; - - var dictionaryEvent = new NetworkDictionaryEvent() - { - Type = NetworkDictionaryEvent.EventType.Value, - Key = key, - Value = value - }; - - HandleAddDictionaryEvent(dictionaryEvent); - } - } - - /// - public ICollection Keys => m_Dictionary.Keys; - - /// - public ICollection Values => m_Dictionary.Values; - - /// - public int Count => m_Dictionary.Count; - - /// - // TODO: remove w/ native containers - public bool IsReadOnly => m_Dictionary.IsReadOnly; - - /// - public void Add(TKey key, TValue value) - { - m_Dictionary.Add(key, value); - - var dictionaryEvent = new NetworkDictionaryEvent() - { - Type = NetworkDictionaryEvent.EventType.Add, - Key = key, - Value = value - }; - - HandleAddDictionaryEvent(dictionaryEvent); - } - - /// - public void Add(KeyValuePair item) - { - m_Dictionary.Add(item); - - var dictionaryEvent = new NetworkDictionaryEvent() - { - Type = NetworkDictionaryEvent.EventType.Add, - Key = item.Key, - Value = item.Value - }; - - HandleAddDictionaryEvent(dictionaryEvent); - } - - /// - public void Clear() - { - m_Dictionary.Clear(); - - var dictionaryEvent = new NetworkDictionaryEvent() - { - Type = NetworkDictionaryEvent.EventType.Clear - }; - - HandleAddDictionaryEvent(dictionaryEvent); - } - - /// - public bool Contains(KeyValuePair item) - { - return m_Dictionary.Contains(item); - } - - /// - public bool ContainsKey(TKey key) - { - return m_Dictionary.ContainsKey(key); - } - - /// - public void CopyTo(KeyValuePair[] array, int arrayIndex) - { - m_Dictionary.CopyTo(array, arrayIndex); - } - - /// - public IEnumerator> GetEnumerator() - { - return m_Dictionary.GetEnumerator(); - } - - /// - public bool Remove(TKey key) - { - m_Dictionary.Remove(key); - - TValue value; - m_Dictionary.TryGetValue(key, out value); - - var dictionaryEvent = new NetworkDictionaryEvent() - { - Type = NetworkDictionaryEvent.EventType.Remove, - Key = key, - Value = value - }; - - HandleAddDictionaryEvent(dictionaryEvent); - - return true; - } - - - /// - public bool Remove(KeyValuePair item) - { - m_Dictionary.Remove(item); - - var dictionaryEvent = new NetworkDictionaryEvent() - { - Type = NetworkDictionaryEvent.EventType.RemovePair, - Key = item.Key, - Value = item.Value - }; - - HandleAddDictionaryEvent(dictionaryEvent); - return true; - } - - /// - IEnumerator IEnumerable.GetEnumerator() - { - return m_Dictionary.GetEnumerator(); - } - - private void HandleAddDictionaryEvent(NetworkDictionaryEvent dictionaryEvent) - { - m_DirtyEvents.Add(dictionaryEvent); - OnDictionaryChanged?.Invoke(dictionaryEvent); - } - - public int LastModifiedTick - { - get - { - // todo: implement proper network tick for NetworkDictionary - return NetworkTickSystem.NoTick; - } - } - } - - /// - /// Struct containing event information about changes to a NetworkDictionary. - /// - /// The type for the dictionary key that the event is about - /// The type for the dictionary value that the event is about - public struct NetworkDictionaryEvent - { - /// - /// Enum representing the different operations available for triggering an event. - /// - public enum EventType - { - /// - /// Add - /// - Add, - - /// - /// Remove - /// - Remove, - - /// - /// Remove pair - /// - RemovePair, - - /// - /// Clear - /// - Clear, - - /// - /// Value changed - /// - Value - } - - /// - /// Enum representing the operation made to the dictionary. - /// - public EventType Type; - - /// - /// the key changed, added or removed if available. - /// - public TKey Key; - - /// - /// The value changed, added or removed if available. - /// - public TValue Value; - } -} diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkDictionary.cs.meta b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkDictionary.cs.meta deleted file mode 100644 index 08f14ca258..0000000000 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkDictionary.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 262db7b2db1128748a2e88dd7e20a764 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs index e95839df17..7099b3c53e 100644 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs +++ b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkList.cs @@ -1,6 +1,7 @@ -using System.Collections; +using System; using System.Collections.Generic; using System.IO; +using Unity.Collections; namespace Unity.Netcode { @@ -8,10 +9,10 @@ namespace Unity.Netcode /// Event based NetworkVariable container for syncing Lists /// /// The type for the list - public class NetworkList : NetworkVariableBase, IList where T : unmanaged + public class NetworkList : NetworkVariableBase where T : unmanaged, IEquatable { - private readonly IList m_List = new List(); - private readonly List> m_DirtyEvents = new List>(); + private NativeList m_List = new NativeList(64, Allocator.Persistent); + private NativeList> m_DirtyEvents = new NativeList>(64, Allocator.Persistent); /// /// Delegate type for list changed event @@ -33,25 +34,25 @@ public NetworkList() { } /// Creates a NetworkList with the default value and custom settings /// /// The read permission to use for the NetworkList - public NetworkList(NetworkVariableReadPermission readPerm) : base(readPerm) { } - - /// - /// Creates a NetworkList with a custom value and custom settings - /// - /// The read permission to use for the NetworkList - /// The initial value to use for the NetworkList - public NetworkList(NetworkVariableReadPermission readPerm, IList value) : base(readPerm) + /// The initial value to use for the NetworkList + public NetworkList(NetworkVariableReadPermission readPerm, IEnumerable values) : base(readPerm) { - m_List = value; + foreach (var value in values) + { + m_List.Add(value); + } } /// /// Creates a NetworkList with a custom value and the default settings /// - /// The initial value to use for the NetworkList - public NetworkList(IList value) + /// The initial value to use for the NetworkList + public NetworkList(IEnumerable values) { - m_List = value; + foreach (var value in values) + { + m_List.Add(value); + } } /// @@ -65,15 +66,15 @@ public override void ResetDirty() public override bool IsDirty() { // we call the base class to allow the SetDirty() mechanism to work - return base.IsDirty() || m_DirtyEvents.Count > 0; + return base.IsDirty() || m_DirtyEvents.Length > 0; } /// public override void WriteDelta(Stream stream) { using var writer = PooledNetworkWriter.Get(stream); - writer.WriteUInt16Packed((ushort)m_DirtyEvents.Count); - for (int i = 0; i < m_DirtyEvents.Count; i++) + writer.WriteUInt16Packed((ushort)m_DirtyEvents.Length); + for (int i = 0; i < m_DirtyEvents.Length; i++) { writer.WriteBits((byte)m_DirtyEvents[i].Type, 3); switch (m_DirtyEvents[i].Type) @@ -118,8 +119,8 @@ public override void WriteDelta(Stream stream) public override void WriteField(Stream stream) { using var writer = PooledNetworkWriter.Get(stream); - writer.WriteUInt16Packed((ushort)m_List.Count); - for (int i = 0; i < m_List.Count; i++) + writer.WriteUInt16Packed((ushort)m_List.Length); + for (int i = 0; i < m_List.Length; i++) { writer.WriteObjectPacked(m_List[i]); //BOX } @@ -156,8 +157,8 @@ public override void ReadDelta(Stream stream, bool keepDirtyDelta) OnListChanged(new NetworkListEvent { Type = eventType, - Index = m_List.Count - 1, - Value = m_List[m_List.Count - 1] + Index = m_List.Length - 1, + Value = m_List[m_List.Length - 1] }); } @@ -166,8 +167,8 @@ public override void ReadDelta(Stream stream, bool keepDirtyDelta) m_DirtyEvents.Add(new NetworkListEvent() { Type = eventType, - Index = m_List.Count - 1, - Value = m_List[m_List.Count - 1] + Index = m_List.Length - 1, + Value = m_List[m_List.Length - 1] }); } } @@ -175,7 +176,8 @@ public override void ReadDelta(Stream stream, bool keepDirtyDelta) case NetworkListEvent.EventType.Insert: { int index = reader.ReadInt32Packed(); - m_List.Insert(index, (T)reader.ReadObjectPacked(typeof(T))); //BOX + m_List.InsertRangeWithBeginEnd(index, index + 1); + m_List[index] = (T)reader.ReadObjectPacked(typeof(T)); //BOX if (OnListChanged != null) { @@ -201,7 +203,12 @@ public override void ReadDelta(Stream stream, bool keepDirtyDelta) case NetworkListEvent.EventType.Remove: { var value = (T)reader.ReadObjectPacked(typeof(T)); //BOX - int index = m_List.IndexOf(value); + int index = NativeArrayExtensions.IndexOf(m_List, value); + if (index == -1) + { + break; + } + m_List.RemoveAt(index); if (OnListChanged != null) @@ -256,7 +263,7 @@ public override void ReadDelta(Stream stream, bool keepDirtyDelta) { int index = reader.ReadInt32Packed(); var value = (T)reader.ReadObjectPacked(typeof(T)); //BOX - if (index < m_List.Count) + if (index < m_List.Length) { m_List[index] = value; } @@ -308,18 +315,12 @@ public override void ReadDelta(Stream stream, bool keepDirtyDelta) } } - /// public IEnumerator GetEnumerator() { return m_List.GetEnumerator(); } - IEnumerator IEnumerable.GetEnumerator() - { - return ((IEnumerable)m_List).GetEnumerator(); - } - /// public void Add(T item) { @@ -329,7 +330,7 @@ public void Add(T item) { Type = NetworkListEvent.EventType.Add, Value = item, - Index = m_List.Count - 1 + Index = m_List.Length - 1 }; HandleAddListEvent(listEvent); @@ -351,20 +352,20 @@ public void Clear() /// public bool Contains(T item) { - return m_List.Contains(item); - } - - /// - public void CopyTo(T[] array, int arrayIndex) - { - m_List.CopyTo(array, arrayIndex); + int index = NativeArrayExtensions.IndexOf(m_List, item); + return index == -1; } /// public bool Remove(T item) { - m_List.Remove(item); + int index = NativeArrayExtensions.IndexOf(m_List, item); + if (index == -1) + { + return false; + } + m_List.RemoveAt(index); var listEvent = new NetworkListEvent() { Type = NetworkListEvent.EventType.Remove, @@ -376,10 +377,7 @@ public bool Remove(T item) } /// - public int Count => m_List.Count; - - /// - public bool IsReadOnly => m_List.IsReadOnly; + public int Count => m_List.Length; /// public int IndexOf(T item) @@ -390,7 +388,8 @@ public int IndexOf(T item) /// public void Insert(int index, T item) { - m_List.Insert(index, item); + m_List.InsertRangeWithBeginEnd(index, index + 1); + m_List[index] = item; var listEvent = new NetworkListEvent() { @@ -416,7 +415,6 @@ public void RemoveAt(int index) HandleAddListEvent(listEvent); } - /// public T this[int index] { @@ -450,6 +448,12 @@ public int LastModifiedTick return NetworkTickSystem.NoTick; } } + + public override void Dispose() + { + m_List.Dispose(); + m_DirtyEvents.Dispose(); + } } /// diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkSet.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkSet.cs deleted file mode 100644 index 10645191f5..0000000000 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkSet.cs +++ /dev/null @@ -1,461 +0,0 @@ -#if !NET35 -using System.Collections; -using System.Collections.Generic; -using System.IO; - -namespace Unity.Netcode -{ - /// - /// Event based NetworkVariable container for syncing Sets - /// - /// The type for the set - public class NetworkSet : NetworkVariableBase, ISet where T : unmanaged - { - private readonly ISet m_Set = new HashSet(); - private readonly List> m_DirtyEvents = new List>(); - - /// - /// Delegate type for set changed event - /// - /// Struct containing information about the change event - public delegate void OnSetChangedDelegate(NetworkSetEvent changeEvent); - - /// - /// The callback to be invoked when the set gets changed - /// - public event OnSetChangedDelegate OnSetChanged; - - /// - /// Creates a NetworkSet with the default value and settings - /// - public NetworkSet() { } - - /// - /// Creates a NetworkSet with the default value and custom settings - /// - /// The settings to use for the NetworkList - public NetworkSet(NetworkVariableReadPermission readPerm) : base(readPerm) { } - - /// - /// Creates a NetworkSet with a custom value and custom settings - /// - /// The settings to use for the NetworkSet - /// The initial value to use for the NetworkSet - public NetworkSet(NetworkVariableReadPermission readPerm, ISet value) : base(readPerm) - { - m_Set = value; - } - - /// - /// Creates a NetworkSet with a custom value and the default settings - /// - /// The initial value to use for the NetworkList - public NetworkSet(ISet value) - { - m_Set = value; - } - - /// - public override void ResetDirty() - { - base.ResetDirty(); - m_DirtyEvents.Clear(); - } - - /// - public override bool IsDirty() - { - return base.IsDirty() || m_DirtyEvents.Count > 0; - } - - /// - public override void WriteDelta(Stream stream) - { - using var writer = PooledNetworkWriter.Get(stream); - writer.WriteUInt16Packed((ushort)m_DirtyEvents.Count); - for (int i = 0; i < m_DirtyEvents.Count; i++) - { - writer.WriteBits((byte)m_DirtyEvents[i].Type, 2); - - switch (m_DirtyEvents[i].Type) - { - case NetworkSetEvent.EventType.Add: - { - writer.WriteObjectPacked(m_DirtyEvents[i].Value); //BOX - } - break; - case NetworkSetEvent.EventType.Remove: - { - writer.WriteObjectPacked(m_DirtyEvents[i].Value); //BOX - } - break; - case NetworkSetEvent.EventType.Clear: - { - //Nothing has to be written - } - break; - } - } - } - - /// - public override void WriteField(Stream stream) - { - using var writer = PooledNetworkWriter.Get(stream); - writer.WriteUInt16Packed((ushort)m_Set.Count); - - foreach (T value in m_Set) - { - writer.WriteObjectPacked(value); //BOX - } - } - - /// - public override void ReadField(Stream stream) - { - using var reader = PooledNetworkReader.Get(stream); - m_Set.Clear(); - ushort count = reader.ReadUInt16Packed(); - - for (int i = 0; i < count; i++) - { - m_Set.Add((T)reader.ReadObjectPacked(typeof(T))); //BOX - } - } - - /// - public override void ReadDelta(Stream stream, bool keepDirtyDelta) - { - using var reader = PooledNetworkReader.Get(stream); - ushort deltaCount = reader.ReadUInt16Packed(); - for (int i = 0; i < deltaCount; i++) - { - var eventType = (NetworkSetEvent.EventType)reader.ReadBits(2); - switch (eventType) - { - case NetworkSetEvent.EventType.Add: - { - var value = (T)reader.ReadObjectPacked(typeof(T)); //BOX - m_Set.Add(value); - - if (OnSetChanged != null) - { - OnSetChanged(new NetworkSetEvent - { - Type = eventType, - Value = value - }); - } - - if (keepDirtyDelta) - { - m_DirtyEvents.Add(new NetworkSetEvent() - { - Type = eventType, - Value = value - }); - } - } - break; - case NetworkSetEvent.EventType.Remove: - { - var value = (T)reader.ReadObjectPacked(typeof(T)); //BOX - m_Set.Remove(value); - - if (OnSetChanged != null) - { - OnSetChanged(new NetworkSetEvent - { - Type = eventType, - Value = value - }); - } - - if (keepDirtyDelta) - { - m_DirtyEvents.Add(new NetworkSetEvent() - { - Type = eventType, - Value = value - }); - } - } - break; - case NetworkSetEvent.EventType.Clear: - { - //Read nothing - m_Set.Clear(); - - if (OnSetChanged != null) - { - OnSetChanged(new NetworkSetEvent - { - Type = eventType, - }); - } - - if (keepDirtyDelta) - { - m_DirtyEvents.Add(new NetworkSetEvent() - { - Type = eventType - }); - } - } - break; - } - } - } - - /// - public IEnumerator GetEnumerator() - { - return m_Set.GetEnumerator(); - } - - /// - IEnumerator IEnumerable.GetEnumerator() - { - return m_Set.GetEnumerator(); - } - - /// - public void ExceptWith(IEnumerable other) - { - foreach (T value in other) - { - if (m_Set.Contains(value)) - { - Remove(value); - } - } - } - - /// - public void IntersectWith(IEnumerable other) - { - var otherSet = new HashSet(other); - - foreach (T value in m_Set) - { - if (!otherSet.Contains(value)) - { - Remove(value); - } - } - } - - /// - public bool IsProperSubsetOf(IEnumerable other) - { - return m_Set.IsProperSubsetOf(other); - } - - /// - public bool IsProperSupersetOf(IEnumerable other) - { - return m_Set.IsProperSupersetOf(other); - } - - /// - public bool IsSubsetOf(IEnumerable other) - { - return m_Set.IsSubsetOf(other); - } - - /// - public bool IsSupersetOf(IEnumerable other) - { - return m_Set.IsSupersetOf(other); - } - - /// - public bool Overlaps(IEnumerable other) - { - return m_Set.Overlaps(other); - } - - /// - public bool SetEquals(IEnumerable other) - { - return m_Set.SetEquals(other); - } - - /// - public void SymmetricExceptWith(IEnumerable other) - { - foreach (T value in other) - { - if (m_Set.Contains(value)) - { - Remove(value); - } - else - { - m_Set.Add(value); - - var setEvent = new NetworkSetEvent() - { - Type = NetworkSetEvent.EventType.Add, - Value = value - }; - m_DirtyEvents.Add(setEvent); - - if (OnSetChanged != null) - { - OnSetChanged(setEvent); - } - } - } - } - - /// - public void UnionWith(IEnumerable other) - { - foreach (T value in other) - { - if (!m_Set.Contains(value)) - { - m_Set.Add(value); - - var setEvent = new NetworkSetEvent() - { - Type = NetworkSetEvent.EventType.Add, - Value = value - }; - m_DirtyEvents.Add(setEvent); - - if (OnSetChanged != null) - { - OnSetChanged(setEvent); - } - } - } - } - - public bool Add(T item) - { - m_Set.Add(item); - - var setEvent = new NetworkSetEvent() - { - Type = NetworkSetEvent.EventType.Add, - Value = item - }; - m_DirtyEvents.Add(setEvent); - - if (OnSetChanged != null) - { - OnSetChanged(setEvent); - } - - return true; - } - - /// - void ICollection.Add(T item) => Add(item); - - /// - public void Clear() - { - m_Set.Clear(); - - var setEvent = new NetworkSetEvent() - { - Type = NetworkSetEvent.EventType.Clear - }; - m_DirtyEvents.Add(setEvent); - - if (OnSetChanged != null) - { - OnSetChanged(setEvent); - } - } - - /// - public bool Contains(T item) - { - return m_Set.Contains(item); - } - - /// - public void CopyTo(T[] array, int arrayIndex) - { - m_Set.CopyTo(array, arrayIndex); - } - - /// - public bool Remove(T item) - { - m_Set.Remove(item); - - var setEvent = new NetworkSetEvent() - { - Type = NetworkSetEvent.EventType.Remove, - Value = item - }; - m_DirtyEvents.Add(setEvent); - - if (OnSetChanged != null) - { - OnSetChanged(setEvent); - } - - return true; - } - - /// - public int Count => m_Set.Count; - - /// - public bool IsReadOnly => m_Set.IsReadOnly; - - public int LastModifiedTick - { - get - { - // todo: implement proper network tick for NetworkSet - return NetworkTickSystem.NoTick; - } - } - } - - /// - /// Struct containing event information about changes to a NetworkSet. - /// - /// The type for the set that the event is about - public struct NetworkSetEvent - { - /// - /// Enum representing the different operations available for triggering an event. - /// - public enum EventType - { - /// - /// Add - /// - Add, - - /// - /// Remove - /// - Remove, - - /// - /// Clear - /// - Clear - } - - /// - /// Enum representing the operation made to the set. - /// - public EventType Type; - - /// - /// The value changed, added or removed if available. - /// - public T Value; - } -} -#endif diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkSet.cs.meta b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkSet.cs.meta deleted file mode 100644 index 60c0a546d2..0000000000 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/Collections/NetworkSet.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 245b489a4e4f2174fbdeb27b9cdee07b -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableBase.cs b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableBase.cs index bc782a7476..84bc6b5bef 100644 --- a/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableBase.cs +++ b/com.unity.netcode.gameobjects/Runtime/NetworkVariable/NetworkVariableBase.cs @@ -1,3 +1,4 @@ +using System; using System.IO; namespace Unity.Netcode @@ -5,7 +6,7 @@ namespace Unity.Netcode /// /// Interface for network value containers /// - public abstract class NetworkVariableBase + public abstract class NetworkVariableBase : IDisposable { /// /// The delivery type (QoS) to send data with @@ -84,16 +85,6 @@ public bool CanClientRead(ulong clientId) return true; } - /// - /// Gets Whether or not a specific client can read to the varaible - /// - /// The clientId of the remote client - /// Whether or not the client can read to the variable - public virtual bool CanClientWrite(ulong clientId) - { - return false; - } - /// /// Writes the dirty changes, that is, the changes since the variable was last dirty, to the writer /// @@ -118,5 +109,9 @@ public virtual bool CanClientWrite(ulong clientId) /// The stream to read the delta from /// Whether or not the delta should be kept as dirty or consumed public abstract void ReadDelta(Stream stream, bool keepDirtyDelta); + + public virtual void Dispose() + { + } } } diff --git a/com.unity.netcode.gameobjects/Runtime/com.unity.netcode.runtime.asmdef b/com.unity.netcode.gameobjects/Runtime/com.unity.netcode.runtime.asmdef index 3798f71cb3..0efd561be1 100644 --- a/com.unity.netcode.gameobjects/Runtime/com.unity.netcode.runtime.asmdef +++ b/com.unity.netcode.gameobjects/Runtime/com.unity.netcode.runtime.asmdef @@ -5,7 +5,8 @@ "Unity.Multiplayer.MetricTypes", "Unity.Multiplayer.NetStats", "Unity.Multiplayer.NetStatsReporting", - "Unity.Multiplayer.NetworkSolutionInterface" + "Unity.Multiplayer.NetworkSolutionInterface", + "Unity.Collections" ], "includePlatforms": [], "excludePlatforms": [], diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/HiddenVariableTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/HiddenVariableTests.cs index 8716d963b5..61db5862fe 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/HiddenVariableTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/HiddenVariableTests.cs @@ -6,7 +6,6 @@ namespace Unity.Netcode.RuntimeTests { public class HiddenVariableTest : NetworkBehaviour { - } public class HiddenVariableObject : NetworkBehaviour diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs index e43c8ac2ee..09f15e7d41 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/NetworkVariableTests.cs @@ -47,8 +47,6 @@ public class NetworkVariableTest : NetworkBehaviour { public readonly NetworkVariable TheScalar = new NetworkVariable(); public readonly NetworkList TheList = new NetworkList(); - public readonly NetworkSet TheSet = new NetworkSet(); - public readonly NetworkDictionary TheDictionary = new NetworkDictionary(); public readonly NetworkVariable FixedStringStruct = new NetworkVariable(); @@ -57,28 +55,14 @@ private void ListChanged(NetworkListEvent e) ListDelegateTriggered = true; } - private void SetChanged(NetworkSetEvent e) - { - SetDelegateTriggered = true; - } - - private void DictionaryChanged(NetworkDictionaryEvent e) - { - DictionaryDelegateTriggered = true; - } - public void Awake() { TheList.OnListChanged += ListChanged; - TheSet.OnSetChanged += SetChanged; - TheDictionary.OnDictionaryChanged += DictionaryChanged; } public readonly NetworkVariable TheStruct = new NetworkVariable(); public bool ListDelegateTriggered; - public bool SetDelegateTriggered; - public bool DictionaryDelegateTriggered; } public class NetworkVariableTests : BaseMultiInstanceTest @@ -93,23 +77,13 @@ public class NetworkVariableTests : BaseMultiInstanceTest private const int k_TestVal3 = 333; private const int k_TestKey1 = 0x0f0f; - private const int k_TestKey2 = 0xf0f0; // Player1 component on the server private NetworkVariableTest m_Player1OnServer; - // Player2 component on the server - private NetworkVariableTest m_Player2OnServer; - // Player1 component on client1 private NetworkVariableTest m_Player1OnClient1; - // Player2 component on client1 - private NetworkVariableTest m_Player2OnClient2; - - // client2's version of client1's player object - private NetworkVariableTest m_Player1OnClient2; - private bool m_TestWithHost; [UnitySetUp] @@ -129,11 +103,6 @@ public override IEnumerator Setup() m_ServerNetworkManager, result)); m_Player1OnServer = result.Result.GetComponent(); - yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation( - x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[1].LocalClientId, - m_ServerNetworkManager, result)); - m_Player2OnServer = result.Result.GetComponent(); - // This is client1's view of itself yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation( x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId, @@ -141,30 +110,13 @@ public override IEnumerator Setup() m_Player1OnClient1 = result.Result.GetComponent(); - // This is client2's view of itself - result = new MultiInstanceHelpers.CoroutineResultWrapper(); - yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation( - x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[1].LocalClientId, - m_ClientNetworkManagers[1], result)); - - m_Player2OnClient2 = result.Result.GetComponent(); - - // This is client2's view of client 1's object - yield return MultiInstanceHelpers.Run(MultiInstanceHelpers.GetNetworkObjectByRepresentation( - x => x.IsPlayerObject && x.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId, - m_ClientNetworkManagers[1], result)); - - m_Player1OnClient2 = result.Result.GetComponent(); - m_Player1OnServer.TheList.Clear(); - m_Player1OnServer.TheSet.Clear(); - m_Player1OnServer.TheDictionary.Clear(); - if (m_Player1OnServer.TheList.Count > 0 || m_Player1OnServer.TheSet.Count > 0 || m_Player1OnServer.TheDictionary.Count > 0) + if (m_Player1OnServer.TheList.Count > 0) { throw new Exception("at least one server network container not empty at start"); } - if (m_Player1OnClient1.TheList.Count > 0 || m_Player1OnClient1.TheSet.Count > 0 || m_Player1OnClient1.TheDictionary.Count > 0) + if (m_Player1OnClient1.TheList.Count > 0) { throw new Exception("at least one client network container not empty at start"); } @@ -277,215 +229,189 @@ public IEnumerator NetworkListAdd([Values(true, false)] bool useHost) } [UnityTest] - public IEnumerator NetworkListRemove([Values(true, false)] bool useHost) + public IEnumerator NetworkListContains([Values(true, false)] bool useHost) { m_TestWithHost = useHost; - // first put some stuff in; re-use the add test - yield return NetworkListAdd(useHost); - yield return MultiInstanceHelpers.RunAndWaitForCondition( - () => m_Player1OnServer.TheList.RemoveAt(0), () => { - return m_Player1OnServer.TheList.Count == 1 && - m_Player1OnClient1.TheList.Count == 1 && - m_Player1OnServer.ListDelegateTriggered && - m_Player1OnClient1.ListDelegateTriggered && - m_Player1OnServer.TheList[0] == k_TestVal2 && - m_Player1OnClient1.TheList[0] == k_TestVal2; - } - ); - } - - [UnityTest] - public IEnumerator NetworkListClear([Values(true, false)] bool useHost) - { - m_TestWithHost = useHost; - - // first put some stuff in; re-use the add test - yield return NetworkListAdd(useHost); - - yield return MultiInstanceHelpers.RunAndWaitForCondition( - () => m_Player1OnServer.TheList.Clear(), + m_Player1OnServer.TheList.Add(k_TestVal1); + }, () => { - return - m_Player1OnServer.ListDelegateTriggered && - m_Player1OnClient1.ListDelegateTriggered && - m_Player1OnServer.TheList.Count == 0 && - m_Player1OnClient1.TheList.Count == 0; + return m_Player1OnServer.TheList.Count == 1 && + m_Player1OnClient1.TheList.Count == 1 && + m_Player1OnServer.TheList.Contains(k_TestKey1) && + m_Player1OnClient1.TheList.Contains(k_TestKey1); } ); } [UnityTest] - public IEnumerator NetworkSetAdd([Values(true, false)] bool useHost) + public IEnumerator NetworkListRemoveValue([Values(true, false)] bool useHost) { m_TestWithHost = useHost; - yield return MultiInstanceHelpers.RunAndWaitForCondition( () => { - m_Player1OnServer.TheSet.Add(k_TestVal1); - m_Player1OnServer.TheSet.Add(k_TestVal2); + m_Player1OnServer.TheList.Add(k_TestVal1); + m_Player1OnServer.TheList.Add(k_TestVal2); + m_Player1OnServer.TheList.Add(k_TestVal3); + m_Player1OnServer.TheList.Remove(k_TestVal2); }, () => { - return m_Player1OnServer.TheSet.Count == 2 && - m_Player1OnClient1.TheSet.Count == 2 && - m_Player1OnServer.SetDelegateTriggered && - m_Player1OnClient1.SetDelegateTriggered && - m_Player1OnServer.TheSet.Contains(k_TestVal1) && - m_Player1OnClient1.TheSet.Contains(k_TestVal1) && - m_Player1OnServer.TheSet.Contains(k_TestVal2) && - m_Player1OnClient1.TheSet.Contains(k_TestVal2); + return m_Player1OnServer.TheList.Count == 2 && + m_Player1OnClient1.TheList.Count == 2 && + m_Player1OnServer.TheList[0] == k_TestVal1 && + m_Player1OnClient1.TheList[0] == k_TestVal1 && + m_Player1OnServer.TheList[1] == k_TestVal3 && + m_Player1OnClient1.TheList[1] == k_TestVal3; } ); } [UnityTest] - public IEnumerator NetworkSetRemove([Values(true, false)] bool useHost) + public IEnumerator NetworkListInsert([Values(true, false)] bool useHost) { m_TestWithHost = useHost; - - // first put some stuff in; re-use the add test - yield return NetworkSetAdd(useHost); - yield return MultiInstanceHelpers.RunAndWaitForCondition( () => { - m_Player1OnServer.TheSet.Remove(k_TestVal1); + m_Player1OnServer.TheList.Add(k_TestVal1); + m_Player1OnServer.TheList.Add(k_TestVal2); + m_Player1OnServer.TheList.Insert(1, k_TestVal3); }, () => { - return m_Player1OnServer.TheSet.Count == 1 && - m_Player1OnClient1.TheSet.Count == 1 && - m_Player1OnServer.SetDelegateTriggered && - m_Player1OnClient1.SetDelegateTriggered && - m_Player1OnServer.TheSet.Contains(k_TestVal2) && - m_Player1OnClient1.TheSet.Contains(k_TestVal2); + return m_Player1OnServer.TheList.Count == 3 && + m_Player1OnClient1.TheList.Count == 3 && + m_Player1OnServer.ListDelegateTriggered && + m_Player1OnClient1.ListDelegateTriggered && + m_Player1OnServer.TheList[0] == k_TestVal1 && + m_Player1OnClient1.TheList[0] == k_TestVal1 && + m_Player1OnServer.TheList[1] == k_TestVal3 && + m_Player1OnClient1.TheList[1] == k_TestVal3 && + m_Player1OnServer.TheList[2] == k_TestVal2 && + m_Player1OnClient1.TheList[2] == k_TestVal2; } ); } [UnityTest] - public IEnumerator NetworkSetClear([Values(true, false)] bool useHost) + public IEnumerator NetworkListIndexOf([Values(true, false)] bool useHost) { - // first put some stuff in; re-use the add test - yield return NetworkSetAdd(useHost); - + m_TestWithHost = useHost; yield return MultiInstanceHelpers.RunAndWaitForCondition( () => { - m_Player1OnServer.TheSet.Clear(); + m_Player1OnServer.TheList.Add(k_TestVal1); + m_Player1OnServer.TheList.Add(k_TestVal2); + m_Player1OnServer.TheList.Add(k_TestVal3); }, () => { - return m_Player1OnServer.TheSet.Count == 0 && - m_Player1OnClient1.TheSet.Count == 0 && - m_Player1OnServer.SetDelegateTriggered && - m_Player1OnClient1.SetDelegateTriggered; + return m_Player1OnServer.TheList.IndexOf(k_TestVal1) == 0 && + m_Player1OnClient1.TheList.IndexOf(k_TestVal1) == 0 && + m_Player1OnServer.TheList.IndexOf(k_TestVal2) == 1 && + m_Player1OnClient1.TheList.IndexOf(k_TestVal2) == 1 && + m_Player1OnServer.TheList.IndexOf(k_TestVal3) == 2 && + m_Player1OnClient1.TheList.IndexOf(k_TestVal3) == 2; } ); } [UnityTest] - public IEnumerator NetworkDictionaryAdd([Values(true, false)] bool useHost) + public IEnumerator NetworkListArrayOperator([Values(true, false)] bool useHost) { m_TestWithHost = useHost; - yield return MultiInstanceHelpers.RunAndWaitForCondition( () => { - m_Player1OnServer.TheDictionary.Add(k_TestKey1, k_TestVal1); - m_Player1OnServer.TheDictionary.Add(k_TestKey2, k_TestVal2); + m_Player1OnServer.TheList.Add(k_TestVal3); + m_Player1OnServer.TheList.Add(k_TestVal3); + m_Player1OnServer.TheList[0] = k_TestVal1; + m_Player1OnServer.TheList[1] = k_TestVal2; }, () => { - return m_Player1OnServer.TheDictionary.Count == 2 && - m_Player1OnClient1.TheDictionary.Count == 2 && - m_Player1OnServer.DictionaryDelegateTriggered && - m_Player1OnClient1.DictionaryDelegateTriggered && - m_Player1OnServer.TheDictionary[k_TestKey1] == k_TestVal1 && - m_Player1OnClient1.TheDictionary[k_TestKey1] == k_TestVal1 && - m_Player1OnServer.TheDictionary[k_TestKey2] == k_TestVal2 && - m_Player1OnClient1.TheDictionary[k_TestKey2] == k_TestVal2; + return m_Player1OnServer.TheList.Count == 2 && + m_Player1OnClient1.TheList.Count == 2 && + m_Player1OnServer.TheList[0] == k_TestVal1 && + m_Player1OnClient1.TheList[0] == k_TestVal1 && + m_Player1OnServer.TheList[1] == k_TestVal2 && + m_Player1OnClient1.TheList[1] == k_TestVal2; } ); } - /* Note, not adding coverage for RemovePair, because we plan to remove - * this in the next PR - */ - [UnityTest] - public IEnumerator NetworkDictionaryRemoveByKey([Values(true, false)] bool useHost) + [Test] + public void NetworkListIEnumerator([Values(true, false)] bool useHost) { m_TestWithHost = useHost; + var correctVals = new int[3]; + correctVals[0] = k_TestVal1; + correctVals[1] = k_TestVal2; + correctVals[2] = k_TestVal3; - // first put some stuff in; re-use the add test - yield return NetworkDictionaryAdd(useHost); + m_Player1OnServer.TheList.Add(correctVals[0]); + m_Player1OnServer.TheList.Add(correctVals[1]); + m_Player1OnServer.TheList.Add(correctVals[2]); - yield return MultiInstanceHelpers.RunAndWaitForCondition( - () => - { - m_Player1OnServer.TheDictionary.Remove(k_TestKey2); - }, - () => + Assert.IsTrue(m_Player1OnServer.TheList.Count == 3); + + int index = 0; + foreach (var val in m_Player1OnServer.TheList) + { + if (val != correctVals[index++]) { - return m_Player1OnServer.TheDictionary.Count == 1 && - m_Player1OnClient1.TheDictionary.Count == 1 && - m_Player1OnServer.DictionaryDelegateTriggered && - m_Player1OnClient1.DictionaryDelegateTriggered && - m_Player1OnServer.TheDictionary[k_TestKey1] == k_TestVal1 && - m_Player1OnClient1.TheDictionary[k_TestKey1] == k_TestVal1; + Assert.Fail(); } - ); + } } [UnityTest] - public IEnumerator NetworkDictionaryChangeValue([Values(true, false)] bool useHost) + public IEnumerator NetworkListRemoveAt([Values(true, false)] bool useHost) { m_TestWithHost = useHost; - // first put some stuff in; re-use the add test - yield return NetworkDictionaryAdd(useHost); - yield return MultiInstanceHelpers.RunAndWaitForCondition( () => { - m_Player1OnServer.TheDictionary[k_TestKey1] = k_TestVal3; + m_Player1OnServer.TheList.Add(k_TestVal1); + m_Player1OnServer.TheList.Add(k_TestVal2); + m_Player1OnServer.TheList.Add(k_TestVal3); + m_Player1OnServer.TheList.RemoveAt(1); }, () => { - return m_Player1OnServer.TheDictionary.Count == 2 && - m_Player1OnClient1.TheDictionary.Count == 2 && - m_Player1OnServer.DictionaryDelegateTriggered && - m_Player1OnClient1.DictionaryDelegateTriggered && - m_Player1OnServer.TheDictionary[k_TestKey1] == k_TestVal3 && - m_Player1OnClient1.TheDictionary[k_TestKey1] == k_TestVal3; + return m_Player1OnServer.TheList.Count == 2 && + m_Player1OnClient1.TheList.Count == 2 && + m_Player1OnServer.TheList[0] == k_TestVal1 && + m_Player1OnClient1.TheList[0] == k_TestVal1 && + m_Player1OnServer.TheList[1] == k_TestVal3 && + m_Player1OnClient1.TheList[1] == k_TestVal3; } ); } [UnityTest] - public IEnumerator NetworkDictionaryClear([Values(true, false)] bool useHost) + public IEnumerator NetworkListClear([Values(true, false)] bool useHost) { m_TestWithHost = useHost; // first put some stuff in; re-use the add test - yield return NetworkDictionaryAdd(useHost); + yield return NetworkListAdd(useHost); yield return MultiInstanceHelpers.RunAndWaitForCondition( + () => m_Player1OnServer.TheList.Clear(), () => { - m_Player1OnServer.TheDictionary.Clear(); - }, - () => - { - return m_Player1OnServer.TheDictionary.Count == 0 && - m_Player1OnClient1.TheDictionary.Count == 0 && - m_Player1OnServer.DictionaryDelegateTriggered && - m_Player1OnClient1.DictionaryDelegateTriggered; + return + m_Player1OnServer.ListDelegateTriggered && + m_Player1OnClient1.ListDelegateTriggered && + m_Player1OnServer.TheList.Count == 0 && + m_Player1OnClient1.TheList.Count == 0; } ); } diff --git a/com.unity.netcode.gameobjects/Tests/Runtime/Profiling/NetworkVariableNameTests.cs b/com.unity.netcode.gameobjects/Tests/Runtime/Profiling/NetworkVariableNameTests.cs index 4161372187..e9d005a3b7 100644 --- a/com.unity.netcode.gameobjects/Tests/Runtime/Profiling/NetworkVariableNameTests.cs +++ b/com.unity.netcode.gameobjects/Tests/Runtime/Profiling/NetworkVariableNameTests.cs @@ -26,21 +26,13 @@ public void TearDown() [Test] public void VerifyNetworkVariableNameInitialization() { - // Properties have the following name format: "k__BackingField" - StringAssert.Contains(nameof(NetworkVariableNameComponent.NetworkVarSet), m_NetworkVariableNameComponent.NetworkVarSet.Name); - // Fields have regular naming Assert.AreEqual(nameof(NetworkVariableNameComponent.NetworkVarList), m_NetworkVariableNameComponent.NetworkVarList.Name); - Assert.AreEqual(nameof(NetworkVariableNameComponent.NetworkVarDictionary), m_NetworkVariableNameComponent.NetworkVarDictionary.Name); } private class NetworkVariableNameComponent : NetworkBehaviour { - public NetworkSet NetworkVarSet { get; } = new NetworkSet(); - public NetworkList NetworkVarList = new NetworkList(); - - public NetworkDictionary NetworkVarDictionary = new NetworkDictionary(); } } }