LocationComponent.cs 3.1 KB

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