using System; using System.Collections.Generic; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace ARMeilleure.Translation.PTC { public class PtcFormatter { #region "Deserialize" [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Dictionary DeserializeDictionary(Stream stream, Func valueFunc) where TKey : unmanaged { Dictionary dictionary = new(); int count = DeserializeStructure(stream); for (int i = 0; i < count; i++) { TKey key = DeserializeStructure(stream); TValue value = valueFunc(stream); dictionary.Add(key, value); } return dictionary; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static List DeserializeList(Stream stream) where T : unmanaged { List list = new(); int count = DeserializeStructure(stream); for (int i = 0; i < count; i++) { T item = DeserializeStructure(stream); list.Add(item); } return list; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T DeserializeStructure(Stream stream) where T : unmanaged { T structure = default(T); Span spanT = MemoryMarshal.CreateSpan(ref structure, 1); stream.Read(MemoryMarshal.AsBytes(spanT)); return structure; } #endregion #region "GetSerializeSize" [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetSerializeSizeDictionary(Dictionary dictionary, Func valueFunc) where TKey : unmanaged { int size = 0; size += Unsafe.SizeOf(); foreach ((_, TValue value) in dictionary) { size += Unsafe.SizeOf(); size += valueFunc(value); } return size; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static int GetSerializeSizeList(List list) where T : unmanaged { int size = 0; size += Unsafe.SizeOf(); size += list.Count * Unsafe.SizeOf(); return size; } #endregion #region "Serialize" [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void SerializeDictionary(Stream stream, Dictionary dictionary, Action valueAction) where TKey : unmanaged { SerializeStructure(stream, dictionary.Count); foreach ((TKey key, TValue value) in dictionary) { SerializeStructure(stream, key); valueAction(stream, value); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void SerializeList(Stream stream, List list) where T : unmanaged { SerializeStructure(stream, list.Count); foreach (T item in list) { SerializeStructure(stream, item); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void SerializeStructure(Stream stream, T structure) where T : unmanaged { Span spanT = MemoryMarshal.CreateSpan(ref structure, 1); stream.Write(MemoryMarshal.AsBytes(spanT)); } #endregion } }