LocationComponent.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System.Collections.Generic;
  2. namespace ETModel
  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.LockInstanceId = lockInstanceId;
  10. self.CoroutineLock = coroutineLock;
  11. }
  12. }
  13. public class LockInfo: Component
  14. {
  15. public long LockInstanceId;
  16. public CoroutineLock CoroutineLock;
  17. public override void Dispose()
  18. {
  19. this.LockInstanceId = 0;
  20. this.CoroutineLock.Dispose();
  21. }
  22. }
  23. public class LocationComponent : Component
  24. {
  25. private readonly Dictionary<long, long> locations = new Dictionary<long, long>();
  26. private readonly Dictionary<long, LockInfo> lockInfos = new Dictionary<long, LockInfo>();
  27. public override void Dispose()
  28. {
  29. if (this.IsDisposed)
  30. {
  31. return;
  32. }
  33. base.Dispose();
  34. this.locations.Clear();
  35. foreach (LockInfo lockInfo in this.lockInfos.Values)
  36. {
  37. lockInfo.Dispose();
  38. }
  39. this.lockInfos.Clear();
  40. }
  41. public async ETTask Add(long key, long instanceId)
  42. {
  43. using (await CoroutineLockComponent.Instance.Wait(key + (int)AppType.Location))
  44. {
  45. this.locations[key] = instanceId;
  46. Log.Info($"location add key: {key} instanceId: {instanceId}");
  47. }
  48. }
  49. public async ETTask Remove(long key)
  50. {
  51. using (await CoroutineLockComponent.Instance.Wait(key + (int)AppType.Location))
  52. {
  53. this.locations.Remove(key);
  54. Log.Info($"location remove key: {key}");
  55. }
  56. }
  57. public async ETTask<long> Get(long key)
  58. {
  59. using (await CoroutineLockComponent.Instance.Wait(key + (int)AppType.Location))
  60. {
  61. this.locations.TryGetValue(key, out long instanceId);
  62. Log.Info($"location get key: {key} {instanceId}");
  63. return instanceId;
  64. }
  65. }
  66. public async ETVoid Lock(long key, long instanceId, int time = 0)
  67. {
  68. if (!this.locations.TryGetValue(key, out long saveInstanceId))
  69. {
  70. Log.Error($"actor没有注册, key: {key} InstanceId: {instanceId}");
  71. return;
  72. }
  73. if (saveInstanceId != instanceId)
  74. {
  75. Log.Error($"actor注册的instanceId与lock的不一致, key: {key} InstanceId: {instanceId} saveInstanceId: {saveInstanceId}");
  76. return;
  77. }
  78. CoroutineLock coroutineLock = await CoroutineLockComponent.Instance.Wait(key + (int)AppType.Location);
  79. LockInfo lockInfo = ComponentFactory.Create<LockInfo, long, CoroutineLock>(instanceId, coroutineLock);
  80. this.lockInfos.Add(key, lockInfo);
  81. Log.Info($"location lock key: {key} InstanceId: {instanceId}");
  82. // 超时则解锁
  83. if (time > 0)
  84. {
  85. await Game.Scene.GetComponent<TimerComponent>().WaitAsync(time);
  86. this.UnLock(key, instanceId, instanceId);
  87. }
  88. }
  89. public void UnLock(long key, long oldInstanceId, long newInstanceId)
  90. {
  91. if (!this.lockInfos.TryGetValue(key, out LockInfo lockInfo))
  92. {
  93. return;
  94. }
  95. if (lockInfo.LockInstanceId != oldInstanceId)
  96. {
  97. Log.Error($"unlock appid is different {lockInfo.LockInstanceId} {oldInstanceId}" );
  98. return;
  99. }
  100. Log.Info($"location unlock key: {key} oldInstanceId: {oldInstanceId} new: {newInstanceId}");
  101. this.locations[key] = newInstanceId;
  102. lockInfo.Dispose();
  103. }
  104. }
  105. }