Brak opisu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DataStreamWriter.cs 39KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. using Unity.Burst;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. using UnityEngine.Scripting.APIUpdating;
  8. namespace Unity.Collections
  9. {
  10. /// <summary>
  11. /// Writes data in an endian format to serialize data.
  12. /// </summary>
  13. /// <remarks>
  14. /// Data streams can be used to serialize data (e.g. over the network). The
  15. /// DataStreamWriter and <see cref="DataStreamReader"/> classes work together
  16. /// to serialize data for sending and then to deserialize when receiving.
  17. ///
  18. /// DataStreamWriter writes data in the endian format native to the current machine architecture.
  19. /// For network byte order use the so named methods.
  20. /// <br/>
  21. /// The reader can be used to deserialize the data from a NativeArray&lt;byte&gt;, writing data
  22. /// to a NativeArray&lt;byte&gt; and reading it back can be done like this:
  23. /// <code>
  24. /// using (var data = new NativeArray&lt;byte&gt;(16, Allocator.Persistent))
  25. /// {
  26. /// var dataWriter = new DataStreamWriter(data);
  27. /// dataWriter.WriteInt(42);
  28. /// dataWriter.WriteInt(1234);
  29. /// // Length is the actual amount of data inside the writer,
  30. /// // Capacity is the total amount.
  31. /// var dataReader = new DataStreamReader(nativeArrayOfBytes.GetSubArray(0, dataWriter.Length));
  32. /// var myFirstInt = dataReader.ReadInt();
  33. /// var mySecondInt = dataReader.ReadInt();
  34. /// }
  35. /// </code>
  36. ///
  37. /// There are a number of functions for various data types. If a copy of the writer
  38. /// is stored it can be used to overwrite the data later on. This is particularly useful when
  39. /// the size of the data is written at the start and you want to write it at
  40. /// the end when you know the value.
  41. /// <seealso cref="IsLittleEndian"/>
  42. ///
  43. /// <code>
  44. /// using (var data = new NativeArray&lt;byte&gt;(16, Allocator.Persistent))
  45. /// {
  46. /// var dataWriter = new DataStreamWriter(data);
  47. /// // My header data
  48. /// var headerSizeMark = dataWriter;
  49. /// dataWriter.WriteUShort((ushort)0);
  50. /// var payloadSizeMark = dataWriter;
  51. /// dataWriter.WriteUShort((ushort)0);
  52. /// dataWriter.WriteInt(42);
  53. /// dataWriter.WriteInt(1234);
  54. /// var headerSize = data.Length;
  55. /// // Update header size to correct value
  56. /// headerSizeMark.WriteUShort((ushort)headerSize);
  57. /// // My payload data
  58. /// byte[] someBytes = Encoding.ASCII.GetBytes("some string");
  59. /// dataWriter.Write(someBytes, someBytes.Length);
  60. /// // Update payload size to correct value
  61. /// payloadSizeMark.WriteUShort((ushort)(dataWriter.Length - headerSize));
  62. /// }
  63. /// </code>
  64. /// </remarks>
  65. [MovedFrom(true, "Unity.Networking.Transport", "Unity.Networking.Transport")]
  66. [StructLayout(LayoutKind.Sequential)]
  67. [GenerateTestsForBurstCompatibility]
  68. public unsafe struct DataStreamWriter
  69. {
  70. /// <summary>
  71. /// Show the byte order in which the current computer architecture stores data.
  72. /// </summary>
  73. /// <remarks>
  74. /// Different computer architectures store data using different byte orders.
  75. /// <list type="bullet">
  76. /// <item>Big-endian: the most significant byte is at the left end of a word.</item>
  77. /// <item>Little-endian: means the most significant byte is at the right end of a word.</item>
  78. /// </list>
  79. /// </remarks>
  80. public static bool IsLittleEndian
  81. {
  82. get
  83. {
  84. uint test = 1;
  85. byte* testPtr = (byte*)&test;
  86. return testPtr[0] == 1;
  87. }
  88. }
  89. struct StreamData
  90. {
  91. public byte* buffer;
  92. public int length;
  93. public int capacity;
  94. public ulong bitBuffer;
  95. public int bitIndex;
  96. public int failedWrites;
  97. }
  98. [NativeDisableUnsafePtrRestriction] StreamData m_Data;
  99. /// <summary>
  100. /// Used for sending data asynchronously.
  101. /// </summary>
  102. public IntPtr m_SendHandleData;
  103. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  104. AtomicSafetyHandle m_Safety;
  105. #endif
  106. /// <summary>
  107. /// Initializes a new instance of the DataStreamWriter struct.
  108. /// </summary>
  109. /// <param name="length">The number of bytes available in the buffer.</param>
  110. /// <param name="allocator">The <see cref="Allocator"/> used to allocate the memory.</param>
  111. public DataStreamWriter(int length, AllocatorManager.AllocatorHandle allocator)
  112. {
  113. CheckAllocator(allocator);
  114. Initialize(out this, CollectionHelper.CreateNativeArray<byte>(length, allocator));
  115. }
  116. /// <summary>
  117. /// Initializes a new instance of the DataStreamWriter struct with a NativeArray&lt;byte&gt;
  118. /// </summary>
  119. /// <param name="data">The buffer to attach to the DataStreamWriter.</param>
  120. public DataStreamWriter(NativeArray<byte> data)
  121. {
  122. Initialize(out this, data);
  123. }
  124. /// <summary>
  125. /// Initializes a new instance of the DataStreamWriter struct with a memory we don't own
  126. /// </summary>
  127. /// <param name="data">Pointer to the data</param>
  128. /// <param name="length">Length of the data</param>
  129. public DataStreamWriter(byte* data, int length)
  130. {
  131. var na = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<byte>(data, length, Allocator.Invalid);
  132. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  133. NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref na, AtomicSafetyHandle.GetTempMemoryHandle());
  134. #endif
  135. Initialize(out this, na);
  136. }
  137. /// <summary>
  138. /// Convert internal data buffer to NativeArray for use in entities APIs.
  139. /// </summary>
  140. /// <returns>NativeArray representation of internal buffer.</returns>
  141. public NativeArray<byte> AsNativeArray()
  142. {
  143. var na = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<byte>(m_Data.buffer, Length, Allocator.Invalid);
  144. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  145. NativeArrayUnsafeUtility.SetAtomicSafetyHandle(ref na, m_Safety);
  146. #endif
  147. return na;
  148. }
  149. static void Initialize(out DataStreamWriter self, NativeArray<byte> data)
  150. {
  151. self.m_SendHandleData = IntPtr.Zero;
  152. self.m_Data.capacity = data.Length;
  153. self.m_Data.length = 0;
  154. self.m_Data.buffer = (byte*)data.GetUnsafePtr();
  155. self.m_Data.bitBuffer = 0;
  156. self.m_Data.bitIndex = 0;
  157. self.m_Data.failedWrites = 0;
  158. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  159. self.m_Safety = NativeArrayUnsafeUtility.GetAtomicSafetyHandle(data);
  160. #endif
  161. }
  162. static short ByteSwap(short val)
  163. {
  164. return (short)(((val & 0xff) << 8) | ((val >> 8) & 0xff));
  165. }
  166. static int ByteSwap(int val)
  167. {
  168. return (int)(((val & 0xff) << 24) | ((val & 0xff00) << 8) | ((val >> 8) & 0xff00) | ((val >> 24) & 0xff));
  169. }
  170. /// <summary>
  171. /// True if there is a valid data buffer present. This would be false
  172. /// if the writer was created with no arguments.
  173. /// </summary>
  174. public readonly bool IsCreated
  175. {
  176. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  177. get { return m_Data.buffer != null; }
  178. }
  179. /// <summary>
  180. /// If there is a write failure this returns true.
  181. /// A failure might happen if an attempt is made to write more than there is capacity for.
  182. /// </summary>
  183. public readonly bool HasFailedWrites
  184. {
  185. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  186. get => m_Data.failedWrites > 0;
  187. }
  188. /// <summary>
  189. /// The total size of the data buffer, see <see cref="Length"/> for
  190. /// the size of space used in the buffer.
  191. /// </summary>
  192. public readonly int Capacity
  193. {
  194. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  195. get
  196. {
  197. CheckRead();
  198. return m_Data.capacity;
  199. }
  200. }
  201. /// <summary>
  202. /// The size of the buffer used. See <see cref="Capacity"/> for the total size.
  203. /// </summary>
  204. public int Length
  205. {
  206. get
  207. {
  208. CheckRead();
  209. SyncBitData();
  210. return m_Data.length + ((m_Data.bitIndex + 7) >> 3);
  211. }
  212. }
  213. /// <summary>
  214. /// The size of the buffer used in bits. See <see cref="Length"/> for the length in bytes.
  215. /// </summary>
  216. public int LengthInBits
  217. {
  218. get
  219. {
  220. CheckRead();
  221. SyncBitData();
  222. return m_Data.length * 8 + m_Data.bitIndex;
  223. }
  224. }
  225. void SyncBitData()
  226. {
  227. var bitIndex = m_Data.bitIndex;
  228. if (bitIndex <= 0)
  229. return;
  230. CheckWrite();
  231. var bitBuffer = m_Data.bitBuffer;
  232. int offset = 0;
  233. while (bitIndex > 0)
  234. {
  235. m_Data.buffer[m_Data.length + offset] = (byte)bitBuffer;
  236. bitIndex -= 8;
  237. bitBuffer >>= 8;
  238. ++offset;
  239. }
  240. }
  241. /// <summary>
  242. /// Causes any buffered bits to be written to the data buffer.
  243. /// Note this needs to be invoked after using methods that writes directly to the bit buffer.
  244. /// </summary>
  245. public void Flush()
  246. {
  247. while (m_Data.bitIndex > 0)
  248. {
  249. m_Data.buffer[m_Data.length++] = (byte)m_Data.bitBuffer;
  250. m_Data.bitIndex -= 8;
  251. m_Data.bitBuffer >>= 8;
  252. }
  253. m_Data.bitIndex = 0;
  254. }
  255. bool WriteBytesInternal(byte* data, int bytes)
  256. {
  257. CheckWrite();
  258. if (m_Data.length + ((m_Data.bitIndex + 7) >> 3) + bytes > m_Data.capacity)
  259. {
  260. ++m_Data.failedWrites;
  261. return false;
  262. }
  263. Flush();
  264. UnsafeUtility.MemCpy(m_Data.buffer + m_Data.length, data, bytes);
  265. m_Data.length += bytes;
  266. return true;
  267. }
  268. /// <summary>
  269. /// Writes an unsigned byte to the current stream and advances the stream position by one byte.
  270. /// </summary>
  271. /// <param name="value">The unsigned byte to write.</param>
  272. /// <returns>Whether the write was successful</returns>
  273. public bool WriteByte(byte value)
  274. {
  275. return WriteBytesInternal((byte*)&value, sizeof(byte));
  276. }
  277. /// <summary>
  278. /// Copy NativeArray of bytes into the writer's data buffer.
  279. /// </summary>
  280. /// <param name="value">Source byte array</param>
  281. /// <returns>Whether the write was successful</returns>
  282. public bool WriteBytes(NativeArray<byte> value)
  283. {
  284. return WriteBytesInternal((byte*)value.GetUnsafeReadOnlyPtr(), value.Length);
  285. }
  286. /// <summary>
  287. /// Copy <c>Span</c> of bytes into the writer's data buffer.
  288. /// </summary>
  289. /// <param name="value">Source byte span</param>
  290. /// <returns>Whether the write was successful</returns>
  291. public bool WriteBytes(Span<byte> value)
  292. {
  293. fixed (byte* data = value)
  294. {
  295. return WriteBytesInternal(data, value.Length);
  296. }
  297. }
  298. /// <summary>
  299. /// Writes a 2-byte signed short to the current stream and advances the stream position by two bytes.
  300. /// </summary>
  301. /// <param name="value">The 2-byte signed short to write.</param>
  302. /// <returns>Whether the write was successful</returns>
  303. public bool WriteShort(short value)
  304. {
  305. return WriteBytesInternal((byte*)&value, sizeof(short));
  306. }
  307. /// <summary>
  308. /// Writes a 2-byte unsigned short to the current stream and advances the stream position by two bytes.
  309. /// </summary>
  310. /// <param name="value">The 2-byte unsigned short to write.</param>
  311. /// <returns>Whether the write was successful</returns>
  312. public bool WriteUShort(ushort value)
  313. {
  314. return WriteBytesInternal((byte*)&value, sizeof(ushort));
  315. }
  316. /// <summary>
  317. /// Writes a 4-byte signed integer from the current stream and advances the current position of the stream by four bytes.
  318. /// </summary>
  319. /// <param name="value">The 4-byte signed integer to write.</param>
  320. /// <returns>Whether the write was successful</returns>
  321. public bool WriteInt(int value)
  322. {
  323. return WriteBytesInternal((byte*)&value, sizeof(int));
  324. }
  325. /// <summary>
  326. /// Reads a 4-byte unsigned integer from the current stream and advances the current position of the stream by four bytes.
  327. /// </summary>
  328. /// <param name="value">The 4-byte unsigned integer to write.</param>
  329. /// <returns>Whether the write was successful</returns>
  330. public bool WriteUInt(uint value)
  331. {
  332. return WriteBytesInternal((byte*)&value, sizeof(uint));
  333. }
  334. /// <summary>
  335. /// Writes an 8-byte signed long from the stream and advances the current position of the stream by eight bytes.
  336. /// </summary>
  337. /// <param name="value">The 8-byte signed long to write.</param>
  338. /// <returns>Whether the write was successful</returns>
  339. public bool WriteLong(long value)
  340. {
  341. return WriteBytesInternal((byte*)&value, sizeof(long));
  342. }
  343. /// <summary>
  344. /// Reads an 8-byte unsigned long from the stream and advances the current position of the stream by eight bytes.
  345. /// </summary>
  346. /// <param name="value">The 8-byte unsigned long to write.</param>
  347. /// <returns>Whether the write was successful</returns>
  348. public bool WriteULong(ulong value)
  349. {
  350. return WriteBytesInternal((byte*)&value, sizeof(ulong));
  351. }
  352. /// <summary>
  353. /// Writes a 2-byte signed short to the current stream using Big-endian byte order and advances the stream position by two bytes.
  354. /// If the stream is in little-endian order, the byte order will be swapped.
  355. /// </summary>
  356. /// <param name="value">The 2-byte signed short to write.</param>
  357. /// <returns>Whether the write was successful</returns>
  358. public bool WriteShortNetworkByteOrder(short value)
  359. {
  360. short netValue = IsLittleEndian ? ByteSwap(value) : value;
  361. return WriteBytesInternal((byte*)&netValue, sizeof(short));
  362. }
  363. /// <summary>
  364. /// Writes a 2-byte unsigned short to the current stream using Big-endian byte order and advances the stream position by two bytes.
  365. /// If the stream is in little-endian order, the byte order will be swapped.
  366. /// </summary>
  367. /// <param name="value">The 2-byte unsigned short to write.</param>
  368. /// <returns>Whether the write was successful</returns>
  369. public bool WriteUShortNetworkByteOrder(ushort value)
  370. {
  371. return WriteShortNetworkByteOrder((short)value);
  372. }
  373. /// <summary>
  374. /// Writes a 4-byte signed integer from the current stream using Big-endian byte order and advances the current position of the stream by four bytes.
  375. /// If the current machine is in little-endian order, the byte order will be swapped.
  376. /// </summary>
  377. /// <param name="value">The 4-byte signed integer to write.</param>
  378. /// <returns>Whether the write was successful</returns>
  379. public bool WriteIntNetworkByteOrder(int value)
  380. {
  381. int netValue = IsLittleEndian ? ByteSwap(value) : value;
  382. return WriteBytesInternal((byte*)&netValue, sizeof(int));
  383. }
  384. /// <summary>
  385. /// Writes a 4-byte unsigned integer from the current stream using Big-endian byte order and advances the current position of the stream by four bytes.
  386. /// If the stream is in little-endian order, the byte order will be swapped.
  387. /// </summary>
  388. /// <param name="value">The 4-byte unsigned integer to write.</param>
  389. /// <returns>Whether the write was successful</returns>
  390. public bool WriteUIntNetworkByteOrder(uint value)
  391. {
  392. return WriteIntNetworkByteOrder((int)value);
  393. }
  394. /// <summary>
  395. /// Writes a 4-byte floating point value to the data stream.
  396. /// </summary>
  397. /// <param name="value">The 4-byte floating point value to write.</param>
  398. /// <returns>Whether the write was successful</returns>
  399. public bool WriteFloat(float value)
  400. {
  401. UIntFloat uf = new UIntFloat();
  402. uf.floatValue = value;
  403. return WriteInt((int)uf.intValue);
  404. }
  405. /// <summary>
  406. /// Writes a 8-byte floating point value to the data stream.
  407. /// </summary>
  408. /// <param name="value">The 8-byte floating point value to write.</param>
  409. /// <returns>Whether the write was successful</returns>
  410. public bool WriteDouble(double value)
  411. {
  412. UIntFloat uf = new UIntFloat();
  413. uf.doubleValue = value;
  414. return WriteLong((long)uf.longValue);
  415. }
  416. void FlushBits()
  417. {
  418. while (m_Data.bitIndex >= 8)
  419. {
  420. m_Data.buffer[m_Data.length++] = (byte)m_Data.bitBuffer;
  421. m_Data.bitIndex -= 8;
  422. m_Data.bitBuffer >>= 8;
  423. }
  424. }
  425. void WriteRawBitsInternal(uint value, int numbits)
  426. {
  427. CheckBits(value, numbits);
  428. m_Data.bitBuffer |= ((ulong)value << m_Data.bitIndex);
  429. m_Data.bitIndex += numbits;
  430. }
  431. /// <summary>
  432. /// Appends a specified number of bits to the data stream.
  433. /// </summary>
  434. /// <param name="value">The bits to write.</param>
  435. /// <param name="numbits">A positive number of bytes to write.</param>
  436. /// <returns>Whether the write was successful</returns>
  437. public bool WriteRawBits(uint value, int numbits)
  438. {
  439. CheckWrite();
  440. if (m_Data.length + ((m_Data.bitIndex + numbits + 7) >> 3) > m_Data.capacity)
  441. {
  442. ++m_Data.failedWrites;
  443. return false;
  444. }
  445. WriteRawBitsInternal(value, numbits);
  446. FlushBits();
  447. return true;
  448. }
  449. /// <summary>
  450. /// Writes a 4-byte unsigned integer value to the data stream using a <see cref="StreamCompressionModel"/>.
  451. /// </summary>
  452. /// <param name="value">The 4-byte unsigned integer to write.</param>
  453. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  454. /// <returns>Whether the write was successful</returns>
  455. public bool WritePackedUInt(uint value, in StreamCompressionModel model)
  456. {
  457. CheckWrite();
  458. int bucket = model.CalculateBucket(value);
  459. uint offset = model.bucketOffsets[bucket];
  460. int bits = model.bucketSizes[bucket];
  461. ushort encodeEntry = model.encodeTable[bucket];
  462. if (m_Data.length + ((m_Data.bitIndex + (encodeEntry & 0xff) + bits + 7) >> 3) > m_Data.capacity)
  463. {
  464. ++m_Data.failedWrites;
  465. return false;
  466. }
  467. WriteRawBitsInternal((uint)(encodeEntry >> 8), encodeEntry & 0xFF);
  468. WriteRawBitsInternal(value - offset, bits);
  469. FlushBits();
  470. return true;
  471. }
  472. /// <summary>
  473. /// Writes an 8-byte unsigned long value to the data stream using a <see cref="StreamCompressionModel"/>.
  474. /// </summary>
  475. /// <param name="value">The 8-byte unsigned long to write.</param>
  476. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  477. /// <returns>Whether the write was successful</returns>
  478. public bool WritePackedULong(ulong value, in StreamCompressionModel model)
  479. {
  480. var data = (uint*)&value;
  481. return WritePackedUInt(data[0], model) &
  482. WritePackedUInt(data[1], model);
  483. }
  484. /// <summary>
  485. /// Writes a 4-byte signed integer value to the data stream using a <see cref="StreamCompressionModel"/>.
  486. /// Negative values are interleaved between positive values, i.e. (0, -1, 1, -2, 2)
  487. /// </summary>
  488. /// <param name="value">The 4-byte signed integer to write.</param>
  489. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  490. /// <returns>Whether the write was successful</returns>
  491. public bool WritePackedInt(int value, in StreamCompressionModel model)
  492. {
  493. uint interleaved = (uint)((value >> 31) ^ (value << 1)); // interleave negative values between positive values: 0, -1, 1, -2, 2
  494. return WritePackedUInt(interleaved, model);
  495. }
  496. /// <summary>
  497. /// Writes a 8-byte signed long value to the data stream using a <see cref="StreamCompressionModel"/>.
  498. /// </summary>
  499. /// <param name="value">The 8-byte signed long to write.</param>
  500. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  501. /// <returns>Whether the write was successful</returns>
  502. public bool WritePackedLong(long value, in StreamCompressionModel model)
  503. {
  504. ulong interleaved = (ulong)((value >> 63) ^ (value << 1)); // interleave negative values between positive values: 0, -1, 1, -2, 2
  505. return WritePackedULong(interleaved, model);
  506. }
  507. /// <summary>
  508. /// Writes a 4-byte floating point value to the data stream using a <see cref="StreamCompressionModel"/>.
  509. /// </summary>
  510. /// <param name="value">The 4-byte floating point value to write.</param>
  511. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  512. /// <returns>Whether the write was successful</returns>
  513. public bool WritePackedFloat(float value, in StreamCompressionModel model)
  514. {
  515. return WritePackedFloatDelta(value, 0, model);
  516. }
  517. /// <summary>
  518. /// Writes a 8-byte floating point value to the data stream using a <see cref="StreamCompressionModel"/>.
  519. /// </summary>
  520. /// <param name="value">The 8-byte floating point value to write.</param>
  521. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  522. /// <returns>Whether the write was successful</returns>
  523. public bool WritePackedDouble(double value, in StreamCompressionModel model)
  524. {
  525. return WritePackedDoubleDelta(value, 0, model);
  526. }
  527. /// <summary>
  528. /// Writes a delta 4-byte unsigned integer value to the data stream using a <see cref="StreamCompressionModel"/>.
  529. /// Note that the Uint values are cast to an Int after computing the diff.
  530. /// </summary>
  531. /// <param name="value">The current 4-byte unsigned integer value.</param>
  532. /// <param name="baseline">The previous 4-byte unsigned integer value, used to compute the diff.</param>
  533. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  534. /// <returns>Whether the write was successful</returns>
  535. public bool WritePackedUIntDelta(uint value, uint baseline, in StreamCompressionModel model)
  536. {
  537. int diff = (int)(baseline - value);
  538. return WritePackedInt(diff, model);
  539. }
  540. /// <summary>
  541. /// Writes a delta 4-byte signed integer value to the data stream using a <see cref="StreamCompressionModel"/>.
  542. /// </summary>
  543. /// <param name="value">The current 4-byte signed integer value.</param>
  544. /// <param name="baseline">The previous 4-byte signed integer value, used to compute the diff.</param>
  545. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  546. /// <returns>Whether the write was successful</returns>
  547. public bool WritePackedIntDelta(int value, int baseline, in StreamCompressionModel model)
  548. {
  549. int diff = (int)(baseline - value);
  550. return WritePackedInt(diff, model);
  551. }
  552. /// <summary>
  553. /// Writes a delta 8-byte signed long value to the data stream using a <see cref="StreamCompressionModel"/>.
  554. /// </summary>
  555. /// <param name="value">The current 8-byte signed long value.</param>
  556. /// <param name="baseline">The previous 8-byte signed long value, used to compute the diff.</param>
  557. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  558. /// <returns>Whether the write was successful</returns>
  559. public bool WritePackedLongDelta(long value, long baseline, in StreamCompressionModel model)
  560. {
  561. long diff = (long)(baseline - value);
  562. return WritePackedLong(diff, model);
  563. }
  564. /// <summary>
  565. /// Writes a delta 8-byte unsigned long value to the data stream using a <see cref="StreamCompressionModel"/>.
  566. /// Note that the unsigned long values are cast to a signed long after computing the diff.
  567. /// </summary>
  568. /// <param name="value">The current 8-byte unsigned long value.</param>
  569. /// <param name="baseline">The previous 8-byte unsigned long, used to compute the diff.</param>
  570. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  571. /// <returns>Whether the write was successful</returns>
  572. public bool WritePackedULongDelta(ulong value, ulong baseline, in StreamCompressionModel model)
  573. {
  574. long diff = (long)(baseline - value);
  575. return WritePackedLong(diff, model);
  576. }
  577. /// <summary>
  578. /// Writes a 4-byte floating point value to the data stream.
  579. ///
  580. /// If the data did not change a zero bit is prepended, otherwise a 1 bit is prepended.
  581. /// When reading back the data, the first bit is then checked for whether the data was changed or not.
  582. /// </summary>
  583. /// <param name="value">The current 4-byte floating point value.</param>
  584. /// <param name="baseline">The previous 4-byte floating value, used to compute the diff.</param>
  585. /// <param name="model">Not currently used.</param>
  586. /// <returns>Whether the write was successful</returns>
  587. public bool WritePackedFloatDelta(float value, float baseline, in StreamCompressionModel model)
  588. {
  589. CheckWrite();
  590. var bits = 0;
  591. if (value != baseline)
  592. bits = 32;
  593. if (m_Data.length + ((m_Data.bitIndex + 1 + bits + 7) >> 3) > m_Data.capacity)
  594. {
  595. ++m_Data.failedWrites;
  596. return false;
  597. }
  598. if (bits == 0)
  599. WriteRawBitsInternal(0, 1);
  600. else
  601. {
  602. WriteRawBitsInternal(1, 1);
  603. UIntFloat uf = new UIntFloat();
  604. uf.floatValue = value;
  605. WriteRawBitsInternal(uf.intValue, bits);
  606. }
  607. FlushBits();
  608. return true;
  609. }
  610. /// <summary>
  611. /// Writes a 8-byte floating point value to the data stream.
  612. ///
  613. /// If the data did not change a zero bit is prepended, otherwise a 1 bit is prepended.
  614. /// When reading back the data, the first bit is then checked for whether the data was changed or not.
  615. /// </summary>
  616. /// <param name="value">The current 8-byte floating point value.</param>
  617. /// <param name="baseline">The previous 8-byte floating value, used to compute the diff.</param>
  618. /// <param name="model">Not currently used.</param>
  619. /// <returns>Whether the write was successful</returns>
  620. public bool WritePackedDoubleDelta(double value, double baseline, in StreamCompressionModel model)
  621. {
  622. CheckWrite();
  623. var bits = 0;
  624. if (value != baseline)
  625. bits = 64;
  626. if (m_Data.length + ((m_Data.bitIndex + 1 + bits + 7) >> 3) > m_Data.capacity)
  627. {
  628. ++m_Data.failedWrites;
  629. return false;
  630. }
  631. if (bits == 0)
  632. WriteRawBitsInternal(0, 1);
  633. else
  634. {
  635. WriteRawBitsInternal(1, 1);
  636. UIntFloat uf = new UIntFloat();
  637. uf.doubleValue = value;
  638. var data = (uint*)&uf.longValue;
  639. WriteRawBitsInternal(data[0], 32);
  640. FlushBits();
  641. WriteRawBitsInternal(data[1], 32);
  642. }
  643. FlushBits();
  644. return true;
  645. }
  646. /// <summary>
  647. /// Writes a <c>FixedString32Bytes</c> value to the data stream.
  648. /// </summary>
  649. /// <param name="str">The <c>FixedString32Bytes</c> to write.</param>
  650. /// <returns>Whether the write was successful</returns>
  651. public unsafe bool WriteFixedString32(FixedString32Bytes str)
  652. {
  653. int length = (int)*((ushort*)&str) + 2;
  654. byte* data = ((byte*)&str);
  655. return WriteBytesInternal(data, length);
  656. }
  657. /// <summary>
  658. /// Writes a <c>FixedString64Bytes</c> value to the data stream.
  659. /// </summary>
  660. /// <param name="str">The <c>FixedString64Bytes</c> to write.</param>
  661. /// <returns>Whether the write was successful</returns>
  662. public unsafe bool WriteFixedString64(FixedString64Bytes str)
  663. {
  664. int length = (int)*((ushort*)&str) + 2;
  665. byte* data = ((byte*)&str);
  666. return WriteBytesInternal(data, length);
  667. }
  668. /// <summary>
  669. /// Writes a <c>FixedString128Bytes</c> value to the data stream.
  670. /// </summary>
  671. /// <param name="str">The <c>FixedString128Bytes</c> to write.</param>
  672. /// <returns>Whether the write was successful</returns>
  673. public unsafe bool WriteFixedString128(FixedString128Bytes str)
  674. {
  675. int length = (int)*((ushort*)&str) + 2;
  676. byte* data = ((byte*)&str);
  677. return WriteBytesInternal(data, length);
  678. }
  679. /// <summary>
  680. /// Writes a <c>FixedString512Bytes</c> value to the data stream.
  681. /// </summary>
  682. /// <param name="str">The <c>FixedString512Bytes</c> to write.</param>
  683. /// <returns>Whether the write was successful</returns>
  684. public unsafe bool WriteFixedString512(FixedString512Bytes str)
  685. {
  686. int length = (int)*((ushort*)&str) + 2;
  687. byte* data = ((byte*)&str);
  688. return WriteBytesInternal(data, length);
  689. }
  690. /// <summary>
  691. /// Writes a <c>FixedString4096Bytes</c> value to the data stream.
  692. /// </summary>
  693. /// <param name="str">The <c>FixedString4096Bytes</c> to write.</param>
  694. /// <returns>Whether the write was successful</returns>
  695. public unsafe bool WriteFixedString4096(FixedString4096Bytes str)
  696. {
  697. int length = (int)*((ushort*)&str) + 2;
  698. byte* data = ((byte*)&str);
  699. return WriteBytesInternal(data, length);
  700. }
  701. /// <summary>
  702. /// Writes a <c>FixedString32Bytes</c> delta value to the data stream using a <see cref="StreamCompressionModel"/>.
  703. /// </summary>
  704. /// <param name="str">The current <c>FixedString32Bytes</c> value.</param>
  705. /// <param name="baseline">The previous <c>FixedString32Bytes</c> value, used to compute the diff.</param>
  706. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  707. /// <returns>Whether the write was successful</returns>
  708. public unsafe bool WritePackedFixedString32Delta(FixedString32Bytes str, FixedString32Bytes baseline, in StreamCompressionModel model)
  709. {
  710. ushort length = *((ushort*)&str);
  711. byte* data = ((byte*)&str) + 2;
  712. return WritePackedFixedStringDelta(data, length, ((byte*)&baseline) + 2, *((ushort*)&baseline), model);
  713. }
  714. /// <summary>
  715. /// Writes a delta <c>FixedString64Bytes</c> value to the data stream using a <see cref="StreamCompressionModel"/>.
  716. /// </summary>
  717. /// <param name="str">The current <c>FixedString64Bytes</c> value.</param>
  718. /// <param name="baseline">The previous <c>FixedString64Bytes</c> value, used to compute the diff.</param>
  719. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  720. /// <returns>Whether the write was successful</returns>
  721. public unsafe bool WritePackedFixedString64Delta(FixedString64Bytes str, FixedString64Bytes baseline, in StreamCompressionModel model)
  722. {
  723. ushort length = *((ushort*)&str);
  724. byte* data = ((byte*)&str) + 2;
  725. return WritePackedFixedStringDelta(data, length, ((byte*)&baseline) + 2, *((ushort*)&baseline), model);
  726. }
  727. /// <summary>
  728. /// Writes a delta <c>FixedString128Bytes</c> value to the data stream using a <see cref="StreamCompressionModel"/>.
  729. /// </summary>
  730. /// <param name="str">The current <c>FixedString128Bytes</c> value.</param>
  731. /// <param name="baseline">The previous <c>FixedString128Bytes</c> value, used to compute the diff.</param>
  732. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  733. /// <returns>Whether the write was successful</returns>
  734. public unsafe bool WritePackedFixedString128Delta(FixedString128Bytes str, FixedString128Bytes baseline, in StreamCompressionModel model)
  735. {
  736. ushort length = *((ushort*)&str);
  737. byte* data = ((byte*)&str) + 2;
  738. return WritePackedFixedStringDelta(data, length, ((byte*)&baseline) + 2, *((ushort*)&baseline), model);
  739. }
  740. /// <summary>
  741. /// Writes a delta <c>FixedString512Bytes</c> value to the data stream using a <see cref="StreamCompressionModel"/>.
  742. /// </summary>
  743. /// <param name="str">The current <c>FixedString512Bytes</c> value.</param>
  744. /// <param name="baseline">The previous <c>FixedString512Bytes</c> value, used to compute the diff.</param>
  745. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  746. /// <returns>Whether the write was successful</returns>
  747. public unsafe bool WritePackedFixedString512Delta(FixedString512Bytes str, FixedString512Bytes baseline, in StreamCompressionModel model)
  748. {
  749. ushort length = *((ushort*)&str);
  750. byte* data = ((byte*)&str) + 2;
  751. return WritePackedFixedStringDelta(data, length, ((byte*)&baseline) + 2, *((ushort*)&baseline), model);
  752. }
  753. /// <summary>
  754. /// Writes a delta <c>FixedString4096Bytes</c> value to the data stream using a <see cref="StreamCompressionModel"/>.
  755. /// </summary>
  756. /// <param name="str">The current <c>FixedString4096Bytes</c> value.</param>
  757. /// <param name="baseline">The previous <c>FixedString4096Bytes</c> value, used to compute the diff.</param>
  758. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  759. /// <returns>Whether the write was successful</returns>
  760. public unsafe bool WritePackedFixedString4096Delta(FixedString4096Bytes str, FixedString4096Bytes baseline, in StreamCompressionModel model)
  761. {
  762. ushort length = *((ushort*)&str);
  763. byte* data = ((byte*)&str) + 2;
  764. return WritePackedFixedStringDelta(data, length, ((byte*)&baseline) + 2, *((ushort*)&baseline), model);
  765. }
  766. /// <summary>
  767. /// Writes a delta FixedString value to the data stream using a <see cref="StreamCompressionModel"/>.
  768. ///
  769. /// If the value cannot be written <see cref="HasFailedWrites"/> will return true. This state can be cleared by
  770. /// calling <see cref="Clear"/>.
  771. /// </summary>
  772. /// <param name="data">Pointer to a packed fixed string.</param>
  773. /// <param name="length">The length of the new value.</param>
  774. /// <param name="baseData">The previous value, used to compute the diff.</param>
  775. /// <param name="baseLength">The length of the previous value.</param>
  776. /// <param name="model"><see cref="StreamCompressionModel"/> model for writing value in a packed manner.</param>
  777. /// <returns>Whether the write was successful</returns>
  778. unsafe bool WritePackedFixedStringDelta(byte* data, uint length, byte* baseData, uint baseLength, in StreamCompressionModel model)
  779. {
  780. var oldData = m_Data;
  781. if (!WritePackedUIntDelta(length, baseLength, model))
  782. return false;
  783. bool didFailWrite = false;
  784. if (length <= baseLength)
  785. {
  786. for (uint i = 0; i < length; ++i)
  787. didFailWrite |= !WritePackedUIntDelta(data[i], baseData[i], model);
  788. }
  789. else
  790. {
  791. for (uint i = 0; i < baseLength; ++i)
  792. didFailWrite |= !WritePackedUIntDelta(data[i], baseData[i], model);
  793. for (uint i = baseLength; i < length; ++i)
  794. didFailWrite |= !WritePackedUInt(data[i], model);
  795. }
  796. // If anything was not written, rewind to the previous position
  797. if (didFailWrite)
  798. {
  799. m_Data = oldData;
  800. ++m_Data.failedWrites;
  801. }
  802. return !didFailWrite;
  803. }
  804. /// <summary>
  805. /// Moves the write position to the start of the data buffer used.
  806. /// </summary>
  807. public void Clear()
  808. {
  809. m_Data.length = 0;
  810. m_Data.bitIndex = 0;
  811. m_Data.bitBuffer = 0;
  812. m_Data.failedWrites = 0;
  813. }
  814. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  815. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  816. readonly void CheckRead()
  817. {
  818. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  819. AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
  820. #endif
  821. }
  822. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
  823. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  824. void CheckWrite()
  825. {
  826. #if ENABLE_UNITY_COLLECTIONS_CHECKS
  827. AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
  828. #endif
  829. }
  830. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  831. static void CheckAllocator(AllocatorManager.AllocatorHandle allocator)
  832. {
  833. if (allocator.ToAllocator != Allocator.Temp)
  834. throw new InvalidOperationException("DataStreamWriters can only be created with temp memory");
  835. }
  836. [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
  837. static void CheckBits(uint value, int numbits)
  838. {
  839. if (numbits < 0 || numbits > 32)
  840. throw new ArgumentOutOfRangeException("Invalid number of bits");
  841. if (value >= (1UL << numbits))
  842. throw new ArgumentOutOfRangeException("Value does not fit in the specified number of bits");
  843. }
  844. }
  845. }