BinReaderExt.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.IO;
  3. namespace OpenVIII
  4. {
  5. public static class BinReaderExt
  6. {
  7. public static bool Read(this BinaryReader br, out uint val)
  8. {
  9. return br.Read(br.ReadUInt32, out val);
  10. }
  11. public static bool Read(this BinaryReader br, out int val)
  12. {
  13. return br.Read(br.ReadInt32, out val);
  14. }
  15. public static bool Read(this BinaryReader br, out ushort val)
  16. {
  17. return br.Read(br.ReadUInt16, out val);
  18. }
  19. public static bool Read(this BinaryReader br, out short val)
  20. {
  21. return br.Read(br.ReadInt16, out val);
  22. }
  23. public static bool Read(this BinaryReader br, out byte val)
  24. {
  25. return br.Read(br.ReadByte, out val);
  26. }
  27. public static bool Read(this BinaryReader br, out sbyte val)
  28. {
  29. return br.Read(br.ReadSByte, out val);
  30. }
  31. public static unsafe bool Read<T>(this BinaryReader br, Func<T> f, out T val) where T : unmanaged
  32. {
  33. if (br.BaseStream.Length - sizeof(T) > br.BaseStream.Position)
  34. {
  35. val = f.Invoke();
  36. return true;
  37. }
  38. val = default;
  39. return false;
  40. }
  41. }
  42. }