Skip to content

Commit

Permalink
refactor: move collections to Mirage.Collections (MirageNet#698)
Browse files Browse the repository at this point in the history
Following .net example, the collections have been moved to a folder
and a namespace.

BREAKING CHANGE: collections moved to Mirage.Collections
  • Loading branch information
paulpach committed Mar 14, 2021
1 parent df35cae commit e22f765
Show file tree
Hide file tree
Showing 62 changed files with 77 additions and 14 deletions.
1 change: 1 addition & 0 deletions Assets/Mirage/Editor/InspectorHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using Mirage.Collections;
using UnityEngine;

namespace Mirage
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Editor/NetworkBehaviourInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Mirage.Collections;
using UnityEditor;
using UnityEngine;
using Mirage.Logging;
Expand Down
8 changes: 8 additions & 0 deletions Assets/Mirage/Runtime/Collections.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace Mirage
namespace Mirage.Collections
{
/// <summary>
/// A sync object is an object that can synchronize it's state
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Collections.Generic;
using JetBrains.Annotations;

namespace Mirage
namespace Mirage.Collections
{
public class SyncIDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ISyncObject, IReadOnlyDictionary<TKey, TValue>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections;
using System.Collections.Generic;

namespace Mirage
namespace Mirage.Collections
{
public class SyncList<T> : IList<T>, IReadOnlyList<T>, ISyncObject
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections;
using System.Collections.Generic;

namespace Mirage
namespace Mirage.Collections
{
public class SyncSet<T> : ISet<T>, ISyncObject
{
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions Assets/Mirage/Runtime/NetworkBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Mirage.Collections;
using Mirage.RemoteCalls;
using UnityEngine;
using Mirage.Logging;
Expand Down
1 change: 1 addition & 0 deletions Assets/Mirage/Weaver/Processors/SyncObjectProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using System.Linq;
using Mirage.Collections;
using Mono.Cecil;
using Mono.Cecil.Cil;

Expand Down
8 changes: 8 additions & 0 deletions Assets/Tests/Runtime/Collections.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Mirage.Collections;
using NSubstitute;
using NUnit.Framework;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using Mirage.Collections;
using NUnit.Framework;

namespace Mirage.Tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Mirage.Collections;
using NUnit.Framework;

namespace Mirage.Tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Mirage.Collections;
using NSubstitute;
using NUnit.Framework;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Mirage.Collections;
using NSubstitute;
using NUnit.Framework;

Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Runtime/Host/NetworkBehaviourTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Mirage.Collections;
using NUnit.Framework;
using UnityEngine;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Mirage.Collections;
using NUnit.Framework;
using UnityEngine;

Expand Down
2 changes: 1 addition & 1 deletion Assets/Tests/Weaver/MonoBehaviourTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void MonoBehaviourSyncVar()
public void MonoBehaviourSyncList()
{
HasError("potato is a SyncObject and must be inside a NetworkBehaviour. MonoBehaviourSyncList is not a NetworkBehaviour",
"Mirage.SyncList`1<System.Int32> MonoBehaviourTests.MonoBehaviourSyncList.MonoBehaviourSyncList::potato");
"Mirage.Collections.SyncList`1<System.Int32> MonoBehaviourTests.MonoBehaviourSyncList.MonoBehaviourSyncList::potato");
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;
using UnityEngine;

namespace MonoBehaviourTests.MonoBehaviourSyncList
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Weaver/SyncDictionaryTests~/SyncDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncDictionaryTests.SyncDictionary
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncDictionaryTests.SyncDictionaryErrorForGenericStructItem
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncDictionaryTests.SyncDictionaryErrorForGenericStructKey
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncDictionaryTests.SyncDictionaryErrorWhenUsingGenericInNetworkBehaviour
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncDictionaryTests.SyncDictionaryGenericAbstractInheritance
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncDictionaryTests.SyncDictionaryGenericInheritance
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncDictionaryTests.SyncDictionaryInheritance
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncDictionaryTests.SyncDictionaryStructItem
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncDictionaryTests.SyncDictionaryStructKey
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Weaver/SyncListTests~/SyncList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncList
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Weaver/SyncListTests~/SyncListByteValid.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListByteValid
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListErrorForGenericStruct
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListErrorForInterface
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListErrorWhenUsingGenericListInNetworkBehaviour
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListGenericAbstractInheritance
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListGenericInheritance
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListGenericInheritanceWithMultipleGeneric
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListGenericStructWithCustomMethods
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Weaver/SyncListTests~/SyncListInheritance.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListInheritance
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListNestedInAbstractClass
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Mirage;
using UnityEngine;
using Mirage.Collections;

namespace SyncListTests.SyncListNestedInAbstractClassWithInvalid
{
Expand Down
3 changes: 2 additions & 1 deletion Assets/Tests/Weaver/SyncListTests~/SyncListNestedInStruct.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListNestedInStruct
{
Expand All @@ -11,7 +12,7 @@ public struct SomeData
{
public int usefulNumber;

public class SyncList : Mirage.SyncList<SomeData> { }
public class SyncList : Mirage.Collections.SyncList<SomeData> { }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Mirage;
using UnityEngine;
using Mirage.Collections;

namespace SyncListTests.SyncListNestedInStructWithInvalid
{
Expand All @@ -13,7 +14,7 @@ public struct SomeData
public int usefulNumber;
public Object target;

public class SyncList : Mirage.SyncList<SomeData> { }
public class SyncList : Mirage.Collections.SyncList<SomeData> { }
}
}
}
1 change: 1 addition & 0 deletions Assets/Tests/Weaver/SyncListTests~/SyncListNestedStruct.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListNestedStruct
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Weaver/SyncListTests~/SyncListStruct.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncListTests.SyncListStruct
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Weaver/SyncSetTests~/SyncSetByteValid.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncSetTests.SyncSetByteValid
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncSetTests.SyncSetGenericAbstractInheritance
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncSetTests.SyncSetGenericInheritance
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Weaver/SyncSetTests~/SyncSetInheritance.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncSetTests.SyncSetInheritance
{
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Weaver/SyncSetTests~/SyncSetStruct.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncSetTests.SyncSetStruct
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/Tests/Weaver/SyncVarTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void SyncVarsCantBeArray()
[Test]
public void SyncVarsSyncList()
{
HasWarning("syncints has [SyncVar] attribute. SyncLists should not be marked with SyncVar", "Mirage.SyncList`1<System.Int32> SyncVarTests.SyncVarsSyncList.SyncVarsSyncList::syncints");
HasWarning("syncints has [SyncVar] attribute. SyncLists should not be marked with SyncVar", "Mirage.Collections.SyncList`1<System.Int32> SyncVarTests.SyncVarsSyncList.SyncVarsSyncList::syncints");
}

[Test]
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/Weaver/SyncVarTests~/SyncVarsSyncList.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Mirage;
using Mirage.Collections;

namespace SyncVarTests.SyncVarsSyncList
{
Expand Down
8 changes: 5 additions & 3 deletions doc/Articles/Guides/Sync/SyncDictionary.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# SyncDictionary
A <xref:Mirage.SyncDictionary`2> is an associative array containing an unordered list of key, value pairs. Keys and values can be any of [Mirage supported types](../DataTypes.md).
A <xref:Mirage.Collections.SyncDictionary`2> is an associative array containing an unordered list of key, value pairs. Keys and values can be any of [Mirage supported types](../DataTypes.md).

SyncDictionary works much like [SyncLists](SyncLists.md): when you make a change on the server, the change is propagated to all clients and the appropriate callback is called.

## Usage
Add a field of type `SyncDictionary<TKey, TValue>` on any <xref:Mirage.NetworkBehaviour> where `TKey` and `TValue` can be any supported Mirage type and initialize it.
Add a field of type <xref:Mirage.Collections.SyncDictionary`2> on any <xref:Mirage.NetworkBehaviour> where `TKey` and `TValue` can be any supported Mirage type and initialize it.

> [!IMPORTANT]
> You need to initialize the SyncDictionary immediately after definition in order for them to work. You can mark them as `readonly` to enforce proper usage.
Expand All @@ -13,6 +13,7 @@ Add a field of type `SyncDictionary<TKey, TValue>` on any <xref:Mirage.NetworkBe
```cs
using UnityEngine;
using Mirage;
using Mirage.Collections;

[System.Serializable]
public struct Item
Expand Down Expand Up @@ -43,7 +44,7 @@ public class Player : NetworkBehaviour
## Callbacks
You can detect when a SyncDictionary changes on the client and/or server. This is especially useful for refreshing your UI, character appearance etc.

There are different callbacks for different operations, such as `OnChange` (any change to the dictionary), `OnInsert` (adding new element) etc. Please check the [SyncDictionary API reference](xref:Mirage.SyncIDictionary`2) for the complete list of callbacks.
There are different callbacks for different operations, such as `OnChange` (any change to the dictionary), `OnInsert` (adding new element) etc. Please check the [SyncDictionary API reference](xref:Mirage.Collections.SyncIDictionary`2) for the complete list of callbacks.

Depending on where you want to invoke the callbacks, you can use these methods to register them:
- `Awake` for both client and server
Expand All @@ -56,6 +57,7 @@ Depending on where you want to invoke the callbacks, you can use these methods t
### Example
```cs
using Mirage;
using Mirage.Collections;

public class Player : NetworkBehaviour {
public readonly SyncDictionary<stirng, Item> Equipment = new SyncDictionary<string, Item>();
Expand Down
Loading

0 comments on commit e22f765

Please sign in to comment.