diff --git a/CHANGELOG.md b/CHANGELOG.md index d21b4a6552..2eade88d08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,13 @@ - Generated Worker ID's for local development are now smaller and easier to read for debugging. [#1197](https://github.com/spatialos/gdk-for-unity/pull/1197) +### Internal + +- Cleaned up Subscriptions and Callbacks. [#1200](https://github.com/spatialos/gdk-for-unity/pull/1200) + - Replaced usage of `GuardedAuthorityCallbackManagerSet` with more generic `GuardedCallbackManagerSet`. + - Removed unused `EntitySubscriptions` class. + - Formatting pass on all Subscriptions and Callbacks files. + ## `0.2.10` - 2019-10-14 ### Breaking Changes diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers.meta similarity index 77% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers.meta index 366ff85bf7..b8b67c0f29 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers.meta +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 3306185743ec0c440a2cec5bfc1e21bf +guid: fe5cb7ba32df7b44e8bf59a91cd3edf8 folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/AuthorityConstraintCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/AuthorityConstraintCallbackManager.cs similarity index 94% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/AuthorityConstraintCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/AuthorityConstraintCallbackManager.cs index c04191c1cf..81bd83a8c8 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/AuthorityConstraintCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/AuthorityConstraintCallbackManager.cs @@ -22,7 +22,7 @@ public AuthorityConstraintCallbackManager(uint componentId, World world) public void InvokeCallbacks() { var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId); - for (int i = 0; i < changes.Count; ++i) + for (var i = 0; i < changes.Count; ++i) { if (changes[i].Authority == Authority.Authoritative) { @@ -38,7 +38,7 @@ public void InvokeCallbacks() public void InvokeLossImminentCallbacks() { var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId); - for (int i = 0; i < changes.Count; ++i) + for (var i = 0; i < changes.Count; ++i) { if (changes[i].Authority == Authority.AuthorityLossImminent) { diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/AuthorityConstraintCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/AuthorityConstraintCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/AuthorityConstraintCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/AuthorityConstraintCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/CommandRequestCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandRequestCallbackManager.cs similarity index 95% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/CommandRequestCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandRequestCallbackManager.cs index 6afb56d9d0..e350d82515 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/CommandRequestCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandRequestCallbackManager.cs @@ -20,7 +20,7 @@ public CommandRequestCallbackManager(World world) public void InvokeCallbacks() { var requests = commandSystem.GetRequests(); - for (int i = 0; i < requests.Count; ++i) + for (var i = 0; i < requests.Count; ++i) { ref readonly var request = ref requests[i]; callbacks.InvokeAll(request.GetEntityId().Id, request); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/CommandRequestCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandRequestCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/CommandRequestCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandRequestCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/CommandResponseCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandResponseCallbackManager.cs similarity index 96% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/CommandResponseCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandResponseCallbackManager.cs index c026328dd2..688a2b4724 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/CommandResponseCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandResponseCallbackManager.cs @@ -20,7 +20,7 @@ public CommandResponseCallbackManager(World world) public void InvokeCallbacks() { var responses = commandSystem.GetResponses(); - for (int i = 0; i < responses.Count; ++i) + for (var i = 0; i < responses.Count; ++i) { ref readonly var response = ref responses[i]; callbacks.InvokeAll(response.GetRequestId(), response); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/CommandResponseCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandResponseCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/CommandResponseCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/CommandResponseCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentAddedCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAddedCallbackManager.cs similarity index 89% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentAddedCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAddedCallbackManager.cs index 1be85006e1..bd70b220d6 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentAddedCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAddedCallbackManager.cs @@ -1,6 +1,5 @@ using System; using Improbable.Gdk.Core; -using Improbable.Worker.CInterop; using Unity.Entities; namespace Improbable.Gdk.Subscriptions @@ -23,9 +22,9 @@ public ComponentAddedCallbackManager(uint componentId, World world) public void InvokeCallbacks() { var entities = componentUpdateSystem.GetComponentsAdded(componentId); - for (int i = 0; i < entities.Count; ++i) + foreach (var entityId in entities) { - callbacks.InvokeAll(entities[i]); + callbacks.InvokeAll(entityId); } } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentAddedCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAddedCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentAddedCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAddedCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentAuthorityCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAuthorityCallbackManager.cs similarity index 76% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentAuthorityCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAuthorityCallbackManager.cs index 92ed7be0f8..fbc6e20049 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentAuthorityCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAuthorityCallbackManager.cs @@ -22,15 +22,16 @@ public ComponentAuthorityCallbackManager(uint componentId, World world) public void InvokeCallbacks() { var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId); - for (int i = 0; i < changes.Count; ++i) + for (var i = 0; i < changes.Count; ++i) { - if (changes[i].Authority == Authority.Authoritative) + switch (changes[i].Authority) { - callbacks.InvokeAll(changes[i].EntityId.Id, changes[i].Authority); - } - else if (changes[i].Authority == Authority.NotAuthoritative) - { - callbacks.InvokeAllReverse(changes[i].EntityId.Id, changes[i].Authority); + case Authority.Authoritative: + callbacks.InvokeAll(changes[i].EntityId.Id, changes[i].Authority); + break; + case Authority.NotAuthoritative: + callbacks.InvokeAllReverse(changes[i].EntityId.Id, changes[i].Authority); + break; } } } @@ -38,7 +39,7 @@ public void InvokeCallbacks() public void InvokeLossImminentCallbacks() { var changes = componentUpdateSystem.GetAuthorityChangesReceived(componentId); - for (int i = 0; i < changes.Count; ++i) + for (var i = 0; i < changes.Count; ++i) { if (changes[i].Authority == Authority.AuthorityLossImminent) { diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentAuthorityCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAuthorityCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentAuthorityCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentAuthorityCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentEventCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentEventCallbackManager.cs similarity index 87% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentEventCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentEventCallbackManager.cs index aa221451d5..4b85268f4d 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentEventCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentEventCallbackManager.cs @@ -4,7 +4,7 @@ namespace Improbable.Gdk.Subscriptions { - internal class ComponentEventCallbackManager : ICallbackManager where T : IEvent + internal class ComponentEventCallbackManager : ICallbackManager where T : struct, IEvent { private readonly IndexedCallbacks callbacks = new IndexedCallbacks(); private readonly ComponentUpdateSystem componentUpdateSystem; @@ -19,10 +19,10 @@ public ComponentEventCallbackManager(World world) public void InvokeCallbacks() { var updates = componentUpdateSystem.GetEventsReceived(); - for (int i = 0; i < updates.Count; ++i) + for (var i = 0; i < updates.Count; ++i) { ref readonly var update = ref updates[i]; - callbacks.InvokeAll(update.EntityId.Id, update.Event); + callbacks.InvokeAll(update.EntityId.Id, in update.Event); } } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentEventCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentEventCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentEventCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentEventCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentRemovedCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentRemovedCallbackManager.cs similarity index 92% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentRemovedCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentRemovedCallbackManager.cs index 4ec88a0443..9807e8add5 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentRemovedCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentRemovedCallbackManager.cs @@ -23,9 +23,9 @@ public void InvokeCallbacks() { // todo like entity stuff this should also be temporarily removed components var entities = componentUpdateSystem.GetComponentsRemoved(componentId); - for (int i = 0; i < entities.Count; ++i) + foreach (var entityId in entities) { - callbacks.InvokeAllReverse(entities[i]); + callbacks.InvokeAllReverse(entityId); } } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentRemovedCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentRemovedCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentRemovedCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentRemovedCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentUdpateCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentUpdateCallbackManager.cs similarity index 86% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentUdpateCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentUpdateCallbackManager.cs index 70d1509469..8a0dd029ae 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentUdpateCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentUpdateCallbackManager.cs @@ -4,7 +4,7 @@ namespace Improbable.Gdk.Subscriptions { - internal class ComponentUpdateCallbackManager : ICallbackManager where T : ISpatialComponentUpdate + internal class ComponentUpdateCallbackManager : ICallbackManager where T : struct, ISpatialComponentUpdate { private readonly IndexedCallbacks callbacks = new IndexedCallbacks(); private readonly ComponentUpdateSystem componentUpdateSystem; @@ -19,10 +19,10 @@ public ComponentUpdateCallbackManager(World world) public void InvokeCallbacks() { var updates = componentUpdateSystem.GetComponentUpdatesReceived(); - for (int i = 0; i < updates.Count; ++i) + for (var i = 0; i < updates.Count; ++i) { ref readonly var update = ref updates[i]; - callbacks.InvokeAll(update.EntityId.Id, update.Update); + callbacks.InvokeAll(update.EntityId.Id, in update.Update); } } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedAuthorityCallbackManagerSet.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentUpdateCallbackManager.cs.meta similarity index 83% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedAuthorityCallbackManagerSet.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentUpdateCallbackManager.cs.meta index ac0c0088cd..3501815463 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedAuthorityCallbackManagerSet.cs.meta +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ComponentUpdateCallbackManager.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 9613ac1c057e68d4e8a597ddc96fd928 +guid: bdecb6cc028221543885146e49bbbf18 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/EntityAddedCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/EntityAddedCallbackManager.cs similarity index 90% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/EntityAddedCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/EntityAddedCallbackManager.cs index e092181a1b..d212cf16a0 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/EntityAddedCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/EntityAddedCallbackManager.cs @@ -19,9 +19,9 @@ public EntityAddedCallbackManager(World world) public void InvokeCallbacks() { var entities = entitySystem.GetEntitiesAdded(); - for (int i = 0; i < entities.Count; ++i) + foreach (var entityId in entities) { - callbacks.InvokeAll(entities[i]); + callbacks.InvokeAll(entityId); } } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/EntityAddedCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/EntityAddedCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/EntityAddedCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/EntityAddedCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/EntityRemovedCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/EntityRemovedCallbackManager.cs similarity index 89% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/EntityRemovedCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/EntityRemovedCallbackManager.cs index 3ace6de783..35513ad246 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/EntityRemovedCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/EntityRemovedCallbackManager.cs @@ -19,9 +19,9 @@ public EntityRemovedCallbackManager(World world) public void InvokeCallbacks() { var entities = entitySystem.GetEntitiesRemoved(); - for (int i = 0; i < entities.Count; ++i) + foreach (var entityId in entities) { - callbacks.InvokeAllReverse(entities[i]); + callbacks.InvokeAllReverse(entityId); } } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/EntityRemovedCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/EntityRemovedCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/EntityRemovedCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/EntityRemovedCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedCallbackManagerSet.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/GuardedCallbackManagerSet.cs similarity index 65% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedCallbackManagerSet.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/GuardedCallbackManagerSet.cs index b0563a537b..fb93aefdb9 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedCallbackManagerSet.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/GuardedCallbackManagerSet.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; namespace Improbable.Gdk.Subscriptions @@ -22,23 +23,25 @@ public void AddCallbackManager(TIndex index, TManager manager) indexesToAdd.Add(index); } - public List GetManagers() + private void UpdateManagers() { - foreach (var indexToAdd in indexesToAdd) + if (indexesToAdd.Count > 0) { - callbackManagers.Add(indexToCallbackManager[indexToAdd]); - } - - indexesToAdd.Clear(); + foreach (var indexToAdd in indexesToAdd) + { + callbackManagers.Add(indexToCallbackManager[indexToAdd]); + } - return callbackManagers; + indexesToAdd.Clear(); + } } - public void InvokeCallbacks() + public void InvokeEach(Action callback) { - foreach (var manager in GetManagers()) + UpdateManagers(); + foreach (var manager in callbackManagers) { - manager.InvokeCallbacks(); + callback(manager); } } } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedCallbackManagerSet.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/GuardedCallbackManagerSet.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedCallbackManagerSet.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/GuardedCallbackManagerSet.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/IAuthorityCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/IAuthorityCallbackManager.cs similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/IAuthorityCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/IAuthorityCallbackManager.cs diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/IAuthorityCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/IAuthorityCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/IAuthorityCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/IAuthorityCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ICallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ICallbackManager.cs similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ICallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ICallbackManager.cs diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ICallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ICallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ICallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/ICallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/WorkerFlagCallbackManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/WorkerFlagCallbackManager.cs similarity index 94% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/WorkerFlagCallbackManager.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/WorkerFlagCallbackManager.cs index 5256b6d0f4..de46df7c57 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/WorkerFlagCallbackManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/WorkerFlagCallbackManager.cs @@ -30,7 +30,7 @@ public bool UnregisterCallback(ulong callbackKey) public void InvokeCallbacks() { var workerFlagChanges = workerSystem.Diff.GetWorkerFlagChanges(); - for (int i = 0; i < workerFlagChanges.Count; ++i) + for (var i = 0; i < workerFlagChanges.Count; ++i) { var pair = workerFlagChanges[i]; callbacks.InvokeAll(pair); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/WorkerFlagCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/WorkerFlagCallbackManager.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/WorkerFlagCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CallbackManagers/WorkerFlagCallbackManager.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks.meta new file mode 100644 index 0000000000..2b4e218169 --- /dev/null +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: dc5e514e5c4bf4d46ae8610b8ff64567 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/Callbacks.cs similarity index 53% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks.cs rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/Callbacks.cs index 50cbaf1be7..7cff3807fe 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/Callbacks.cs @@ -4,7 +4,7 @@ namespace Improbable.Gdk.Subscriptions { - internal class Callbacks + internal class Callbacks where T : struct { private readonly List callbacks = new List(); private readonly HashSet currentKeys = new HashSet(); @@ -50,7 +50,7 @@ public bool Remove(ulong key) return callbacks.Remove(new WrappedCallback(key, default)); } - public void InvokeAll(T op) + public void InvokeAll(in T op) { isInInvoke = true; @@ -58,7 +58,7 @@ public void InvokeAll(T op) { try { - callbacks[i].Invoke(op); + callbacks[i].Invoke(in op); } catch (Exception e) { @@ -71,7 +71,7 @@ public void InvokeAll(T op) FlushDeferredOperations(); } - public void InvokeAllReverse(T op) + public void InvokeAllReverse(in T op) { isInInvoke = true; @@ -124,7 +124,7 @@ private void FlushDeferredOperations() } } - private struct WrappedCallback : IEquatable + private readonly struct WrappedCallback : IEquatable { public readonly ulong Key; private readonly Action action; @@ -135,7 +135,7 @@ public WrappedCallback(ulong key, Action callback) action = callback; } - public void Invoke(T arg) + public void Invoke(in T arg) { action(arg); } @@ -156,103 +156,4 @@ public override int GetHashCode() } } } - - // Slight adjustment to the component callbacks class - // todo check if this can have in params for ops (i.e. can it be used with a non readonly struct) - public class IndexedCallbacks - { - private readonly Dictionary> callbacks = new Dictionary>(); - private readonly Dictionary callbackKeyToIndex = new Dictionary(); - - public void Add(long index, ulong callbackKey, Action value) - { - if (!callbacks.TryGetValue(index, out var callbacksForIndex)) - { - callbacksForIndex = new Callbacks(); - callbacks.Add(index, callbacksForIndex); - } - - callbackKeyToIndex.Add(callbackKey, index); - callbacks[index].Add(callbackKey, value); - } - - public bool Remove(ulong callbackKey) - { - if (!callbackKeyToIndex.TryGetValue(callbackKey, out var index)) - { - return false; - } - - callbacks[index].Remove(callbackKey); - callbackKeyToIndex.Remove(callbackKey); - return true; - } - - public void InvokeAll(long index, T op) - { - if (callbacks.ContainsKey(index)) - { - callbacks[index].InvokeAll(op); - } - } - - public void InvokeAllReverse(long index, T op) - { - if (callbacks.ContainsKey(index)) - { - callbacks[index].InvokeAllReverse(op); - } - } - } - - // Efficient ability to remove all callbacks for an index, but slow to remove a single callback - public class SingleUseIndexCallbacks - { - private readonly Dictionary> callbacks = new Dictionary>(); - - public void Add(long index, ulong callbackKey, Action value) - { - if (!callbacks.TryGetValue(index, out var callbacksForIndex)) - { - callbacksForIndex = new Callbacks(); - callbacks.Add(index, callbacksForIndex); - } - - callbacksForIndex.Add(callbackKey, value); - } - - public void RemoveAllCallbacksForIndex(long index) - { - callbacks.Remove(index); - } - - public bool Remove(ulong callbackKey) - { - foreach (var callback in callbacks) - { - if (callback.Value.Remove(callbackKey)) - { - return true; - } - } - - return false; - } - - public void InvokeAll(long index, T op) - { - if (callbacks.TryGetValue(index, out var callback)) - { - callback.InvokeAll(op); - } - } - - public void InvokeAllReverse(long index, T op) - { - if (callbacks.TryGetValue(index, out var callback)) - { - callback.InvokeAllReverse(op); - } - } - } } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/Callbacks.cs.meta similarity index 100% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/Callbacks.cs.meta diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/IndexedCallbacks.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/IndexedCallbacks.cs new file mode 100644 index 0000000000..5c062299c2 --- /dev/null +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/IndexedCallbacks.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; + +namespace Improbable.Gdk.Subscriptions +{ + public class IndexedCallbacks where T : struct + { + private readonly Dictionary> indexedCallbacks = new Dictionary>(); + private readonly Dictionary callbackKeyToIndex = new Dictionary(); + + public void Add(long index, ulong callbackKey, Action value) + { + if (!indexedCallbacks.TryGetValue(index, out var callbacks)) + { + callbacks = new Callbacks(); + indexedCallbacks.Add(index, callbacks); + } + + callbackKeyToIndex.Add(callbackKey, index); + callbacks.Add(callbackKey, value); + } + + public bool Remove(ulong callbackKey) + { + if (!callbackKeyToIndex.TryGetValue(callbackKey, out var index)) + { + return false; + } + + indexedCallbacks[index].Remove(callbackKey); + callbackKeyToIndex.Remove(callbackKey); + return true; + } + + public void InvokeAll(long index, in T argument) + { + if (indexedCallbacks.TryGetValue(index, out var callbacks)) + { + callbacks.InvokeAll(in argument); + } + } + + public void InvokeAllReverse(long index, in T argument) + { + if (indexedCallbacks.TryGetValue(index, out var callbacks)) + { + callbacks.InvokeAllReverse(in argument); + } + } + } +} diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/EntitySubscriptions.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/IndexedCallbacks.cs.meta similarity index 83% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/EntitySubscriptions.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/IndexedCallbacks.cs.meta index 1f7d225d67..c784a41263 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/EntitySubscriptions.cs.meta +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/IndexedCallbacks.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 9d76e177445b94247b06859d15356575 +guid: 7f3c88769a3cd134c8708b1c42ddef12 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/SingleUseIndexCallbacks.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/SingleUseIndexCallbacks.cs new file mode 100644 index 0000000000..e6bc96ba3d --- /dev/null +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/SingleUseIndexCallbacks.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; + +namespace Improbable.Gdk.Subscriptions +{ + // Efficient ability to remove all callbacks for an index, but slow to remove a single callback + public class SingleUseIndexCallbacks where T : struct + { + private readonly Dictionary> indexedCallbacks = new Dictionary>(); + + public void Add(long index, ulong callbackKey, Action value) + { + if (!indexedCallbacks.TryGetValue(index, out var callbacksForIndex)) + { + callbacksForIndex = new Callbacks(); + indexedCallbacks.Add(index, callbacksForIndex); + } + + callbacksForIndex.Add(callbackKey, value); + } + + public void RemoveAllCallbacksForIndex(long index) + { + indexedCallbacks.Remove(index); + } + + public bool Remove(ulong callbackKey) + { + foreach (var pair in indexedCallbacks) + { + var callbacks = pair.Value; + if (callbacks.Remove(callbackKey)) + { + return true; + } + } + + return false; + } + + public void InvokeAll(long index, T argument) + { + if (indexedCallbacks.TryGetValue(index, out var callbacks)) + { + callbacks.InvokeAll(in argument); + } + } + + public void InvokeAllReverse(long index, T argument) + { + if (indexedCallbacks.TryGetValue(index, out var callbacks)) + { + callbacks.InvokeAllReverse(in argument); + } + } + } +} diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentUdpateCallbackManager.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/SingleUseIndexCallbacks.cs.meta similarity index 83% rename from workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentUdpateCallbackManager.cs.meta rename to workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/SingleUseIndexCallbacks.cs.meta index 0cefc8f7c0..cd5770fd08 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/ComponentUdpateCallbackManager.cs.meta +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Callbacks/SingleUseIndexCallbacks.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 212d83f0a90bce741988be24ca63e9f6 +guid: fc7a8fc767133ac4390f7da866da1801 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedAuthorityCallbackManagerSet.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedAuthorityCallbackManagerSet.cs deleted file mode 100644 index 99bc61686e..0000000000 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CalllbackManagers/GuardedAuthorityCallbackManagerSet.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace Improbable.Gdk.Subscriptions -{ - internal class GuardedAuthorityCallbackManagerSet : GuardedCallbackManagerSet - where TManager : IAuthorityCallbackManager - { - public void InvokeLossImminentCallbacks() - { - foreach (var manager in GetManagers()) - { - manager.InvokeLossImminentCallbacks(); - } - } - } -} diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/EntityGameObjectLinker.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/EntityGameObjectLinker.cs index 31cb817b65..0895b6efb8 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/EntityGameObjectLinker.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/EntityGameObjectLinker.cs @@ -197,7 +197,7 @@ private void AddComponentsToEntity(EntityId entityId, GameObject gameObject, if (!workerSystem.TryGetEntity(entityId, out var entity)) { throw new ArgumentException( - $"Can not add GameObjet components to entity {entityId}. Entity not in view"); + $"Can not add GameObject components to entity {entityId}. Entity not in view"); } var componentTypes = new List(componentTypesToAdd.Length); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/EntitySubscriptions.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/EntitySubscriptions.cs deleted file mode 100644 index 61b66317a5..0000000000 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/EntitySubscriptions.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using Improbable.Gdk.Core; -using Unity.Entities; - -namespace Improbable.Gdk.Subscriptions -{ - public static class EntitySubscriptions - { - public static Subscription Subscribe(EntityId entityId, World world) - { - var subscriptionSystem = world.GetExistingSystem(); - if (subscriptionSystem == null) - { - throw new ArgumentException("No subscription system"); - } - - return (Subscription) subscriptionSystem.Subscribe(entityId); - } - - public static void TryRegisterManager(SubscriptionManagerBase manager, World world) - { - TryRegisterManager(typeof(T), manager, world); - } - - public static void TryRegisterManager(Type type, SubscriptionManagerBase manager, World world) - { - var subscriptionSystem = world.GetExistingSystem(); - if (subscriptionSystem == null) - { - throw new ArgumentException("No subscription system"); - } - - subscriptionSystem.RegisterSubscriptionManager(type, manager); - } - } -} diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsDatabase.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsDatabase.cs index d48a6edb3e..abec901978 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsDatabase.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsDatabase.cs @@ -7,7 +7,7 @@ namespace Improbable.Gdk.Subscriptions { internal static class RequiredSubscriptionsDatabase { - private static Dictionary typeToRequiredSubscriptionsInfo; + private static readonly Dictionary typeToRequiredSubscriptionsInfo; static RequiredSubscriptionsDatabase() { diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsInfo.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsInfo.cs index 2927fc9c10..670ef65d1f 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsInfo.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsInfo.cs @@ -25,7 +25,7 @@ public RequiredSubscriptionsInfo(Type type) } RequiredTypes = new Type[RequiredFields.Count]; - for (int i = 0; i < RequiredFields.Count; ++i) + for (var i = 0; i < RequiredFields.Count; ++i) { RequiredTypes[i] = RequiredFields[i].FieldType; } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsInjector.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsInjector.cs index 6bdd1c12cc..73e7d88814 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsInjector.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/RequiredSubscriptionsInjector.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using Improbable.Gdk.Core; -using UnityEngine; namespace Improbable.Gdk.Subscriptions { @@ -13,8 +12,8 @@ public class RequiredSubscriptionsInjector private readonly RequiredSubscriptionsInfo info; private readonly object target; - private Action onEnable; - private Action onDisable; + private readonly Action onEnable; + private readonly Action onDisable; // todo should either special case monobehaviours or not use callbacks // for non monobehaviours we could use functors @@ -33,7 +32,7 @@ public RequiredSubscriptionsInjector(object target, EntityId entityId, Subscript return; } - subscriptions = new SubscriptionAggregate(subscriptionSystem, entityId, info.RequiredTypes); + subscriptions = subscriptionSystem.Subscribe(entityId, info.RequiredTypes); subscriptions.SetAvailabilityHandler(Handler.Pool.Rent(this)); } @@ -69,7 +68,6 @@ private void HandleSubscriptionsSatisfied() { foreach (var field in info.RequiredFields) { - // todo should really do this as they become available rather than all at once field.SetValue(target, subscriptions.GetErasedValue(field.FieldType)); } @@ -105,13 +103,13 @@ public void OnUnavailable() injector.HandleSubscriptionsNoLongerSatisfied(); } - public class Pool + public static class Pool { private static readonly Stack HandlerPool = new Stack(); public static Handler Rent(RequiredSubscriptionsInjector injector) { - Handler handler = HandlerPool.Count == 0 + var handler = HandlerPool.Count == 0 ? new Handler() : HandlerPool.Pop(); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/StandardSubscriptionManagers/WorkerFlagSubscriptionManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/StandardSubscriptionManagers/WorkerFlagSubscriptionManager.cs index a561d8ab9a..7b3ff5897a 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/StandardSubscriptionManagers/WorkerFlagSubscriptionManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/StandardSubscriptionManagers/WorkerFlagSubscriptionManager.cs @@ -8,9 +8,7 @@ namespace Improbable.Gdk.Core [AutoRegisterSubscriptionManager] public class WorkerFlagSubscriptionManager : SubscriptionManager { - private World world; - - private HashSet> readerSubscriptions = new HashSet>(); + private readonly World world; public WorkerFlagSubscriptionManager(World world) { @@ -23,7 +21,6 @@ public override void Cancel(ISubscription subscription) readerSubscription.Value.IsValid = false; readerSubscription.Value.RemoveAllCallbacks(); - readerSubscriptions.Remove(readerSubscription); } public override void ResetValue(ISubscription subscription) @@ -36,8 +33,6 @@ public override void ResetValue(ISubscription subscription) public override Subscription Subscribe(EntityId entityId) { var subscription = new Subscription(this, new EntityId(0)); - readerSubscriptions.Add(subscription); - subscription.SetAvailable(new WorkerFlagReader(world)); return subscription; diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Subscription.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Subscription.cs index d0516f5ab0..7f5f27a93e 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Subscription.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Subscription.cs @@ -23,9 +23,11 @@ public interface ISubscription // might need to make HasValue a property though and give it an ID public class Subscription : ISubscription { - // Want a custom event thing that gives an add and invoke option - public bool HasValue { get; private set; } + public bool HasValue => value.HasValue; public EntityId EntityId { get; } + public T Value => value.Value; + + private Option value; private readonly SubscriptionManagerBase manager; @@ -54,21 +56,6 @@ public event Action OnAvailable public event Action OnUnavailable; - private T value; - - public T Value - { - get - { - if (HasValue) - { - return value; - } - - throw new InvalidOperationException("Subscribed object not available"); - } - } - public Subscription(SubscriptionManagerBase manager, EntityId entityId) { this.manager = manager; @@ -85,7 +72,6 @@ public void Cancel() public void SetAvailable(T subscribedObject) { value = subscribedObject; - HasValue = true; availabilityHandler?.OnAvailable(); @@ -100,8 +86,7 @@ public void SetAvailable(T subscribedObject) public void SetUnavailable() { - HasValue = false; - value = default(T); + value = Option.Empty; availabilityHandler?.OnUnavailable(); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/SubscriptionAggregate.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/SubscriptionAggregate.cs index 5605abac31..ec45262c4f 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/SubscriptionAggregate.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/SubscriptionAggregate.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -using Improbable.Gdk.Core; +using UnityEngine.Assertions; namespace Improbable.Gdk.Subscriptions { @@ -10,28 +10,26 @@ public class SubscriptionAggregate private readonly Dictionary typesToSubscriptionIndexes = new Dictionary(); private ISubscriptionAvailabilityHandler availabilityHandler; - private int subscriptionsSatisfied; - public SubscriptionAggregate(SubscriptionSystem subscriptionSystem, EntityId entityId, - params Type[] typesToSubscribeTo) + private bool IsSatisfied => subscriptionsSatisfied == subscriptions.Length; + + public SubscriptionAggregate(IReadOnlyList types, ISubscription[] subscriptions) { - subscriptions = new ISubscription[typesToSubscribeTo.Length]; + Assert.AreEqual(types.Count, subscriptions.Length); - for (int i = 0; i < typesToSubscribeTo.Length; ++i) + this.subscriptions = subscriptions; + for (var i = 0; i < types.Count; i++) { - var subscription = subscriptionSystem.Subscribe(entityId, typesToSubscribeTo[i]); - typesToSubscriptionIndexes.Add(typesToSubscribeTo[i], i); - subscriptions[i] = subscription; - - subscription.SetAvailabilityHandler(Handler.Pool.Rent(this)); + typesToSubscriptionIndexes.Add(types[i], i); + subscriptions[i].SetAvailabilityHandler(Handler.Pool.Rent(this)); } } public void SetAvailabilityHandler(ISubscriptionAvailabilityHandler handler) { availabilityHandler = handler; - if (subscriptionsSatisfied == subscriptions.Length) + if (IsSatisfied) { availabilityHandler?.OnAvailable(); } @@ -44,34 +42,36 @@ public ISubscriptionAvailabilityHandler GetAvailabilityHandler() public T GetValue() { - if (subscriptionsSatisfied != subscriptions.Length) + if (!IsSatisfied) { throw new InvalidOperationException("Subscriptions not all satisfied"); } - if (!typesToSubscriptionIndexes.TryGetValue(typeof(T), out var subscriptionIndex)) - { - throw new InvalidOperationException("No subscription with that type"); - } - - var sub = (Subscription) subscriptions[subscriptionIndex]; + var subscriptionIndex = GetTypeIndex(typeof(T)); + var subscription = (Subscription) subscriptions[subscriptionIndex]; - return sub.Value; + return subscription.Value; } public object GetErasedValue(Type type) { - if (subscriptionsSatisfied != subscriptions.Length) + if (!IsSatisfied) { throw new InvalidOperationException("Subscriptions not all satisfied"); } + var subscriptionIndex = GetTypeIndex(type); + return subscriptions[subscriptionIndex].GetErasedValue(); + } + + private int GetTypeIndex(Type type) + { if (!typesToSubscriptionIndexes.TryGetValue(type, out var subscriptionIndex)) { throw new InvalidOperationException("No subscription with that type"); } - return subscriptions[subscriptionIndex].GetErasedValue(); + return subscriptionIndex; } public void Cancel() @@ -85,7 +85,7 @@ public void Cancel() private void HandleSubscriptionUnavailable() { - if (subscriptionsSatisfied == subscriptions.Length) + if (IsSatisfied) { foreach (var sub in subscriptions) { @@ -101,7 +101,7 @@ private void HandleSubscriptionUnavailable() private void HandleSubscriptionAvailable() { ++subscriptionsSatisfied; - if (subscriptionsSatisfied == subscriptions.Length) + if (IsSatisfied) { availabilityHandler?.OnAvailable(); } @@ -121,13 +121,13 @@ public void OnUnavailable() aggregate?.HandleSubscriptionUnavailable(); } - public class Pool + public static class Pool { private static readonly Stack HandlerPool = new Stack(); public static Handler Rent(SubscriptionAggregate aggregate) { - Handler handler = HandlerPool.Count == 0 + var handler = HandlerPool.Count == 0 ? new Handler() : HandlerPool.Pop(); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/CommandCallbackSystem.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/CommandCallbackSystem.cs index 2b90422db6..143b4a8b8d 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/CommandCallbackSystem.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/CommandCallbackSystem.cs @@ -16,8 +16,8 @@ public class CommandCallbackSystem : ComponentSystem private readonly GuardedCallbackManagerSet callbackManagers = new GuardedCallbackManagerSet(); - private readonly Dictionary keyToInternalKeyAndManager = - new Dictionary(); + private readonly Dictionary keyToInternalKeyAndManager = + new Dictionary(); private ulong callbacksRegistered = 1; @@ -56,13 +56,13 @@ public bool UnregisterCommandRequestCallback(ulong callbackKey) return false; } - return keyAndManager.Item2.UnregisterCallback(keyAndManager.Item1); + return keyAndManager.manager.UnregisterCallback(keyAndManager.key); } internal void InvokeCallbacks() { // todo could split these out to ensure requests are done before responses - callbackManagers.InvokeCallbacks(); + callbackManagers.InvokeEach(manager => manager.InvokeCallbacks()); } protected override void OnCreate() diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/ComponentCallbackSystem.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/ComponentCallbackSystem.cs index 23548d14c7..fd60acf918 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/ComponentCallbackSystem.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/ComponentCallbackSystem.cs @@ -15,8 +15,8 @@ public class ComponentCallbackSystem : ComponentSystem private readonly GuardedCallbackManagerSet componentCallbackManagers = new GuardedCallbackManagerSet(); - private readonly GuardedAuthorityCallbackManagerSet authorityCallbackManagers = - new GuardedAuthorityCallbackManagerSet(); + private readonly GuardedCallbackManagerSet authorityCallbackManagers = + new GuardedCallbackManagerSet(); private readonly Dictionary keyToInternalKeyAndManager = new Dictionary(); @@ -24,7 +24,7 @@ public class ComponentCallbackSystem : ComponentSystem private ulong callbacksRegistered = 1; public ulong RegisterComponentUpdateCallback(EntityId entityId, Action callback) - where T : ISpatialComponentUpdate + where T : struct, ISpatialComponentUpdate { if (!componentCallbackManagers.TryGetManager(typeof(T), out var manager)) { @@ -38,7 +38,7 @@ public ulong RegisterComponentUpdateCallback(EntityId entityId, Action cal } public ulong RegisterComponentEventCallback(EntityId entityId, Action callback) - where T : IEvent + where T : struct, IEvent { if (!componentCallbackManagers.TryGetManager(typeof(T), out var manager)) { @@ -76,14 +76,13 @@ public bool UnregisterCallback(ulong callbackKey) internal void InvokeNoLossImminent() { - // todo might want to split updates and events out to ensure updates are called first - componentCallbackManagers.InvokeCallbacks(); - authorityCallbackManagers.InvokeCallbacks(); + componentCallbackManagers.InvokeEach(manager => manager.InvokeCallbacks()); + authorityCallbackManagers.InvokeEach(manager => manager.InvokeCallbacks()); } internal void InvokeLossImminent() { - authorityCallbackManagers.InvokeLossImminentCallbacks(); + authorityCallbackManagers.InvokeEach(manager => manager.InvokeLossImminentCallbacks()); } protected override void OnCreate() diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/ComponentConstraintsCallbackSystem.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/ComponentConstraintsCallbackSystem.cs index e9b1cc3419..7948d90110 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/ComponentConstraintsCallbackSystem.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/ComponentConstraintsCallbackSystem.cs @@ -22,8 +22,8 @@ public class ComponentConstraintsCallbackSystem : ComponentSystem private EntityAddedCallbackManager entityAdded; private EntityRemovedCallbackManager entityRemoved; - private readonly Dictionary keyToInternalKeyAndManager = - new Dictionary(); + private readonly Dictionary keyToInternalKeyAndManager = + new Dictionary(); private ulong callbacksRegistered = 1; @@ -98,16 +98,16 @@ internal bool UnregisterCallback(ulong callbackKey) return false; } - return keyAndManager.Item2.UnregisterCallback(callbackKey); + return keyAndManager.manager.UnregisterCallback(callbackKey); } internal void Invoke() { - componentRemoved.InvokeCallbacks(); + componentRemoved.InvokeEach(manager => manager.InvokeCallbacks()); entityRemoved?.InvokeCallbacks(); entityAdded?.InvokeCallbacks(); - componentAdded.InvokeCallbacks(); - authority.InvokeCallbacks(); + componentAdded.InvokeEach(manager => manager.InvokeCallbacks()); + authority.InvokeEach(manager => manager.InvokeCallbacks()); } protected override void OnCreate() diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/RequireLifecycleSystem.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/RequireLifecycleSystem.cs index 9686c0f379..58d37d36fd 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/RequireLifecycleSystem.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/RequireLifecycleSystem.cs @@ -44,18 +44,21 @@ protected override void OnUpdate() componentCallbackSystem.InvokeNoLossImminent(); - foreach (var behaviour in behavioursToEnable) + if (behavioursToEnable.Count > 0) { - if (behaviour == null) + foreach (var behaviour in behavioursToEnable) { - continue; + if (behaviour == null) + { + continue; + } + + behaviour.enabled = true; } - behaviour.enabled = true; + behavioursToEnable.Clear(); } - behavioursToEnable.Clear(); - componentCallbackSystem.InvokeLossImminent(); commandCallbackSystem.InvokeCallbacks(); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/SubscriptionSystem.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/SubscriptionSystem.cs index 9fd89592c6..953f35369f 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/SubscriptionSystem.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Systems/SubscriptionSystem.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Reflection; using Improbable.Gdk.Core; using Unity.Entities; @@ -12,14 +11,22 @@ public class SubscriptionSystem : ComponentSystem private readonly Dictionary typeToSubscriptionManager = new Dictionary(); + protected override void OnCreate() + { + base.OnCreate(); + Enabled = false; + + AutoRegisterManagers(); + } + public void RegisterSubscriptionManager(Type type, SubscriptionManagerBase manager) { if (typeToSubscriptionManager.ContainsKey(type)) { - throw new InvalidOperationException("Already a manager registered"); + throw new InvalidOperationException($"Duplicate manager for {type.Name}."); } - typeToSubscriptionManager[type] = manager; + typeToSubscriptionManager.Add(type, manager); } public Subscription Subscribe(EntityId entity) @@ -42,12 +49,15 @@ public ISubscription Subscribe(EntityId entity, Type type) return manager.SubscribeTypeErased(entity); } - protected override void OnCreate() + public SubscriptionAggregate Subscribe(EntityId entity, params Type[] types) { - base.OnCreate(); - Enabled = false; + var subscriptions = new ISubscription[types.Length]; + for (var i = 0; i < types.Length; i++) + { + subscriptions[i] = Subscribe(entity, types[i]); + } - AutoRegisterManagers(); + return new SubscriptionAggregate(types, subscriptions); } protected override void OnUpdate() diff --git a/workers/unity/Packages/io.improbable.gdk.core/Tests/Editmode/Subscriptions/AggregateSubscriptionTests.cs b/workers/unity/Packages/io.improbable.gdk.core/Tests/Editmode/Subscriptions/AggregateSubscriptionTests.cs index d168d10431..d529af40dd 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Tests/Editmode/Subscriptions/AggregateSubscriptionTests.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Tests/Editmode/Subscriptions/AggregateSubscriptionTests.cs @@ -14,7 +14,7 @@ public class AggregateSubscriptionTests : SubscriptionTestBase public void AggregateSubscription_should_be_available_if_all_constraints_satisfied() { var handler = new AvailabilityHandler(); - var aggSub = new SubscriptionAggregate(SubscriptionSystem, entityId, typeof(EntityId), typeof(World)); + var aggSub = SubscriptionSystem.Subscribe(entityId, typeof(EntityId), typeof(World)); aggSub.SetAvailabilityHandler(handler); Assert.IsTrue(handler.IsAvailable); @@ -26,7 +26,7 @@ public void AggregateSubscription_should_be_available_if_all_constraints_satisfi public void AggregateSubscription_should_not_be_available_if_not_all_constraints_satisfied() { var handler = new AvailabilityHandler(); - var aggSub = new SubscriptionAggregate(SubscriptionSystem, entityId, typeof(Entity), typeof(World)); + var aggSub = SubscriptionSystem.Subscribe(entityId, typeof(Entity), typeof(World)); aggSub.SetAvailabilityHandler(handler); Assert.IsFalse(handler.IsAvailable);