PtcFormatter.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. public 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 : unmanaged
  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 : unmanaged
  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 : unmanaged
  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 : unmanaged
  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 : unmanaged
  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 : unmanaged
  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 : unmanaged
  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 : unmanaged
  89. {
  90. Span<T> spanT = MemoryMarshal.CreateSpan(ref structure, 1);
  91. stream.Write(MemoryMarshal.AsBytes(spanT));
  92. }
  93. #endregion
  94. }
  95. }