From c565332db8146580d54cb5e044873eb79af7eb35 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 25 Feb 2024 01:11:02 +0800 Subject: [PATCH] separate core with hybrid --- src/Pools/EcsHybridPool.cs | 566 --------------------------- src/Pools/EcsHybridPool.cs.meta | 11 - src/Pools/EcsInterfacePool.cs | 293 -------------- src/Pools/Utils/VirtualHybridPool.cs | 168 -------- 4 files changed, 1038 deletions(-) delete mode 100644 src/Pools/EcsHybridPool.cs delete mode 100644 src/Pools/EcsHybridPool.cs.meta delete mode 100644 src/Pools/EcsInterfacePool.cs delete mode 100644 src/Pools/Utils/VirtualHybridPool.cs diff --git a/src/Pools/EcsHybridPool.cs b/src/Pools/EcsHybridPool.cs deleted file mode 100644 index ca3013e..0000000 --- a/src/Pools/EcsHybridPool.cs +++ /dev/null @@ -1,566 +0,0 @@ -using DCFApixels.DragonECS.Internal; -using System; -using System.Collections; -using System.Collections.Generic; -using System.Reflection; -using System.Runtime.CompilerServices; - -namespace DCFApixels.DragonECS -{ - /// Pool for IEcsHybridComponent components - public sealed class EcsHybridPool : IEcsPoolImplementation, IEcsHybridPool, IEcsHybridPoolInternal, IEnumerable //IEnumerable - IntelliSense hack - where T : class, IEcsHybridComponent - { - private EcsWorld _source; - private int _componentTypeID; - private EcsMaskChunck _maskBit; - - private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID - private T[] _items; //dense - private int[] _entities; - private int _itemsCount; - - private int[] _recycledItems; - private int _recycledItemsCount; - - private List _listeners = new List(); - - private EcsWorld.PoolsMediator _mediator; - - private HybridPoolGraph _graph; - - #region Properites - public int Count - { - get { return _itemsCount; } - } - public int Capacity - { - get { return _items.Length; } - } - public int ComponentTypeID - { - get { return _componentTypeID; } - } - public Type ComponentType - { - get { return typeof(T); } - } - public EcsWorld World - { - get { return _source; } - } - public bool IReadOnly - { - get { return false; } - } - #endregion - - #region Methods - void IEcsHybridPoolInternal.AddRefInternal(int entityID, object component, bool isMain) - { - AddInternal(entityID, (T)component, isMain); - } - private void AddInternal(int entityID, T component, bool isMain) - { - ref int itemIndex = ref _mapping[entityID]; -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (itemIndex > 0) EcsPoolThrowHalper.ThrowAlreadyHasComponent(entityID); -#endif - if (_recycledItemsCount > 0) - { - itemIndex = _recycledItems[--_recycledItemsCount]; - _itemsCount++; - } - else - { - itemIndex = ++_itemsCount; - if (itemIndex >= _items.Length) - { - Array.Resize(ref _items, _items.Length << 1); - Array.Resize(ref _entities, _items.Length); - } - } - _mediator.RegisterComponent(entityID, _componentTypeID, _maskBit); - _listeners.InvokeOnAdd(entityID); - if (isMain) - { - component.OnAddToPool(_source.GetEntityLong(entityID)); - } - _items[itemIndex] = component; - _entities[itemIndex] = entityID; - } - public void Add(int entityID, T component) - { - //HybridMapping mapping = _source.GetHybridMapping(component.GetType()); - //mapping.GetTargetTypePool().AddRefInternal(entityID, component, true); - //foreach (var pool in mapping.GetPools()) - //{ - // pool.AddRefInternal(entityID, component, false); - //} - _graph.GetBranch(component.GetType()).Add(entityID, component); - } - public void Set(int entityID, T component) - { - if (Has(entityID)) - { - Del(entityID); - } - Add(entityID, component); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T Get(int entityID) - { -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); -#endif - _listeners.InvokeOnGet(entityID); - return _items[_mapping[entityID]]; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref readonly T Read(int entityID) - { -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); -#endif - return ref _items[_mapping[entityID]]; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Has(int entityID) - { - return _mapping[entityID] > 0; - } - void IEcsHybridPoolInternal.DelInternal(int entityID, bool isMain) - { - DelInternal(entityID, isMain); - } - private void DelInternal(int entityID, bool isMain) - { -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); -#endif - ref int itemIndex = ref _mapping[entityID]; - T component = _items[itemIndex]; - if (isMain) - { - component.OnDelFromPool(_source.GetEntityLong(entityID)); - } - if (_recycledItemsCount >= _recycledItems.Length) - { - Array.Resize(ref _recycledItems, _recycledItems.Length << 1); - } - _recycledItems[_recycledItemsCount++] = itemIndex; - _mapping[entityID] = 0; - _entities[itemIndex] = 0; - _itemsCount--; - _mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit); - _listeners.InvokeOnDel(entityID); - } - public void Del(int entityID) - { - var component = Get(entityID); - HybridMapping mapping = _source.GetHybridMapping(component.GetType()); - mapping.GetTargetTypePool().DelInternal(entityID, true); - foreach (var pool in mapping.GetPools()) - { - pool.DelInternal(entityID, false); - } - } - public void TryDel(int entityID) - { - if (Has(entityID)) Del(entityID); - } - public void Copy(int fromEntityID, int toEntityID) - { -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(fromEntityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(fromEntityID); -#endif - Set(toEntityID, Get(fromEntityID)); - } - public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) - { -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(fromEntityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(fromEntityID); -#endif - toWorld.GetPool().Set(toEntityID, Get(fromEntityID)); - } - - public void ClearNotAliveComponents() - { - for (int i = _itemsCount - 1; i >= 0; i--) - { - if (!_items[i].IsAlive) - { - Del(_entities[i]); - } - } - } - #endregion - - #region Callbacks - void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID) - { - _graph = world.Get().Graph; - - _source = world; - _mediator = mediator; - _componentTypeID = componentTypeID; - _maskBit = EcsMaskChunck.FromID(componentTypeID); - - const int capacity = 512;//TODO çàìåíèòü íà çíà÷åíèå èç êîíôèãà - - _mapping = new int[world.Capacity]; - _recycledItems = new int[128]; - _recycledItemsCount = 0; - _items = new T[capacity]; - _entities = new int[capacity]; - _itemsCount = 0; - } - void IEcsPoolImplementation.OnWorldResize(int newSize) - { - Array.Resize(ref _mapping, newSize); - } - void IEcsPoolImplementation.OnWorldDestroy() { } - void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan buffer) - { - foreach (var entityID in buffer) - TryDel(entityID); - } - #endregion - - #region Other - void IEcsPool.AddRaw(int entityID, object dataRaw) => Add(entityID, (T)dataRaw); - object IEcsReadonlyPool.GetRaw(int entityID) => Read(entityID); - void IEcsPool.SetRaw(int entityID, object dataRaw) => Set(entityID, (T)dataRaw); - #endregion - - #region Listeners - public void AddListener(IEcsPoolEventListener listener) - { - if (listener == null) { throw new ArgumentNullException("listener is null"); } - _listeners.Add(listener); - } - public void RemoveListener(IEcsPoolEventListener listener) - { - if (listener == null) { throw new ArgumentNullException("listener is null"); } - _listeners.Remove(listener); - } - #endregion - - #region IEnumerator - IntelliSense hack - IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); - IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException(); - #endregion - - #region Devirtualize - void IEcsHybridPoolInternal.Devirtualize(VirtualHybridPool virtualHybridPool) - { - _mapping = virtualHybridPool._mapping; - _itemsCount = virtualHybridPool._itemsCount; - } - #endregion - } - /// Hybrid component - public interface IEcsHybridComponent - { - bool IsAlive { get; } - void OnAddToPool(entlong entity); - void OnDelFromPool(entlong entity); - } - public static class EcsHybridPoolExtensions - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool IsNullOrNotAlive(this IEcsHybridComponent self) => self == null || self.IsAlive; - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsHybridPool GetPool(this EcsWorld self) where T : class, IEcsHybridComponent - { - return self.GetPool>(); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsHybridPool GetPoolUnchecked(this EcsWorld self) where T : class, IEcsHybridComponent - { - return self.GetPoolUnchecked>(); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsHybridPool Include(this EcsAspect.Builder self) where T : class, IEcsHybridComponent - { - return self.Include>(); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsHybridPool Exclude(this EcsAspect.Builder self) where T : class, IEcsHybridComponent - { - return self.Exclude>(); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsHybridPool Optional(this EcsAspect.Builder self) where T : class, IEcsHybridComponent - { - return self.Optional>(); - } - - //------------------------------------------------- - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsHybridPool GetHybridPool(this EcsWorld self) where T : class, IEcsHybridComponent - { - return self.GetPool>(); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsHybridPool GetHybridPoolUnchecked(this EcsWorld self) where T : class, IEcsHybridComponent - { - return self.GetPoolUnchecked>(); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsHybridPool IncludeHybrid(this EcsAspect.Builder self) where T : class, IEcsHybridComponent - { - return self.Include>(); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsHybridPool ExcludeHybrid(this EcsAspect.Builder self) where T : class, IEcsHybridComponent - { - return self.Exclude>(); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static EcsHybridPool OptionalHybrid(this EcsAspect.Builder self) where T : class, IEcsHybridComponent - { - return self.Optional>(); - } - } - - public partial class EcsWorld - { - private Dictionary _hybridMapping = new Dictionary(); - internal HybridMapping GetHybridMapping(Type type) - { - if (!_hybridMapping.TryGetValue(type, out HybridMapping mapping)) - { - mapping = new HybridMapping(this, type); - _hybridMapping.Add(type, mapping); - } - return mapping; - } - } - - internal class HybridMapping - { - private EcsWorld _source; - private object[] _sourceForReflection; - private Type _type; - - private IEcsHybridPoolInternal _targetTypePool; - private List _relatedPools; - - private static Type hybridPoolType = typeof(EcsHybridPool<>); - private static MethodInfo getHybridPoolMethod = typeof(EcsHybridPoolExtensions).GetMethod($"{nameof(EcsHybridPoolExtensions.GetPool)}", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic); - - private static HashSet _hybridComponents = new HashSet(); - static HybridMapping() - { - Type hybridComponentType = typeof(IEcsHybridComponent); - foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) - { - var types = assembly.GetTypes(); - foreach (var type in types) - { - if (type.GetInterface(nameof(IEcsHybridComponent)) != null && type != hybridComponentType) - { - _hybridComponents.Add(type); - } - } - } - } - public static bool IsEcsHybridComponentType(Type type) - { - return _hybridComponents.Contains(type); - } - - public HybridMapping(EcsWorld source, Type type) - { - if (!type.IsClass) - throw new ArgumentException(); - - _source = source; - _type = type; - _relatedPools = new List(); - _sourceForReflection = new object[] { source }; - _targetTypePool = CreateHybridPool(type); - foreach (var item in type.GetInterfaces()) - { - if (IsEcsHybridComponentType(item)) - { - _relatedPools.Add(CreateHybridPool(item)); - } - } - Type baseType = type.BaseType; - while (baseType != typeof(object) && IsEcsHybridComponentType(baseType)) - { - _relatedPools.Add(CreateHybridPool(baseType)); - baseType = baseType.BaseType; - } - } - private IEcsHybridPoolInternal CreateHybridPool(Type componentType) - { - return (IEcsHybridPoolInternal)getHybridPoolMethod.MakeGenericMethod(componentType).Invoke(null, _sourceForReflection); - } - - public IEcsHybridPoolInternal GetTargetTypePool() - { - return _targetTypePool; - } - public List GetPools() - { - return _relatedPools; - } - } -} - - - - -namespace DCFApixels.DragonECS.Internal -{ - internal interface IEcsHybridPoolInternal - { - Type ComponentType { get; } - void AddRefInternal(int entityID, object component, bool isMain); - void DelInternal(int entityID, bool isMain); - void Devirtualize(VirtualHybridPool virtualHybridPool); - } - internal readonly struct HybridPoolGraphCmp : IEcsWorldComponent - { - public readonly HybridPoolGraph Graph; - private HybridPoolGraphCmp(EcsWorld world) - { - Graph = new HybridPoolGraph(world); - } - public void Init(ref HybridPoolGraphCmp component, EcsWorld world) - { - component = new HybridPoolGraphCmp(world); - } - public void OnDestroy(ref HybridPoolGraphCmp component, EcsWorld world) - { - component = default; - } - } - public class HybridPoolGraph - { - private EcsWorld _world; - private Dictionary _branches = new Dictionary(); - - public HybridPoolGraph(EcsWorld world) - { - _world = world; - } - - public bool IsInstantiable(Type type) - { - return _branches.ContainsKey(type); - } - public bool TryGetBranch(Type type, out HybridPoolBranch branch) - { - return _branches.TryGetValue(type, out branch); - } - public void InitNewPool(IEcsHybridPoolInternal pool) - { - foreach (var pair in _branches) - { - var type = pair.Key; - var branch = pair.Value; - if (type.IsAssignableFrom(pool.ComponentType)) - { - if (type == pool.ComponentType) - { - branch.InitRootTypePool(pool); - } - else - { - branch.InitNewPool(pool); - } - } - } - } - - public HybridPoolBranch GetBranch(Type targetType) - { - if (_branches.TryGetValue(targetType, out HybridPoolBranch branch) == false) - { - branch = new HybridPoolBranch(_world, targetType, null); - _branches.Add(targetType, branch); - } - return branch; - } - } - public class HybridPoolBranch - { - private EcsWorld _world; - - private Type _rootComponentType; - private int _rootComponentTypeID; - private IEcsHybridPoolInternal _rootTypePool; - private List _relatedPools = new List(); - - private VirtualHybridPool _virtualPoolRef; - private bool _isVirtualPool = false; - - public bool IsVirtualPool - { - get { return _isVirtualPool; } - } - - public HybridPoolBranch(EcsWorld world, Type rootComponentType, IEcsHybridPoolInternal rootTypePool) - { - _world = world; - - _rootComponentType = rootComponentType; - _rootComponentTypeID = world.GetComponentTypeID(rootComponentType); - - if (rootTypePool == null) - { - _virtualPoolRef = new VirtualHybridPool(world, rootComponentType); - rootTypePool = _virtualPoolRef; - _isVirtualPool = true; - } - _rootTypePool = rootTypePool; - } - - - public void InitRootTypePool(IEcsHybridPoolInternal rootTypePool) - { - if (_isVirtualPool == false) - { - Throw.UndefinedException(); - } - _isVirtualPool = false; - rootTypePool.Devirtualize(_virtualPoolRef); - _rootTypePool = rootTypePool; - _virtualPoolRef = null; - } - public void InitNewPool(IEcsHybridPoolInternal pool) - { - _relatedPools.Add(pool); - } - - public void Set(int entityID, object component) - { - throw new NotImplementedException(); - } - public void Add(int entityID, object component) - { - _rootTypePool.AddRefInternal(entityID, component, true); - foreach (var pool in _relatedPools) - { - pool.AddRefInternal(entityID, component, false); - } - } - public void Del(int entityID) - { - _rootTypePool.DelInternal(entityID, true); - foreach (var pool in _relatedPools) - { - pool.DelInternal(entityID, false); - } - } - } -} \ No newline at end of file diff --git a/src/Pools/EcsHybridPool.cs.meta b/src/Pools/EcsHybridPool.cs.meta deleted file mode 100644 index ec1c8be..0000000 --- a/src/Pools/EcsHybridPool.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 511487e83f936f94780e572063b68a87 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/src/Pools/EcsInterfacePool.cs b/src/Pools/EcsInterfacePool.cs deleted file mode 100644 index 6975d19..0000000 --- a/src/Pools/EcsInterfacePool.cs +++ /dev/null @@ -1,293 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace DCFApixels.DragonECS -{ - - public interface IEcsInterfacePool : IEcsReadonlyPool where T : class - { - T Get(int entityID); - } - internal interface IEcsInterfacePoolInternal - { - void Add(int entityID, object component); - void Del(int entityID); - } - public interface IEcsInterfaceComponent { } - public class EcsInterfacePool : IEcsPoolImplementation, IEcsInterfacePool, IEcsInterfacePoolInternal, IEnumerable //IEnumerable - IntelliSense hack - where T : class, IEcsInterfaceComponent - { - private EcsWorld _source; - private int _componentTypeID; - - private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID - private T[] _items; //dense - private int _itemsCount; - private int[] _recycledItems; - private int _recycledItemsCount; - - private List _listeners = new List(); - - private EcsWorld.PoolsMediator _mediator; - - #region Properties - public int ComponentTypeID - { - get { return _componentTypeID; } - } - public Type ComponentType - { - get { return typeof(T); } - } - public EcsWorld World - { - get { return _source; } - } - public int Count - { - get { return _itemsCount; } - } - public bool IReadOnly - { - get { return true; } - } - #endregion - - #region Methdos - public T Get(int entityID) - { -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) { EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); } -#endif - _listeners.InvokeOnGet(entityID); - return _items[_mapping[entityID]]; - } - public bool Has(int entityID) - { - return _mapping[entityID] > 0; - } - #endregion - - #region IEcsInterfacePoolInternal - void IEcsInterfacePoolInternal.Add(int entityID, object component) - { - - } - void IEcsInterfacePoolInternal.Del(int entityID) - { - - } - #endregion - - #region Other - object IEcsReadonlyPool.GetRaw(int entityID) - { - return Get(entityID); - } - public void Copy(int fromEntityID, int toEntityID) { } - public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) { } - void IEcsPool.AddRaw(int entityID, object dataRaw) - { - EcsDebug.PrintWarning("Is read only!"); - } - void IEcsPool.SetRaw(int entityID, object dataRaw) - { - EcsDebug.PrintWarning("Is read only!"); - } - void IEcsPool.Del(int entityID) - { - EcsDebug.PrintWarning("Is read only!"); - } - #endregion - - #region Callbacks - void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID) - { - throw new NotImplementedException(); - } - void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan buffer) - { - throw new NotImplementedException(); - } - void IEcsPoolImplementation.OnWorldResize(int newSize) - { - throw new NotImplementedException(); - } - void IEcsPoolImplementation.OnWorldDestroy() - { - throw new NotImplementedException(); - } - #endregion - - #region Listeners - public void AddListener(IEcsPoolEventListener listener) - { - if (listener == null) { throw new ArgumentNullException("listener is null"); } - _listeners.Add(listener); - } - public void RemoveListener(IEcsPoolEventListener listener) - { - if (listener == null) { throw new ArgumentNullException("listener is null"); } - _listeners.Remove(listener); - } - #endregion - - #region IEnumerator - IntelliSense hack - IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } - IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } - #endregion - } -} - - - - - - - - - - - -namespace DCFApixels.DragonECS.Internal -{ - internal readonly struct InterfacePoolGraphCmp : IEcsWorldComponent - { - public readonly InterfacePoolGraph Graph; - private InterfacePoolGraphCmp(EcsWorld world) - { - Graph = new InterfacePoolGraph(world); - } - public void Init(ref InterfacePoolGraphCmp component, EcsWorld world) - { - component = new InterfacePoolGraphCmp(world); - } - public void OnDestroy(ref InterfacePoolGraphCmp component, EcsWorld world) - { - component = default; - } - } - public class InterfacePoolGraph - { - private EcsWorld _world; - private Dictionary _branches = new Dictionary(); - - public InterfacePoolGraph(EcsWorld world) - { - _world = world; - } - - public bool IsInstantiable(Type type) - { - return _branches.ContainsKey(type); - } - public bool TryGetBranch(Type type, out InterfacePoolBranch branch) - { - return _branches.TryGetValue(type, out branch); - } - public void InitNewInterfacePool(IEcsHybridPoolInternal pool) - { - foreach (var pair in _branches) - { - var type = pair.Key; - var branch = pair.Value; - if (type.IsAssignableFrom(pool.ComponentType)) - { - if (type == pool.ComponentType) - { - branch.InitRootTypePool(pool); - } - else - { - branch.InitNewPool(pool); - } - } - } - } - - public InterfacePoolBranch GetBranch(Type targetType) - { - if (_branches.TryGetValue(targetType, out InterfacePoolBranch branch) == false) - { - branch = new InterfacePoolBranch(_world, targetType, null); - _branches.Add(targetType, branch); - } - return branch; - } - } - public class InterfacePoolBranch - { - private EcsWorld _world; - - private Type _rootComponentType; - private int _rootComponentTypeID; - private IEcsHybridPoolInternal _rootTypePool; - private List _relatedPools = new List(); - - private VirtualHybridPool _virtualPoolRef; - private bool _isVirtualPool = false; - - public bool IsVirtualPool - { - get { return _isVirtualPool; } - } - - public HybridPoolBranch(EcsWorld world, Type rootComponentType, IEcsHybridPoolInternal rootTypePool) - { - _world = world; - - _rootComponentType = rootComponentType; - _rootComponentTypeID = world.GetComponentTypeID(rootComponentType); - - if (rootTypePool == null) - { - _virtualPoolRef = new VirtualHybridPool(world, rootComponentType); - rootTypePool = _virtualPoolRef; - _isVirtualPool = true; - } - _rootTypePool = rootTypePool; - } - - - public void InitRootTypePool(IEcsHybridPoolInternal rootTypePool) - { - if (_isVirtualPool == false) - { - Throw.UndefinedException(); - } - _isVirtualPool = false; - rootTypePool.Devirtualize(_virtualPoolRef); - _rootTypePool = rootTypePool; - _virtualPoolRef = null; - } - public void InitNewPool(IEcsHybridPoolInternal pool) - { - _relatedPools.Add(pool); - } - - public void Set(int entityID, object component) - { - throw new NotImplementedException(); - } - public void Add(int entityID, object component) - { - _rootTypePool.AddRefInternal(entityID, component, true); - foreach (var pool in _relatedPools) - { - pool.AddRefInternal(entityID, component, false); - } - } - public void Del(int entityID) - { - _rootTypePool.DelInternal(entityID, true); - foreach (var pool in _relatedPools) - { - pool.DelInternal(entityID, false); - } - } - } -} diff --git a/src/Pools/Utils/VirtualHybridPool.cs b/src/Pools/Utils/VirtualHybridPool.cs deleted file mode 100644 index ede8fce..0000000 --- a/src/Pools/Utils/VirtualHybridPool.cs +++ /dev/null @@ -1,168 +0,0 @@ -using System; -using System.Runtime.CompilerServices; - -namespace DCFApixels.DragonECS.Internal -{ - internal class VirtualHybridPool : IEcsHybridPoolInternal - { - private EcsWorld _source; - private Type _componentType; - - internal int[] _mapping; - internal object[] _items; - private int[] _entities; - internal int _itemsCount = 0; - - internal int[] _recycledItems; - internal int _recycledItemsCount; - - private bool _isDevirtualized = false; - - #region Properties - public Type ComponentType - { - get { return _componentType; } - } - public EcsWorld World - { - get { return _source; } - } - public int Count - { - get { return _itemsCount; } - } - public int Capacity - { - get { return _mapping.Length; } - } - public bool IsDevirtualized - { - get { return _isDevirtualized; } - } - #endregion - - #region Constructors - public VirtualHybridPool(EcsWorld world, Type componentType) - { - _source = world; - _componentType = componentType; - - _mapping = new int[world.Capacity]; - _recycledItems = new int[world.Config.Get_PoolRecycledComponentsCapacity()]; - _recycledItemsCount = 0; - _items = new object[ArrayUtility.NormalizeSizeToPowerOfTwo(world.Config.Get_PoolComponentsCapacity())]; - _itemsCount = 0; - } - #endregion - - #region Callbacks - public void OnWorldResize(int newSize) - { - Array.Resize(ref _mapping, newSize); - } - #endregion - - #region Methods - public void AddRaw(int entityID, object dataRaw) - { - ref int itemIndex = ref _mapping[entityID]; -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (itemIndex > 0) { EcsPoolThrowHalper.ThrowAlreadyHasComponent(_componentType, entityID); } -#endif - if (_recycledItemsCount > 0) - { - itemIndex = _recycledItems[--_recycledItemsCount]; - _itemsCount++; - } - else - { - itemIndex = ++_itemsCount; - if (_itemsCount >= _items.Length) - { - Array.Resize(ref _items, _items.Length << 1); - } - } - _items[itemIndex] = dataRaw; - } - public bool TryAddRaw(int entityID, object dataRaw) - { - if (Has(entityID)) - { - return false; - } - AddRaw(entityID, dataRaw); - return true; - } - public void SetRaw(int entityID, object dataRaw) - { -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) { EcsPoolThrowHalper.ThrowNotHaveComponent(_componentType, entityID); } -#endif - _items[_mapping[entityID]] = dataRaw; - } - public object GetRaw(int entityID) - { -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) { EcsPoolThrowHalper.ThrowNotHaveComponent(_componentType, entityID); } -#endif - return _items[_mapping[entityID]]; - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public bool Has(int entityID) - { - return _mapping[entityID] > 0; - } - public void Del(int entityID) - { - ref int itemIndex = ref _mapping[entityID]; -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (itemIndex <= 0) EcsPoolThrowHalper.ThrowNotHaveComponent(_componentType, entityID); -#endif - _items[itemIndex] = null; - if (_recycledItemsCount >= _recycledItems.Length) - { - Array.Resize(ref _recycledItems, _recycledItems.Length << 1); - } - _recycledItems[_recycledItemsCount++] = itemIndex; - itemIndex = 0; - _itemsCount--; - } - public bool TryDel(int entityID) - { - if (Has(entityID)) - { - Del(entityID); - return true; - } - return false; - } - - public void Copy(int fromEntityID, int toEntityID) - { - throw new NotImplementedException(); - } - public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) - { - Throw.Exception("Copying data to another world is not supported for virtual pools, devirtualize the pool first."); - } - #endregion - - #region IEcsHybridPoolInternal - public void AddRefInternal(int entityID, object component, bool isMain) - { - AddRaw(entityID, component); - } - public void DelInternal(int entityID, bool isMain) - { - Del(entityID); - } - #endregion - - #region Devirtualize - void IEcsHybridPoolInternal.Devirtualize(VirtualHybridPool virtualHybridPool) - { - Throw.UndefinedException(); - } - #endregion - } -} \ No newline at end of file