ActionScheduler.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.LowLevel;
  5. using Unity.Services.Core.Internal;
  6. using NotNull = JetBrains.Annotations.NotNullAttribute;
  7. namespace Unity.Services.Core.Scheduler.Internal
  8. {
  9. class ScheduledInvocation : IComparable<ScheduledInvocation>
  10. {
  11. public Action Action;
  12. public DateTime InvocationTime;
  13. public long ActionId;
  14. public int CompareTo(ScheduledInvocation that)
  15. {
  16. var compareResult = InvocationTime.CompareTo(that.InvocationTime);
  17. //Actions with same invocation time will execute in id order (schedule order).
  18. if (compareResult == 0)
  19. {
  20. compareResult = ActionId.CompareTo(that.ActionId);
  21. }
  22. return compareResult;
  23. }
  24. }
  25. class ActionScheduler : IActionScheduler
  26. {
  27. readonly ITimeProvider m_TimeProvider;
  28. readonly MinimumBinaryHeap<ScheduledInvocation> m_ScheduledActions
  29. = new MinimumBinaryHeap<ScheduledInvocation>();
  30. readonly Dictionary<long, ScheduledInvocation> m_IdScheduledInvocationMap
  31. = new Dictionary<long, ScheduledInvocation>();
  32. const long k_MinimumIdValue = 1;
  33. internal readonly PlayerLoopSystem SchedulerLoopSystem;
  34. long m_NextId = k_MinimumIdValue;
  35. public ActionScheduler()
  36. : this(new UtcTimeProvider()) {}
  37. public ActionScheduler(ITimeProvider timeProvider)
  38. {
  39. m_TimeProvider = timeProvider;
  40. SchedulerLoopSystem = new PlayerLoopSystem
  41. {
  42. type = typeof(ActionScheduler),
  43. updateDelegate = ExecuteExpiredActions
  44. };
  45. }
  46. public int ScheduledActionsCount => m_ScheduledActions.Count;
  47. public long ScheduleAction([NotNull] Action action, double delaySeconds = 0)
  48. {
  49. if (delaySeconds < 0)
  50. {
  51. throw new ArgumentException("delaySeconds can not be negative");
  52. }
  53. if (action is null)
  54. {
  55. throw new ArgumentNullException(nameof(action));
  56. }
  57. var scheduledInvocation = new ScheduledInvocation
  58. {
  59. Action = action,
  60. InvocationTime = m_TimeProvider.Now.AddSeconds(delaySeconds),
  61. ActionId = m_NextId++
  62. };
  63. if (m_NextId < k_MinimumIdValue)
  64. {
  65. m_NextId = k_MinimumIdValue;
  66. }
  67. m_ScheduledActions.Insert(scheduledInvocation);
  68. m_IdScheduledInvocationMap.Add(scheduledInvocation.ActionId, scheduledInvocation);
  69. return scheduledInvocation.ActionId;
  70. }
  71. public void CancelAction(long actionId)
  72. {
  73. if (!m_IdScheduledInvocationMap.ContainsKey(actionId))
  74. {
  75. return;
  76. }
  77. var scheduledInvocation = m_IdScheduledInvocationMap[actionId];
  78. m_ScheduledActions.Remove(scheduledInvocation);
  79. m_IdScheduledInvocationMap.Remove(scheduledInvocation.ActionId);
  80. }
  81. internal void ExecuteExpiredActions()
  82. {
  83. while (m_ScheduledActions.Count > 0
  84. && m_ScheduledActions.Min.InvocationTime <= m_TimeProvider.Now)
  85. {
  86. var scheduledInvocation = m_ScheduledActions.ExtractMin();
  87. m_IdScheduledInvocationMap.Remove(scheduledInvocation.ActionId);
  88. try
  89. {
  90. scheduledInvocation.Action();
  91. }
  92. catch (Exception e)
  93. {
  94. CoreLogger.LogException(e);
  95. }
  96. }
  97. }
  98. #if UNITY_EDITOR
  99. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  100. static void ClearActionSchedulerFromPlayerLoop()
  101. {
  102. var currentPlayerLoop = PlayerLoop.GetCurrentPlayerLoop();
  103. var currentSubSystems = new List<PlayerLoopSystem>(currentPlayerLoop.subSystemList);
  104. for (var i = currentSubSystems.Count - 1; i >= 0; i--)
  105. {
  106. if (currentSubSystems[i].type == typeof(ActionScheduler))
  107. {
  108. currentSubSystems.RemoveAt(i);
  109. }
  110. }
  111. UpdateSubSystemList(currentSubSystems, currentPlayerLoop);
  112. }
  113. #endif
  114. static void UpdateSubSystemList(List<PlayerLoopSystem> subSystemList, PlayerLoopSystem currentPlayerLoop)
  115. {
  116. currentPlayerLoop.subSystemList = subSystemList.ToArray();
  117. PlayerLoop.SetPlayerLoop(currentPlayerLoop);
  118. }
  119. public void JoinPlayerLoopSystem()
  120. {
  121. var currentPlayerLoop = PlayerLoop.GetCurrentPlayerLoop();
  122. var currentSubSystems = new List<PlayerLoopSystem>(currentPlayerLoop.subSystemList);
  123. if (!currentSubSystems.Contains(SchedulerLoopSystem))
  124. {
  125. currentSubSystems.Add(SchedulerLoopSystem);
  126. UpdateSubSystemList(currentSubSystems, currentPlayerLoop);
  127. }
  128. }
  129. public void QuitPlayerLoopSystem()
  130. {
  131. var currentPlayerLoop = PlayerLoop.GetCurrentPlayerLoop();
  132. var currentSubSystems = new List<PlayerLoopSystem>(currentPlayerLoop.subSystemList);
  133. currentSubSystems.Remove(SchedulerLoopSystem);
  134. UpdateSubSystemList(currentSubSystems, currentPlayerLoop);
  135. }
  136. }
  137. }