Platforms.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using System.Collections.Generic;
  2. using System.Runtime.CompilerServices;
  3. using UnityEngine;
  4. using Mirror;
  5. namespace FirstGearGames.Utilities.Networks
  6. {
  7. public static class Platforms
  8. {
  9. /// <summary>
  10. /// Returns the NetworkId for a NetworkIdentity.
  11. /// </summary>
  12. /// <param name="ni"></param>
  13. /// <returns></returns>
  14. public static uint ReturnNetworkId(this NetworkIdentity ni)
  15. {
  16. return ni.netId;
  17. }
  18. /// <summary>
  19. /// Sends a message to the server.
  20. /// </summary>
  21. /// <typeparam name="T"></typeparam>
  22. /// <param name="nm"></param>
  23. /// <param name="msg"></param>
  24. /// <param name="channel"></param>
  25. public static void ClientSend<T>(NetworkManager nm, T msg, int channel) where T : struct, NetworkMessage
  26. {
  27. NetworkClient.Send(msg, channel);
  28. }
  29. /// <summary>
  30. /// Sends a message to all clients.
  31. /// </summary>
  32. /// <typeparam name="T"></typeparam>
  33. /// <param name="nm"></param>
  34. /// <param name="msg"></param>
  35. /// <param name="channel"></param>
  36. public static void ServerSendToAll<T>(NetworkManager nm, T msg, int channel) where T : struct, NetworkMessage
  37. {
  38. NetworkServer.SendToAll(msg, channel, true);
  39. }
  40. /// <summary>
  41. /// Returns true if object has an owner.
  42. /// </summary>
  43. /// <returns></returns>
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public static bool ReturnHasOwner(this NetworkBehaviour nb)
  46. {
  47. return (nb.connectionToClient != null);
  48. }
  49. /// <summary>
  50. /// Returns the networkId for the networkIdentity.
  51. /// </summary>
  52. /// <returns></returns>
  53. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  54. public static uint ReturnNetId(this NetworkBehaviour nb)
  55. {
  56. return nb.netIdentity.netId;
  57. }
  58. /// <summary>
  59. /// Returns current owner of this object.
  60. /// </summary>
  61. /// <returns></returns>
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. public static NetworkConnection ReturnOwner(this NetworkBehaviour nb)
  64. {
  65. return nb.connectionToClient;
  66. }
  67. /// <summary>
  68. /// Returns if current client has authority.
  69. /// </summary>
  70. /// <returns></returns>
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. public static bool ReturnHasAuthority(this NetworkBehaviour nb)
  73. {
  74. return nb.hasAuthority;
  75. }
  76. /// <summary>
  77. /// Returns if is server.
  78. /// </summary>
  79. /// <returns></returns>
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public static bool ReturnIsServer(this NetworkBehaviour nb)
  82. {
  83. return nb.isServer;
  84. }
  85. /// <summary>
  86. /// Returns if is server only.
  87. /// </summary>
  88. /// <returns></returns>
  89. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  90. public static bool ReturnIsServerOnly(this NetworkBehaviour nb)
  91. {
  92. return nb.isServerOnly;
  93. }
  94. /// <summary>
  95. /// Returns if is client.
  96. /// </summary>
  97. /// <returns></returns>
  98. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  99. public static bool ReturnIsClient(this NetworkBehaviour nb)
  100. {
  101. return nb.isClient;
  102. }
  103. /// <summary>
  104. /// Returns if client is active.
  105. /// </summary>
  106. /// <returns></returns>
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. public static bool ReturnServerActive(NetworkManager nm)
  109. {
  110. return NetworkServer.active;
  111. }
  112. /// <summary>
  113. /// Returns if client is active.
  114. /// </summary>
  115. /// <returns></returns>
  116. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  117. public static bool ReturnClientActive(NetworkManager nm)
  118. {
  119. return NetworkClient.active;
  120. }
  121. /// <summary>
  122. /// Returns if a connection is ready.
  123. /// </summary>
  124. /// <param name="nc"></param>
  125. /// <returns></returns>
  126. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  127. public static bool IsReady(this NetworkConnection nc)
  128. {
  129. return nc.isReady;
  130. }
  131. /// <summary>
  132. /// Returns currently spawned NetworkIdentities.
  133. /// </summary>
  134. /// <returns></returns>
  135. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  136. public static Dictionary<uint, NetworkIdentity> ReturnSpawned(NetworkManager nm)
  137. {
  138. return NetworkIdentity.spawned;
  139. }
  140. /// <summary>
  141. /// Sets MTU values.
  142. /// </summary>
  143. /// <param name="reliable"></param>
  144. /// <param name="unreliable"></param>
  145. /// <returns></returns>
  146. public static void SetMTU(ref int reliable, ref int unreliable, int maxPacketSize)
  147. {
  148. if (Transport.activeTransport != null)
  149. {
  150. reliable = Mathf.Min(maxPacketSize, Transport.activeTransport.GetMaxPacketSize(0));
  151. unreliable = Mathf.Min(maxPacketSize, Transport.activeTransport.GetMaxPacketSize(1));
  152. }
  153. //If packet sizes are not calculated then use max.
  154. if (reliable == -1)
  155. reliable = maxPacketSize;
  156. if (unreliable == -1)
  157. unreliable = maxPacketSize;
  158. }
  159. }
  160. }