From 73ea4568ab277493c6717fa004cf19f9b7aae449 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Wed, 5 Aug 2020 02:04:29 -0700 Subject: [PATCH 1/3] change Span returns to ReadOnlySpan --- encompass-cs/Collections/ComponentDeltaStore.cs | 2 +- encompass-cs/Collections/ComponentStore.cs | 4 ++-- encompass-cs/Collections/MessageStore.cs | 2 +- encompass-cs/Collections/TypedComponentStore.cs | 8 ++++---- encompass-cs/Collections/TypedMessageStore.cs | 4 ++-- encompass-cs/ComponentManager.cs | 14 +++++++------- encompass-cs/Engine.cs | 6 +++--- encompass-cs/MessageManager.cs | 2 +- encompass-cs/Renderer.cs | 4 ++-- 9 files changed, 23 insertions(+), 23 deletions(-) diff --git a/encompass-cs/Collections/ComponentDeltaStore.cs b/encompass-cs/Collections/ComponentDeltaStore.cs index 48d44c4..75a3b75 100644 --- a/encompass-cs/Collections/ComponentDeltaStore.cs +++ b/encompass-cs/Collections/ComponentDeltaStore.cs @@ -74,7 +74,7 @@ namespace Encompass } } - public Span AllEntities() where TComponent : struct + public ReadOnlySpan AllEntities() where TComponent : struct { return _store.AllEntities(); } diff --git a/encompass-cs/Collections/ComponentStore.cs b/encompass-cs/Collections/ComponentStore.cs index bfbc8af..986a5b0 100644 --- a/encompass-cs/Collections/ComponentStore.cs +++ b/encompass-cs/Collections/ComponentStore.cs @@ -113,12 +113,12 @@ namespace Encompass return Lookup().Count > 0; } - public Span All() where TComponent : struct + public ReadOnlySpan All() where TComponent : struct { return Lookup().AllComponents(); } - public Span AllEntities() where TComponent : struct + public ReadOnlySpan AllEntities() where TComponent : struct { return Lookup().AllEntities(); } diff --git a/encompass-cs/Collections/MessageStore.cs b/encompass-cs/Collections/MessageStore.cs index 3f57158..196dc7f 100644 --- a/encompass-cs/Collections/MessageStore.cs +++ b/encompass-cs/Collections/MessageStore.cs @@ -41,7 +41,7 @@ namespace Encompass return ref Lookup().First(); } - public Span All() where TMessage : struct, IMessage + public ReadOnlySpan All() where TMessage : struct, IMessage { return Lookup().All(); } diff --git a/encompass-cs/Collections/TypedComponentStore.cs b/encompass-cs/Collections/TypedComponentStore.cs index 232fa2b..83cd785 100644 --- a/encompass-cs/Collections/TypedComponentStore.cs +++ b/encompass-cs/Collections/TypedComponentStore.cs @@ -127,14 +127,14 @@ namespace Encompass _priorities.Clear(); } - public Span AllEntities() + public ReadOnlySpan AllEntities() { - return new Span(_storageIndexToEntities, 0, _nextID); + return new ReadOnlySpan(_storageIndexToEntities, 0, _nextID); } - public Span AllComponents() + public ReadOnlySpan AllComponents() { - return new Span(_components, 0, _nextID); + return new ReadOnlySpan(_components, 0, _nextID); } } } diff --git a/encompass-cs/Collections/TypedMessageStore.cs b/encompass-cs/Collections/TypedMessageStore.cs index c8a263e..a9f222b 100644 --- a/encompass-cs/Collections/TypedMessageStore.cs +++ b/encompass-cs/Collections/TypedMessageStore.cs @@ -92,9 +92,9 @@ namespace Encompass return _nextIndex != 0; } - public Span All() + public ReadOnlySpan All() { - return new Span(_store, 0, _nextIndex); + return new ReadOnlySpan(_store, 0, _nextIndex); } public IEnumerable WithEntity(int entityID) diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 49fe78d..868ba05 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -98,7 +98,7 @@ namespace Encompass // existing or immediate reads - internal Span ReadExistingAndImmediateComponentsByType() where TComponent : struct + internal ReadOnlySpan ReadExistingAndImmediateComponentsByType() where TComponent : struct { return _upToDateComponentStore.All(); } @@ -108,7 +108,7 @@ namespace Encompass return ref _upToDateComponentStore.Singular(); } - internal Span GetExistingAndImmediateEntities() where TComponent : struct + internal ReadOnlySpan GetExistingAndImmediateEntities() where TComponent : struct { return _upToDateComponentStore.AllEntities(); } @@ -125,7 +125,7 @@ namespace Encompass // existing reads - internal Span GetExistingComponents() where TComponent : struct + internal ReadOnlySpan GetExistingComponents() where TComponent : struct { return _existingComponentStore.All(); } @@ -135,7 +135,7 @@ namespace Encompass return ref _existingComponentStore.Singular(); } - internal Span GetExistingEntities() where TComponent : struct + internal ReadOnlySpan GetExistingEntities() where TComponent : struct { return _existingComponentStore.AllEntities(); } @@ -152,7 +152,7 @@ namespace Encompass // immediate reads - internal Span ReadImmediateComponentsByType() where TComponent : struct + internal ReadOnlySpan ReadImmediateComponentsByType() where TComponent : struct { return _immediateComponentStore.All(); } @@ -162,7 +162,7 @@ namespace Encompass return ref _immediateComponentStore.Singular(); } - internal Span GetImmediateEntities() where TComponent : struct + internal ReadOnlySpan GetImmediateEntities() where TComponent : struct { return _immediateComponentStore.AllEntities(); } @@ -226,7 +226,7 @@ namespace Encompass return _immediateComponentStore.Has(type, entityID); } - internal Span GetComponentsByType() where TComponent : struct + internal ReadOnlySpan GetComponentsByType() where TComponent : struct { return _existingComponentStore.All(); } diff --git a/encompass-cs/Engine.cs b/encompass-cs/Engine.cs index 548d7f6..46abe4d 100644 --- a/encompass-cs/Engine.cs +++ b/encompass-cs/Engine.cs @@ -223,7 +223,7 @@ namespace Encompass /// /// Returns all Entities containing the specified Component type. /// - protected Span ReadEntities() where TComponent : struct, IComponent + protected ReadOnlySpan ReadEntities() where TComponent : struct, IComponent { var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent)); @@ -273,7 +273,7 @@ namespace Encompass /// /// Returns all of the Components with the specified Component Type. /// - protected Span ReadComponents() where TComponent : struct, IComponent + protected ReadOnlySpan ReadComponents() where TComponent : struct, IComponent { var immediateRead = ReadImmediateTypes.Contains(typeof(TComponent)); var existingRead = ReadTypes.Contains(typeof(TComponent)); @@ -550,7 +550,7 @@ namespace Encompass /// /// Thrown when the Engine does not declare that it Receives the specified Message Type. /// - protected Span ReadMessages() where TMessage : struct, IMessage + protected ReadOnlySpan ReadMessages() where TMessage : struct, IMessage { CheckMessageRead(); return _messageManager.GetMessagesByType(); diff --git a/encompass-cs/MessageManager.cs b/encompass-cs/MessageManager.cs index 234f46f..73f4c20 100644 --- a/encompass-cs/MessageManager.cs +++ b/encompass-cs/MessageManager.cs @@ -38,7 +38,7 @@ namespace Encompass _messageStore.ProcessDelayedMessages(dt * _timeManager.TimeDilationFactor, dt); } - internal Span GetMessagesByType() where TMessage : struct, IMessage + internal ReadOnlySpan GetMessagesByType() where TMessage : struct, IMessage { return _messageStore.All(); } diff --git a/encompass-cs/Renderer.cs b/encompass-cs/Renderer.cs index 85b5489..697e377 100644 --- a/encompass-cs/Renderer.cs +++ b/encompass-cs/Renderer.cs @@ -18,7 +18,7 @@ namespace Encompass _componentManager = componentManager; } - protected Span ReadEntities() where TComponent : struct, IComponent + protected ReadOnlySpan ReadEntities() where TComponent : struct, IComponent { return _componentManager.GetExistingEntities(); } @@ -28,7 +28,7 @@ namespace Encompass return ref _componentManager.ExistingSingularEntity(); } - protected Span ReadComponents() where TComponent : struct, IComponent + protected ReadOnlySpan ReadComponents() where TComponent : struct, IComponent { return _componentManager.GetComponentsByType(); } -- 2.25.1 From 0a30dab3c2a8ef4ce435af8123bcb64efe4926d8 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Wed, 5 Aug 2020 12:22:03 -0700 Subject: [PATCH 2/3] expose some enumerable iterators on Renderer --- encompass-cs/Collections/ComponentStore.cs | 10 ++++++++++ encompass-cs/Collections/TypedComponentStore.cs | 10 ++++++++++ encompass-cs/ComponentManager.cs | 10 ++++++++++ encompass-cs/Renderer.cs | 10 ++++++++++ 4 files changed, 40 insertions(+) diff --git a/encompass-cs/Collections/ComponentStore.cs b/encompass-cs/Collections/ComponentStore.cs index 986a5b0..99b85d3 100644 --- a/encompass-cs/Collections/ComponentStore.cs +++ b/encompass-cs/Collections/ComponentStore.cs @@ -118,11 +118,21 @@ namespace Encompass return Lookup().AllComponents(); } + public IEnumerable AllAsEnumerable() where TComponent : struct + { + return Lookup().AllComponentsAsEnumerable(); + } + public ReadOnlySpan AllEntities() where TComponent : struct { return Lookup().AllEntities(); } + public IEnumerable AllEntitiesAsEnumerable() where TComponent : struct + { + return Lookup().AllEntitiesAsEnumerable(); + } + public void Clear() where TComponent : struct { Lookup().Clear(); diff --git a/encompass-cs/Collections/TypedComponentStore.cs b/encompass-cs/Collections/TypedComponentStore.cs index 83cd785..5b837c6 100644 --- a/encompass-cs/Collections/TypedComponentStore.cs +++ b/encompass-cs/Collections/TypedComponentStore.cs @@ -132,9 +132,19 @@ namespace Encompass return new ReadOnlySpan(_storageIndexToEntities, 0, _nextID); } + public IEnumerable AllEntitiesAsEnumerable() + { + return new ArraySegment(_storageIndexToEntities, 0, _nextID); + } + public ReadOnlySpan AllComponents() { return new ReadOnlySpan(_components, 0, _nextID); } + + public IEnumerable AllComponentsAsEnumerable() + { + return new ArraySegment(_components, 0, _nextID); + } } } diff --git a/encompass-cs/ComponentManager.cs b/encompass-cs/ComponentManager.cs index 868ba05..da54e8e 100644 --- a/encompass-cs/ComponentManager.cs +++ b/encompass-cs/ComponentManager.cs @@ -140,6 +140,11 @@ namespace Encompass return _existingComponentStore.AllEntities(); } + internal IEnumerable GetExistingEntitiesAsEnumerable() where TComponent : struct + { + return _existingComponentStore.AllEntitiesAsEnumerable(); + } + internal ref readonly Entity ExistingSingularEntity() where TComponent : struct { return ref _existingComponentStore.SingularEntity(); @@ -231,6 +236,11 @@ namespace Encompass return _existingComponentStore.All(); } + internal IEnumerable GetComponentsByTypeEnumerable() where TComponent : struct + { + return _existingComponentStore.AllAsEnumerable(); + } + internal ref readonly TComponent GetComponentByEntityAndType(int entityID) where TComponent : struct { return ref _existingComponentStore.Get(entityID); diff --git a/encompass-cs/Renderer.cs b/encompass-cs/Renderer.cs index 697e377..7891489 100644 --- a/encompass-cs/Renderer.cs +++ b/encompass-cs/Renderer.cs @@ -23,6 +23,11 @@ namespace Encompass return _componentManager.GetExistingEntities(); } + protected IEnumerable ReadEntitiesAsEnumerable() where TComponent : struct, IComponent + { + return _componentManager.GetExistingEntitiesAsEnumerable(); + } + protected ref readonly Entity ReadEntity() where TComponent : struct, IComponent { return ref _componentManager.ExistingSingularEntity(); @@ -33,6 +38,11 @@ namespace Encompass return _componentManager.GetComponentsByType(); } + protected IEnumerable ReadComponentsAsEnumerable() where TComponent : struct, IComponent + { + return _componentManager.GetComponentsByTypeEnumerable(); + } + protected ref readonly TComponent ReadComponent() where TComponent : struct, IComponent { return ref _componentManager.ExistingSingular(); -- 2.25.1 From bdd9f595493609444d8ebb31b9d07b7ef320eafb Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Thu, 6 Aug 2020 14:11:28 -0700 Subject: [PATCH 3/3] fix worldBuilder being able to set a non-component --- encompass-cs/WorldBuilder.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/encompass-cs/WorldBuilder.cs b/encompass-cs/WorldBuilder.cs index 7da9ced..2fe307e 100644 --- a/encompass-cs/WorldBuilder.cs +++ b/encompass-cs/WorldBuilder.cs @@ -82,7 +82,7 @@ namespace Encompass /// /// Sets Component data for the specified Component Type on the specified Entity. /// - public void SetComponent(Entity entity, in TComponent component) where TComponent : struct + public void SetComponent(Entity entity, in TComponent component) where TComponent : struct, IComponent { RegisterComponentType(); _startingExistingComponentStore.Set(entity.ID, component); @@ -95,7 +95,7 @@ namespace Encompass } } - internal void RegisterComponentType() where TComponent : struct + internal void RegisterComponentType() where TComponent : struct, IComponent { if (!_typeToIndex.ContainsKey(typeof(TComponent))) { -- 2.25.1