LocationComponent.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections.Generic;
  2. namespace ET
  3. {
  4. [ObjectSystem]
  5. public class LockInfoAwakeSystem: AwakeSystem<LockInfo, long, CoroutineLock>
  6. {
  7. public override void Awake(LockInfo self, long lockInstanceId, CoroutineLock coroutineLock)
  8. {
  9. self.CoroutineLock = coroutineLock;
  10. self.LockInstanceId = lockInstanceId;
  11. }
  12. }
  13. public class LockInfo: Entity
  14. {
  15. public long LockInstanceId;
  16. public CoroutineLock CoroutineLock;
  17. public override void Dispose()
  18. {
  19. if (this.IsDisposed)
  20. {
  21. return;
  22. }
  23. base.Dispose();
  24. this.CoroutineLock.Dispose();
  25. LockInstanceId = 0;
  26. }
  27. }
  28. public class LocationComponent: Entity
  29. {
  30. public readonly Dictionary<long, long> locations = new Dictionary<long, long>();
  31. private readonly Dictionary<long, LockInfo> lockInfos = new Dictionary<long, LockInfo>();
  32. public async ETTask Add(long key, long instanceId)
  33. {
  34. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
  35. {
  36. this.locations[key] = instanceId;
  37. Log.Debug($"location add key: {key} instanceId: {instanceId}");
  38. }
  39. }
  40. public async ETTask Remove(long key)
  41. {
  42. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
  43. {
  44. this.locations.Remove(key);
  45. Log.Debug($"location remove key: {key}");
  46. }
  47. }
  48. public async ETVoid Lock(long key, long instanceId, int time = 0)
  49. {
  50. CoroutineLock coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key);
  51. LockInfo lockInfo = EntityFactory.Create<LockInfo, long, CoroutineLock>(this.Domain, instanceId, coroutineLock);
  52. lockInfo.Parent = this;
  53. this.lockInfos.Add(key, lockInfo);
  54. Log.Debug($"location lock key: {key} instanceId: {instanceId}");
  55. if (time > 0)
  56. {
  57. long lockInfoInstanceId = lockInfo.InstanceId;
  58. await TimerComponent.Instance.WaitAsync(time);
  59. if (lockInfo.InstanceId != lockInfoInstanceId)
  60. {
  61. return;
  62. }
  63. UnLock(key, instanceId, instanceId);
  64. }
  65. }
  66. public void UnLock(long key, long oldInstanceId, long newInstanceId)
  67. {
  68. if (!this.lockInfos.TryGetValue(key, out LockInfo lockInfo))
  69. {
  70. Log.Error($"location unlock not found key: {key} {oldInstanceId}");
  71. return;
  72. }
  73. if (oldInstanceId != lockInfo.LockInstanceId)
  74. {
  75. Log.Error($"location unlock oldInstanceId is different: {key} {oldInstanceId}");
  76. return;
  77. }
  78. Log.Debug($"location unlock key: {key} instanceId: {oldInstanceId} newInstanceId: {newInstanceId}");
  79. this.locations[key] = newInstanceId;
  80. this.lockInfos.Remove(key);
  81. // 解锁
  82. lockInfo.Dispose();
  83. }
  84. public async ETTask<long> Get(long key)
  85. {
  86. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
  87. {
  88. this.locations.TryGetValue(key, out long instanceId);
  89. Log.Debug($"location get key: {key} instanceId: {instanceId}");
  90. return instanceId;
  91. }
  92. }
  93. public override void Dispose()
  94. {
  95. if (this.IsDisposed)
  96. {
  97. return;
  98. }
  99. base.Dispose();
  100. this.locations.Clear();
  101. this.lockInfos.Clear();
  102. }
  103. }
  104. }