NetworkLoop.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // our ideal update looks like this:
  2. // transport.process_incoming()
  3. // update_world()
  4. // transport.process_outgoing()
  5. //
  6. // this way we avoid unnecessary latency for low-ish server tick rates.
  7. // for example, if we were to use this tick:
  8. // transport.process_incoming/outgoing()
  9. // update_world()
  10. //
  11. // then anything sent in update_world wouldn't be actually sent out by the
  12. // transport until the next frame. if server runs at 60Hz, then this can add
  13. // 16ms latency for every single packet.
  14. //
  15. // => instead we process incoming, update world, process_outgoing in the same
  16. // frame. it's more clear (no race conditions) and lower latency.
  17. // => we need to add custom Update functions to the Unity engine:
  18. // NetworkEarlyUpdate before Update()/FixedUpdate()
  19. // NetworkLateUpdate after LateUpdate()
  20. // this way the user can update the world in Update/FixedUpdate/LateUpdate
  21. // and networking still runs before/after those functions no matter what!
  22. // => see also: https://docs.unity3d.com/Manual/ExecutionOrder.html
  23. // => update order:
  24. // * we add to the end of EarlyUpdate so it runs after any Unity initializations
  25. // * we add to the end of PreLateUpdate so it runs after LateUpdate(). adding
  26. // to the beginning of PostLateUpdate doesn't actually work.
  27. using System;
  28. using UnityEngine;
  29. using UnityEngine.LowLevel;
  30. using UnityEngine.PlayerLoop;
  31. namespace Mirror
  32. {
  33. public static class NetworkLoop
  34. {
  35. // helper enum to add loop to begin/end of subSystemList
  36. internal enum AddMode { Beginning, End }
  37. // callbacks for others to hook into if they need Early/LateUpdate.
  38. public static Action OnEarlyUpdate;
  39. public static Action OnLateUpdate;
  40. // RuntimeInitializeOnLoadMethod -> fast playmode without domain reload
  41. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  42. static void ResetStatics()
  43. {
  44. OnEarlyUpdate = null;
  45. OnLateUpdate = null;
  46. }
  47. // helper function to find an update function's index in a player loop
  48. // type. this is used for testing to guarantee our functions are added
  49. // at the beginning/end properly.
  50. internal static int FindPlayerLoopEntryIndex(PlayerLoopSystem.UpdateFunction function, PlayerLoopSystem playerLoop, Type playerLoopSystemType)
  51. {
  52. // did we find the type? e.g. EarlyUpdate/PreLateUpdate/etc.
  53. if (playerLoop.type == playerLoopSystemType)
  54. return Array.FindIndex(playerLoop.subSystemList, (elem => elem.updateDelegate == function));
  55. // recursively keep looking
  56. if (playerLoop.subSystemList != null)
  57. {
  58. for (int i = 0; i < playerLoop.subSystemList.Length; ++i)
  59. {
  60. int index = FindPlayerLoopEntryIndex(function, playerLoop.subSystemList[i], playerLoopSystemType);
  61. if (index != -1) return index;
  62. }
  63. }
  64. return -1;
  65. }
  66. // MODIFIED AddSystemToPlayerLoopList from Unity.Entities.ScriptBehaviourUpdateOrder (ECS)
  67. //
  68. // => adds an update function to the Unity internal update type.
  69. // => Unity has different update loops:
  70. // https://medium.com/@thebeardphantom/unity-2018-and-playerloop-5c46a12a677
  71. // EarlyUpdate
  72. // FixedUpdate
  73. // PreUpdate
  74. // Update
  75. // PreLateUpdate
  76. // PostLateUpdate
  77. //
  78. // function: the custom update function to add
  79. // IMPORTANT: according to a comment in Unity.Entities.ScriptBehaviourUpdateOrder,
  80. // the UpdateFunction can not be virtual because
  81. // Mono 4.6 has problems invoking virtual methods
  82. // as delegates from native!
  83. // ownerType: the .type to fill in so it's obvious who the new function
  84. // belongs to. seems to be mostly for debugging. pass any.
  85. // addMode: prepend or append to update list
  86. internal static bool AddToPlayerLoop(PlayerLoopSystem.UpdateFunction function, Type ownerType, ref PlayerLoopSystem playerLoop, Type playerLoopSystemType, AddMode addMode)
  87. {
  88. // did we find the type? e.g. EarlyUpdate/PreLateUpdate/etc.
  89. if (playerLoop.type == playerLoopSystemType)
  90. {
  91. // debugging
  92. //Debug.Log($"Found playerLoop of type {playerLoop.type} with {playerLoop.subSystemList.Length} Functions:");
  93. //foreach (PlayerLoopSystem sys in playerLoop.subSystemList)
  94. // Debug.Log($" ->{sys.type}");
  95. // resize & expand subSystemList to fit one more entry
  96. int oldListLength = (playerLoop.subSystemList != null) ? playerLoop.subSystemList.Length : 0;
  97. Array.Resize(ref playerLoop.subSystemList, oldListLength + 1);
  98. // IMPORTANT: always insert a FRESH PlayerLoopSystem!
  99. // We CAN NOT resize and then OVERWRITE an entry's type/loop.
  100. // => PlayerLoopSystem has native IntPtr loop members
  101. // => forgetting to clear those would cause undefined behaviour!
  102. // see also: https://github.com/vis2k/Mirror/pull/2652
  103. PlayerLoopSystem system = new PlayerLoopSystem {
  104. type = ownerType,
  105. updateDelegate = function
  106. };
  107. // prepend our custom loop to the beginning
  108. if (addMode == AddMode.Beginning)
  109. {
  110. // shift to the right, write into first array element
  111. Array.Copy(playerLoop.subSystemList, 0, playerLoop.subSystemList, 1, playerLoop.subSystemList.Length - 1);
  112. playerLoop.subSystemList[0] = system;
  113. }
  114. // append our custom loop to the end
  115. else if (addMode == AddMode.End)
  116. {
  117. // simply write into last array element
  118. playerLoop.subSystemList[oldListLength] = system;
  119. }
  120. // debugging
  121. //Debug.Log($"New playerLoop of type {playerLoop.type} with {playerLoop.subSystemList.Length} Functions:");
  122. //foreach (PlayerLoopSystem sys in playerLoop.subSystemList)
  123. // Debug.Log($" ->{sys.type}");
  124. return true;
  125. }
  126. // recursively keep looking
  127. if (playerLoop.subSystemList != null)
  128. {
  129. for (int i = 0; i < playerLoop.subSystemList.Length; ++i)
  130. {
  131. if (AddToPlayerLoop(function, ownerType, ref playerLoop.subSystemList[i], playerLoopSystemType, addMode))
  132. return true;
  133. }
  134. }
  135. return false;
  136. }
  137. // hook into Unity runtime to actually add our custom functions
  138. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  139. static void RuntimeInitializeOnLoad()
  140. {
  141. //Debug.Log("Mirror: adding Network[Early/Late]Update to Unity...");
  142. // get loop
  143. // 2019 has GetCURRENTPlayerLoop which is safe to use without
  144. // breaking other custom system's custom loops.
  145. // see also: https://github.com/vis2k/Mirror/pull/2627/files
  146. PlayerLoopSystem playerLoop = PlayerLoop.GetCurrentPlayerLoop();
  147. // add NetworkEarlyUpdate to the end of EarlyUpdate so it runs after
  148. // any Unity initializations but before the first Update/FixedUpdate
  149. AddToPlayerLoop(NetworkEarlyUpdate, typeof(NetworkLoop), ref playerLoop, typeof(EarlyUpdate), AddMode.End);
  150. // add NetworkLateUpdate to the end of PreLateUpdate so it runs after
  151. // LateUpdate(). adding to the beginning of PostLateUpdate doesn't
  152. // actually work.
  153. AddToPlayerLoop(NetworkLateUpdate, typeof(NetworkLoop), ref playerLoop, typeof(PreLateUpdate), AddMode.End);
  154. // set the new loop
  155. PlayerLoop.SetPlayerLoop(playerLoop);
  156. }
  157. static void NetworkEarlyUpdate()
  158. {
  159. //Debug.Log($"NetworkEarlyUpdate {Time.time}");
  160. NetworkServer.NetworkEarlyUpdate();
  161. NetworkClient.NetworkEarlyUpdate();
  162. // invoke event after mirror has done it's early updating.
  163. OnEarlyUpdate?.Invoke();
  164. }
  165. static void NetworkLateUpdate()
  166. {
  167. //Debug.Log($"NetworkLateUpdate {Time.time}");
  168. // invoke event before mirror does its final late updating.
  169. OnLateUpdate?.Invoke();
  170. NetworkServer.NetworkLateUpdate();
  171. NetworkClient.NetworkLateUpdate();
  172. }
  173. }
  174. }