// Licensed to Timothy Schenk under the GNU AGPL Version 3 License. using System.Runtime.InteropServices; namespace Wonderking.Game.Reader; public static class BinaryReader where T : new() { public static readonly Func Read; #pragma warning disable MA0051 static BinaryReader() #pragma warning restore MA0051 { var type = typeof(T); if (type == typeof(bool)) { Read = (Func)(Delegate)(Func)(p => p.ReadBoolean()); } else if (type == typeof(char)) { Read = (Func)(Delegate)(Func)(p => p.ReadChar()); } else if (type == typeof(string)) { Read = (Func)(Delegate)(Func)(p => p.ReadString()); } else if (type == typeof(sbyte)) { Read = (Func)(Delegate)(Func)(p => p.ReadSByte()); } else if (type == typeof(short)) { Read = (Func)(Delegate)(Func)(p => p.ReadInt16()); } else if (type == typeof(int)) { Read = (Func)(Delegate)(Func)(p => p.ReadInt32()); } else if (type == typeof(long)) { Read = (Func)(Delegate)(Func)(p => p.ReadInt64()); } else if (type == typeof(byte)) { Read = (Func)(Delegate)(Func)(p => p.ReadByte()); } else if (type == typeof(ushort)) { Read = (Func)(Delegate)(Func)(p => p.ReadUInt16()); } else if (type == typeof(uint)) { Read = (Func)(Delegate)(Func)(p => p.ReadUInt32()); } else if (type == typeof(ulong)) { Read = (Func)(Delegate)(Func)(p => p.ReadUInt64()); } else if (type == typeof(float)) { Read = (Func)(Delegate)(Func)(p => p.ReadSingle()); } else if (type == typeof(double)) { Read = (Func)(Delegate)(Func)(p => p.ReadDouble()); } else if (type == typeof(decimal)) { Read = (Func)(Delegate)(Func)(p => p.ReadDecimal()); } else { Read = (Func)(p => (T)(object)p.ReadBytes(Marshal.SizeOf(new T()))); } } }