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
{
[DebugHide, DebugColor(DebugColor.Black)]
public class SystemsBlockMarkerSystem : IEcsSystem
public class SystemsLayerMarkerSystem : IEcsSystem
{
public readonly string name;
public SystemsBlockMarkerSystem(string name) => this.name = name;
public SystemsLayerMarkerSystem(string name) => this.name = name;
}
[DebugHide, DebugColor(DebugColor.Grey)]
public class DeleteEmptyEntitesSystem : IEcsRunProcess, IEcsPreInject

View File

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

View File

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

View File

@ -182,7 +182,7 @@ namespace DCFApixels.DragonECS
List<IEcsSystem> 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);
}
if ((_uniqueTypes.Add(system.GetType()) == false && isUnique))

View File

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

View File

@ -67,6 +67,7 @@ namespace DCFApixels.DragonECS
return (TSubject)newSubject;
}
#region Include/Exclude/Optional
public sealed override TPool Include<TComponent, TPool>()
{
IncludeImplicit<TComponent>();
@ -81,7 +82,6 @@ namespace DCFApixels.DragonECS
{
return _world.GetPool<TComponent, TPool>();
}
public void IncludeImplicit<TComponent>()
{
int id = _world.GetComponentID<TComponent>();
@ -98,6 +98,7 @@ namespace DCFApixels.DragonECS
#endif
_exc.Add(_world.GetComponentID<TComponent>());
}
#endregion
private void End(out EcsMask mask)
{
@ -108,6 +109,20 @@ namespace DCFApixels.DragonECS
_inc = 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
}
@ -195,7 +210,7 @@ namespace DCFApixels.DragonECS
public EcsSubjectIterator(TSubject s, EcsReadonlyGroup sourceGroup)
{
this.s = s;
this._sourceGroup = sourceGroup;
_sourceGroup = sourceGroup;
_enumerator = default;
}

View File

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

View File

@ -35,27 +35,7 @@
#if (DEBUG && !DISABLE_DEBUG) || !DISABLE_DRAGONECS_ASSERT_CHEKS
if (sourceGroup.IsNull) throw new System.ArgumentNullException();//TODO составить текст исключения.
#endif
//_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;
}
_subject.GetIteratorFor(sourceGroup).CopyTo(_filteredGroup);
return new EcsWhereResult<TSubject>(this, _filteredGroup.Readonly);
}
}
@ -70,7 +50,6 @@
public readonly EcsReadonlyGroup group;
private readonly long _version;
public bool IsRelevant => _version == _executer.ExecuteVersion;
public EcsWhereResult(EcsWhereExecutor<TSubject> executer, EcsReadonlyGroup group)
{
_executer = executer;
@ -85,7 +64,6 @@
#endif
return group.GetEnumerator();
}
public override string ToString()
{
return group.ToString();

View File

@ -31,15 +31,6 @@ namespace DCFApixels.DragonECS
void RemoveListener(IEcsPoolEventListener listener);
#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>
{
ref T Add(int entityID);
@ -47,7 +38,6 @@ namespace DCFApixels.DragonECS
ref T Write(int entityID);
}
/// <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
{
void OnInit(EcsWorld world, int componentID);
@ -98,7 +88,7 @@ namespace DCFApixels.DragonECS
}
}
#region Dummy
#region Dummy EcsNullPool
namespace Internal
{
public struct NullComponent { }
@ -216,7 +206,16 @@ namespace DCFApixels.DragonECS
}
#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
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

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