Utils.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Security.Cryptography;
  4. using UnityEngine;
  5. namespace Mirror
  6. {
  7. // Handles network messages on client and server
  8. public delegate void NetworkMessageDelegate(NetworkConnection conn, NetworkReader reader, int channelId);
  9. // Handles requests to spawn objects on the client
  10. public delegate GameObject SpawnDelegate(Vector3 position, Guid assetId);
  11. public delegate GameObject SpawnHandlerDelegate(SpawnMessage msg);
  12. // Handles requests to unspawn objects on the client
  13. public delegate void UnSpawnDelegate(GameObject spawned);
  14. // channels are const ints instead of an enum so people can add their own
  15. // channels (can't extend an enum otherwise).
  16. //
  17. // note that Mirror is slowly moving towards quake style networking which
  18. // will only require reliable for handshake, and unreliable for the rest.
  19. // so eventually we can change this to an Enum and transports shouldn't
  20. // add custom channels anymore.
  21. public static class Channels
  22. {
  23. public const int Reliable = 0; // ordered
  24. public const int Unreliable = 1; // unordered
  25. }
  26. public static class Utils
  27. {
  28. public static uint GetTrueRandomUInt()
  29. {
  30. // use Crypto RNG to avoid having time based duplicates
  31. using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
  32. {
  33. byte[] bytes = new byte[4];
  34. rng.GetBytes(bytes);
  35. return BitConverter.ToUInt32(bytes, 0);
  36. }
  37. }
  38. public static bool IsPrefab(GameObject obj)
  39. {
  40. #if UNITY_EDITOR
  41. return UnityEditor.PrefabUtility.IsPartOfPrefabAsset(obj);
  42. #else
  43. return false;
  44. #endif
  45. }
  46. public static bool IsSceneObjectWithPrefabParent(GameObject gameObject, out GameObject prefab)
  47. {
  48. prefab = null;
  49. #if UNITY_EDITOR
  50. if (!UnityEditor.PrefabUtility.IsPartOfPrefabInstance(gameObject))
  51. {
  52. return false;
  53. }
  54. prefab = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject);
  55. #endif
  56. if (prefab == null)
  57. {
  58. Debug.LogError($"Failed to find prefab parent for scene object [name:{gameObject.name}]");
  59. return false;
  60. }
  61. return true;
  62. }
  63. // is a 2D point in screen? (from ummorpg)
  64. // (if width = 1024, then indices from 0..1023 are valid (=1024 indices)
  65. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  66. public static bool IsPointInScreen(Vector2 point) =>
  67. 0 <= point.x && point.x < Screen.width &&
  68. 0 <= point.y && point.y < Screen.height;
  69. // pretty print bytes as KB/MB/GB/etc. from DOTSNET
  70. // long to support > 2GB
  71. // divides by floats to return "2.5MB" etc.
  72. public static string PrettyBytes(long bytes)
  73. {
  74. // bytes
  75. if (bytes < 1024)
  76. return $"{bytes} B";
  77. // kilobytes
  78. else if (bytes < 1024L * 1024L)
  79. return $"{(bytes / 1024f):F2} KB";
  80. // megabytes
  81. else if (bytes < 1024 * 1024L * 1024L)
  82. return $"{(bytes / (1024f * 1024f)):F2} MB";
  83. // gigabytes
  84. return $"{(bytes / (1024f * 1024f * 1024f)):F2} GB";
  85. }
  86. // universal .spawned function
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public static NetworkIdentity GetSpawnedInServerOrClient(uint netId)
  89. {
  90. // server / host mode: use the one from server.
  91. // host mode has access to all spawned.
  92. if (NetworkServer.active)
  93. {
  94. NetworkServer.spawned.TryGetValue(netId, out NetworkIdentity entry);
  95. return entry;
  96. }
  97. // client
  98. if (NetworkClient.active)
  99. {
  100. NetworkClient.spawned.TryGetValue(netId, out NetworkIdentity entry);
  101. return entry;
  102. }
  103. return null;
  104. }
  105. }
  106. }