This commit is contained in:
CortexCore
2025-04-14 15:39:28 +08:00
parent c1273357de
commit d8b8ddb8b6
23 changed files with 447 additions and 116 deletions

View File

@@ -7,4 +7,9 @@ namespace BITKit.Entities
{
}
public class OwnedByLocalPlayer
{
}
}

View File

@@ -22,20 +22,18 @@ namespace BITKit.Entities
}
}
public unsafe class EntitiesService : IEntitiesService, IDisposable
public class EntitiesService : IEntitiesService, IDisposable
{
private static int _count;
private readonly ILogger<EntitiesService> _logger;
private readonly ITicker _ticker;
private readonly ConcurrentQueue<IEntity> _onAddQueue = new();
private readonly HashSet<IEntity> _addingEntities = new();
private readonly Dictionary<int, HashSet<int>> _typeCaches = new();
private readonly Dictionary<int, ConcurrentDictionary<int, object>> _typeInstances = new();
private readonly ConcurrentDictionary<int, object> _staticCaches = new();
public EntitiesService(ILogger<EntitiesService> logger, ITicker ticker)
{
_logger = logger;
@@ -48,35 +46,17 @@ namespace BITKit.Entities
logger.LogInformation($"已创建EntitiesService,当前有:{_count}个实例");
_ticker?.Add(OnTick);
OnAdd += ClearCache;
OnRemove += ClearCache;
OnRemove += UnRegisterEntity;
}
private void UnRegisterEntity(IEntity entity)
{
}
private void ClearCache(IEntity obj)
{
foreach (var id in _cachedIdCollection)
{
var hashSet = _typeCaches[id];
_pool.Return(hashSet);
_typeCaches.Remove(id);
}
_cachedIdCollection.Clear();
_staticCaches.Clear();
}
private void OnTick(float obj)
{
while (_onAddQueue.TryDequeue(out var entity))
{
if(_addingEntities.Remove(entity) is false)continue;
OnAdd?.Invoke(entity);
_staticCaches.Clear();
MakeCache(entity);
}
_addingEntities.Clear();
}
private void MakeCache(IEntity entity)
@@ -94,7 +74,6 @@ namespace BITKit.Entities
public event Action<IEntity> OnRemove;
IReadOnlyDictionary<int, IEntity> IEntitiesService.Entities => _entitiesInternal;
private readonly Pool<HashSet<int>> _pool;
private readonly HashSet<int> _cachedIdCollection = new();
private HashSet<int> ObjectGenerator()
{
@@ -106,16 +85,14 @@ namespace BITKit.Entities
if (!_entitiesInternal.TryAdd(entity.Id, entity)) return false;
if (_ticker is not null)
{
_onAddQueue.Enqueue(entity);
_addingEntities.Add(entity);
_onAddQueue.Enqueue(entity);
}
else
{
_staticCaches.Clear();
OnAdd?.Invoke(entity);
MakeCache(entity);
}
return true;
}
@@ -123,7 +100,8 @@ namespace BITKit.Entities
public bool UnRegister(IEntity entity)
{
if (!_entitiesInternal.TryRemove(entity.Id)) return false;
OnRemove?.Invoke(entity);
_addingEntities.Remove(entity);
foreach (var serviceDescriptor in entity.ServiceCollection)
{
@@ -134,7 +112,7 @@ namespace BITKit.Entities
_typeInstances.TryRemove(entity.Id);
_staticCaches.Clear();
OnRemove?.Invoke(entity);
return true;
}
@@ -379,36 +357,24 @@ namespace BITKit.Entities
where T3 : class
where T4 : class
{
var combinedHash = typeof(T).GetHashCode() +
typeof(T1).GetHashCode() +
typeof(T2).GetHashCode() +
typeof(T3).GetHashCode() +
typeof(T4).GetHashCode();
if (_typeCaches.TryGetValue(combinedHash, out var collection) is false)
{
var hashset = _typeCaches.GetOrCreate(typeof(T).GetHashCode());
var t1Set = _typeCaches.GetOrCreate(typeof(T1).GetHashCode());
var t2Set = _typeCaches.GetOrCreate(typeof(T2).GetHashCode());
var t3Set = _typeCaches.GetOrCreate(typeof(T3).GetHashCode());
var t4Set = _typeCaches.GetOrCreate(typeof(T4).GetHashCode());
var pool = ArrayPool<(T, T1, T2, T3,T4)>.Shared;
collection = _pool.Take();
collection.Clear();
var hashset = _typeCaches.GetOrCreate(typeof(T).GetHashCode());
var t1Set = _typeCaches.GetOrCreate(typeof(T1).GetHashCode());
var t2Set = _typeCaches.GetOrCreate(typeof(T2).GetHashCode());
var t3Set = _typeCaches.GetOrCreate(typeof(T3).GetHashCode());
var t4Set = _typeCaches.GetOrCreate(typeof(T4).GetHashCode());
collection.UnionWith(hashset);
collection.IntersectWith(t1Set);
collection.IntersectWith(t2Set);
collection.IntersectWith(t3Set);
collection.IntersectWith(t4Set);
var count = math.max(hashset.Count, math.max(t1Set.Count, math.max(t2Set.Count,math.max(t3Set.Count,t4Set.Count))));
_typeCaches[combinedHash] = collection;
_cachedIdCollection.Add(combinedHash);
}
var pool = ArrayPool<(T, T1, T2, T3, T4)>.Shared;
var array = pool.Rent(math.ceilpow2(collection.Count));
var array = pool.Rent(count);
var collection = _pool.Take();
collection.Clear();
collection.UnionWith(hashset);
collection.IntersectWith(t1Set);
collection.IntersectWith(t2Set);
collection.IntersectWith(t3Set);
var i = 0;
foreach (var id in collection)
@@ -440,24 +406,24 @@ namespace BITKit.Entities
{
instances[h3] = v3 = _entitiesInternal[id].ServiceProvider.GetRequiredService<T3>();
}
if (instances.TryGetValue(h4, out var v4) is false)
{
instances[h4] = v4 = _entitiesInternal[id].ServiceProvider.GetRequiredService<T4>();
}
array[i] = (Unsafe.As<T>(v0), Unsafe.As<T1>(v1), Unsafe.As<T2>(v2), Unsafe.As<T3>(v3),
Unsafe.As<T4>(v4));
array[i] = (Unsafe.As<T>(v0), Unsafe.As<T1>(v1), Unsafe.As<T2>(v2), Unsafe.As<T3>(v3),Unsafe.As<T4>(v4));
i++;
}
try
{
return new Span<(T, T1, T2, T3, T4)>(array, 0, i);
return new Span<(T, T1, T2, T3,T4)>(array, 0, i);
}
finally
{
pool.Return(array);
_pool.Return(collection);
}
}
@@ -541,6 +507,8 @@ namespace BITKit.Entities
}
pool.Return(array);
_pool.Return(collection);
return new Span<(T, T1, T2, T3, T4, T5)>(array, 0, i);
}

