refactoring

This commit is contained in:
Mikhail 2023-05-28 05:53:08 +08:00
parent 5c805ba555
commit 42a8be7345
10 changed files with 50 additions and 75 deletions

View File

@ -6,10 +6,10 @@ namespace DCFApixels.DragonECS
namespace Internal namespace Internal
{ {
[DebugHide, DebugColor(DebugColor.Black)] [DebugHide, DebugColor(DebugColor.Black)]
public class SystemsBlockMarkerSystem : IEcsSystem public class SystemsLayerMarkerSystem : IEcsSystem
{ {
public readonly string name; public readonly string name;
public SystemsBlockMarkerSystem(string name) => this.name = name; public SystemsLayerMarkerSystem(string name) => this.name = name;
} }
[DebugHide, DebugColor(DebugColor.Grey)] [DebugHide, DebugColor(DebugColor.Grey)]
public class DeleteEmptyEntitesSystem : IEcsRunProcess, IEcsPreInject public class DeleteEmptyEntitesSystem : IEcsRunProcess, IEcsPreInject

View File

@ -1,9 +1,5 @@
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
internal sealed class EcsNullWorld : EcsWorld<EcsNullWorld>
{
public EcsNullWorld() : base(false) { }
}
public sealed class EcsDefaultWorld : EcsWorld<EcsDefaultWorld> { } public sealed class EcsDefaultWorld : EcsWorld<EcsDefaultWorld> { }
public sealed class EcsEventWorld : EcsWorld<EcsEventWorld> { } public sealed class EcsEventWorld : EcsWorld<EcsEventWorld> { }
} }

View File

