LocationComponentSystem.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. namespace ET
  2. {
  3. [ObjectSystem]
  4. public class LockInfoAwakeSystem: AwakeSystem<LockInfo, long, CoroutineLock>
  5. {
  6. public override void Awake(LockInfo self, long lockInstanceId, CoroutineLock coroutineLock)
  7. {
  8. self.CoroutineLock = coroutineLock;
  9. self.LockInstanceId = lockInstanceId;
  10. }
  11. }
  12. [ObjectSystem]
  13. public class LockInfoDestroySystem: DestroySystem<LockInfo>
  14. {
  15. public override void Destroy(LockInfo self)
  16. {
  17. self.CoroutineLock.Dispose();
  18. self.LockInstanceId = 0;
  19. }
  20. }
  21. [FriendOf(typeof(LocationComponent))]
  22. [FriendOf(typeof(LockInfo))]
  23. public static class LocationComponentSystem
  24. {
  25. public static async ETTask Add(this LocationComponent self, long key, long instanceId)
  26. {
  27. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
  28. {
  29. self.locations[key] = instanceId;
  30. Log.Info($"location add key: {key} instanceId: {instanceId}");
  31. }
  32. }
  33. public static async ETTask Remove(this LocationComponent self, long key)
  34. {
  35. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
  36. {
  37. self.locations.Remove(key);
  38. Log.Info($"location remove key: {key}");
  39. }
  40. }
  41. public static async ETTask Lock(this LocationComponent self, long key, long instanceId, int time = 0)
  42. {
  43. CoroutineLock coroutineLock = await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key);
  44. LockInfo lockInfo = self.AddChild<LockInfo, long, CoroutineLock>(instanceId, coroutineLock);
  45. self.lockInfos.Add(key, lockInfo);
  46. Log.Info($"location lock key: {key} instanceId: {instanceId}");
  47. if (time > 0)
  48. {
  49. long lockInfoInstanceId = lockInfo.InstanceId;
  50. await TimerComponent.Instance.WaitAsync(time);
  51. if (lockInfo.InstanceId != lockInfoInstanceId)
  52. {
  53. return;
  54. }
  55. self.UnLock(key, instanceId, instanceId);
  56. }
  57. }
  58. public static void UnLock(this LocationComponent self, long key, long oldInstanceId, long newInstanceId)
  59. {
  60. if (!self.lockInfos.TryGetValue(key, out LockInfo lockInfo))
  61. {
  62. Log.Error($"location unlock not found key: {key} {oldInstanceId}");
  63. return;
  64. }
  65. if (oldInstanceId != lockInfo.LockInstanceId)
  66. {
  67. Log.Error($"location unlock oldInstanceId is different: {key} {oldInstanceId}");
  68. return;
  69. }
  70. Log.Info($"location unlock key: {key} instanceId: {oldInstanceId} newInstanceId: {newInstanceId}");
  71. self.locations[key] = newInstanceId;
  72. self.lockInfos.Remove(key);
  73. // 解锁
  74. lockInfo.Dispose();
  75. }
  76. public static async ETTask<long> Get(this LocationComponent self, long key)
  77. {
  78. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.Location, key))
  79. {
  80. self.locations.TryGetValue(key, out long instanceId);
  81. Log.Info($"location get key: {key} instanceId: {instanceId}");
  82. return instanceId;
  83. }
  84. }
  85. }
  86. }