ExtapathyExtended.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.IO;
  4. namespace OpenVIII
  5. {
  6. //Class that provides language extensions made by JWP/Extapathy
  7. internal class ExtapathyExtended
  8. {
  9. [SuppressMessage("ReSharper", "UnusedMember.Global")]
  10. public class BitReader : BinaryReader
  11. {
  12. private static readonly int[] PositionReadHelper = { 3, 6, 9, 16 };
  13. private static readonly int[] RotationReadHelper = { 3, 6, 8, 12 };
  14. private int _bitPosition;
  15. private long Position { get => BaseStream.Position; set => BaseStream.Position = value; }
  16. public BitReader(Stream input) : base(input) { }
  17. public short ReadBits(int count)
  18. {
  19. if (count > 16)
  20. throw new ArgumentException();
  21. var position = Position;
  22. var byte1 = BaseStream.ReadByte();
  23. var byte2 = BaseStream.ReadByte();
  24. var byte3 = BaseStream.ReadByte();
  25. var temp = byte1 | byte2 << 8 | byte3 << 16;
  26. temp = (short)((temp >> _bitPosition) & ~(0xFFFFFFFF << count));
  27. var value = (short)((temp << (32 - count)) >> (32 - count));
  28. Position = position + (count + _bitPosition) / 8;
  29. _bitPosition = (count + _bitPosition) % 8;
  30. return value;
  31. }
  32. public short ReadPositionType()
  33. {
  34. var countIndex = ReadBits(2) & 3;
  35. return ReadBits(PositionReadHelper[countIndex]);
  36. }
  37. //+Maki
  38. public byte ReadPositionLength() => (byte)PositionReadHelper[ReadBits(2) & 0b11];
  39. public byte ReadRotationLength() => (byte)RotationReadHelper[ReadBits(2) & 0b11];
  40. //-Maki
  41. public short ReadRotationType()
  42. {
  43. var readRotation = (ReadBits(1) & 1) != 0;
  44. if (!readRotation)
  45. return 0;
  46. var countIndex = ReadBits(2) & 3;
  47. return ReadBits(RotationReadHelper[countIndex]);
  48. }
  49. }
  50. }
  51. }