LocationComponent.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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.Info($"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.Info($"location remove key: {key}");
  46. }
  47. }
  48. public async ETTask Lock(long key, long instanceId, int time = 0)
  49. {
  50. CoroutineLock coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key);
  51. LockInfo lockInfo = this.AddChild<LockInfo, long, CoroutineLock>(instanceId, coroutineLock);
  52. this.lockInfos.Add(key, lockInfo);
  53. Log.Info($"location lock key: {key} instanceId: {instanceId}");
  54. if (time > 0)
  55. {
  56. long lockInfoInstanceId = lockInfo.InstanceId;
  57. await TimerComponent.Instance.WaitAsync(time);
  58. if (lockInfo.InstanceId != lockInfoInstanceId)
  59. {
  60. return;
  61. }
  62. UnLock(key, instanceId, instanceId);
  63. }
  64. }
  65. public void UnLock(long key, long oldInstanceId, long newInstanceId)
  66. {
  67. if (!this.lockInfos.TryGetValue(key, out LockInfo lockInfo))
  68. {
  69. Log.Error($"location unlock not found key: {key} {oldInstanceId}");
  70. return;
  71. }
  72. if (oldInstanceId != lockInfo.LockInstanceId)
  73. {
  74. Log.Error($"location unlock oldInstanceId is different: {key} {oldInstanceId}");
  75. return;
  76. }
  77. Log.Info($"location unlock key: {key} instanceId: {oldInstanceId} newInstanceId: {newInstanceId}");
  78. this.locations[key] = newInstanceId;
  79. this.lockInfos.Remove(key);
  80. // 解锁
  81. lockInfo.Dispose();
  82. }
  83. public async ETTask<long> Get(long key)
  84. {
  85. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
  86. {
  87. this.locations.TryGetValue(key, out long instanceId);
  88. Log.Info($"location get key: {key} instanceId: {instanceId}");
  89. return instanceId;
  90. }
  91. }
  92. public override void Dispose()
  93. {
  94. if (this.IsDisposed)
  95. {
  96. return;
  97. }
  98. base.Dispose();
  99. this.locations.Clear();
  100. this.lockInfos.Clear();
  101. }
  102. }
  103. }