LocationComponent.cs 3.1 KB

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