NetworkWriterExtensions.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using UnityEngine;
  7. namespace Mirror
  8. {
  9. // Mirror's Weaver automatically detects all NetworkWriter function types,
  10. // but they do all need to be extensions.
  11. public static class NetworkWriterExtensions
  12. {
  13. // cache encoding instead of creating it with BinaryWriter each time
  14. // 1000 readers before: 1MB GC, 30ms
  15. // 1000 readers after: 0.8MB GC, 18ms
  16. static readonly UTF8Encoding encoding = new UTF8Encoding(false, true);
  17. static readonly byte[] stringBuffer = new byte[NetworkWriter.MaxStringLength];
  18. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  19. public static void WriteByte(this NetworkWriter writer, byte value) => writer.WriteBlittable(value);
  20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  21. public static void WriteByteNullable(this NetworkWriter writer, byte? value) => writer.WriteBlittableNullable(value);
  22. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  23. public static void WriteSByte(this NetworkWriter writer, sbyte value) => writer.WriteBlittable(value);
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. public static void WriteSByteNullable(this NetworkWriter writer, sbyte? value) => writer.WriteBlittableNullable(value);
  26. // char is not blittable. convert to ushort.
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public static void WriteChar(this NetworkWriter writer, char value) => writer.WriteBlittable((ushort)value);
  29. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  30. public static void WriteCharNullable(this NetworkWriter writer, char? value) => writer.WriteBlittableNullable((ushort?)value);
  31. // bool is not blittable. convert to byte.
  32. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  33. public static void WriteBool(this NetworkWriter writer, bool value) => writer.WriteBlittable((byte)(value ? 1 : 0));
  34. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  35. public static void WriteBoolNullable(this NetworkWriter writer, bool? value) => writer.WriteBlittableNullable(value.HasValue ? ((byte)(value.Value ? 1 : 0)) : new byte?());
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public static void WriteShort(this NetworkWriter writer, short value) => writer.WriteBlittable(value);
  38. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  39. public static void WriteShortNullable(this NetworkWriter writer, short? value) => writer.WriteBlittableNullable(value);
  40. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  41. public static void WriteUShort(this NetworkWriter writer, ushort value) => writer.WriteBlittable(value);
  42. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  43. public static void WriteUShortNullable(this NetworkWriter writer, ushort? value) => writer.WriteBlittableNullable(value);
  44. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  45. public static void WriteInt(this NetworkWriter writer, int value) => writer.WriteBlittable(value);
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public static void WriteIntNullable(this NetworkWriter writer, int? value) => writer.WriteBlittableNullable(value);
  48. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  49. public static void WriteUInt(this NetworkWriter writer, uint value) => writer.WriteBlittable(value);
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public static void WriteUIntNullable(this NetworkWriter writer, uint? value) => writer.WriteBlittableNullable(value);
  52. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  53. public static void WriteLong(this NetworkWriter writer, long value) => writer.WriteBlittable(value);
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. public static void WriteLongNullable(this NetworkWriter writer, long? value) => writer.WriteBlittableNullable(value);
  56. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  57. public static void WriteULong(this NetworkWriter writer, ulong value) => writer.WriteBlittable(value);
  58. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  59. public static void WriteULongNullable(this NetworkWriter writer, ulong? value) => writer.WriteBlittableNullable(value);
  60. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  61. public static void WriteFloat(this NetworkWriter writer, float value) => writer.WriteBlittable(value);
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. public static void WriteFloatNullable(this NetworkWriter writer, float? value) => writer.WriteBlittableNullable(value);
  64. [StructLayout(LayoutKind.Explicit)]
  65. internal struct UIntDouble
  66. {
  67. [FieldOffset(0)]
  68. public double doubleValue;
  69. [FieldOffset(0)]
  70. public ulong longValue;
  71. }
  72. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  73. public static void WriteDouble(this NetworkWriter writer, double value)
  74. {
  75. // DEBUG: try to find the exact value that fails.
  76. //UIntDouble convert = new UIntDouble{doubleValue = value};
  77. //Debug.Log($"=> NetworkWriter.WriteDouble: {value} => 0x{convert.longValue:X8}");
  78. writer.WriteBlittable(value);
  79. }
  80. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  81. public static void WriteDoubleNullable(this NetworkWriter writer, double? value) => writer.WriteBlittableNullable(value);
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. public static void WriteDecimal(this NetworkWriter writer, decimal value) => writer.WriteBlittable(value);
  84. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  85. public static void WriteDecimalNullable(this NetworkWriter writer, decimal? value) => writer.WriteBlittableNullable(value);
  86. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  87. public static void WriteString(this NetworkWriter writer, string value)
  88. {
  89. // write 0 for null support, increment real size by 1
  90. // (note: original HLAPI would write "" for null strings, but if a
  91. // string is null on the server then it should also be null
  92. // on the client)
  93. if (value == null)
  94. {
  95. writer.WriteUShort(0);
  96. return;
  97. }
  98. // write string with same method as NetworkReader
  99. // convert to byte[]
  100. int size = encoding.GetBytes(value, 0, value.Length, stringBuffer, 0);
  101. // check if within max size
  102. if (size >= NetworkWriter.MaxStringLength)
  103. {
  104. throw new IndexOutOfRangeException($"NetworkWriter.Write(string) too long: {size}. Limit: {NetworkWriter.MaxStringLength}");
  105. }
  106. // write size and bytes
  107. writer.WriteUShort(checked((ushort)(size + 1)));
  108. writer.WriteBytes(stringBuffer, 0, size);
  109. }
  110. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  111. public static void WriteBytesAndSizeSegment(this NetworkWriter writer, ArraySegment<byte> buffer)
  112. {
  113. writer.WriteBytesAndSize(buffer.Array, buffer.Offset, buffer.Count);
  114. }
  115. // Weaver needs a write function with just one byte[] parameter
  116. // (we don't name it .Write(byte[]) because it's really a WriteBytesAndSize since we write size / null info too)
  117. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  118. public static void WriteBytesAndSize(this NetworkWriter writer, byte[] buffer)
  119. {
  120. // buffer might be null, so we can't use .Length in that case
  121. writer.WriteBytesAndSize(buffer, 0, buffer != null ? buffer.Length : 0);
  122. }
  123. // for byte arrays with dynamic size, where the reader doesn't know how many will come
  124. // (like an inventory with different items etc.)
  125. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  126. public static void WriteBytesAndSize(this NetworkWriter writer, byte[] buffer, int offset, int count)
  127. {
  128. // null is supported because [SyncVar]s might be structs with null byte[] arrays
  129. // write 0 for null array, increment normal size by 1 to save bandwidth
  130. // (using size=-1 for null would limit max size to 32kb instead of 64kb)
  131. if (buffer == null)
  132. {
  133. writer.WriteUInt(0u);
  134. return;
  135. }
  136. writer.WriteUInt(checked((uint)count) + 1u);
  137. writer.WriteBytes(buffer, offset, count);
  138. }
  139. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  140. public static void WriteArraySegment<T>(this NetworkWriter writer, ArraySegment<T> segment)
  141. {
  142. int length = segment.Count;
  143. writer.WriteInt(length);
  144. for (int i = 0; i < length; i++)
  145. {
  146. writer.Write(segment.Array[segment.Offset + i]);
  147. }
  148. }
  149. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  150. public static void WriteVector2(this NetworkWriter writer, Vector2 value) => writer.WriteBlittable(value);
  151. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  152. public static void WriteVector2Nullable(this NetworkWriter writer, Vector2? value) => writer.WriteBlittableNullable(value);
  153. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  154. public static void WriteVector3(this NetworkWriter writer, Vector3 value) => writer.WriteBlittable(value);
  155. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  156. public static void WriteVector3Nullable(this NetworkWriter writer, Vector3? value) => writer.WriteBlittableNullable(value);
  157. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  158. public static void WriteVector4(this NetworkWriter writer, Vector4 value) => writer.WriteBlittable(value);
  159. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  160. public static void WriteVector4Nullable(this NetworkWriter writer, Vector4? value) => writer.WriteBlittableNullable(value);
  161. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  162. public static void WriteVector2Int(this NetworkWriter writer, Vector2Int value) => writer.WriteBlittable(value);
  163. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  164. public static void WriteVector2IntNullable(this NetworkWriter writer, Vector2Int? value) => writer.WriteBlittableNullable(value);
  165. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  166. public static void WriteVector3Int(this NetworkWriter writer, Vector3Int value) => writer.WriteBlittable(value);
  167. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  168. public static void WriteVector3IntNullable(this NetworkWriter writer, Vector3Int? value) => writer.WriteBlittableNullable(value);
  169. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  170. public static void WriteColor(this NetworkWriter writer, Color value) => writer.WriteBlittable(value);
  171. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  172. public static void WriteColorNullable(this NetworkWriter writer, Color? value) => writer.WriteBlittableNullable(value);
  173. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  174. public static void WriteColor32(this NetworkWriter writer, Color32 value) => writer.WriteBlittable(value);
  175. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  176. public static void WriteColor32Nullable(this NetworkWriter writer, Color32? value) => writer.WriteBlittableNullable(value);
  177. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  178. public static void WriteQuaternion(this NetworkWriter writer, Quaternion value) => writer.WriteBlittable(value);
  179. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  180. public static void WriteQuaternionNullable(this NetworkWriter writer, Quaternion? value) => writer.WriteBlittableNullable(value);
  181. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  182. public static void WriteRect(this NetworkWriter writer, Rect value) => writer.WriteBlittable(value);
  183. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  184. public static void WriteRectNullable(this NetworkWriter writer, Rect? value) => writer.WriteBlittableNullable(value);
  185. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  186. public static void WritePlane(this NetworkWriter writer, Plane value) => writer.WriteBlittable(value);
  187. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  188. public static void WritePlaneNullable(this NetworkWriter writer, Plane? value) => writer.WriteBlittableNullable(value);
  189. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  190. public static void WriteRay(this NetworkWriter writer, Ray value) => writer.WriteBlittable(value);
  191. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  192. public static void WriteRayNullable(this NetworkWriter writer, Ray? value) => writer.WriteBlittableNullable(value);
  193. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  194. public static void WriteMatrix4x4(this NetworkWriter writer, Matrix4x4 value) => writer.WriteBlittable(value);
  195. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  196. public static void WriteMatrix4x4Nullable(this NetworkWriter writer, Matrix4x4? value) => writer.WriteBlittableNullable(value);
  197. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  198. public static void WriteGuid(this NetworkWriter writer, Guid value)
  199. {
  200. byte[] data = value.ToByteArray();
  201. writer.WriteBytes(data, 0, data.Length);
  202. }
  203. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  204. public static void WriteGuidNullable(this NetworkWriter writer, Guid? value)
  205. {
  206. writer.WriteBool(value.HasValue);
  207. if (value.HasValue)
  208. writer.WriteGuid(value.Value);
  209. }
  210. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  211. public static void WriteNetworkIdentity(this NetworkWriter writer, NetworkIdentity value)
  212. {
  213. if (value == null)
  214. {
  215. writer.WriteUInt(0);
  216. return;
  217. }
  218. // users might try to use unspawned / prefab GameObjects in
  219. // rpcs/cmds/syncvars/messages. they would be null on the other
  220. // end, and it might not be obvious why. let's make it obvious.
  221. // https://github.com/vis2k/Mirror/issues/2060
  222. //
  223. // => warning (instead of exception) because we also use a warning
  224. // if a GameObject doesn't have a NetworkIdentity component etc.
  225. if (value.netId == 0)
  226. Debug.LogWarning($"Attempted to serialize unspawned GameObject: {value.name}. Prefabs and unspawned GameObjects would always be null on the other side. Please spawn it before using it in [SyncVar]s/Rpcs/Cmds/NetworkMessages etc.");
  227. writer.WriteUInt(value.netId);
  228. }
  229. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  230. public static void WriteNetworkBehaviour(this NetworkWriter writer, NetworkBehaviour value)
  231. {
  232. if (value == null)
  233. {
  234. writer.WriteUInt(0);
  235. return;
  236. }
  237. writer.WriteUInt(value.netId);
  238. writer.WriteByte((byte)value.ComponentIndex);
  239. }
  240. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  241. public static void WriteTransform(this NetworkWriter writer, Transform value)
  242. {
  243. if (value == null)
  244. {
  245. writer.WriteUInt(0);
  246. return;
  247. }
  248. NetworkIdentity identity = value.GetComponent<NetworkIdentity>();
  249. if (identity != null)
  250. {
  251. writer.WriteUInt(identity.netId);
  252. }
  253. else
  254. {
  255. Debug.LogWarning($"NetworkWriter {value} has no NetworkIdentity");
  256. writer.WriteUInt(0);
  257. }
  258. }
  259. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  260. public static void WriteGameObject(this NetworkWriter writer, GameObject value)
  261. {
  262. if (value == null)
  263. {
  264. writer.WriteUInt(0);
  265. return;
  266. }
  267. // warn if the GameObject doesn't have a NetworkIdentity,
  268. NetworkIdentity identity = value.GetComponent<NetworkIdentity>();
  269. if (identity == null)
  270. Debug.LogWarning($"NetworkWriter {value} has no NetworkIdentity");
  271. // serialize the correct amount of data in any case to make sure
  272. // that the other end can read the expected amount of data too.
  273. writer.WriteNetworkIdentity(identity);
  274. }
  275. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  276. public static void WriteList<T>(this NetworkWriter writer, List<T> list)
  277. {
  278. if (list is null)
  279. {
  280. writer.WriteInt(-1);
  281. return;
  282. }
  283. writer.WriteInt(list.Count);
  284. for (int i = 0; i < list.Count; i++)
  285. writer.Write(list[i]);
  286. }
  287. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  288. public static void WriteArray<T>(this NetworkWriter writer, T[] array)
  289. {
  290. if (array is null)
  291. {
  292. writer.WriteInt(-1);
  293. return;
  294. }
  295. writer.WriteInt(array.Length);
  296. for (int i = 0; i < array.Length; i++)
  297. writer.Write(array[i]);
  298. }
  299. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  300. public static void WriteUri(this NetworkWriter writer, Uri uri)
  301. {
  302. writer.WriteString(uri?.ToString());
  303. }
  304. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  305. public static void WriteTexture2D(this NetworkWriter writer, Texture2D texture2D)
  306. {
  307. writer.WriteArray(texture2D.GetPixels32());
  308. }
  309. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  310. public static void WriteSprite(this NetworkWriter writer, Sprite sprite)
  311. {
  312. writer.WriteTexture2D(sprite.texture);
  313. writer.WriteRect(sprite.rect);
  314. writer.WriteVector2(sprite.pivot);
  315. }
  316. }
  317. }