View File

@@ -154,12 +154,7 @@ namespace BITKit
}
public static bool TryRemove<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey t)
{
if (self.ContainsKey(t))
{
self.Remove(t);
return true;
}
return false;
return self.ContainsKey(t) && self.Remove(t);
}
public static void Set<TKey, TValue>(this IDictionary<TKey, TValue> self, TKey key, TValue value)
{

View File

@@ -27,6 +27,7 @@ namespace BITKit
/// 获取所有Item的只读副本
/// </summary>
IRuntimeItem[] GetItems();
IReadOnlyDictionary<int,IRuntimeItem> ItemDictionary { get; }
/// <summary>
/// 添加物品的接口
/// </summary>
@@ -104,6 +105,7 @@ namespace BITKit
public ValidHandle IsBusy { get; } = new();
public int Id { get; set; }
public IRuntimeItem[] GetItems()=>Items.Values.ToArray();
public IReadOnlyDictionary<int, IRuntimeItem> ItemDictionary => Items;
public bool Add(IRuntimeItem item)
{

View File

@@ -43,7 +43,14 @@ namespace BITKit.Tween
{
from = func(from, to, delta*BITApp.Time.DeltaTime);
#if UNITY_5_3_OR_NEWER
await UniTask.NextFrame(PlayerLoopTiming.FixedUpdate,cancellationToken);
try
{
await UniTask.NextFrame(PlayerLoopTiming.FixedUpdate, cancellationToken);
}
catch (OperationCanceledException)
{
return;
}
#else
await UniTask.Yield();