2025-03-14 16:53:25 +08:00
|
|
|
|
#if DISABLE_DEBUG
|
|
|
|
|
#undef DEBUG
|
|
|
|
|
#endif
|
|
|
|
|
using DCFApixels.DragonECS.Internal;
|
2023-06-25 23:22:01 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2024-11-01 12:41:10 +08:00
|
|
|
|
using System.ComponentModel;
|
2024-12-04 16:09:52 +08:00
|
|
|
|
using System.Linq;
|
2023-06-25 23:22:01 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
|
|
|
|
namespace DCFApixels.DragonECS
|
|
|
|
|
{
|
2024-02-22 15:39:37 +08:00
|
|
|
|
public partial class EcsWorld
|
2023-06-25 23:22:01 +08:00
|
|
|
|
{
|
2024-12-04 16:09:52 +08:00
|
|
|
|
#region Consts
|
2024-03-07 03:17:51 +08:00
|
|
|
|
private const short NULL_WORLD_ID = 0;
|
|
|
|
|
|
2024-11-15 15:08:44 +08:00
|
|
|
|
private const short GEN_STATUS_SEPARATOR = 0;
|
|
|
|
|
private const short GEN_WAKEUP_MASK = 0x7fff;
|
|
|
|
|
private const short GEN_SLEEP_MASK = ~GEN_WAKEUP_MASK;
|
2024-04-16 12:46:09 +08:00
|
|
|
|
|
2023-11-22 11:53:41 +08:00
|
|
|
|
private const int DEL_ENT_BUFFER_SIZE_OFFSET = 5;
|
|
|
|
|
private const int DEL_ENT_BUFFER_MIN_SIZE = 64;
|
2024-12-04 16:09:52 +08:00
|
|
|
|
#endregion
|
2023-06-25 23:22:01 +08:00
|
|
|
|
|
2024-02-15 20:28:38 +08:00
|
|
|
|
private static EcsWorld[] _worlds = Array.Empty<EcsWorld>();
|
2025-03-10 22:36:12 +08:00
|
|
|
|
private static readonly IdDispenser _worldIdDispenser = new IdDispenser(4, 0, n => Array.Resize(ref _worlds, n));
|
2023-06-25 23:22:01 +08:00
|
|
|
|
|
2024-12-04 16:09:52 +08:00
|
|
|
|
private static StructList<WorldComponentPoolAbstract> _allWorldComponentPools = new StructList<WorldComponentPoolAbstract>(64);
|
|
|
|
|
private StructList<WorldComponentPoolAbstract> _worldComponentPools;
|
|
|
|
|
private int _builtinWorldComponentsCount = 0;
|
2024-09-09 18:21:21 +08:00
|
|
|
|
private static readonly object _worldLock = new object();
|
2024-09-09 14:25:32 +08:00
|
|
|
|
|
2023-06-25 23:22:01 +08:00
|
|
|
|
static EcsWorld()
|
|
|
|
|
{
|
2024-03-07 03:17:51 +08:00
|
|
|
|
_worlds[NULL_WORLD_ID] = new NullWorld();
|
2023-06-25 23:22:01 +08:00
|
|
|
|
}
|
2024-12-04 16:09:52 +08:00
|
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static EcsWorld GetWorld(short worldID)
|
|
|
|
|
{// ts
|
|
|
|
|
return _worlds[worldID];
|
|
|
|
|
}
|
2025-03-15 15:00:07 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public static bool TryGetWorld(short worldID, out EcsWorld world)
|
|
|
|
|
{// ts
|
|
|
|
|
world = _worlds[worldID];
|
|
|
|
|
return
|
|
|
|
|
world != null &&
|
|
|
|
|
world.IsDestroyed != false &&
|
|
|
|
|
worldID != 0;
|
|
|
|
|
}
|
2024-12-04 16:09:52 +08:00
|
|
|
|
|
|
|
|
|
private void ReleaseData(short worldID)
|
2024-09-09 14:25:32 +08:00
|
|
|
|
{// ts
|
2024-09-09 18:21:21 +08:00
|
|
|
|
lock (_worldLock)
|
2024-02-15 00:53:20 +08:00
|
|
|
|
{
|
2024-12-04 16:09:52 +08:00
|
|
|
|
foreach (var controller in _worldComponentPools)
|
2024-09-09 14:25:32 +08:00
|
|
|
|
{
|
2024-12-04 16:09:52 +08:00
|
|
|
|
controller.Release(worldID);
|
2024-09-09 14:25:32 +08:00
|
|
|
|
}
|
2024-12-04 16:09:52 +08:00
|
|
|
|
_worldComponentPools.Clear();
|
2024-02-15 00:53:20 +08:00
|
|
|
|
}
|
2023-06-25 23:22:01 +08:00
|
|
|
|
}
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-12-04 16:09:52 +08:00
|
|
|
|
public static ref T GetData<T>(short worldID)
|
|
|
|
|
{
|
|
|
|
|
return ref WorldComponentPool<T>.GetForWorld(worldID);
|
2024-04-16 12:46:09 +08:00
|
|
|
|
}
|
2023-06-25 23:22:01 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-12-04 16:09:52 +08:00
|
|
|
|
public static bool HasData<T>(short worldID)
|
2024-04-16 12:46:09 +08:00
|
|
|
|
{
|
2024-12-04 16:09:52 +08:00
|
|
|
|
return WorldComponentPool<T>.Has(worldID);
|
2024-04-16 12:46:09 +08:00
|
|
|
|
}
|
2023-07-03 02:44:35 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-12-04 16:09:52 +08:00
|
|
|
|
public static ref T GetDataUnchecked<T>(short worldID)
|
2024-04-16 12:46:09 +08:00
|
|
|
|
{
|
|
|
|
|
return ref WorldComponentPool<T>.GetForWorldUnchecked(worldID);
|
|
|
|
|
}
|
2023-06-25 23:22:01 +08:00
|
|
|
|
|
2025-03-10 22:36:12 +08:00
|
|
|
|
public static void ResetStaticState()
|
2025-03-10 22:28:54 +08:00
|
|
|
|
{
|
|
|
|
|
for (int i = 1; i < _worlds.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
var world = _worlds[i];
|
|
|
|
|
if (world == null) { continue; }
|
|
|
|
|
|
2025-03-11 14:30:33 +08:00
|
|
|
|
if (world.IsDestroyed == false)
|
2025-03-10 22:28:54 +08:00
|
|
|
|
{
|
|
|
|
|
world.Destroy();
|
|
|
|
|
}
|
|
|
|
|
world = null;
|
|
|
|
|
}
|
2025-03-10 22:36:12 +08:00
|
|
|
|
_worldIdDispenser.ReleaseAll();
|
2025-03-10 22:28:54 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-04 16:09:52 +08:00
|
|
|
|
#region WorldComponentPool
|
|
|
|
|
public ReadOnlySpan<WorldComponentPoolAbstract> GetWorldComponents()
|
2023-06-25 23:22:01 +08:00
|
|
|
|
{
|
2024-12-31 23:55:38 +08:00
|
|
|
|
return new ReadOnlySpan<WorldComponentPoolAbstract>(
|
|
|
|
|
_worldComponentPools._items,
|
|
|
|
|
_builtinWorldComponentsCount,
|
|
|
|
|
_worldComponentPools._count - _builtinWorldComponentsCount);
|
|
|
|
|
//return new ReadOnlySpan<WorldComponentPoolAbstract>(_worldComponentPools._items, 0, _builtinWorldComponentsCount);
|
2024-12-04 16:09:52 +08:00
|
|
|
|
}
|
|
|
|
|
public ReadOnlySpan<WorldComponentPoolAbstract> GetAllWorldComponents()
|
|
|
|
|
{
|
|
|
|
|
return _worldComponentPools.ToReadOnlySpan();
|
|
|
|
|
}
|
|
|
|
|
public abstract class WorldComponentPoolAbstract
|
|
|
|
|
{
|
|
|
|
|
protected static readonly Type[] _builtinTypes = new Type[]
|
|
|
|
|
{
|
|
|
|
|
typeof(AspectCache<>),
|
|
|
|
|
typeof(PoolCache<>),
|
|
|
|
|
typeof(WhereQueryCache<,>),
|
|
|
|
|
typeof(EcsMask.WorldMaskComponent),
|
|
|
|
|
};
|
|
|
|
|
internal readonly bool _isBuiltin;
|
|
|
|
|
protected WorldComponentPoolAbstract()
|
|
|
|
|
{
|
|
|
|
|
Type type = ComponentType;
|
|
|
|
|
if (type.IsGenericType) { type = type.GetGenericTypeDefinition(); }
|
|
|
|
|
_isBuiltin = Array.IndexOf(_builtinTypes, type) >= 0;
|
|
|
|
|
}
|
|
|
|
|
public abstract Type ComponentType { get; }
|
|
|
|
|
public abstract void Has(short worldID);
|
|
|
|
|
public abstract void Release(short worldID);
|
|
|
|
|
public abstract object GetRaw(short worldID);
|
|
|
|
|
public abstract void SetRaw(short worldID, object raw);
|
2023-06-25 23:22:01 +08:00
|
|
|
|
}
|
|
|
|
|
private static class WorldComponentPool<T>
|
|
|
|
|
{
|
|
|
|
|
private static T[] _items = new T[4];
|
2023-06-27 01:23:01 +08:00
|
|
|
|
private static short[] _mapping = new short[4];
|
|
|
|
|
private static short _count;
|
|
|
|
|
private static short[] _recycledItems = new short[4];
|
|
|
|
|
private static short _recycledItemsCount;
|
2023-06-25 23:22:01 +08:00
|
|
|
|
private static IEcsWorldComponent<T> _interface = EcsWorldComponentHandler<T>.instance;
|
2024-12-04 16:09:52 +08:00
|
|
|
|
private static Abstract _controller = new Abstract();
|
|
|
|
|
static WorldComponentPool()
|
|
|
|
|
{
|
|
|
|
|
_allWorldComponentPools.Add(_controller);
|
|
|
|
|
}
|
2023-06-25 23:22:01 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-09-09 18:21:21 +08:00
|
|
|
|
public static ref T GetItem(int itemIndex)
|
2024-09-09 14:25:32 +08:00
|
|
|
|
{// ts
|
2023-12-31 13:07:53 +08:00
|
|
|
|
return ref _items[itemIndex];
|
|
|
|
|
}
|
2023-06-25 23:22:01 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-12-04 16:09:52 +08:00
|
|
|
|
public static ref T GetForWorld(short worldID)
|
2024-09-09 14:25:32 +08:00
|
|
|
|
{// зависит от GetItemIndex
|
2024-09-09 18:21:21 +08:00
|
|
|
|
return ref GetItem(GetItemIndex(worldID));
|
2023-12-31 13:07:53 +08:00
|
|
|
|
}
|
2023-07-02 16:17:13 +08:00
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2024-12-04 16:09:52 +08:00
|
|
|
|
public static ref T GetForWorldUnchecked(short worldID)
|
2024-09-09 14:25:32 +08:00
|
|
|
|
{// ts
|
2025-03-15 15:00:07 +08:00
|
|
|
|
#if DEBUG
|
2024-11-06 19:55:33 +08:00
|
|
|
|
if (_mapping[worldID] <= 0) { Throw.ArgumentOutOfRange(); }
|
2023-12-31 13:07:53 +08:00
|
|
|
|
#endif
|
|
|
|
|
return ref _items[_mapping[worldID]];
|
|
|
|
|
}
|
2024-12-04 16:09:52 +08:00
|
|
|
|
public static int GetItemIndex(short worldID)
|
2024-09-09 14:25:32 +08:00
|
|
|
|
{// ts
|
2024-02-15 20:28:38 +08:00
|
|
|
|
if (_mapping.Length < _worlds.Length)
|
2023-12-31 21:02:53 +08:00
|
|
|
|
{
|
2024-09-09 18:21:21 +08:00
|
|
|
|
lock (_worldLock)
|
2023-06-25 23:22:01 +08:00
|
|
|
|
{
|
2024-09-09 14:25:32 +08:00
|
|
|
|
if (_mapping.Length < _worlds.Length)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _mapping, _worlds.Length);
|
|
|
|
|
}
|
2023-06-25 23:22:01 +08:00
|
|
|
|
}
|
2024-09-09 14:25:32 +08:00
|
|
|
|
}
|
|
|
|
|
short itemIndex = _mapping[worldID];
|
|
|
|
|
|
|
|
|
|
if (itemIndex == 0)
|
|
|
|
|
{
|
2024-09-09 18:21:21 +08:00
|
|
|
|
lock (_worldLock)
|
2023-12-31 21:02:53 +08:00
|
|
|
|
{
|
2024-09-09 14:25:32 +08:00
|
|
|
|
itemIndex = _mapping[worldID];
|
|
|
|
|
if (itemIndex <= 0)
|
|
|
|
|
{
|
|
|
|
|
if (_recycledItemsCount > 0)
|
|
|
|
|
{
|
|
|
|
|
_count++;
|
|
|
|
|
itemIndex = _recycledItems[--_recycledItemsCount];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
itemIndex = ++_count;
|
|
|
|
|
}
|
|
|
|
|
_mapping[worldID] = itemIndex;
|
|
|
|
|
|
|
|
|
|
if (_items.Length <= itemIndex)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _items, _items.Length << 1);
|
|
|
|
|
}
|
2024-09-09 18:21:21 +08:00
|
|
|
|
|
2025-03-10 13:00:30 +08:00
|
|
|
|
#if DEBUG
|
|
|
|
|
AllowedInWorldsAttribute.CheckAllows<T>(_worlds[worldID]);
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-09-09 14:25:32 +08:00
|
|
|
|
_interface.Init(ref _items[itemIndex], _worlds[worldID]);
|
2024-12-04 16:09:52 +08:00
|
|
|
|
|
|
|
|
|
var world = GetWorld(worldID);
|
|
|
|
|
world._worldComponentPools.Add(_controller);
|
|
|
|
|
if (_controller._isBuiltin)
|
|
|
|
|
{
|
|
|
|
|
world._builtinWorldComponentsCount++;
|
|
|
|
|
world._worldComponentPools.SwapAt(
|
|
|
|
|
world._worldComponentPools.Count - 1,
|
|
|
|
|
world._builtinWorldComponentsCount - 1);
|
|
|
|
|
}
|
2024-09-09 14:25:32 +08:00
|
|
|
|
}
|
2023-12-31 21:02:53 +08:00
|
|
|
|
}
|
2023-06-25 23:22:01 +08:00
|
|
|
|
}
|
|
|
|
|
return itemIndex;
|
|
|
|
|
}
|
2024-12-04 16:09:52 +08:00
|
|
|
|
private static void Release(short worldID)
|
2024-09-09 14:25:32 +08:00
|
|
|
|
{// ts
|
2024-09-09 18:21:21 +08:00
|
|
|
|
lock (_worldLock)
|
2023-06-25 23:22:01 +08:00
|
|
|
|
{
|
2024-09-09 14:25:32 +08:00
|
|
|
|
if (_mapping.Length < _worlds.Length)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _mapping, _worlds.Length);
|
|
|
|
|
}
|
|
|
|
|
ref short itemIndex = ref _mapping[worldID];
|
|
|
|
|
if (itemIndex != 0)
|
|
|
|
|
{
|
|
|
|
|
_interface.OnDestroy(ref _items[itemIndex], _worlds[worldID]);
|
2024-09-11 10:37:18 +08:00
|
|
|
|
if (_recycledItemsCount >= _recycledItems.Length)
|
2024-09-10 09:29:08 +08:00
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _recycledItems, _recycledItems.Length << 1);
|
|
|
|
|
}
|
2024-09-09 14:25:32 +08:00
|
|
|
|
_recycledItems[_recycledItemsCount++] = itemIndex;
|
|
|
|
|
itemIndex = 0;
|
|
|
|
|
}
|
2023-06-25 23:22:01 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-04 16:09:52 +08:00
|
|
|
|
public static bool Has(short worldID)
|
|
|
|
|
{// ts
|
|
|
|
|
if (_mapping.Length < _worlds.Length)
|
|
|
|
|
{
|
|
|
|
|
lock (_worldLock)
|
|
|
|
|
{
|
|
|
|
|
if (_mapping.Length < _worlds.Length)
|
|
|
|
|
{
|
|
|
|
|
Array.Resize(ref _mapping, _worlds.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
short itemIndex = _mapping[worldID];
|
|
|
|
|
return itemIndex > 0;
|
|
|
|
|
}
|
|
|
|
|
private sealed class Abstract : WorldComponentPoolAbstract
|
2023-06-25 23:22:01 +08:00
|
|
|
|
{
|
2024-12-04 16:09:52 +08:00
|
|
|
|
public sealed override Type ComponentType
|
|
|
|
|
{
|
|
|
|
|
get { return typeof(T); }
|
|
|
|
|
}
|
|
|
|
|
public override void SetRaw(short worldID, object raw)
|
|
|
|
|
{
|
|
|
|
|
WorldComponentPool<T>.GetItem(worldID) = (T)raw;
|
|
|
|
|
}
|
|
|
|
|
public sealed override void Has(short worldID)
|
|
|
|
|
{
|
|
|
|
|
WorldComponentPool<T>.Has(worldID);
|
|
|
|
|
}
|
|
|
|
|
public sealed override object GetRaw(short worldID)
|
|
|
|
|
{
|
|
|
|
|
return WorldComponentPool<T>.GetItem(worldID);
|
|
|
|
|
}
|
|
|
|
|
public sealed override void Release(short worldID)
|
2023-06-25 23:22:01 +08:00
|
|
|
|
{
|
|
|
|
|
WorldComponentPool<T>.Release(worldID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-04 16:09:52 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2025-03-10 22:28:54 +08:00
|
|
|
|
#region NullWorld
|
2024-02-03 01:12:53 +08:00
|
|
|
|
private sealed class NullWorld : EcsWorld
|
|
|
|
|
{
|
2024-12-03 16:59:32 +08:00
|
|
|
|
internal NullWorld() : base(new EcsWorldConfig(4, 4, 4, 4, 4), null, 0) { }
|
2024-02-03 01:12:53 +08:00
|
|
|
|
}
|
2024-11-01 12:41:10 +08:00
|
|
|
|
|
2025-03-10 22:28:54 +08:00
|
|
|
|
#endregion
|
|
|
|
|
|
2024-12-04 16:09:52 +08:00
|
|
|
|
#region DebuggerProxy
|
|
|
|
|
protected partial class DebuggerProxy
|
|
|
|
|
{
|
|
|
|
|
private short _worldID;
|
|
|
|
|
public IEnumerable<object> WorldComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
_worldID = _world.ID;
|
|
|
|
|
return _world._worldComponentPools.ToEnumerable().Skip(_world._builtinWorldComponentsCount).Select(o => o.GetRaw(_worldID));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public IEnumerable<object> AllWorldComponents
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
_worldID = _world.ID;
|
|
|
|
|
return _world._worldComponentPools.ToEnumerable().Select(o => o.GetRaw(_worldID));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
|
|
|
|
|
2024-11-01 12:41:10 +08:00
|
|
|
|
#region Obsolete
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
|
[Obsolete("Use EcsWorld.ID")]
|
|
|
|
|
public short id
|
|
|
|
|
{
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
get { return ID; }
|
|
|
|
|
}
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
|
[Obsolete("The GetPoolInstance(int componentTypeID) method will be removed in future updates, use FindPoolInstance(Type componentType)")]
|
|
|
|
|
public IEcsPool GetPoolInstance(int componentTypeID)
|
|
|
|
|
{
|
|
|
|
|
return FindPoolInstance(componentTypeID);
|
|
|
|
|
}
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
|
[Obsolete("The GetPoolInstance(Type componentType) method will be removed in future updates, use FindPoolInstance(Type componentType)")]
|
|
|
|
|
public IEcsPool GetPoolInstance(Type componentType)
|
|
|
|
|
{
|
|
|
|
|
return FindPoolInstance(componentType);
|
|
|
|
|
}
|
|
|
|
|
#endregion
|
2023-07-04 00:00:25 +08:00
|
|
|
|
}
|
2024-12-04 16:09:52 +08:00
|
|
|
|
}
|