From 9ea8fd462272a55c407bbfd23d0e94f0a8d95d9c Mon Sep 17 00:00:00 2001 From: Martijn Gerkes Date: Fri, 7 Aug 2020 14:24:16 +0100 Subject: [PATCH] Remove the View (#1364) * Remove Components from the view Remove Authority from the view Remove AuthImminentLoss support * Remove entities from View * Remove ViewStorage types * Fix up WorkerInspector to no longer use View * Move worker flags to Worker * Changelog + Upgrade guide --- CHANGELOG.md | 8 + UPGRADE_GUIDE.md | 17 ++ .../ToggleRotationCommandSender.cs | 3 +- .../.codegen/Source/CoreCodegenJob.cs | 1 - .../Generators/Core/MetaclassGenerator.cs | 1 - .../Components/IComponentMetaclass.cs | 1 - .../Dynamic/ComponentDatabase.cs | 10 ++ ...ndSenderReceiverSubscriptionManagerBase.cs | 13 +- .../Subscriptions/Reader.cs | 8 +- .../WorkerFlagSubscriptionManager.cs | 6 +- .../WriterSubscriptionManager.cs | 8 +- .../Systems/ComponentUpdateSystem.cs | 17 -- .../Systems/EntitySystem.cs | 14 -- .../Systems/SpatialOSReceiveSystem.cs | 11 +- .../Systems/WorkerSystem.cs | 15 +- .../io.improbable.gdk.core/View/View.cs | 160 ------------------ .../io.improbable.gdk.core/View/View.cs.meta | 11 -- .../View/ViewStorage.cs | 27 --- .../View/ViewStorage.cs.meta | 11 -- .../io.improbable.gdk.core/Worker/Worker.cs | 14 +- .../WorkerInspector/WorkerDetail.cs | 12 +- .../GameObjectInitializationSystem.cs | 10 +- .../PlayerLifecycleHelper.cs | 9 +- .../TransformSynchronization.cs | 3 +- 24 files changed, 96 insertions(+), 294 deletions(-) delete mode 100644 workers/unity/Packages/io.improbable.gdk.core/View/View.cs delete mode 100644 workers/unity/Packages/io.improbable.gdk.core/View/View.cs.meta delete mode 100644 workers/unity/Packages/io.improbable.gdk.core/View/ViewStorage.cs delete mode 100644 workers/unity/Packages/io.improbable.gdk.core/View/ViewStorage.cs.meta diff --git a/CHANGELOG.md b/CHANGELOG.md index 2fe161088d..3e45a29d49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ ## Unreleased +### Breaking Changes + +- The `Authority` field on Readers and Writers has been changed to `HasAuthority`. It now returns a bool to indicate whether you have authority over the component it represents. [#1364](https://github.com/spatialos/gdk-for-unity/pull/1364) + - This no longer supports AuthorityImminentLoss. +- ComponentUpdateSystem no longer has the API `GetAuthority`, `GetComponent`, and `HasComponent`. [#1364](https://github.com/spatialos/gdk-for-unity/pull/1364) + - Use the Unity Entities `EntityManager` instead. + ### Added - Added capability to test commands through the `MockConnectionHandler`. [#1437](https://github.com/spatialos/gdk-for-unity/pull/1437) @@ -21,6 +28,7 @@ - Added C# bindings for C Event Tracing API. [#1440](https://github.com/spatialos/gdk-for-unity/pull/1440) - Added native classes for IO operations in Event Tracing API. [#1444](https://github.com/spatialos/gdk-for-unity/pull/1444) +- Removed the `View` class and its generated storage classes. [#1364](https://github.com/spatialos/gdk-for-unity/pull/1364) ## `0.3.9` - 2020-07-24 diff --git a/UPGRADE_GUIDE.md b/UPGRADE_GUIDE.md index f44b85824e..3290911304 100644 --- a/UPGRADE_GUIDE.md +++ b/UPGRADE_GUIDE.md @@ -1,5 +1,22 @@ # Upgrade Guide +## From `0.3.9` to `0.3.10` + +### ComponentUpdateSystem API changes + +The methods `GetAuthority`, `GetComponent`, and `HasComponent` have been removed from the `ComponentUpdateSystem`. +They can be replaced by similar usage of the `EntityManager`. + +* `componentUpdateSystem.GetAuthority(entityId, componentId)` to `EntityManager.HasComponent(entity)`. +* `componentUpdateSystem.GetComponent(entityId)` to `EntityManager.GetComponentData(entity)`. +* `componentUpdateSystem.HasComponent(componentId, entityId)` to `EntityManager.HasComponent(entity)` + +The `WorkerSystem` and `ComponentDatabase` can be used to convert from `entityId` to `entity`, and from `componentId` to a component type. + +### Readers and Writers Authority + +The `Reader` and `Writer` classes have had their `public Authority Authority` property changed to `public bool HasAuthority`. `HasAuthority` now indicates whether you have authority over the given component or not. + ## From `0.3.7` to `0.3.8` ### Asset based entity representation diff --git a/workers/unity/Assets/Playground/Scripts/MonoBehaviours/ToggleRotationCommandSender.cs b/workers/unity/Assets/Playground/Scripts/MonoBehaviours/ToggleRotationCommandSender.cs index b0ae2035b5..dc059a89de 100644 --- a/workers/unity/Assets/Playground/Scripts/MonoBehaviours/ToggleRotationCommandSender.cs +++ b/workers/unity/Assets/Playground/Scripts/MonoBehaviours/ToggleRotationCommandSender.cs @@ -1,6 +1,5 @@ using Improbable.Gdk.Core; using Improbable.Gdk.Subscriptions; -using Improbable.Worker.CInterop; using UnityEngine; namespace Playground.MonoBehaviours @@ -16,7 +15,7 @@ public class ToggleRotationCommandSender : MonoBehaviour private void Update() { - if (reader.Authority != Authority.NotAuthoritative) + if (reader.HasAuthority) { // Perform sending logic only on non-authoritative workers. return; diff --git a/workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/CoreCodegenJob.cs b/workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/CoreCodegenJob.cs index 6546cf5672..cda8a3a785 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/CoreCodegenJob.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/CoreCodegenJob.cs @@ -56,7 +56,6 @@ public CoreCodegenJob(CodegenJobOptions options, IFileSystem fileSystem, Details c => ($"{c.Name}EcsViewManager.cs", UnityEcsViewManagerGenerator.Generate), c => ($"{c.Name}ComponentDiffStorage.cs", ComponentDiffStorageGenerator.Generate), c => ($"{c.Name}ComponentDiffDeserializer.cs", ComponentDiffDeserializerGenerator.Generate), - c => ($"{c.Name}ViewStorage.cs", ViewStorageGenerator.Generate), c => ($"{c.Name}Metaclass.cs", MetaclassGenerator.Generate)); Logger.Trace("Adding job targets for commands."); diff --git a/workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/Generators/Core/MetaclassGenerator.cs b/workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/Generators/Core/MetaclassGenerator.cs index 059dbdee7a..fe904f02eb 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/Generators/Core/MetaclassGenerator.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/.codegen/Source/Generators/Core/MetaclassGenerator.cs @@ -45,7 +45,6 @@ public static CodeWriter Generate(UnityComponentDetails componentDetails) public Type DiffDeserializer {{ get; }} = typeof({rootNamespace}.DiffComponentDeserializer); public Type DiffStorage {{ get; }} = typeof({rootNamespace}.DiffComponentStorage); -public Type ViewStorage {{ get; }} = typeof({rootNamespace}.{componentDetails.Name}ViewStorage); public Type EcsViewManager {{ get; }} = typeof({rootNamespace}.EcsViewManager); public Type DynamicInvokable {{ get; }} = typeof({rootNamespace}.{componentDetails.Name}Dynamic); "); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Components/IComponentMetaclass.cs b/workers/unity/Packages/io.improbable.gdk.core/Components/IComponentMetaclass.cs index a7fc5ad7df..7278501198 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Components/IComponentMetaclass.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Components/IComponentMetaclass.cs @@ -18,7 +18,6 @@ public interface IComponentMetaclass Type DiffDeserializer { get; } Type DiffStorage { get; } - Type ViewStorage { get; } Type EcsViewManager { get; } Type DynamicInvokable { get; } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Dynamic/ComponentDatabase.cs b/workers/unity/Packages/io.improbable.gdk.core/Dynamic/ComponentDatabase.cs index 13aee37a5c..7349fe4e29 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Dynamic/ComponentDatabase.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Dynamic/ComponentDatabase.cs @@ -43,6 +43,16 @@ public static IComponentMetaclass GetMetaclass(uint componentId) return metaclass; } + public static IComponentMetaclass GetMetaclass() where T : ISpatialComponentData + { + if (!ComponentsToIds.TryGetValue(typeof(T), out var id)) + { + throw new ArgumentException($"Can not find Metaclass for unregistered SpatialOS component {nameof(T)}."); + } + + return Metaclasses[id]; + } + public static uint GetComponentId() where T : ISpatialComponentData { if (!ComponentsToIds.TryGetValue(typeof(T), out var id)) diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CommandSenderReceiverSubscriptionManagerBase.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CommandSenderReceiverSubscriptionManagerBase.cs index 5bf056d1b6..9c31510fd6 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CommandSenderReceiverSubscriptionManagerBase.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/CommandSenderReceiverSubscriptionManagerBase.cs @@ -124,19 +124,21 @@ public override void ResetValue(ISubscription subscription) public abstract class CommandReceiverSubscriptionManagerBase : SubscriptionManager where T : ICommandReceiver { - private readonly ComponentUpdateSystem componentUpdateSystem; + private readonly EntityManager entityManager; private Dictionary>> entityIdToReceiveSubscriptions; private HashSet entitiesMatchingRequirements = new HashSet(); private HashSet entitiesNotMatchingRequirements = new HashSet(); - private readonly uint componentId; + private readonly ComponentType componentAuthType; protected CommandReceiverSubscriptionManagerBase(World world, uint componentId) : base(world) { - this.componentId = componentId; - componentUpdateSystem = world.GetExistingSystem(); + var componentMetaClass = ComponentDatabase.GetMetaclass(componentId); + componentAuthType = componentMetaClass.Authority; + + entityManager = world.EntityManager; var constraintSystem = world.GetExistingSystem(); @@ -196,8 +198,7 @@ public override Subscription Subscribe(EntityId entityId) } if (WorkerSystem.TryGetEntity(entityId, out var entity) - && componentUpdateSystem.HasComponent(componentId, entityId) - && componentUpdateSystem.GetAuthority(entityId, componentId) != Authority.NotAuthoritative) + && entityManager.HasComponent(entity, componentAuthType)) { entitiesMatchingRequirements.Add(entityId); subscription.SetAvailable(CreateReceiver(World, entity, entityId)); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Reader.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Reader.cs index d73500e9f2..f682f1757c 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Reader.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/Reader.cs @@ -17,7 +17,9 @@ public abstract class Reader : IRequireable protected readonly EntityManager EntityManager; protected readonly Entity Entity; protected readonly EntityId EntityId; + private static readonly uint ComponentId = ComponentDatabase.GetComponentId(); + private static readonly ComponentType ComponentAuthType = ComponentDatabase.GetMetaclass().Authority; private Dictionary, ulong> authorityCallbackToCallbackKey; private Dictionary, ulong> updateCallbackToCallbackKey; @@ -35,16 +37,16 @@ public TComponent Data } } - public Authority Authority + public bool HasAuthority { get { if (!IsValid) { - throw new InvalidOperationException("Cannot read authority when Reader is not valid"); + throw new InvalidOperationException($"Cannot read {nameof(HasAuthority)} when Reader is not valid"); } - return ComponentUpdateSystem.GetAuthority(EntityId, ComponentId); + return EntityManager.HasComponent(Entity, ComponentAuthType); } } 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 bc0526a766..5c76ea00ce 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 @@ -41,7 +41,7 @@ public class WorkerFlagReader public bool IsValid; private readonly WorkerFlagCallbackSystem callbackSystem; - private readonly View view; + private readonly WorkerSystem workerSystem; private Dictionary, ulong> callbackToKey; @@ -49,7 +49,7 @@ public WorkerFlagReader(World world) { IsValid = true; callbackSystem = world.GetExistingSystem(); - view = world.GetExistingSystem().View; + workerSystem = world.GetExistingSystem(); } public event Action OnWorkerFlagChange @@ -92,7 +92,7 @@ public event Action OnWorkerFlagChange public string GetFlag(string name) { - return view.GetWorkerFlag(name); + return workerSystem.GetWorkerFlag(name); } internal void RemoveAllCallbacks() diff --git a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/WriterSubscriptionManager.cs b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/WriterSubscriptionManager.cs index 122f1f4539..15e49618e4 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/WriterSubscriptionManager.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Subscriptions/WriterSubscriptionManager.cs @@ -9,17 +9,18 @@ namespace Improbable.Gdk.Subscriptions public abstract class WriterSubscriptionManager : SubscriptionManager where TWriter : IRequireable where TComponent : ISpatialComponentData { - private readonly ComponentUpdateSystem componentUpdateSystem; + private readonly EntityManager entityManager; private Dictionary>> entityIdToWriterSubscriptions; private readonly HashSet entitiesMatchingRequirements = new HashSet(); private readonly HashSet entitiesNotMatchingRequirements = new HashSet(); private static readonly uint ComponentId = ComponentDatabase.GetComponentId(); + private static readonly ComponentType ComponentAuthType = ComponentDatabase.GetMetaclass().Authority; protected WriterSubscriptionManager(World world) : base(world) { - componentUpdateSystem = world.GetExistingSystem(); + entityManager = World.EntityManager; RegisterComponentCallbacks(); } @@ -84,8 +85,7 @@ public override Subscription Subscribe(EntityId entityId) } if (WorkerSystem.TryGetEntity(entityId, out var entity) - && componentUpdateSystem.HasComponent(ComponentId, entityId) - && componentUpdateSystem.GetAuthority(entityId, ComponentId) != Authority.NotAuthoritative) + && entityManager.HasComponent(entity, ComponentAuthType)) { entitiesMatchingRequirements.Add(entityId); subscription.SetAvailable(CreateWriter(entity, entityId)); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Systems/ComponentUpdateSystem.cs b/workers/unity/Packages/io.improbable.gdk.core/Systems/ComponentUpdateSystem.cs index 5ec2bbeb4f..b0dd205b47 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Systems/ComponentUpdateSystem.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Systems/ComponentUpdateSystem.cs @@ -1,5 +1,4 @@ using System.Collections.Generic; -using Improbable.Worker.CInterop; using Unity.Entities; namespace Improbable.Gdk.Core @@ -12,7 +11,6 @@ public class ComponentUpdateSystem : ComponentSystem public void SendUpdate(in T update, EntityId entityId) where T : struct, ISpatialComponentUpdate { - worker.View.UpdateComponent(entityId, in update); worker.MessagesToSend.AddComponentUpdate(in update, entityId.Id); } @@ -72,26 +70,11 @@ public List GetComponentsRemoved(uint componentId) return manager.GetComponentsRemoved(); } - public Authority GetAuthority(EntityId entityId, uint componentId) - { - return worker.View.GetAuthority(entityId, componentId); - } - - public T GetComponent(EntityId entityId) where T : struct, ISpatialComponentSnapshot - { - return worker.View.GetComponent(entityId); - } - public void AcknowledgeAuthorityLoss(EntityId entityId, uint componentId) { worker.MessagesToSend.AcknowledgeAuthorityLoss(entityId.Id, componentId); } - public bool HasComponent(uint componentId, EntityId entityId) - { - return worker.View.HasComponent(entityId, componentId); - } - protected override void OnCreate() { base.OnCreate(); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Systems/EntitySystem.cs b/workers/unity/Packages/io.improbable.gdk.core/Systems/EntitySystem.cs index c30066c432..a2361571a0 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Systems/EntitySystem.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Systems/EntitySystem.cs @@ -17,8 +17,6 @@ public class EntitySystem : ComponentSystem private ProfilerMarker applyDiffMarker = new ProfilerMarker("EntitySystem.ApplyDiff"); - private WorkerSystem workerSystem; - public List GetEntitiesAdded() { return entitiesAdded; @@ -29,11 +27,6 @@ public List GetEntitiesRemoved() return entitiesRemoved; } - public HashSet GetEntitiesInView() - { - return workerSystem.View.GetEntityIds(); - } - internal void ApplyDiff(ViewDiff diff) { using (applyDiffMarker.Auto()) @@ -59,13 +52,6 @@ internal void ApplyDiff(ViewDiff diff) } } - protected override void OnCreate() - { - base.OnCreate(); - - workerSystem = World.GetExistingSystem(); - } - protected override void OnUpdate() { entitiesAdded.Clear(); diff --git a/workers/unity/Packages/io.improbable.gdk.core/Systems/SpatialOSReceiveSystem.cs b/workers/unity/Packages/io.improbable.gdk.core/Systems/SpatialOSReceiveSystem.cs index 29ecdba8e6..68202fa35c 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Systems/SpatialOSReceiveSystem.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Systems/SpatialOSReceiveSystem.cs @@ -12,7 +12,7 @@ namespace Improbable.Gdk.Core [UpdateInGroup(typeof(SpatialOSReceiveGroup.InternalSpatialOSReceiveGroup))] public class SpatialOSReceiveSystem : ComponentSystem { - private WorkerSystem worker; + private WorkerSystem workerSystem; private EcsViewSystem ecsViewSystem; private EntitySystem entitySystem; private NetworkStatisticsSystem networkStatisticsSystem; @@ -21,7 +21,7 @@ protected override void OnCreate() { base.OnCreate(); - worker = World.GetExistingSystem(); + workerSystem = World.GetExistingSystem(); ecsViewSystem = World.GetOrCreateSystem(); entitySystem = World.GetOrCreateSystem(); networkStatisticsSystem = World.GetOrCreateSystem(); @@ -31,17 +31,16 @@ protected override void OnUpdate() { try { - worker.Tick(); + workerSystem.Tick(); - var diff = worker.Diff; - worker.View.ApplyDiff(diff); + var diff = workerSystem.Diff; ecsViewSystem.ApplyDiff(diff); entitySystem.ApplyDiff(diff); networkStatisticsSystem.ApplyDiff(diff); } catch (Exception e) { - worker.LogDispatcher.HandleLog(LogType.Exception, new LogEvent("Exception:") + workerSystem.LogDispatcher.HandleLog(LogType.Exception, new LogEvent("Exception:") .WithException(e)); } } diff --git a/workers/unity/Packages/io.improbable.gdk.core/Systems/WorkerSystem.cs b/workers/unity/Packages/io.improbable.gdk.core/Systems/WorkerSystem.cs index 642f303573..1685a9da87 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Systems/WorkerSystem.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Systems/WorkerSystem.cs @@ -30,11 +30,11 @@ public class WorkerSystem : ComponentSystem /// public bool IsConnected => Worker.IsConnected; - internal readonly View View; + public IReadOnlyDictionary WorkerFlags => Worker.WorkerFlags; internal readonly Dictionary EntityIdToEntity = new Dictionary(); - internal WorkerInWorld Worker; + internal readonly WorkerInWorld Worker; internal ViewDiff Diff => Worker.ViewDiff; internal MessagesToSend MessagesToSend => Worker.MessagesToSend; @@ -50,7 +50,6 @@ public WorkerSystem(WorkerInWorld worker) WorkerType = worker.WorkerType; WorkerId = worker.WorkerId; Origin = worker.Origin; - View = worker.View; } /// @@ -94,6 +93,16 @@ public void SendLogMessage(string message, string loggerName, LogLevel logLevel, Worker.MessagesToSend.AddLogMessage(new LogMessageToSend(message, loggerName, logLevel, entityId?.Id)); } + /// + /// Gets the value for a given worker flag. + /// + /// The key of the worker flag. + /// The value of the flag, if it exists, null otherwise. + public string GetWorkerFlag(string key) + { + return Worker.GetWorkerFlag(key); + } + public void SendMetrics(Metrics metrics) { Worker.MessagesToSend.AddMetrics(metrics); diff --git a/workers/unity/Packages/io.improbable.gdk.core/View/View.cs b/workers/unity/Packages/io.improbable.gdk.core/View/View.cs deleted file mode 100644 index f5822e692d..0000000000 --- a/workers/unity/Packages/io.improbable.gdk.core/View/View.cs +++ /dev/null @@ -1,160 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using Improbable.Worker.CInterop; -using Unity.Profiling; - -namespace Improbable.Gdk.Core -{ - public class View - { - public IReadOnlyDictionary WorkerFlags => workerFlags; - - private readonly Dictionary typeToViewStorage = new Dictionary(); - private readonly Dictionary componentIdToViewStorage = new Dictionary(); - private readonly List viewStorages = new List(); - - private readonly HashSet entities = new HashSet(); - private readonly Dictionary workerFlags = new Dictionary(); - - private ProfilerMarker applyDiffMarker = new ProfilerMarker("View.ApplyDiff"); - - public View() - { - var types = ComponentDatabase.Metaclasses.Select(type => type.Value.ViewStorage); - foreach (var type in types) - { - var instance = (IViewStorage) Activator.CreateInstance(type); - - typeToViewStorage.Add(instance.GetSnapshotType(), instance); - typeToViewStorage.Add(instance.GetUpdateType(), instance); - componentIdToViewStorage.Add(instance.GetComponentId(), instance); - - viewStorages.Add(instance); - } - } - - public void UpdateComponent(EntityId entityId, in T update) where T : struct, ISpatialComponentUpdate - { - var storage = (IViewComponentUpdater) typeToViewStorage[typeof(T)]; - storage.ApplyUpdate(entityId.Id, in update); - } - - public HashSet GetEntityIds() - { - return entities; - } - - public bool HasEntity(EntityId entityId) - { - return entities.Contains(entityId); - } - - // TODO: Make ref readonly when we have a dictionary type with ref indexing semantics. - public T GetComponent(EntityId entityId) where T : struct, ISpatialComponentSnapshot - { - if (!HasComponent(entityId)) - { - throw new ArgumentException($"The view does not have entity with Entity ID: {entityId.Id} and component with ID: {ComponentDatabase.GetSnapshotComponentId()}"); - } - - var storage = (IViewComponentStorage) typeToViewStorage[typeof(T)]; - return storage.GetComponent(entityId.Id); - } - - public bool HasComponent(EntityId entityId) where T : struct, ISpatialComponentSnapshot - { - if (!HasEntity(entityId)) - { - return false; - } - - var storage = typeToViewStorage[typeof(T)]; - return storage.HasComponent(entityId.Id); - } - - public bool HasComponent(EntityId entityId, uint componentId) - { - if (!HasEntity(entityId)) - { - return false; - } - - var storage = componentIdToViewStorage[componentId]; - return storage.HasComponent(entityId.Id); - } - - public Authority GetAuthority(EntityId entityId) where T : struct, ISpatialComponentSnapshot - { - if (!HasEntity(entityId)) - { - throw new ArgumentException($"The view does not have entity with Entity ID: {entityId.Id}"); - } - - return typeToViewStorage[typeof(T)].GetAuthority(entityId.Id); - } - - public Authority GetAuthority(EntityId entityId, uint componentId) - { - if (!HasEntity(entityId)) - { - throw new ArgumentException($"The view does not have entity with Entity ID: {entityId.Id}"); - } - - return componentIdToViewStorage[componentId].GetAuthority(entityId.Id); - } - - public bool IsAuthoritative(EntityId entityId) where T : struct, ISpatialComponentSnapshot - { - if (!HasComponent(entityId)) - { - return false; - } - - return typeToViewStorage[typeof(T)].GetAuthority(entityId.Id) == Authority.Authoritative; - } - - public bool IsAuthoritative(EntityId entityId, uint componentId) - { - if (!HasComponent(entityId, componentId)) - { - return false; - } - - return componentIdToViewStorage[componentId].GetAuthority(entityId.Id) == Authority.Authoritative; - } - - public string GetWorkerFlag(string name) - { - return workerFlags.TryGetValue(name, out var value) ? value : null; - } - - internal void ApplyDiff(ViewDiff diff) - { - using (applyDiffMarker.Auto()) - { - var entitiesAdded = diff.GetEntitiesAdded(); - foreach (var entity in entitiesAdded) - { - entities.Add(entity); - } - - var entitiesRemoved = diff.GetEntitiesRemoved(); - foreach (var entity in entitiesRemoved) - { - entities.Remove(entity); - } - - foreach (var storage in viewStorages) - { - storage.ApplyDiff(diff); - } - - foreach (var pair in diff.GetWorkerFlagChanges()) - { - workerFlags[pair.Item1] = pair.Item2; - } - } - } - } -} diff --git a/workers/unity/Packages/io.improbable.gdk.core/View/View.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/View/View.cs.meta deleted file mode 100644 index f54ed6d8a0..0000000000 --- a/workers/unity/Packages/io.improbable.gdk.core/View/View.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 66956872847b3a34eb3136d138991431 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/workers/unity/Packages/io.improbable.gdk.core/View/ViewStorage.cs b/workers/unity/Packages/io.improbable.gdk.core/View/ViewStorage.cs deleted file mode 100644 index 56f0fc3369..0000000000 --- a/workers/unity/Packages/io.improbable.gdk.core/View/ViewStorage.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using Improbable.Worker.CInterop; - -namespace Improbable.Gdk.Core -{ - public interface IViewStorage - { - Type GetSnapshotType(); - Type GetUpdateType(); - uint GetComponentId(); - - bool HasComponent(long entityId); - Authority GetAuthority(long entityId); - - void ApplyDiff(ViewDiff viewDiff); - } - - public interface IViewComponentStorage where T : struct, ISpatialComponentSnapshot - { - T GetComponent(long entityId); - } - - public interface IViewComponentUpdater where T : struct, ISpatialComponentUpdate - { - void ApplyUpdate(long entityId, in T update); - } -} diff --git a/workers/unity/Packages/io.improbable.gdk.core/View/ViewStorage.cs.meta b/workers/unity/Packages/io.improbable.gdk.core/View/ViewStorage.cs.meta deleted file mode 100644 index 91e8ddea82..0000000000 --- a/workers/unity/Packages/io.improbable.gdk.core/View/ViewStorage.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 4ee4ba1a5f9a19446a900b72fd168c4d -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/workers/unity/Packages/io.improbable.gdk.core/Worker/Worker.cs b/workers/unity/Packages/io.improbable.gdk.core/Worker/Worker.cs index 99e91e92cc..fe4b8b09c3 100644 --- a/workers/unity/Packages/io.improbable.gdk.core/Worker/Worker.cs +++ b/workers/unity/Packages/io.improbable.gdk.core/Worker/Worker.cs @@ -40,14 +40,17 @@ public class Worker : IDisposable /// public bool IsConnected => ConnectionHandler.IsConnected(); + public IReadOnlyDictionary WorkerFlags => workerFlags; + protected IConnectionHandler ConnectionHandler; // todo replace internal with real apis - internal readonly View View; internal ViewDiff ViewDiff; internal MessagesToSend MessagesToSend; + private readonly Dictionary workerFlags = new Dictionary(); + protected Worker(IConnectionHandler connectionHandler, string workerType, ILogDispatcher logDispatcher) { ConnectionHandler = connectionHandler; @@ -60,7 +63,6 @@ protected Worker(IConnectionHandler connectionHandler, string workerType, ILogDi logDispatcher.WorkerType = workerType; MessagesToSend = connectionHandler.GetMessagesToSendContainer(); - View = new View(); } /// @@ -87,6 +89,12 @@ public static async Task CreateWorkerAsync(IConnectionHandlerBuilder con public void Tick() { ConnectionHandler.GetMessagesReceived(ref ViewDiff); + + // Update worker flags + foreach (var (key, value) in ViewDiff.GetWorkerFlagChanges()) + { + workerFlags[key] = value; + } } public void EnsureMessagesFlushed(NetFrameStats frameStats) @@ -118,7 +126,7 @@ public void SendLogMessage(LogLevel logLevel, string message, string loggerName, /// The value of the flag, if it exists, null otherwise. public string GetWorkerFlag(string key) { - return View.GetWorkerFlag(key); + return workerFlags.TryGetValue(key, out var value) ? value : null; } public virtual void Dispose() diff --git a/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/WorkerDetail.cs b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/WorkerDetail.cs index 31d6ae13aa..6a741a1fc4 100644 --- a/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/WorkerDetail.cs +++ b/workers/unity/Packages/io.improbable.gdk.debug/WorkerInspector/WorkerDetail.cs @@ -17,7 +17,7 @@ internal class WorkerDetail : VisualElement private readonly List> workerFlagData = new List>(); - private View view; + private WorkerSystem workerSystem; public WorkerDetail() { @@ -46,29 +46,27 @@ public void SetWorld(World world) workerId.value = ""; workerFlagData.Clear(); workerFlags.Refresh(); - view = null; + workerSystem = null; return; } - var workerSystem = world.GetExistingSystem(); + workerSystem = world.GetExistingSystem(); workerType.value = workerSystem.WorkerType; workerId.value = workerSystem.WorkerId; - - view = workerSystem.View; Update(); } public void Update() { - if (view == null) + if (workerSystem == null) { return; } workerFlagData.Clear(); - foreach (var pair in view.WorkerFlags) + foreach (var pair in workerSystem.WorkerFlags) { workerFlagData.Add(pair); } diff --git a/workers/unity/Packages/io.improbable.gdk.gameobjectcreation/GameObjectInitializationSystem.cs b/workers/unity/Packages/io.improbable.gdk.gameobjectcreation/GameObjectInitializationSystem.cs index 3e731de160..f74ed7722d 100644 --- a/workers/unity/Packages/io.improbable.gdk.gameobjectcreation/GameObjectInitializationSystem.cs +++ b/workers/unity/Packages/io.improbable.gdk.gameobjectcreation/GameObjectInitializationSystem.cs @@ -26,8 +26,6 @@ internal class GameObjectInitializationSystem : ComponentSystem private readonly GameObject workerGameObject; - private EntitySystem entitySystem; - private EntityQuery newEntitiesQuery; private EntityQuery removedEntitiesQuery; @@ -74,8 +72,6 @@ protected override void OnCreate() { base.OnCreate(); - entitySystem = World.GetExistingSystem(); - Linker = new EntityGameObjectLinker(World); if (workerGameObject != null) @@ -107,10 +103,10 @@ protected override void OnDestroy() Linker.UnlinkAllGameObjects(); - foreach (var entityId in entitySystem.GetEntitiesInView()) + Entities.ForEach((ref SpatialEntityId entityId) => { - gameObjectCreator.OnEntityRemoved(entityId); - } + gameObjectCreator.OnEntityRemoved(entityId.EntityId); + }); base.OnDestroy(); } diff --git a/workers/unity/Packages/io.improbable.gdk.playerlifecycle/PlayerLifecycleHelper.cs b/workers/unity/Packages/io.improbable.gdk.playerlifecycle/PlayerLifecycleHelper.cs index 5c3f8b8cde..270158b4c4 100644 --- a/workers/unity/Packages/io.improbable.gdk.playerlifecycle/PlayerLifecycleHelper.cs +++ b/workers/unity/Packages/io.improbable.gdk.playerlifecycle/PlayerLifecycleHelper.cs @@ -25,25 +25,24 @@ public static void AddPlayerLifecycleComponents(EntityTemplate template, public static bool IsOwningWorker(EntityId entityId, World workerWorld) { var worker = workerWorld.GetExistingSystem(); - var updateSystem = workerWorld.GetExistingSystem(); - var entitySystem = workerWorld.GetExistingSystem(); + var entityManager = workerWorld.EntityManager; if (worker == null) { throw new InvalidOperationException("Provided World does not have an associated worker"); } - if (!entitySystem.GetEntitiesInView().Contains(entityId)) + if (!worker.TryGetEntity(entityId, out var entity)) { return false; } - if (!updateSystem.HasComponent(OwningWorker.ComponentId, entityId)) + if (!entityManager.HasComponent(entity)) { return false; } - var ownerId = updateSystem.GetComponent(entityId).WorkerId; + var ownerId = entityManager.GetComponentData(entity).WorkerId; return worker.WorkerId == ownerId; } diff --git a/workers/unity/Packages/io.improbable.gdk.transformsynchronization/MonoBehaviours/TransformSynchronization.cs b/workers/unity/Packages/io.improbable.gdk.transformsynchronization/MonoBehaviours/TransformSynchronization.cs index a92e83551d..4cd103c1af 100644 --- a/workers/unity/Packages/io.improbable.gdk.transformsynchronization/MonoBehaviours/TransformSynchronization.cs +++ b/workers/unity/Packages/io.improbable.gdk.transformsynchronization/MonoBehaviours/TransformSynchronization.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using Improbable.Gdk.Core; using Improbable.Gdk.Subscriptions; -using Improbable.Worker.CInterop; using Unity.Collections; using Unity.Entities; using UnityEngine; @@ -39,7 +38,7 @@ public uint TickNumber var manager = world.EntityManager; - if (transformReader.Authority != Authority.NotAuthoritative) + if (transformReader.HasAuthority) { return manager.GetComponentData(entity) .NumberOfTicks + transformReader.Data.PhysicsTick;