PtcFormatter.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.CompilerServices;
  5. using System.Runtime.InteropServices;
  6. namespace ARMeilleure.Translation.PTC
  7. {
  8. static class PtcFormatter
  9. {
  10. #region "Deserialize"
  11. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  12. public static Dictionary<TKey, TValue> DeserializeDictionary<TKey, TValue>(Stream stream, Func<Stream, TValue> valueFunc) where TKey : struct
  13. {
  14. Dictionary<TKey, TValue> dictionary = new();
  15. int count = DeserializeStructure<int>(stream);
  16. for (int i = 0; i < count; i++)
  17. {
  18. TKey key = DeserializeStructure<TKey>(stream);
  19. TValue value = valueFunc(stream);
  20. dictionary.Add(key, value);
  21. }
  22. return dictionary;
  23. }
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. public static List<T> DeserializeList<T>(Stream stream) where T : struct
  26. {
  27. List<T> list = new();
  28. int count = DeserializeStructure<int>(stream);
  29. for (int i = 0; i < count; i++)
  30. {
  31. T item = DeserializeStructure<T>(stream);
  32. list.Add(item);
  33. }
  34. return list;
  35. }
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public static T DeserializeStructure<T>(Stream stream) where T : struct
  38. {
  39. T structure = default(T);
  40. Span<T> spanT = MemoryMarshal.CreateSpan(ref structure, 1);
  41. int bytesCount = stream.Read(MemoryMarshal.AsBytes(spanT));
  42. if (bytesCount != Unsafe.SizeOf<T>())
  43. {
  44. throw new EndOfStreamException();
  45. }
  46. return structure;
  47. }
  48. #endregion
  49. #region "GetSerializeSize"
  50. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  51. public static int GetSerializeSizeDictionary<TKey, TValue>(Dictionary<TKey, TValue> dictionary, Func<TValue, int> valueFunc) where TKey : struct
  52. {
  53. int size = 0;
  54. size += Unsafe.SizeOf<int>();
  55. foreach ((_, TValue value) in dictionary)
  56. {
  57. size += Unsafe.SizeOf<TKey>();
  58. size += valueFunc(value);
  59. }
  60. return size;
  61. }
  62. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  63. public static int GetSerializeSizeList<T>(List<T> list) where T : struct
  64. {
  65. int size = 0;
  66. size += Unsafe.SizeOf<int>();
  67. size += list.Count * Unsafe.SizeOf<T>();
  68. return size;
  69. }
  70. #endregion
  71. #region "Serialize"
  72. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  73. public static void SerializeDictionary<TKey, TValue>(Stream stream, Dictionary<TKey, TValue> dictionary, Action<Stream, TValue> valueAction) where TKey : struct
  74. {
  75. SerializeStructure<int>(stream, dictionary.Count);
  76. foreach ((TKey key, TValue value) in dictionary)
  77. {
  78. SerializeStructure<TKey>(stream, key);
  79. valueAction(stream, value);
  80. }
  81. }
  82. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  83. public static void SerializeList<T>(Stream stream, List<T> list) where T : struct
  84. {
  85. SerializeStructure<int>(stream, list.Count);
  86. foreach (T item in list)
  87. {
  88. SerializeStructure<T>(stream, item);
  89. }
  90. }
  91. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  92. public static void SerializeStructure<T>(Stream stream, T structure) where T : struct
  93. {
  94. Span<T> spanT = MemoryMarshal.CreateSpan(ref structure, 1);
  95. stream.Write(MemoryMarshal.AsBytes(spanT));
  96. }
  97. #endregion
  98. #region "Extension methods"
  99. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  100. public static void ReadFrom<T>(this List<T[]> list, Stream stream) where T : struct
  101. {
  102. int count = DeserializeStructure<int>(stream);
  103. for (int i = 0; i < count; i++)
  104. {
  105. int itemLength = DeserializeStructure<int>(stream);
  106. T[] item = new T[itemLength];
  107. int bytesCount = stream.Read(MemoryMarshal.AsBytes(item.AsSpan()));
  108. if (bytesCount != itemLength)
  109. {
  110. throw new EndOfStreamException();
  111. }
  112. list.Add(item);
  113. }
  114. }
  115. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  116. public static long Length<T>(this List<T[]> list) where T : struct
  117. {
  118. long size = 0L;
  119. size += Unsafe.SizeOf<int>();
  120. foreach (T[] item in list)
  121. {
  122. size += Unsafe.SizeOf<int>();
  123. size += item.Length;
  124. }
  125. return size;
  126. }
  127. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  128. public static void WriteTo<T>(this List<T[]> list, Stream stream) where T : struct
  129. {
  130. SerializeStructure<int>(stream, list.Count);
  131. foreach (T[] item in list)
  132. {
  133. SerializeStructure<int>(stream, item.Length);
  134. stream.Write(MemoryMarshal.AsBytes(item.AsSpan()));
  135. }
  136. }
  137. #endregion
  138. }
  139. }