ExtapathyExtended.cs 2.1 KB

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