add PoolsCore namespace, add IEcsComponentType

This commit is contained in:
Mikhail 2024-04-30 16:09:57 +08:00
parent d87e0fa948
commit d2f761a884
8 changed files with 157 additions and 132 deletions

View File

@ -1,4 +1,6 @@
namespace DCFApixels.DragonECS
using DCFApixels.DragonECS.PoolsCore;
namespace DCFApixels.DragonECS
{
public sealed class EmptyAspect : EcsAspect { }

View File

@ -1,4 +1,5 @@
using DCFApixels.DragonECS.Internal;
using DCFApixels.DragonECS.PoolsCore;
using System;
using System.Collections.Generic;
using System.Reflection;

View File

@ -1,4 +1,6 @@
namespace DCFApixels.DragonECS
using DCFApixels.DragonECS.PoolsCore;
namespace DCFApixels.DragonECS
{
public partial class EcsWorld
{

View File

@ -1,4 +1,5 @@
using DCFApixels.DragonECS.Internal;
using DCFApixels.DragonECS.PoolsCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;

View File

@ -1,4 +1,5 @@
using DCFApixels.DragonECS.Internal;
using DCFApixels.DragonECS.PoolsCore;
using System;
using System.Linq;
using System.Runtime.CompilerServices;

View File

@ -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
{
/// <summary>Standard component</summary>
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<T>(OptionalMarker a) { return a.GetInstance<EcsPool<T>>(); }
#endregion
}
/// <summary>Standard component</summary>
public interface IEcsComponent { }
public static class EcsPoolExt
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -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 { }
/// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary>
public interface IEcsPoolImplementation : IEcsPool
{
#region Methods
void OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID);
void OnWorldResize(int newSize);
void OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer);
void OnWorldDestroy();
#endregion
}
/// <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<T> : IEcsPoolImplementation { }
public static class EcsPoolThrowHalper
{
public static void ThrowAlreadyHasComponent<T>(int entityID)
{
throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName<T>()}.");
}
public static void ThrowNotHaveComponent<T>(int entityID)
{
throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName<T>()}.");
}
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<NullComponent>
{
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<int> 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
/// <summary>A pool for struct components.</summary>
public interface IEcsStructPool<T> : IEcsPool where T : struct
{
#region Methods
ref T Add(int entityID);
ref readonly T Read(int entityID);
ref T Get(int entityID);
#endregion
}
/// <summary>A pool for reference components of type T that instantiates components itself.</summary>
public interface IEcsClassPool<T> : IEcsPool where T : class
{
#region Methods
T Add(int entityID);
T Get(int entityID);
#endregion
}
/// <summary>A pool for reference components of type T, which does not instantiate components itself but receives components from external sources..</summary>
public interface IEcsHybridPool<T> : IEcsPool where T : class
{
#region Methods
void Add(int entityID, T component);
T Get(int entityID);
#endregion
}
/// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary>
public interface IEcsPoolImplementation : IEcsPool
{
void OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID);
void OnWorldResize(int newSize);
void OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer);
void OnWorldDestroy();
}
/// <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<T> : IEcsPoolImplementation { }
#endregion
public static class EcsPoolThrowHalper
{
public static void ThrowAlreadyHasComponent<T>(int entityID)
{
throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName<T>()}.");
}
public static void ThrowNotHaveComponent<T>(int entityID)
{
throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName<T>()}.");
}
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<NullComponent>
{
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<int> buffer) { }
#endregion
#region Listeners
void IEcsReadonlyPool.AddListener(IEcsPoolEventListener listener) { }
void IEcsReadonlyPool.RemoveListener(IEcsPoolEventListener listener) { }
#endregion
}
}
#endregion
#region Callbacks Interface

View File

@ -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
{
/// <summary>Component without data</summary>
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<T>(OptionalMarker a) { return a.GetInstance<EcsTagPool<T>>(); }
#endregion
}
/// <summary>Component without data</summary>
public interface IEcsTagComponent { }
public static class EcsTagPoolExt
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]