From d2f761a884628b0514d1610fb10db2b57287cefa Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Tue, 30 Apr 2024 16:09:57 +0800 Subject: [PATCH] add PoolsCore namespace, add IEcsComponentType --- src/Builtin/Aspects.cs | 4 +- src/EcsAspect.cs | 1 + src/EcsWorld.cache.cs | 4 +- src/EcsWorld.cs | 1 + src/EcsWorld.pools.cs | 1 + src/Pools/EcsPool.cs | 5 +- src/Pools/EcsPoolBase.cs | 267 +++++++++++++++++++++------------------ src/Pools/EcsTagPool.cs | 6 +- 8 files changed, 157 insertions(+), 132 deletions(-) diff --git a/src/Builtin/Aspects.cs b/src/Builtin/Aspects.cs index fd83108..a04ea7a 100644 --- a/src/Builtin/Aspects.cs +++ b/src/Builtin/Aspects.cs @@ -1,4 +1,6 @@ -namespace DCFApixels.DragonECS +using DCFApixels.DragonECS.PoolsCore; + +namespace DCFApixels.DragonECS { public sealed class EmptyAspect : EcsAspect { } diff --git a/src/EcsAspect.cs b/src/EcsAspect.cs index 42959d0..f285ee4 100644 --- a/src/EcsAspect.cs +++ b/src/EcsAspect.cs @@ -1,4 +1,5 @@ using DCFApixels.DragonECS.Internal; +using DCFApixels.DragonECS.PoolsCore; using System; using System.Collections.Generic; using System.Reflection; diff --git a/src/EcsWorld.cache.cs b/src/EcsWorld.cache.cs index 1238410..d6cd397 100644 --- a/src/EcsWorld.cache.cs +++ b/src/EcsWorld.cache.cs @@ -1,4 +1,6 @@ -namespace DCFApixels.DragonECS +using DCFApixels.DragonECS.PoolsCore; + +namespace DCFApixels.DragonECS { public partial class EcsWorld { diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 413b026..39baf86 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -1,4 +1,5 @@ using DCFApixels.DragonECS.Internal; +using DCFApixels.DragonECS.PoolsCore; using System; using System.Collections.Generic; using System.Diagnostics; diff --git a/src/EcsWorld.pools.cs b/src/EcsWorld.pools.cs index 077be5e..9f76adc 100644 --- a/src/EcsWorld.pools.cs +++ b/src/EcsWorld.pools.cs @@ -1,4 +1,5 @@ using DCFApixels.DragonECS.Internal; +using DCFApixels.DragonECS.PoolsCore; using System; using System.Linq; using System.Runtime.CompilerServices; diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index 2c01eaa..5d65ea6 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -1,4 +1,5 @@ using DCFApixels.DragonECS.Internal; +using DCFApixels.DragonECS.PoolsCore; using System; using System.Collections; using System.Collections.Generic; @@ -6,6 +7,8 @@ using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { + /// Standard component + public interface IEcsComponent : IEcsComponentType { } #if ENABLE_IL2CPP using Unity.IL2CPP.CompilerServices; [Il2CppSetOption (Option.NullChecks, false)] @@ -297,8 +300,6 @@ namespace DCFApixels.DragonECS public static implicit operator EcsPool(OptionalMarker a) { return a.GetInstance>(); } #endregion } - /// Standard component - public interface IEcsComponent { } public static class EcsPoolExt { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/Pools/EcsPoolBase.cs b/src/Pools/EcsPoolBase.cs index c1bf8f1..372e1f2 100644 --- a/src/Pools/EcsPoolBase.cs +++ b/src/Pools/EcsPoolBase.cs @@ -1,10 +1,144 @@ using DCFApixels.DragonECS.Internal; +using DCFApixels.DragonECS.PoolsCore; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; +namespace DCFApixels.DragonECS.PoolsCore +{ + public interface IEcsComponentType { } + /// Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool. + public interface IEcsPoolImplementation : IEcsPool + { + #region Methods + void OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID); + void OnWorldResize(int newSize); + void OnReleaseDelEntityBuffer(ReadOnlySpan buffer); + void OnWorldDestroy(); + #endregion + } + /// Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool. + /// Component type + public interface IEcsPoolImplementation : IEcsPoolImplementation { } + + public static class EcsPoolThrowHalper + { + public static void ThrowAlreadyHasComponent(int entityID) + { + throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName()}."); + } + public static void ThrowNotHaveComponent(int entityID) + { + throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName()}."); + } + public static void ThrowAlreadyHasComponent(Type type, int entityID) + { + throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName(type)}."); + } + public static void ThrowNotHaveComponent(Type type, int entityID) + { + throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName(type)}."); + } + public static void ThrowNullListener() + { + throw new ArgumentNullException("listener is null"); + } + } +} + +namespace DCFApixels.DragonECS.Internal +{ + public struct NullComponent { } + public sealed class EcsNullPool : IEcsPoolImplementation + { + public static readonly EcsNullPool instance = new EcsNullPool(); + + #region Properties + int IEcsReadonlyPool.ComponentTypeID { get { return 0; } }//TODO Првоерить что NullComponent всегда имеет id 0 + Type IEcsReadonlyPool.ComponentType { get { return typeof(NullComponent); } } + EcsWorld IEcsReadonlyPool.World + { + get + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#else + return EcsWorld.GetWorld(0); +#endif + } + } + public int Count { get { return 0; } } + public bool IsReadOnly { get { return true; } } + #endregion + + #region Methods + bool IEcsReadonlyPool.Has(int index) + { + return false; + } + void IEcsPool.Del(int entityID) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + void IEcsPool.AddRaw(int entityID, object dataRaw) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + object IEcsReadonlyPool.GetRaw(int entityID) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#else + return null; +#endif + } + void IEcsPool.SetRaw(int entity, object dataRaw) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + void IEcsReadonlyPool.Copy(int fromEntityID, int toEntityID) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + void IEcsReadonlyPool.Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + void IEcsPool.ClearAll() + { +#if (DEBUG && !DISABLE_DEBUG) + throw new NullInstanceException(); +#endif + } + #endregion + + #region Callbacks + void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID) { } + void IEcsPoolImplementation.OnWorldDestroy() { } + void IEcsPoolImplementation.OnWorldResize(int newSize) { } + void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan buffer) { } + #endregion + + #region Listeners + void IEcsReadonlyPool.AddListener(IEcsPoolEventListener listener) { } + void IEcsReadonlyPool.RemoveListener(IEcsPoolEventListener listener) { } + #endregion + } +} + namespace DCFApixels.DragonECS { + #region Interfaces public interface IEcsReadonlyPool { #region Properties @@ -39,57 +173,31 @@ namespace DCFApixels.DragonECS /// A pool for struct components. public interface IEcsStructPool : IEcsPool where T : struct { + #region Methods ref T Add(int entityID); ref readonly T Read(int entityID); ref T Get(int entityID); + #endregion } /// A pool for reference components of type T that instantiates components itself. public interface IEcsClassPool : IEcsPool where T : class { + #region Methods T Add(int entityID); T Get(int entityID); + #endregion } /// A pool for reference components of type T, which does not instantiate components itself but receives components from external sources.. public interface IEcsHybridPool : IEcsPool where T : class { + #region Methods void Add(int entityID, T component); T Get(int entityID); + #endregion } - /// Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool. - public interface IEcsPoolImplementation : IEcsPool - { - void OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID); - void OnWorldResize(int newSize); - void OnReleaseDelEntityBuffer(ReadOnlySpan buffer); - void OnWorldDestroy(); - } - /// Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool. - /// Component type - public interface IEcsPoolImplementation : IEcsPoolImplementation { } + #endregion - public static class EcsPoolThrowHalper - { - public static void ThrowAlreadyHasComponent(int entityID) - { - throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName()}."); - } - public static void ThrowNotHaveComponent(int entityID) - { - throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName()}."); - } - public static void ThrowAlreadyHasComponent(Type type, int entityID) - { - throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName(type)}."); - } - public static void ThrowNotHaveComponent(Type type, int entityID) - { - throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName(type)}."); - } - public static void ThrowNullListener() - { - throw new ArgumentNullException("listener is null"); - } - } + #region Extensions public static class IEcsPoolImplementationExtensions { [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -98,97 +206,6 @@ namespace DCFApixels.DragonECS return self == null || self == EcsNullPool.instance; } } - - #region Dummy EcsNullPool - namespace Internal - { - public struct NullComponent { } - public sealed class EcsNullPool : IEcsPoolImplementation - { - public static readonly EcsNullPool instance = new EcsNullPool(); - - #region Properties - int IEcsReadonlyPool.ComponentTypeID { get { return 0; } }//TODO Првоерить что NullComponent всегда имеет id 0 - Type IEcsReadonlyPool.ComponentType { get { return typeof(NullComponent); } } - EcsWorld IEcsReadonlyPool.World - { - get - { -#if (DEBUG && !DISABLE_DEBUG) - throw new NullInstanceException(); -#else - return EcsWorld.GetWorld(0); -#endif - } - } - public int Count { get { return 0; } } - public bool IsReadOnly { get { return true; } } - #endregion - - #region Methods - bool IEcsReadonlyPool.Has(int index) - { - return false; - } - void IEcsPool.Del(int entityID) - { -#if (DEBUG && !DISABLE_DEBUG) - throw new NullInstanceException(); -#endif - } - void IEcsPool.AddRaw(int entityID, object dataRaw) - { -#if (DEBUG && !DISABLE_DEBUG) - throw new NullInstanceException(); -#endif - } - object IEcsReadonlyPool.GetRaw(int entityID) - { -#if (DEBUG && !DISABLE_DEBUG) - throw new NullInstanceException(); -#else - return null; -#endif - } - void IEcsPool.SetRaw(int entity, object dataRaw) - { -#if (DEBUG && !DISABLE_DEBUG) - throw new NullInstanceException(); -#endif - } - void IEcsReadonlyPool.Copy(int fromEntityID, int toEntityID) - { -#if (DEBUG && !DISABLE_DEBUG) - throw new NullInstanceException(); -#endif - } - void IEcsReadonlyPool.Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) - { -#if (DEBUG && !DISABLE_DEBUG) - throw new NullInstanceException(); -#endif - } - void IEcsPool.ClearAll() - { -#if (DEBUG && !DISABLE_DEBUG) - throw new NullInstanceException(); -#endif - } - #endregion - - #region Callbacks - void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID) { } - void IEcsPoolImplementation.OnWorldDestroy() { } - void IEcsPoolImplementation.OnWorldResize(int newSize) { } - void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan buffer) { } - #endregion - - #region Listeners - void IEcsReadonlyPool.AddListener(IEcsPoolEventListener listener) { } - void IEcsReadonlyPool.RemoveListener(IEcsPoolEventListener listener) { } - #endregion - } - } #endregion #region Callbacks Interface diff --git a/src/Pools/EcsTagPool.cs b/src/Pools/EcsTagPool.cs index d032b9f..017afa3 100644 --- a/src/Pools/EcsTagPool.cs +++ b/src/Pools/EcsTagPool.cs @@ -1,3 +1,4 @@ +using DCFApixels.DragonECS.PoolsCore; using System; using System.Collections; using System.Collections.Generic; @@ -6,6 +7,8 @@ using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { + /// Component without data + public interface IEcsTagComponent : IEcsComponentType { } #if ENABLE_IL2CPP using Unity.IL2CPP.CompilerServices; [Il2CppSetOption (Option.NullChecks, false)] @@ -255,9 +258,6 @@ namespace DCFApixels.DragonECS public static implicit operator EcsTagPool(OptionalMarker a) { return a.GetInstance>(); } #endregion } - - /// Component without data - public interface IEcsTagComponent { } public static class EcsTagPoolExt { [MethodImpl(MethodImplOptions.AggressiveInlining)]