From 42a8be73455988b12f38b5d9e772b22b98311981 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 28 May 2023 05:53:08 +0800 Subject: [PATCH] refactoring --- src/Builtin/Systems.cs | 4 ++-- src/Builtin/Worlds.cs | 4 ---- src/Debug/EcsDebug.cs | 7 ------- src/EcsPipeline.cs | 2 +- src/EcsRunner.cs | 8 ++++---- src/EcsSubject.cs | 19 ++++++++++++++++-- src/EcsWorld.cs | 32 ++++++++++++++----------------- src/Executors/EcsWhereExecutor.cs | 24 +---------------------- src/Pools/EcsPoolBase.cs | 23 +++++++++++----------- src/entlong.cs | 2 -- 10 files changed, 50 insertions(+), 75 deletions(-) diff --git a/src/Builtin/Systems.cs b/src/Builtin/Systems.cs index 15ff527..5f2fc45 100644 --- a/src/Builtin/Systems.cs +++ b/src/Builtin/Systems.cs @@ -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 diff --git a/src/Builtin/Worlds.cs b/src/Builtin/Worlds.cs index 23f26cb..d92e45e 100644 --- a/src/Builtin/Worlds.cs +++ b/src/Builtin/Worlds.cs @@ -1,9 +1,5 @@ namespace DCFApixels.DragonECS { - internal sealed class EcsNullWorld : EcsWorld - { - public EcsNullWorld() : base(false) { } - } public sealed class EcsDefaultWorld : EcsWorld { } public sealed class EcsEventWorld : EcsWorld { } } diff --git a/src/Debug/EcsDebug.cs b/src/Debug/EcsDebug.cs index 57bbc1e..a2f8d2a 100644 --- a/src/Debug/EcsDebug.cs +++ b/src/Debug/EcsDebug.cs @@ -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) diff --git a/src/EcsPipeline.cs b/src/EcsPipeline.cs index 2d51e6e..2af0ecd 100644 --- a/src/EcsPipeline.cs +++ b/src/EcsPipeline.cs @@ -182,7 +182,7 @@ namespace DCFApixels.DragonECS List list; if (!_systems.TryGetValue(layerName, out list)) { - list = new List { new SystemsBlockMarkerSystem(layerName.ToString()) }; + list = new List { new SystemsLayerMarkerSystem(layerName.ToString()) }; _systems.Add(layerName, list); } if ((_uniqueTypes.Add(system.GetType()) == false && isUnique)) diff --git a/src/EcsRunner.cs b/src/EcsRunner.cs index 910e4f3..40fd715 100644 --- a/src/EcsRunner.cs +++ b/src/EcsRunner.cs @@ -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.Register(runnerType); } } - [EditorBrowsable(EditorBrowsableState.Never)] - public abstract class EcsRunner : IEcsSystem, IEcsRunner +#if UNITY_2020_3_OR_NEWER + [UnityEngine.Scripting.RequireDerived, UnityEngine.Scripting.Preserve] +#endif + public abstract class EcsRunner : IEcsSystem, IEcsRunner where TInterface : IEcsSystem { #region Register diff --git a/src/EcsSubject.cs b/src/EcsSubject.cs index af1e44f..a1f60b3 100644 --- a/src/EcsSubject.cs +++ b/src/EcsSubject.cs @@ -67,6 +67,7 @@ namespace DCFApixels.DragonECS return (TSubject)newSubject; } + #region Include/Exclude/Optional public sealed override TPool Include() { IncludeImplicit(); @@ -81,7 +82,6 @@ namespace DCFApixels.DragonECS { return _world.GetPool(); } - public void IncludeImplicit() { int id = _world.GetComponentID(); @@ -98,6 +98,7 @@ namespace DCFApixels.DragonECS #endif _exc.Add(_world.GetComponentID()); } + #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() where TPool : IEcsPoolImplementation, new() + { + Include(); + Exclude(); + Optional(); + IncludeImplicit(); + ExcludeImplicit(); + } + #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; } diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index c9f833b..42d4aab 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -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() where TPool : IEcsPoolImplementation, new() { int uniqueID = WorldMetaStorage.GetComponentId(_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 WhereFor(EcsReadonlyGroup sourceGroup, out TSubject subject) where TSubject : EcsSubject { var executor = GetExecutor>(); @@ -366,6 +360,13 @@ namespace DCFApixels.DragonECS } } #endregion + + #region NullWorld + private sealed class EcsNullWorld : EcsWorld + { + public EcsNullWorld() : base(false) { } + } + #endregion } public abstract class EcsWorld : EcsWorld @@ -382,7 +383,7 @@ namespace DCFApixels.DragonECS private static List _resizer = new List(); private static int _tokenCount = 0; - private static WorldMeta[] _metas = new WorldMeta[0]; + private static WorldTypeMeta[] _metas = new WorldTypeMeta[0]; private static Dictionary _worldIds = new Dictionary(); private static class WorldIndex { @@ -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 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(); diff --git a/src/Executors/EcsWhereExecutor.cs b/src/Executors/EcsWhereExecutor.cs index db88c69..4673825 100644 --- a/src/Executors/EcsWhereExecutor.cs +++ b/src/Executors/EcsWhereExecutor.cs @@ -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(this, _filteredGroup.Readonly); } } @@ -70,7 +50,6 @@ public readonly EcsReadonlyGroup group; private readonly long _version; public bool IsRelevant => _version == _executer.ExecuteVersion; - public EcsWhereResult(EcsWhereExecutor executer, EcsReadonlyGroup group) { _executer = executer; @@ -85,7 +64,6 @@ #endif return group.GetEnumerator(); } - public override string ToString() { return group.ToString(); diff --git a/src/Pools/EcsPoolBase.cs b/src/Pools/EcsPoolBase.cs index 2e28565..1628aeb 100644 --- a/src/Pools/EcsPoolBase.cs +++ b/src/Pools/EcsPoolBase.cs @@ -31,15 +31,6 @@ namespace DCFApixels.DragonECS void RemoveListener(IEcsPoolEventListener listener); #endregion } - public interface IEcsPoolEventListener - { - /// Called after adding an entity to the pool, but before changing values. - void OnAdd(int entityID); - /// Is called when EcsPool.Write or EcsPool.Add is called, but before changing values. - void OnWrite(int entityID); - /// Called after deleting an entity from the pool - void OnDel(int entityID); - } public interface IEcsPool { ref T Add(int entityID); @@ -47,7 +38,6 @@ namespace DCFApixels.DragonECS ref T Write(int entityID); } /// Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool. - /// Component type 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 + { + /// Called after adding an entity to the pool, but before changing values. + void OnAdd(int entityID); + /// Is called when EcsPool.Write or EcsPool.Add is called, but before changing values. + void OnWrite(int entityID); + /// Called after deleting an entity from the pool + void OnDel(int entityID); + } public static class PoolEventListExtensions { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/entlong.cs b/src/entlong.cs index bdbca2f..5ce81df 100644 --- a/src/entlong.cs +++ b/src/entlong.cs @@ -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)]