You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.8 KiB
64 lines
1.8 KiB
3 years ago
|
namespace ET
|
||
|
{
|
||
|
public class UnitCacheDestroySystem : DestroySystem<UnitCache>
|
||
|
{
|
||
|
public override void Destroy(UnitCache self)
|
||
|
{
|
||
|
foreach (var entity in self.CacheComponentsDict.Values)
|
||
|
{
|
||
|
entity.Dispose();
|
||
|
}
|
||
|
|
||
|
self.CacheComponentsDict.Clear();
|
||
|
self.key = null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
[FriendClass(typeof(UnitCache))]
|
||
|
public static class UnitCacheSystem
|
||
|
{
|
||
|
public static void AddOrUpdate(this UnitCache self, Entity entity)
|
||
|
{
|
||
|
if (entity == null || entity.IsDisposed)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (self.CacheComponentsDict.TryGetValue(entity.Id, out Entity oldEntity))
|
||
|
{
|
||
|
if (entity != oldEntity)
|
||
|
{
|
||
|
oldEntity.Dispose();
|
||
|
}
|
||
|
|
||
|
self.CacheComponentsDict.Remove(entity.Id);
|
||
|
}
|
||
|
|
||
|
self.CacheComponentsDict.Add(entity.Id, entity);
|
||
|
}
|
||
|
|
||
|
public static async ETTask<Entity> Get(this UnitCache self, long unitId)
|
||
|
{
|
||
|
Entity entity = null;
|
||
|
if (!self.CacheComponentsDict.TryGetValue(unitId, out entity))
|
||
|
{
|
||
|
entity = await DBManagerComponent.Instance.GetZoneDB(self.DomainZone()).Query<Entity>(unitId, self.key);
|
||
|
if (entity != null)
|
||
|
{
|
||
|
self.AddOrUpdate(entity);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return entity;
|
||
|
}
|
||
|
|
||
|
public static void Delete(this UnitCache self, long unitId)
|
||
|
{
|
||
|
if (self.CacheComponentsDict.TryGetValue(unitId, out Entity entity))
|
||
|
{
|
||
|
entity.Dispose();
|
||
|
self.CacheComponentsDict.Remove(unitId);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|