From 841bdf0608c83a44601d425547a0005e0d74b0a1 Mon Sep 17 00:00:00 2001 From: Simon Schmid Date: Thu, 27 Jul 2023 22:03:55 +0200 Subject: [PATCH] Remove DesperateDevs dependencies from Entitas project --- .../Entitas.Unity.Editor.csproj | 1 + src/Entitas/Context/Context.cs | 19 ++-- src/Entitas/Context/ContextExtension.cs | 23 ----- src/Entitas/Context/IContext.cs | 2 +- src/Entitas/Entitas.csproj | 5 - .../PublicMemberInfoEntityExtension.cs | 29 ------ .../PublicMemberInfoEntityExtensionTests.cs | 97 ------------------- 7 files changed, 14 insertions(+), 162 deletions(-) delete mode 100644 src/Entitas/Context/ContextExtension.cs delete mode 100644 src/Entitas/Extensions/PublicMemberInfoEntityExtension.cs delete mode 100644 tests/Entitas.Tests/PublicMemberInfoEntityExtensionTests.cs diff --git a/src/Entitas.Unity.Editor/Entitas.Unity.Editor.csproj b/src/Entitas.Unity.Editor/Entitas.Unity.Editor.csproj index ac5158013..6dfef8b6a 100644 --- a/src/Entitas.Unity.Editor/Entitas.Unity.Editor.csproj +++ b/src/Entitas.Unity.Editor/Entitas.Unity.Editor.csproj @@ -6,6 +6,7 @@ + diff --git a/src/Entitas/Context/Context.cs b/src/Entitas/Context/Context.cs index bac44ce90..eab903a33 100644 --- a/src/Entitas/Context/Context.cs +++ b/src/Entitas/Context/Context.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using DesperateDevs.Caching; namespace Entitas { @@ -108,17 +107,16 @@ public Context(int totalComponents, int startCreationIndex, ContextInfo contextI _componentPools = new Stack[totalComponents]; _entityIndexes = new Dictionary(); - var groupChangedListPool = new ObjectPool>>( - () => new List>(), - list => list.Clear() - ); - + var groupChangedListPool = new Stack>>(); _onEntityChangedDelegate = (entity, index, component) => { var groups = _groupsForIndex[index]; if (groups != null) { - var events = groupChangedListPool.Get(); + var events = groupChangedListPool.Count != 0 + ? groupChangedListPool.Pop() + : new List>(); + var tEntity = (TEntity)entity; for (var i = 0; i < groups.Count; i++) @@ -127,6 +125,7 @@ public Context(int totalComponents, int startCreationIndex, ContextInfo contextI for (var i = 0; i < events.Count; i++) events[i]?.Invoke(groups[i], tEntity, index, component); + events.Clear(); groupChangedListPool.Push(events); } }; @@ -238,6 +237,12 @@ public TEntity[] GetEntities() return _entitiesCache ??= _entities.ToArray(); } + /// Returns all entities matching the specified matcher. + public TEntity[] GetEntities(IMatcher matcher) + { + return GetGroup(matcher).GetEntities(); + } + /// Returns a group for the specified matcher. /// Calling context.GetGroup(matcher) with the same matcher will always /// return the same instance of the group. diff --git a/src/Entitas/Context/ContextExtension.cs b/src/Entitas/Context/ContextExtension.cs deleted file mode 100644 index 1342c2d70..000000000 --- a/src/Entitas/Context/ContextExtension.cs +++ /dev/null @@ -1,23 +0,0 @@ -namespace Entitas -{ - public static class ContextExtension - { - /// Returns all entities matching the specified matcher. - public static TEntity[] GetEntities(this IContext context, IMatcher matcher) where TEntity : Entity => - context.GetGroup(matcher).GetEntities(); - - /// Creates a new entity and adds copies of all - /// specified components to it. - /// If replaceExisting is true it will replace exising components. - public static TEntity CloneEntity(this IContext context, - Entity entity, - bool replaceExisting = false, - params int[] indexes) - where TEntity : Entity - { - var target = context.CreateEntity(); - entity.CopyTo(target, replaceExisting, indexes); - return target; - } - } -} diff --git a/src/Entitas/Context/IContext.cs b/src/Entitas/Context/IContext.cs index 01ca50eb2..b825bfbfd 100644 --- a/src/Entitas/Context/IContext.cs +++ b/src/Entitas/Context/IContext.cs @@ -41,7 +41,7 @@ public interface IContext : IContext where TEntity : Entity bool HasEntity(TEntity entity); TEntity[] GetEntities(); - + TEntity[] GetEntities(IMatcher matcher); IGroup GetGroup(IMatcher matcher); } } diff --git a/src/Entitas/Entitas.csproj b/src/Entitas/Entitas.csproj index 2a63b5e90..85e734f6e 100644 --- a/src/Entitas/Entitas.csproj +++ b/src/Entitas/Entitas.csproj @@ -12,9 +12,4 @@ - - - - - diff --git a/src/Entitas/Extensions/PublicMemberInfoEntityExtension.cs b/src/Entitas/Extensions/PublicMemberInfoEntityExtension.cs deleted file mode 100644 index 1495bfb84..000000000 --- a/src/Entitas/Extensions/PublicMemberInfoEntityExtension.cs +++ /dev/null @@ -1,29 +0,0 @@ -using DesperateDevs.Reflection; - -namespace Entitas -{ - public static class PublicMemberInfoEntityExtension - { - /// Adds copies of all specified components to the target entity. - /// If replaceExisting is true it will replace exising components. - public static void CopyTo(this Entity entity, Entity target, bool replaceExisting = false, params int[] indexes) - { - var componentIndexes = indexes.Length == 0 - ? entity.GetComponentIndexes() - : indexes; - - for (var i = 0; i < componentIndexes.Length; i++) - { - var index = componentIndexes[i]; - var component = entity.GetComponent(index); - var clonedComponent = target.CreateComponent(index, component.GetType()); - component.CopyPublicMemberValues(clonedComponent); - - if (replaceExisting) - target.ReplaceComponent(index, clonedComponent); - else - target.AddComponent(index, clonedComponent); - } - } - } -} diff --git a/tests/Entitas.Tests/PublicMemberInfoEntityExtensionTests.cs b/tests/Entitas.Tests/PublicMemberInfoEntityExtensionTests.cs deleted file mode 100644 index 15641d834..000000000 --- a/tests/Entitas.Tests/PublicMemberInfoEntityExtensionTests.cs +++ /dev/null @@ -1,97 +0,0 @@ -using FluentAssertions; -using Xunit; - -namespace Entitas.Tests -{ - public class PublicMemberInfoEntityExtensionTests - { - readonly TestEntity _entity; - readonly TestEntity _target; - readonly UserComponent _user; - - public PublicMemberInfoEntityExtensionTests() - { - var context = new TestContext(CID.TotalComponents); - _entity = context.CreateEntity(); - _target = context.CreateEntity(); - _user = new UserComponent { Name = "Max", Age = 42 }; - } - - [Fact] - public void DoesNotChangeEntityIfOriginalDoesNotHaveAnyComponents() - { - _entity.CopyTo(_target); - _entity.Id.Should().Be(0); - _target.Id.Should().Be(1); - _target.GetComponents().Length.Should().Be(0); - } - - [Fact] - public void AddsCopiesOfAllComponentsToTargetEntity() - { - _entity.AddComponentA(); - _entity.AddComponent(CID.ComponentB, _user); - _entity.CopyTo(_target); - - _target.GetComponents().Length.Should().Be(2); - _target.HasComponentA().Should().BeTrue(); - _target.HasComponentB().Should().BeTrue(); - _target.GetComponentA().Should().NotBeSameAs(Component.A); - _target.GetComponent(CID.ComponentB).Should().NotBeSameAs(_user); - - var clonedComponent = (UserComponent)_target.GetComponent(CID.ComponentB); - clonedComponent.Name.Should().Be(_user.Name); - clonedComponent.Age.Should().Be(_user.Age); - } - - [Fact] - public void ThrowsWhenTargetAlreadyHasComponentAtIndex() - { - _entity.AddComponentA(); - _entity.AddComponent(CID.ComponentB, _user); - var component = new UserComponent(); - _target.AddComponent(CID.ComponentB, component); - FluentActions.Invoking(() => _entity.CopyTo(_target)) - .Should().Throw(); - } - - [Fact] - public void ReplacesExistingComponentsWhenOverwriteIsSet() - { - _entity.AddComponentA(); - _entity.AddComponent(CID.ComponentB, _user); - var component = new UserComponent(); - _target.AddComponent(CID.ComponentB, component); - _entity.CopyTo(_target, true); - - var copy = _target.GetComponent(CID.ComponentB); - copy.Should().NotBeSameAs(_user); - copy.Should().NotBeSameAs(component); - ((UserComponent)copy).Name.Should().Be(_user.Name); - ((UserComponent)copy).Age.Should().Be(_user.Age); - } - - [Fact] - public void OnlyAddsCopiesOfSpecifiedComponentsToTargetEntity() - { - _entity.AddComponentA(); - _entity.AddComponentB(); - _entity.AddComponentC(); - _entity.CopyTo(_target, false, CID.ComponentB, CID.ComponentC); - - _target.GetComponents().Length.Should().Be(2); - _target.HasComponentB().Should().BeTrue(); - _target.HasComponentC().Should().BeTrue(); - } - - [Fact] - public void UsesComponentPool() - { - _entity.AddComponentA(); - var component = new ComponentA(); - _target.GetComponentPool(CID.ComponentA).Push(component); - _entity.CopyTo(_target); - _target.GetComponentA().Should().BeSameAs(component); - } - } -}