fix changes

This commit is contained in:
Mikhail 2023-04-21 01:05:06 +08:00
parent 212d45f778
commit 905cd3033a
7 changed files with 105 additions and 88 deletions

View File

@ -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<T> : 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<T> : IEcsReadonlyPool where T : struct
{
public ref readonly T Read(int entityID);
}
public interface IEcsPool<T> : IEcsPool, IEcsReadonlyPool<T> where T : struct
{
public ref T Add(int entityID);
public ref T Write(int entityID);
}
public abstract class EcsPoolBase<T> : 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<NullComponent>
public sealed class EcsNullPool : EcsPoolBase<NullComponent>
{
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<T> : IEcsPool<T>
public sealed class EcsPool<T> : EcsPoolBase<T>
where T : struct
{
public static EcsPool<T> Builder(EcsWorld source)
{
return new EcsPool<T>(source, 512, default);
return new EcsPool<T>(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<T>.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<T>(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
}
}

View File

@ -53,16 +53,16 @@ namespace DCFApixels.DragonECS
public override inc_<TComponent> Include<TComponent>() where TComponent : struct
{
_inc.Add(_world.GetComponentID<TComponent>());
return new inc_<TComponent>(_world.GetPool<TComponent>());
return new inc_<TComponent>(_world.GetOrCreatePool<TComponent>());
}
public override exc_<TComponent> Exclude<TComponent>() where TComponent : struct
{
_exc.Add(_world.GetComponentID<TComponent>());
return new exc_<TComponent>(_world.GetPool<TComponent>());
return new exc_<TComponent>(_world.GetOrCreatePool<TComponent>());
}
public override opt_<TComponent> Optional<TComponent>() where TComponent : struct
{
return new opt_<TComponent>(_world.GetPool<TComponent>());
return new opt_<TComponent>(_world.GetOrCreatePool<TComponent>());
}
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<Edge>();
attachPool = World.GetOrCreatePool<Edge>();
}
public sealed override void Execute()
{

View File

@ -6,7 +6,7 @@ namespace DCFApixels.DragonECS
public interface IEcsQueryMember { }
public interface IEcsQueryReadonlyField<TComponent> : IEcsQueryMember
{
public ref TComponent Read(ent entityID);
public ref readonly TComponent Read(ent entityID);
public bool Has(ent entityID);
}
public interface IEcsQueryField<TComponent> : IEcsQueryReadonlyField<TComponent>
@ -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)]

View File

@ -51,7 +51,6 @@ namespace DCFApixels.DragonECS
private List<WeakReference<EcsGroup>> _groups;
private Stack<EcsGroup> _groupsPool = new Stack<EcsGroup>(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<IEcsEntityCreate>();
_entityDestry = _pipeline.GetRunner<IEcsEntityDestroy>();
_pipeline.GetRunner<IEcsInject<EcsWorld>>().Inject(this);
@ -123,7 +121,7 @@ namespace DCFApixels.DragonECS
#endregion
#region GetPool
public EcsPool<TComponent> GetPool<TComponent>() where TComponent : struct
public IEcsPool<TComponent> GetOrCreatePool<TComponent>(Func<EcsWorld, IEcsPool> builder) where TComponent : struct
{
int uniqueID = WorldMetaStorage.GetComponentId<TComponent>(_worldArchetypeID);
@ -135,9 +133,9 @@ namespace DCFApixels.DragonECS
}
if (_pools[uniqueID] == _nullPool)
_pools[uniqueID] = new EcsPool<TComponent>(this, 512, _poolRunners);
_pools[uniqueID] = builder(this);
return (EcsPool<TComponent>)_pools[uniqueID];
return (IEcsPool<TComponent>)_pools[uniqueID];
}
#endregion

View File

@ -91,37 +91,37 @@ namespace DCFApixels.DragonECS
where T : struct
{
//using (_ReadMarker.Auto())
return ref EcsWorld.Worlds[world].GetPool<T>().Read(id);
return ref EcsWorld.Worlds[world].GetOrCreatePool<T>().Read(id);
}
public ref T Add<T>()
where T : struct
{
return ref EcsWorld.Worlds[world].GetPool<T>().Add(id);
return ref EcsWorld.Worlds[world].GetOrCreatePool<T>().Add(id);
}
public ref T Write<T>()
where T : struct
{
//using (_WriteMarker.Auto())
return ref EcsWorld.Worlds[world].GetPool<T>().Write(id);
return ref EcsWorld.Worlds[world].GetOrCreatePool<T>().Write(id);
}
public bool Has<T>()
where T : struct
{
//using (_HasMarker.Auto())
return EcsWorld.Worlds[world].GetPool<T>().Has(id);
return EcsWorld.Worlds[world].GetOrCreatePool<T>().Has(id);
}
public bool NotHas<T>()
where T : struct
{
//using (_HasMarker.Auto())
return EcsWorld.Worlds[world].GetPool<T>().Has(id);
return EcsWorld.Worlds[world].GetOrCreatePool<T>().Has(id);
}
public void Del<T>()
where T : struct
{
//using (_DelMarker.Auto())
EcsWorld.Worlds[world].GetPool<T>().Del(id);
EcsWorld.Worlds[world].GetOrCreatePool<T>().Del(id);
}
}

View File

@ -15,7 +15,7 @@ namespace DCFApixels.DragonECS
#region Methods
public int GetComponentID<T>();
public EcsPool<T> GetPool<T>() where T : struct;
public IEcsPool<TComponent> GetOrCreatePool<TComponent>(Func<EcsWorld, IEcsPool> builder) where TComponent : struct;
public ReadOnlySpan<IEcsPool> GetAllPools();
public TQuery Where<TQuery>(out TQuery query) where TQuery : EcsQueryBase;
public TQuery Select<TQuery>() where TQuery : EcsQueryBase;

View File

@ -40,53 +40,48 @@ namespace DCFApixels.DragonECS.Test
}
public interface IPool { }
public class Pool1<TComponent> : IEcsPool<TComponent>
public class Pool1<TComponent> : EcsPoolBase<TComponent>
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();
}