PtcFormatter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. stream.Read(MemoryMarshal.AsBytes(spanT));
  42. return structure;
  43. }
  44. #endregion
  45. #region "GetSerializeSize"
  46. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  47. public static int GetSerializeSizeDictionary<TKey, TValue>(Dictionary<TKey, TValue> dictionary, Func<TValue, int> valueFunc) where TKey : struct
  48. {
  49. int size = 0;
  50. size += Unsafe.SizeOf<int>();
  51. foreach ((_, TValue value) in dictionary)
  52. {
  53. size += Unsafe.SizeOf<TKey>();
  54. size += valueFunc(value);
  55. }
  56. return size;
  57. }
  58. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  59. public static int GetSerializeSizeList<T>(List<T> list) where T : struct
  60. {
  61. int size = 0;
  62. size += Unsafe.SizeOf<int>();
  63. size += list.Count * Unsafe.SizeOf<T>();
  64. return size;
  65. }
  66. #endregion
  67. #region "Serialize"
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. public static void SerializeDictionary<TKey, TValue>(Stream stream, Dictionary<TKey, TValue> dictionary, Action<Stream, TValue> valueAction) where TKey : struct
  70. {
  71. SerializeStructure<int>(stream, dictionary.Count);
  72. foreach ((TKey key, TValue value) in dictionary)
  73. {
  74. SerializeStructure<TKey>(stream, key);
  75. valueAction(stream, value);
  76. }
  77. }
  78. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. public static void SerializeList<T>(Stream stream, List<T> list) where T : struct
  80. {
  81. SerializeStructure<int>(stream, list.Count);
  82. foreach (T item in list)
  83. {
  84. SerializeStructure<T>(stream, item);
  85. }
  86. }
  87. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  88. public static void SerializeStructure<T>(Stream stream, T structure) where T : struct
  89. {
  90. Span<T> spanT = MemoryMarshal.CreateSpan(ref structure, 1);
  91. stream.Write(MemoryMarshal.AsBytes(spanT));
  92. }
  93. #endregion
  94. #region "Extension methods"
  95. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  96. public static void ReadFrom<T>(this List<T[]> list, Stream stream) where T : struct
  97. {
  98. int count = DeserializeStructure<int>(stream);
  99. for (int i = 0; i < count; i++)
  100. {
  101. int itemLength = DeserializeStructure<int>(stream);
  102. T[] item = new T[itemLength];
  103. stream.Read(MemoryMarshal.AsBytes(item.AsSpan()));
  104. list.Add(item);
  105. }
  106. }
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. public static long Length<T>(this List<T[]> list) where T : struct
  109. {
  110. long size = 0L;
  111. size += Unsafe.SizeOf<int>();
  112. foreach (T[] item in list)
  113. {
  114. size += Unsafe.SizeOf<int>();
  115. size += item.Length;
  116. }
  117. return size;
  118. }
  119. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  120. public static void WriteTo<T>(this List<T[]> list, Stream stream) where T : struct
  121. {
  122. SerializeStructure<int>(stream, list.Count);
  123. foreach (T[] item in list)
  124. {
  125. SerializeStructure<int>(stream, item.Length);
  126. stream.Write(MemoryMarshal.AsBytes(item.AsSpan()));
  127. }
  128. }
  129. #endregion
  130. }
  131. }