From 905cd3033a516aaf466646d26416ef6cfc5bff5a Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 21 Apr 2023 01:05:06 +0800 Subject: [PATCH] fix changes --- src/EcsPool.cs | 126 +++++++++++++++++++++--------------- src/EcsQuery.cs | 8 +-- src/EcsQueryMember.cs | 8 +-- src/EcsWorld.cs | 8 +-- src/Entities/EcsEntity.cs | 12 ++-- src/Interfaces/IEcsTable.cs | 2 +- src/TestPool.cs | 29 ++++----- 7 files changed, 105 insertions(+), 88 deletions(-) diff --git a/src/EcsPool.cs b/src/EcsPool.cs index f3e856c..f8478fc 100644 --- a/src/EcsPool.cs +++ b/src/EcsPool.cs @@ -4,7 +4,7 @@ using Unity.Profiling; namespace DCFApixels.DragonECS { - public interface IEcsPool + public interface IEcsPoolBase { #region Properties public Type ComponentType { get; } @@ -14,24 +14,58 @@ namespace DCFApixels.DragonECS #endregion #region Methods - public void Add(int entityID); - public void Write(int entityID, object data); public bool Has(int entityID); - public void Del(int entityID); - #endregion - - #region Internal - internal void OnWorldResize(int newSize); #endregion } - public interface IEcsPool : IEcsPool where T : struct + public interface IEcsReadonlyPool : IEcsPoolBase { - public new ref T Add(int entityID); + #region Methods + public object Get(int entityID); + #endregion + } + public interface IEcsPool : IEcsReadonlyPool + { + #region Methods + public void AddOrWrite(int entityID, object data); + public void Del(int entityID); + #endregion + } + public interface IEcsReadonlyPool : IEcsReadonlyPool where T : struct + { + public ref readonly T Read(int entityID); + } + public interface IEcsPool : IEcsPool, IEcsReadonlyPool where T : struct + { + public ref T Add(int entityID); public ref T Write(int entityID); } + + public abstract class EcsPoolBase : IEcsPoolBase + where T : struct + { + #region Properties + public abstract Type ComponentType { get; } + public abstract EcsWorld World { get; } + public abstract int Count { get; } + public abstract int Capacity { get; } + #endregion + + #region Methods + public abstract bool Has(int entityID); + + protected abstract void OnWorldResize(int newSize); + protected abstract void OnDestroy(); + #endregion + + #region Internal + internal void InvokeOnWorldResize(int newSize) => OnWorldResize(newSize); + internal void InvokeOnDestroy() => OnDestroy(); + #endregion + } + public struct NullComponent { } - public sealed class EcsNullPool : IEcsPool + public sealed class EcsNullPool : EcsPoolBase { public static EcsNullPool instance => new EcsNullPool(null); private EcsWorld _source; @@ -39,31 +73,32 @@ namespace DCFApixels.DragonECS private EcsNullPool(EcsWorld source) => _source = source; #region Properties - public Type ComponentType => typeof(NullComponent); - public int ComponentID => -1; - public EcsWorld World => _source; - public int Count => 0; - public int Capacity => 1; + public sealed override Type ComponentType => typeof(NullComponent); + public sealed override EcsWorld World => _source; + public sealed override int Count => 0; + public sealed override int Capacity => 1; #endregion #region Methods - public ref NullComponent Add(int entity) => ref fakeComponent; - public bool Has(int index) => false; - public ref NullComponent Read(int entity) => ref fakeComponent; - public ref NullComponent Write(int entity) => ref fakeComponent; - public void Del(int index) { } - void IEcsPool.Write(int entityID, object data) { } - void IEcsPool.Add(int entityID) { } - void IEcsPool.OnWorldResize(int newSize) { } + public sealed override ref NullComponent Add(int entity) => ref fakeComponent; + public sealed override bool Has(int index) => false; + public sealed override ref readonly NullComponent Read(int entity) => ref fakeComponent; + public sealed override ref NullComponent Write(int entity) => ref fakeComponent; + public sealed override void Del(int index) { } + #endregion + + #region WorldCallbacks + protected override void OnWorldResize(int newSize) { } + protected override void OnDestroy() { } #endregion } - public sealed class EcsPool : IEcsPool + public sealed class EcsPool : EcsPoolBase where T : struct { public static EcsPool Builder(EcsWorld source) { - return new EcsPool(source, 512, default); + return new EcsPool(source, 512, new PoolRunners(source.Pipeline)); } private readonly EcsWorld _source; @@ -78,14 +113,14 @@ namespace DCFApixels.DragonECS private PoolRunners _poolRunners; #region Properites - public int Count => _itemsCount; - public int Capacity => _items.Length; - public EcsWorld World => _source; - public Type ComponentType => typeof(T); + public sealed override int Count => _itemsCount; + public sealed override int Capacity => _items.Length; + public sealed override EcsWorld World => _source; + public sealed override Type ComponentType => typeof(T); #endregion #region Constructors - internal EcsPool(EcsWorld source, int capacity, PoolRunners poolRunnres) + internal EcsPool(EcsWorld source, int capacity, PoolRunners poolRunners) { _source = source; @@ -96,7 +131,7 @@ namespace DCFApixels.DragonECS _itemsCount = 0; _componentResetHandler = EcsComponentResetHandler.instance; - _poolRunners = poolRunnres; + _poolRunners = poolRunners; } #endregion @@ -106,7 +141,7 @@ namespace DCFApixels.DragonECS private ProfilerMarker _readMark = new ProfilerMarker("EcsPoo.Read"); private ProfilerMarker _hasMark = new ProfilerMarker("EcsPoo.Has"); private ProfilerMarker _delMark = new ProfilerMarker("EcsPoo.Del"); - public ref T Add(int entityID) + public sealed override ref T Add(int entityID) { // using (_addMark.Auto()) // { @@ -133,25 +168,25 @@ namespace DCFApixels.DragonECS // } } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref T Write(int entityID) + public sealed override ref T Write(int entityID) { // using (_writeMark.Auto()) _poolRunners.write.OnComponentWrite(entityID); return ref _items[_mapping[entityID]]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref T Read(int entityID) + public sealed override ref readonly T Read(int entityID) { // using (_readMark.Auto()) return ref _items[_mapping[entityID]]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Has(int entityID) + public sealed override bool Has(int entityID) { // using (_hasMark.Auto()) return _mapping[entityID] > 0; } - public void Del(int entityID) + public sealed override void Del(int entityID) { // using (_delMark.Auto()) // { @@ -167,23 +202,12 @@ namespace DCFApixels.DragonECS } #endregion - #region IEcsPool - void IEcsPool.Write(int entityID, object data) - { - Write(entityID) = (T)data; - } - void IEcsPool.Add(int entityID) - { - Add(entityID); - } - #endregion - - #region Internal - [MethodImpl(MethodImplOptions.AggressiveInlining)] - void IEcsPool.OnWorldResize(int newSize) + #region WorldCallbacks + protected override void OnWorldResize(int newSize) { Array.Resize(ref _mapping, newSize); } + protected override void OnDestroy() { } #endregion } } diff --git a/src/EcsQuery.cs b/src/EcsQuery.cs index a48ae83..93cedb1 100644 --- a/src/EcsQuery.cs +++ b/src/EcsQuery.cs @@ -53,16 +53,16 @@ namespace DCFApixels.DragonECS public override inc_ Include() where TComponent : struct { _inc.Add(_world.GetComponentID()); - return new inc_(_world.GetPool()); + return new inc_(_world.GetOrCreatePool()); } public override exc_ Exclude() where TComponent : struct { _exc.Add(_world.GetComponentID()); - return new exc_(_world.GetPool()); + return new exc_(_world.GetOrCreatePool()); } public override opt_ Optional() where TComponent : struct { - return new opt_(_world.GetPool()); + return new opt_(_world.GetOrCreatePool()); } private void End(out EcsQueryMask mask) @@ -93,7 +93,7 @@ namespace DCFApixels.DragonECS private ProfilerMarker _getEnumerator = new ProfilerMarker("EcsGraphQuery.Execute"); protected sealed override void OnBuildAfter() { - attachPool = World.GetPool(); + attachPool = World.GetOrCreatePool(); } public sealed override void Execute() { diff --git a/src/EcsQueryMember.cs b/src/EcsQueryMember.cs index f5293c5..c892ddb 100644 --- a/src/EcsQueryMember.cs +++ b/src/EcsQueryMember.cs @@ -6,7 +6,7 @@ namespace DCFApixels.DragonECS public interface IEcsQueryMember { } public interface IEcsQueryReadonlyField : IEcsQueryMember { - public ref TComponent Read(ent entityID); + public ref readonly TComponent Read(ent entityID); public bool Has(ent entityID); } public interface IEcsQueryField : IEcsQueryReadonlyField @@ -30,7 +30,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref TComponent Write(ent entityID) => ref pool.Write(entityID.id); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref TComponent Read(ent entityID) => ref pool.Read(entityID.id); + public ref readonly TComponent Read(ent entityID) => ref pool.Read(entityID.id); [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Has(ent entityID) => pool.Has(entityID.id); [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -51,7 +51,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref TComponent Write(ent entityID) => ref pool.Write(entityID.id); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref TComponent Read(ent entityID) => ref pool.Read(entityID.id); + public ref readonly TComponent Read(ent entityID) => ref pool.Read(entityID.id); [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Has(ent entityID) => pool.Has(entityID.id); [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -72,7 +72,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref TComponent Write(ent entityID) => ref pool.Write(entityID.id); [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref TComponent Read(ent entityID) => ref pool.Read(entityID.id); + public ref readonly TComponent Read(ent entityID) => ref pool.Read(entityID.id); [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Has(ent entityID) => pool.Has(entityID.id); [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 979cdac..6dc90ef 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -51,7 +51,6 @@ namespace DCFApixels.DragonECS private List> _groups; private Stack _groupsPool = new Stack(64); - private PoolRunners _poolRunners; private IEcsEntityCreate _entityCreate; private IEcsEntityDestroy _entityDestry; @@ -97,7 +96,6 @@ namespace DCFApixels.DragonECS _queries = new EcsQuery[128]; - _poolRunners = new PoolRunners(_pipeline); _entityCreate = _pipeline.GetRunner(); _entityDestry = _pipeline.GetRunner(); _pipeline.GetRunner>().Inject(this); @@ -123,7 +121,7 @@ namespace DCFApixels.DragonECS #endregion #region GetPool - public EcsPool GetPool() where TComponent : struct + public IEcsPool GetOrCreatePool(Func builder) where TComponent : struct { int uniqueID = WorldMetaStorage.GetComponentId(_worldArchetypeID); @@ -135,9 +133,9 @@ namespace DCFApixels.DragonECS } if (_pools[uniqueID] == _nullPool) - _pools[uniqueID] = new EcsPool(this, 512, _poolRunners); + _pools[uniqueID] = builder(this); - return (EcsPool)_pools[uniqueID]; + return (IEcsPool)_pools[uniqueID]; } #endregion diff --git a/src/Entities/EcsEntity.cs b/src/Entities/EcsEntity.cs index 6cae0a6..48ecf2f 100644 --- a/src/Entities/EcsEntity.cs +++ b/src/Entities/EcsEntity.cs @@ -91,37 +91,37 @@ namespace DCFApixels.DragonECS where T : struct { //using (_ReadMarker.Auto()) - return ref EcsWorld.Worlds[world].GetPool().Read(id); + return ref EcsWorld.Worlds[world].GetOrCreatePool().Read(id); } public ref T Add() where T : struct { - return ref EcsWorld.Worlds[world].GetPool().Add(id); + return ref EcsWorld.Worlds[world].GetOrCreatePool().Add(id); } public ref T Write() where T : struct { //using (_WriteMarker.Auto()) - return ref EcsWorld.Worlds[world].GetPool().Write(id); + return ref EcsWorld.Worlds[world].GetOrCreatePool().Write(id); } public bool Has() where T : struct { //using (_HasMarker.Auto()) - return EcsWorld.Worlds[world].GetPool().Has(id); + return EcsWorld.Worlds[world].GetOrCreatePool().Has(id); } public bool NotHas() where T : struct { //using (_HasMarker.Auto()) - return EcsWorld.Worlds[world].GetPool().Has(id); + return EcsWorld.Worlds[world].GetOrCreatePool().Has(id); } public void Del() where T : struct { //using (_DelMarker.Auto()) - EcsWorld.Worlds[world].GetPool().Del(id); + EcsWorld.Worlds[world].GetOrCreatePool().Del(id); } } diff --git a/src/Interfaces/IEcsTable.cs b/src/Interfaces/IEcsTable.cs index 3842ab6..c8eb368 100644 --- a/src/Interfaces/IEcsTable.cs +++ b/src/Interfaces/IEcsTable.cs @@ -15,7 +15,7 @@ namespace DCFApixels.DragonECS #region Methods public int GetComponentID(); - public EcsPool GetPool() where T : struct; + public IEcsPool GetOrCreatePool(Func builder) where TComponent : struct; public ReadOnlySpan GetAllPools(); public TQuery Where(out TQuery query) where TQuery : EcsQueryBase; public TQuery Select() where TQuery : EcsQueryBase; diff --git a/src/TestPool.cs b/src/TestPool.cs index 3caeb13..8030aab 100644 --- a/src/TestPool.cs +++ b/src/TestPool.cs @@ -40,53 +40,48 @@ namespace DCFApixels.DragonECS.Test } public interface IPool { } - public class Pool1 : IEcsPool + public class Pool1 : EcsPoolBase where TComponent : struct { - public Type ComponentType => throw new NotImplementedException(); + public override Type ComponentType => throw new NotImplementedException(); - public EcsWorld World => throw new NotImplementedException(); + public override EcsWorld World => throw new NotImplementedException(); - public int Count => throw new NotImplementedException(); + public override int Count => throw new NotImplementedException(); - public int Capacity => throw new NotImplementedException(); + public override int Capacity => throw new NotImplementedException(); - public ref TComponent Add(int entityID) + public override ref TComponent Add(int entityID) { throw new NotImplementedException(); } - public void Del(int entityID) + public override void Del(int entityID) { throw new NotImplementedException(); } - public bool Has(int entityID) + public override bool Has(int entityID) { throw new NotImplementedException(); } - public ref TComponent Read(int entityID) + public override ref readonly TComponent Read(int entityID) { throw new NotImplementedException(); } - public ref TComponent Write(int entityID) + public override ref TComponent Write(int entityID) { throw new NotImplementedException(); } - void IEcsPool.Add(int entityID) + protected override void OnDestroy() { throw new NotImplementedException(); } - void IEcsPool.OnWorldResize(int newSize) - { - throw new NotImplementedException(); - } - - void IEcsPool.Write(int entityID, object data) + protected override void OnWorldResize(int newSize) { throw new NotImplementedException(); }