@ -17,7 +17,6 @@ namespace DCFApixels.DragonECS
public void End() => EcsDebug.ProfileMarkEnd(id); public void End() => EcsDebug.ProfileMarkEnd(id);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public AutoScope Auto() => new AutoScope(id); public AutoScope Auto() => new AutoScope(id);
public readonly ref struct AutoScope public readonly ref struct AutoScope
{ {
private readonly int _id; private readonly int _id;
@ -146,7 +145,6 @@ namespace DCFApixels.DragonECS
{ {
private Stopwatch[] _stopwatchs; private Stopwatch[] _stopwatchs;
private string[] _stopwatchsNames; private string[] _stopwatchsNames;
public DefaultDebugService() public DefaultDebugService()
{ {
#if !DISABLE_DRAGONECS_DEBUGGER #if !DISABLE_DRAGONECS_DEBUGGER
@ -154,17 +152,14 @@ namespace DCFApixels.DragonECS
_stopwatchsNames= new string[64]; _stopwatchsNames= new string[64];
#endif #endif
} }
public override void Print(string tag, object v) public override void Print(string tag, object v)
{ {
Console.WriteLine($"[{tag}] {v}"); Console.WriteLine($"[{tag}] {v}");
} }
public override void ProfileMarkBegin(int id) public override void ProfileMarkBegin(int id)
{ {
_stopwatchs[id].Start(); _stopwatchs[id].Start();
} }
public override void ProfileMarkEnd(int id) public override void ProfileMarkEnd(int id)
{ {
_stopwatchs[id].Stop(); _stopwatchs[id].Stop();
@ -172,12 +167,10 @@ namespace DCFApixels.DragonECS
_stopwatchs[id].Reset(); _stopwatchs[id].Reset();
Print(_stopwatchsNames[id] + " s:" + time.TotalSeconds); Print(_stopwatchsNames[id] + " s:" + time.TotalSeconds);
} }
protected override void OnDelMark(int id) protected override void OnDelMark(int id)
{ {
_stopwatchs[id] = null; _stopwatchs[id] = null;
} }
protected override void OnNewMark(int id, string name) protected override void OnNewMark(int id, string name)
{ {
if (id >= _stopwatchs.Length) if (id >= _stopwatchs.Length)

View File

@ -182,7 +182,7 @@ namespace DCFApixels.DragonECS
List<IEcsSystem> list; List<IEcsSystem> list;
if (!_systems.TryGetValue(layerName, out list)) if (!_systems.TryGetValue(layerName, out list))
{ {
list = new List<IEcsSystem> { new SystemsBlockMarkerSystem(layerName.ToString()) }; list = new List<IEcsSystem> { new SystemsLayerMarkerSystem(layerName.ToString()) };
_systems.Add(layerName, list); _systems.Add(layerName, list);
} }
if ((_uniqueTypes.Add(system.GetType()) == false && isUnique)) if ((_uniqueTypes.Add(system.GetType()) == false && isUnique))

View File

@ -3,7 +3,6 @@ using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -28,7 +27,6 @@ namespace DCFApixels.DragonECS
namespace RunnersCore namespace RunnersCore
{ {
[EditorBrowsable(EditorBrowsableState.Never)]
public interface IEcsRunner public interface IEcsRunner
{ {
EcsPipeline Source { get; } EcsPipeline Source { get; }
@ -121,7 +119,9 @@ namespace DCFApixels.DragonECS
EcsRunner<TInterface>.Register(runnerType); EcsRunner<TInterface>.Register(runnerType);
} }
} }
[EditorBrowsable(EditorBrowsableState.Never)] #if UNITY_2020_3_OR_NEWER
[UnityEngine.Scripting.RequireDerived, UnityEngine.Scripting.Preserve]
#endif
public abstract class EcsRunner<TInterface> : IEcsSystem, IEcsRunner public abstract class EcsRunner<TInterface> : IEcsSystem, IEcsRunner
where TInterface : IEcsSystem where TInterface : IEcsSystem
{ {

View File

@ -67,6 +67,7 @@ namespace DCFApixels.DragonECS
return (TSubject)newSubject; return (TSubject)newSubject;
} }
#region Include/Exclude/Optional
public sealed override TPool Include<TComponent, TPool>() public sealed override TPool Include<TComponent, TPool>()
{ {
IncludeImplicit<TComponent>(); IncludeImplicit<TComponent>();
@ -81,7 +82,6 @@ namespace DCFApixels.DragonECS
{ {
return _world.GetPool<TComponent, TPool>(); return _world.GetPool<TComponent, TPool>();
} }
public void IncludeImplicit<TComponent>() public void IncludeImplicit<TComponent>()
{ {
int id = _world.GetComponentID<TComponent>(); int id = _world.GetComponentID<TComponent>();
@ -98,6 +98,7 @@ namespace DCFApixels.DragonECS
#endif #endif
_exc.Add(_world.GetComponentID<TComponent>()); _exc.Add(_world.GetComponentID<TComponent>());
} }
#endregion
private void End(out EcsMask mask) private void End(out EcsMask mask)
{ {
@ -108,6 +109,20 @@ namespace DCFApixels.DragonECS
_inc = null; _inc = null;
_exc = null; _exc = null;
} }
#region SupportReflectionHack
#if UNITY_2020_3_OR_NEWER
[UnityEngine.Scripting.Preserve]
#endif
private void SupportReflectionHack<TComponent, TPool>() where TPool : IEcsPoolImplementation<TComponent>, new()
{
Include<TComponent, TPool>();
Exclude<TComponent, TPool>();
Optional<TComponent, TPool>();
IncludeImplicit<TComponent>();
ExcludeImplicit<TComponent>();
}
#endregion
} }
#endregion #endregion
} }
@ -195,7 +210,7 @@ namespace DCFApixels.DragonECS
public EcsSubjectIterator(TSubject s, EcsReadonlyGroup sourceGroup) public EcsSubjectIterator(TSubject s, EcsReadonlyGroup sourceGroup)
{ {
this.s = s; this.s = s;
this._sourceGroup = sourceGroup; _sourceGroup = sourceGroup;
_enumerator = default; _enumerator = default;
} }

View File

@ -92,7 +92,6 @@ namespace DCFApixels.DragonECS
_subjects = new EcsSubject[128]; _subjects = new EcsSubject[128];
_executors = new EcsQueryExecutor[128]; _executors = new EcsQueryExecutor[128];
} }
public void Destroy() public void Destroy()
{ {
_entityDispenser = null; _entityDispenser = null;
@ -106,10 +105,6 @@ namespace DCFApixels.DragonECS
Worlds[uniqueID] = null; Worlds[uniqueID] = null;
_worldIdDispenser.Release(uniqueID); _worldIdDispenser.Release(uniqueID);
} }
public void DestryWithPipeline()
{
Destroy();
}
#endregion #endregion
#region GetComponentID #region GetComponentID
@ -118,17 +113,18 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region GetPool #region GetPool
#if UNITY_2020_3_OR_NEWER
[UnityEngine.Scripting.Preserve]
#endif
public TPool GetPool<TComponent, TPool>() where TPool : IEcsPoolImplementation<TComponent>, new() public TPool GetPool<TComponent, TPool>() where TPool : IEcsPoolImplementation<TComponent>, new()
{ {
int uniqueID = WorldMetaStorage.GetComponentId<TComponent>(_worldTypeID); int uniqueID = WorldMetaStorage.GetComponentId<TComponent>(_worldTypeID);
if (uniqueID >= _pools.Length) if (uniqueID >= _pools.Length)
{ {
int oldCapacity = _pools.Length; int oldCapacity = _pools.Length;
Array.Resize(ref _pools, _pools.Length << 1); Array.Resize(ref _pools, _pools.Length << 1);
ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length); ArrayUtility.Fill(_pools, _nullPool, oldCapacity, oldCapacity - _pools.Length);
} }
if (_pools[uniqueID] == _nullPool) if (_pools[uniqueID] == _nullPool)
{ {
var pool = new TPool(); var pool = new TPool();
@ -136,7 +132,6 @@ namespace DCFApixels.DragonECS
pool.OnInit(this, uniqueID); pool.OnInit(this, uniqueID);
_poolsCount++; _poolsCount++;
} }
return (TPool)_pools[uniqueID]; return (TPool)_pools[uniqueID];
} }
#endregion #endregion
@ -167,7 +162,6 @@ namespace DCFApixels.DragonECS
} }
return (TExecutor)_executors[id]; return (TExecutor)_executors[id];
} }
public EcsWhereResult<TSubject> WhereFor<TSubject>(EcsReadonlyGroup sourceGroup, out TSubject subject) where TSubject : EcsSubject public EcsWhereResult<TSubject> WhereFor<TSubject>(EcsReadonlyGroup sourceGroup, out TSubject subject) where TSubject : EcsSubject
{ {
var executor = GetExecutor<EcsWhereExecutor<TSubject>>(); var executor = GetExecutor<EcsWhereExecutor<TSubject>>();
@ -366,6 +360,13 @@ namespace DCFApixels.DragonECS
} }
} }
#endregion #endregion
#region NullWorld
private sealed class EcsNullWorld : EcsWorld<EcsNullWorld>
{
public EcsNullWorld() : base(false) { }
}
#endregion
} }
public abstract class EcsWorld<TWorldArchetype> : EcsWorld public abstract class EcsWorld<TWorldArchetype> : EcsWorld
@ -382,7 +383,7 @@ namespace DCFApixels.DragonECS
private static List<Resizer> _resizer = new List<Resizer>(); private static List<Resizer> _resizer = new List<Resizer>();
private static int _tokenCount = 0; private static int _tokenCount = 0;
private static WorldMeta[] _metas = new WorldMeta[0]; private static WorldTypeMeta[] _metas = new WorldTypeMeta[0];
private static Dictionary<Type, int> _worldIds = new Dictionary<Type, int>(); private static Dictionary<Type, int> _worldIds = new Dictionary<Type, int>();
private static class WorldIndex<TWorldArchetype> private static class WorldIndex<TWorldArchetype>
{ {
@ -390,7 +391,7 @@ namespace DCFApixels.DragonECS
} }
private static int GetToken() private static int GetToken()
{ {
WorldMeta meta = new WorldMeta(); WorldTypeMeta meta = new WorldTypeMeta();
meta.id = _tokenCount; meta.id = _tokenCount;
Array.Resize(ref _metas, ++_tokenCount); Array.Resize(ref _metas, ++_tokenCount);
_metas[_tokenCount - 1] = meta; _metas[_tokenCount - 1] = meta;
@ -498,17 +499,14 @@ namespace DCFApixels.DragonECS
} }
} }
private class WorldMeta private class WorldTypeMeta
{ {
public int id; public int id;
public int componentCount; public int componentCount;
public int subjectsCount; public int subjectsCount;
public int executorsCount; public int executorsCount;
private Type[] types; private Type[] types;
private HashSet<Type> declaredComponentTypes; private HashSet<Type> declaredComponentTypes;
public void AddType(int id, Type type) public void AddType(int id, Type type)
{ {
if(types.Length <= id) if(types.Length <= id)
@ -517,11 +515,9 @@ namespace DCFApixels.DragonECS
declaredComponentTypes.Add(type); declaredComponentTypes.Add(type);
} }
public Type GetComponentType(int componentID) => types[componentID]; public Type GetComponentType(int componentID) => types[componentID];
public bool IsDeclaredType(Type type) => declaredComponentTypes.Contains(type); public bool IsDeclaredType(Type type) => declaredComponentTypes.Contains(type);
public WorldTypeMeta()
public WorldMeta()
{ {
types = new Type[10]; types = new Type[10];
declaredComponentTypes = new HashSet<Type>(); declaredComponentTypes = new HashSet<Type>();

View File

@ -35,27 +35,7 @@
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
if (sourceGroup.IsNull) throw new System.ArgumentNullException();//TODO составить текст исключения. if (sourceGroup.IsNull) throw new System.ArgumentNullException();//TODO составить текст исключения.
#endif #endif
//_subject.GetIteratorFor(sourceGroup).CopyTo(_filteredGroup); _subject.GetIteratorFor(sourceGroup).CopyTo(_filteredGroup);
var pools = _subject.World._pools;
var mask = _subject.Mask;
_filteredGroup.Clear();
foreach (var e in sourceGroup)
{
for (int i = 0, iMax = mask._inc.Length; i < iMax; i++)
{
if (!pools[mask._inc[i]].Has(e))
goto next;
}
for (int i = 0, iMax = mask._exc.Length; i < iMax; i++)
{
if (pools[mask._exc[i]].Has(e))
goto next;
}
_filteredGroup.AddInternal(e);
next: continue;
}
return new EcsWhereResult<TSubject>(this, _filteredGroup.Readonly); return new EcsWhereResult<TSubject>(this, _filteredGroup.Readonly);
} }
} }
@ -70,7 +50,6 @@
public readonly EcsReadonlyGroup group; public readonly EcsReadonlyGroup group;
private readonly long _version; private readonly long _version;
public bool IsRelevant => _version == _executer.ExecuteVersion; public bool IsRelevant => _version == _executer.ExecuteVersion;
public EcsWhereResult(EcsWhereExecutor<TSubject> executer, EcsReadonlyGroup group) public EcsWhereResult(EcsWhereExecutor<TSubject> executer, EcsReadonlyGroup group)
{ {
_executer = executer; _executer = executer;
@ -85,7 +64,6 @@
#endif #endif
return group.GetEnumerator(); return group.GetEnumerator();
} }
public override string ToString() public override string ToString()
{ {
return group.ToString(); return group.ToString();

View File

@ -31,15 +31,6 @@ namespace DCFApixels.DragonECS
void RemoveListener(IEcsPoolEventListener listener); void RemoveListener(IEcsPoolEventListener listener);
#endregion #endregion
} }
public interface IEcsPoolEventListener
{
/// <summary>Called after adding an entity to the pool, but before changing values.</summary>
void OnAdd(int entityID);
/// <summary>Is called when EcsPool.Write or EcsPool.Add is called, but before changing values.</summary>
void OnWrite(int entityID);
/// <summary>Called after deleting an entity from the pool</summary>
void OnDel(int entityID);
}
public interface IEcsPool<T> public interface IEcsPool<T>
{ {
ref T Add(int entityID); ref T Add(int entityID);
@ -47,7 +38,6 @@ namespace DCFApixels.DragonECS
ref T Write(int entityID); ref T Write(int entityID);
} }
/// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary> /// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary>
/// <typeparam name="T">Component type</typeparam>
public interface IEcsPoolImplementation : IEcsPool public interface IEcsPoolImplementation : IEcsPool
{ {
void OnInit(EcsWorld world, int componentID); void OnInit(EcsWorld world, int componentID);
@ -98,7 +88,7 @@ namespace DCFApixels.DragonECS
} }
} }
#region Dummy #region Dummy EcsNullPool
namespace Internal namespace Internal
{ {
public struct NullComponent { } public struct NullComponent { }
@ -216,7 +206,16 @@ namespace DCFApixels.DragonECS
} }
#endregion #endregion
#region Extensions #region Callbacks Interface
public interface IEcsPoolEventListener
{
/// <summary>Called after adding an entity to the pool, but before changing values.</summary>
void OnAdd(int entityID);
/// <summary>Is called when EcsPool.Write or EcsPool.Add is called, but before changing values.</summary>
void OnWrite(int entityID);
/// <summary>Called after deleting an entity from the pool</summary>
void OnDel(int entityID);
}
public static class PoolEventListExtensions public static class PoolEventListExtensions
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -1,6 +1,5 @@
#pragma warning disable IDE1006 #pragma warning disable IDE1006
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
@ -36,7 +35,6 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get => this == NULL; get => this == NULL;
} }
public int ID public int ID
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]