2023-11-08 15:15:10 +08:00
|
|
|
using DCFApixels.DragonECS.Internal;
|
2023-06-29 23:53:26 +08:00
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2023-11-08 15:15:10 +08:00
|
|
|
using System.Reflection;
|
2023-06-29 23:53:26 +08:00
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
{
|
2023-11-08 15:15:10 +08:00
|
|
|
namespace Internal
|
|
|
|
{
|
|
|
|
public interface IEcsHybridPoolInternal : IEcsPool
|
|
|
|
{
|
|
|
|
void AddRefInternal(int entityID, object component, bool isAppend);
|
|
|
|
void DelInternal(int entityID, bool isAppend);
|
|
|
|
}
|
|
|
|
}
|
2023-10-31 03:03:13 +08:00
|
|
|
/// <summary>Pool for IEcsHybridComponent components</summary>
|
2023-11-08 15:15:10 +08:00
|
|
|
public sealed class EcsHybridPool<T> : IEcsPoolImplementation<T>, IEcsHybridPool<T>, IEcsHybridPoolInternal, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
|
2023-06-29 23:53:26 +08:00
|
|
|
where T : IEcsHybridComponent
|
|
|
|
{
|
|
|
|
private EcsWorld _source;
|
2023-12-06 18:58:06 +08:00
|
|
|
private int _componentTypeID;
|
2024-01-07 18:52:54 +08:00
|
|
|
private EcsMaskChunck _maskBit;
|
2023-06-29 23:53:26 +08:00
|
|
|
|
|
|
|
private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
|
|
|
|
private T[] _items; //dense
|
2023-07-17 15:55:23 +08:00
|
|
|
private int[] _entities;
|
2023-06-29 23:53:26 +08:00
|
|
|
private int _itemsCount;
|
2023-07-17 15:55:23 +08:00
|
|
|
|
2023-06-29 23:53:26 +08:00
|
|
|
private int[] _recycledItems;
|
|
|
|
private int _recycledItemsCount;
|
|
|
|
|
|
|
|
private List<IEcsPoolEventListener> _listeners = new List<IEcsPoolEventListener>();
|
|
|
|
|
2023-12-06 18:58:06 +08:00
|
|
|
private EcsWorld.PoolsMediator _mediator;
|
|
|
|
|
2023-06-29 23:53:26 +08:00
|
|
|
#region Properites
|
|
|
|
public int Count => _itemsCount;
|
|
|
|
public int Capacity => _items.Length;
|
2023-12-06 18:58:06 +08:00
|
|
|
public int ComponentID => _componentTypeID;
|
2023-06-29 23:53:26 +08:00
|
|
|
public Type ComponentType => typeof(T);
|
|
|
|
public EcsWorld World => _source;
|
|
|
|
#endregion
|
|
|
|
|
2023-11-08 15:15:10 +08:00
|
|
|
#region Methods
|
|
|
|
void IEcsHybridPoolInternal.AddRefInternal(int entityID, object component, bool isMain)
|
2023-06-29 23:53:26 +08:00
|
|
|
{
|
2023-11-08 15:15:10 +08:00
|
|
|
AddInternal(entityID, (T)component, isMain);
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
2023-11-08 15:15:10 +08:00
|
|
|
private void AddInternal(int entityID, T component, bool isMain)
|
2023-06-29 23:53:26 +08:00
|
|
|
{
|
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
|
|
|
if (itemIndex > 0) EcsPoolThrowHalper.ThrowAlreadyHasComponent<T>(entityID);
|
|
|
|
#endif
|
|
|
|
if (_recycledItemsCount > 0)
|
|
|
|
{
|
|
|
|
itemIndex = _recycledItems[--_recycledItemsCount];
|
|
|
|
_itemsCount++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itemIndex = ++_itemsCount;
|
|
|
|
if (itemIndex >= _items.Length)
|
2023-07-17 15:55:23 +08:00
|
|
|
{
|
2023-06-29 23:53:26 +08:00
|
|
|
Array.Resize(ref _items, _items.Length << 1);
|
2023-07-17 15:55:23 +08:00
|
|
|
Array.Resize(ref _entities, _items.Length);
|
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
2023-12-06 18:58:06 +08:00
|
|
|
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
|
2023-06-29 23:53:26 +08:00
|
|
|
_listeners.InvokeOnAdd(entityID);
|
2023-12-20 23:21:10 +08:00
|
|
|
if (isMain)
|
2024-01-18 21:25:26 +08:00
|
|
|
{
|
2023-11-08 15:15:10 +08:00
|
|
|
component.OnAddToPool(_source.GetEntityLong(entityID));
|
2024-01-18 21:25:26 +08:00
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
_items[itemIndex] = component;
|
2023-07-17 15:55:23 +08:00
|
|
|
_entities[itemIndex] = entityID;
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
2023-11-08 15:15:10 +08:00
|
|
|
public void Add(int entityID, T component)
|
|
|
|
{
|
|
|
|
HybridMapping mapping = _source.GetHybridMapping(component.GetType());
|
2024-01-18 21:25:26 +08:00
|
|
|
mapping.GetTargetTypePool().AddRefInternal(entityID, component, true);
|
2023-11-08 15:15:10 +08:00
|
|
|
foreach (var pool in mapping.GetPools())
|
2024-01-18 21:25:26 +08:00
|
|
|
pool.AddRefInternal(entityID, component, false);
|
2023-11-08 15:15:10 +08:00
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
public void Set(int entityID, T component)
|
|
|
|
{
|
2023-12-20 23:21:10 +08:00
|
|
|
if (Has(entityID))
|
2023-11-08 15:15:10 +08:00
|
|
|
Del(entityID);
|
|
|
|
Add(entityID, component);
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public T Get(int entityID)
|
|
|
|
{
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
|
|
|
if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(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<T>(entityID);
|
|
|
|
#endif
|
|
|
|
return ref _items[_mapping[entityID]];
|
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public bool Has(int entityID)
|
|
|
|
{
|
|
|
|
return _mapping[entityID] > 0;
|
|
|
|
}
|
2023-11-08 15:15:10 +08:00
|
|
|
void IEcsHybridPoolInternal.DelInternal(int entityID, bool isMain)
|
|
|
|
{
|
|
|
|
DelInternal(entityID, isMain);
|
|
|
|
}
|
|
|
|
private void DelInternal(int entityID, bool isMain)
|
2023-06-29 23:53:26 +08:00
|
|
|
{
|
|
|
|
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
|
|
|
if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent<T>(entityID);
|
|
|
|
#endif
|
|
|
|
ref int itemIndex = ref _mapping[entityID];
|
|
|
|
T component = _items[itemIndex];
|
2023-12-20 23:21:10 +08:00
|
|
|
if (isMain)
|
2024-01-18 21:25:26 +08:00
|
|
|
{
|
2023-11-08 15:15:10 +08:00
|
|
|
component.OnDelFromPool(_source.GetEntityLong(entityID));
|
2024-01-18 21:25:26 +08:00
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
if (_recycledItemsCount >= _recycledItems.Length)
|
2024-01-18 21:25:26 +08:00
|
|
|
{
|
2023-06-29 23:53:26 +08:00
|
|
|
Array.Resize(ref _recycledItems, _recycledItems.Length << 1);
|
2024-01-18 21:25:26 +08:00
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
_recycledItems[_recycledItemsCount++] = itemIndex;
|
|
|
|
_mapping[entityID] = 0;
|
2023-07-17 15:55:23 +08:00
|
|
|
_entities[itemIndex] = 0;
|
2023-06-29 23:53:26 +08:00
|
|
|
_itemsCount--;
|
2023-12-06 18:58:06 +08:00
|
|
|
_mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit);
|
2023-06-29 23:53:26 +08:00
|
|
|
_listeners.InvokeOnDel(entityID);
|
|
|
|
}
|
2023-11-08 15:15:10 +08:00
|
|
|
public void Del(int entityID)
|
|
|
|
{
|
|
|
|
var component = Get(entityID);
|
|
|
|
HybridMapping mapping = _source.GetHybridMapping(component.GetType());
|
2024-01-18 21:25:26 +08:00
|
|
|
mapping.GetTargetTypePool().DelInternal(entityID, true);
|
2023-11-08 15:15:10 +08:00
|
|
|
foreach (var pool in mapping.GetPools())
|
2024-01-18 21:25:26 +08:00
|
|
|
{
|
|
|
|
pool.DelInternal(entityID, false);
|
|
|
|
}
|
2023-11-08 15:15:10 +08:00
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
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<T>(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<T>(fromEntityID);
|
|
|
|
#endif
|
|
|
|
toWorld.GetPool<T>().Set(toEntityID, Get(fromEntityID));
|
|
|
|
}
|
2023-07-17 15:55:23 +08:00
|
|
|
|
|
|
|
public void ClearNotAliveComponents()
|
|
|
|
{
|
|
|
|
for (int i = _itemsCount - 1; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (!_items[i].IsAlive)
|
|
|
|
Del(_entities[i]);
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Callbacks
|
2023-12-06 18:58:06 +08:00
|
|
|
void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID)
|
2023-11-08 15:15:10 +08:00
|
|
|
{
|
|
|
|
_source = world;
|
2023-12-06 18:58:06 +08:00
|
|
|
_mediator = mediator;
|
|
|
|
_componentTypeID = componentTypeID;
|
2024-01-07 18:52:54 +08:00
|
|
|
_maskBit = EcsMaskChunck.FromID(componentTypeID);
|
2023-11-08 15:15:10 +08:00
|
|
|
|
|
|
|
const int capacity = 512;
|
|
|
|
|
|
|
|
_mapping = new int[world.Capacity];
|
|
|
|
_recycledItems = new int[128];
|
|
|
|
_recycledItemsCount = 0;
|
|
|
|
_items = new T[capacity];
|
|
|
|
_entities = new int[capacity];
|
|
|
|
_itemsCount = 0;
|
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
void IEcsPoolImplementation.OnWorldResize(int newSize)
|
|
|
|
{
|
|
|
|
Array.Resize(ref _mapping, newSize);
|
|
|
|
}
|
|
|
|
void IEcsPoolImplementation.OnWorldDestroy() { }
|
|
|
|
void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer)
|
|
|
|
{
|
|
|
|
foreach (var entityID in buffer)
|
|
|
|
TryDel(entityID);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region Other
|
|
|
|
void IEcsPool.AddRaw(int entityID, object dataRaw) => Add(entityID, (T)dataRaw);
|
|
|
|
object IEcsPool.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<T> IEnumerable<T>.GetEnumerator() => throw new NotImplementedException();
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
/// <summary>Hybrid component</summary>
|
|
|
|
public interface IEcsHybridComponent
|
|
|
|
{
|
|
|
|
bool IsAlive { get; }
|
2023-06-30 01:01:19 +08:00
|
|
|
void OnAddToPool(entlong entity);
|
|
|
|
void OnDelFromPool(entlong entity);
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
2023-11-08 15:15:10 +08:00
|
|
|
public static class EcsHybridPoolExtensions
|
2023-10-31 03:03:13 +08:00
|
|
|
{
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static bool IsNullOrNotAlive(this IEcsHybridComponent self) => self == null || self.IsAlive;
|
2023-11-08 15:15:10 +08:00
|
|
|
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-06-29 23:53:26 +08:00
|
|
|
public static EcsHybridPool<T> GetPool<T>(this EcsWorld self) where T : IEcsHybridComponent
|
|
|
|
{
|
|
|
|
return self.GetPool<EcsHybridPool<T>>();
|
|
|
|
}
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-11-21 10:41:41 +08:00
|
|
|
public static EcsHybridPool<T> GetPoolUnchecked<T>(this EcsWorld self) where T : IEcsHybridComponent
|
2023-07-03 02:50:16 +08:00
|
|
|
{
|
2023-11-21 10:41:41 +08:00
|
|
|
return self.GetPoolUnchecked<EcsHybridPool<T>>();
|
2023-07-03 02:50:16 +08:00
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 01:28:18 +08:00
|
|
|
public static EcsHybridPool<T> Include<T>(this EcsAspect.Builder self) where T : IEcsHybridComponent
|
2023-06-29 23:53:26 +08:00
|
|
|
{
|
|
|
|
return self.Include<EcsHybridPool<T>>();
|
|
|
|
}
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 01:28:18 +08:00
|
|
|
public static EcsHybridPool<T> Exclude<T>(this EcsAspect.Builder self) where T : IEcsHybridComponent
|
2023-06-29 23:53:26 +08:00
|
|
|
{
|
|
|
|
return self.Exclude<EcsHybridPool<T>>();
|
|
|
|
}
|
2023-07-03 02:50:16 +08:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 01:28:18 +08:00
|
|
|
public static EcsHybridPool<T> Optional<T>(this EcsAspect.Builder self) where T : IEcsHybridComponent
|
2023-06-29 23:53:26 +08:00
|
|
|
{
|
|
|
|
return self.Optional<EcsHybridPool<T>>();
|
|
|
|
}
|
2023-11-21 10:41:41 +08:00
|
|
|
|
|
|
|
//-------------------------------------------------
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static EcsHybridPool<T> GetHybridPool<T>(this EcsWorld self) where T : IEcsHybridComponent
|
|
|
|
{
|
|
|
|
return self.GetPool<EcsHybridPool<T>>();
|
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
public static EcsHybridPool<T> GetHybridPoolUnchecked<T>(this EcsWorld self) where T : IEcsHybridComponent
|
|
|
|
{
|
|
|
|
return self.GetPoolUnchecked<EcsHybridPool<T>>();
|
|
|
|
}
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 01:28:18 +08:00
|
|
|
public static EcsHybridPool<T> IncludeHybrid<T>(this EcsAspect.Builder self) where T : IEcsHybridComponent
|
2023-11-21 10:41:41 +08:00
|
|
|
{
|
|
|
|
return self.Include<EcsHybridPool<T>>();
|
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 01:28:18 +08:00
|
|
|
public static EcsHybridPool<T> ExcludeHybrid<T>(this EcsAspect.Builder self) where T : IEcsHybridComponent
|
2023-11-21 10:41:41 +08:00
|
|
|
{
|
|
|
|
return self.Exclude<EcsHybridPool<T>>();
|
|
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-02-11 01:28:18 +08:00
|
|
|
public static EcsHybridPool<T> OptionalHybrid<T>(this EcsAspect.Builder self) where T : IEcsHybridComponent
|
2023-11-21 10:41:41 +08:00
|
|
|
{
|
|
|
|
return self.Optional<EcsHybridPool<T>>();
|
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|
2023-11-01 02:30:19 +08:00
|
|
|
|
2024-02-22 15:39:37 +08:00
|
|
|
public partial class EcsWorld
|
2023-11-08 15:15:10 +08:00
|
|
|
{
|
2023-12-06 18:58:06 +08:00
|
|
|
private Dictionary<Type, HybridMapping> _hybridMapping = new Dictionary<Type, HybridMapping>();
|
2023-11-08 15:15:10 +08:00
|
|
|
internal HybridMapping GetHybridMapping(Type type)
|
|
|
|
{
|
2023-12-20 23:21:10 +08:00
|
|
|
if (!_hybridMapping.TryGetValue(type, out HybridMapping mapping))
|
2023-11-08 15:15:10 +08:00
|
|
|
{
|
|
|
|
mapping = new HybridMapping(this, type);
|
2023-12-06 18:58:06 +08:00
|
|
|
_hybridMapping.Add(type, mapping);
|
2023-11-08 15:15:10 +08:00
|
|
|
}
|
|
|
|
return mapping;
|
|
|
|
}
|
|
|
|
}
|
2023-11-01 02:30:19 +08:00
|
|
|
|
2023-11-08 15:15:10 +08:00
|
|
|
internal class HybridMapping
|
|
|
|
{
|
|
|
|
private EcsWorld _source;
|
|
|
|
private object[] _sourceForReflection;
|
|
|
|
private Type _type;
|
2023-11-01 02:30:19 +08:00
|
|
|
|
2023-11-08 15:15:10 +08:00
|
|
|
private IEcsHybridPoolInternal _targetTypePool;
|
|
|
|
private List<IEcsHybridPoolInternal> _relatedPools;
|
2023-11-01 02:30:19 +08:00
|
|
|
|
2023-12-20 23:21:10 +08:00
|
|
|
private static Type hybridPoolType = typeof(EcsHybridPool<>);
|
2023-11-08 15:15:10 +08:00
|
|
|
private static MethodInfo getHybridPoolMethod = typeof(EcsHybridPoolExtensions).GetMethod($"{nameof(EcsHybridPoolExtensions.GetPool)}", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
|
2023-11-01 02:30:19 +08:00
|
|
|
|
2023-11-08 15:15:10 +08:00
|
|
|
private static HashSet<Type> _hybridComponents = new HashSet<Type>();
|
|
|
|
static HybridMapping()
|
2023-11-01 02:30:19 +08:00
|
|
|
{
|
2023-11-08 15:15:10 +08:00
|
|
|
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);
|
2023-11-01 02:30:19 +08:00
|
|
|
}
|
|
|
|
|
2023-11-08 15:15:10 +08:00
|
|
|
public HybridMapping(EcsWorld source, Type type)
|
|
|
|
{
|
|
|
|
if (!type.IsClass)
|
|
|
|
throw new ArgumentException();
|
|
|
|
|
|
|
|
_source = source;
|
|
|
|
_type = type;
|
|
|
|
_relatedPools = new List<IEcsHybridPoolInternal>();
|
2023-12-20 23:21:10 +08:00
|
|
|
_sourceForReflection = new object[] { source };
|
2023-11-08 15:15:10 +08:00
|
|
|
_targetTypePool = CreateHybridPool(type);
|
|
|
|
foreach (var item in type.GetInterfaces())
|
|
|
|
{
|
2023-12-20 23:21:10 +08:00
|
|
|
if (IsEcsHybridComponentType(item))
|
2023-11-08 15:15:10 +08:00
|
|
|
{
|
|
|
|
_relatedPools.Add(CreateHybridPool(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Type baseType = type.BaseType;
|
2023-12-20 23:21:10 +08:00
|
|
|
while (baseType != typeof(object) && IsEcsHybridComponentType(baseType))
|
2023-11-08 15:15:10 +08:00
|
|
|
{
|
|
|
|
_relatedPools.Add(CreateHybridPool(baseType));
|
|
|
|
baseType = baseType.BaseType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private IEcsHybridPoolInternal CreateHybridPool(Type componentType)
|
2023-11-01 02:30:19 +08:00
|
|
|
{
|
2023-11-08 15:15:10 +08:00
|
|
|
//var x = (IEcsHybridPoolInternal)getHybridPoolMethod.MakeGenericMethod(componentType).Invoke(null, _sourceForReflection);
|
|
|
|
//Debug.Log("_" + x.ComponentID + "_" +x.ComponentType.Name);
|
|
|
|
//return x;
|
|
|
|
return (IEcsHybridPoolInternal)getHybridPoolMethod.MakeGenericMethod(componentType).Invoke(null, _sourceForReflection);
|
2023-11-01 02:30:19 +08:00
|
|
|
}
|
|
|
|
|
2023-11-08 15:15:10 +08:00
|
|
|
public IEcsHybridPoolInternal GetTargetTypePool()
|
|
|
|
{
|
|
|
|
return _targetTypePool;
|
|
|
|
}
|
|
|
|
public List<IEcsHybridPoolInternal> GetPools()
|
|
|
|
{
|
|
|
|
return _relatedPools;
|
2023-11-01 02:30:19 +08:00
|
|
|
}
|
|
|
|
}
|
2023-06-29 23:53:26 +08:00
|
|
|
}
|