AnimationSequence.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace OpenVIII.Battle.Dat
  7. {
  8. public struct AnimationSequence : IReadOnlyList<byte>
  9. {
  10. #region Fields
  11. public readonly int ID;
  12. public readonly uint Offset;
  13. private readonly IReadOnlyList<byte> _data;
  14. #endregion Fields
  15. #region Constructors
  16. private AnimationSequence(BinaryReader br, uint start, uint end, int id) : this()
  17. {
  18. br.BaseStream.Seek(start, SeekOrigin.Begin);
  19. ID = id;
  20. Offset = start;
  21. _data = br.ReadBytes(((int)Math.Abs(end - start)));
  22. }
  23. #endregion Constructors
  24. #region Properties
  25. public int Count => _data.Count;
  26. #endregion Properties
  27. #region Indexers
  28. public byte this[int index] => _data[index];
  29. #endregion Indexers
  30. #region Methods
  31. public static IReadOnlyList<AnimationSequence> CreateInstances(BinaryReader br, uint start, uint end)
  32. {
  33. // nothing final in here just was trying to dump data to see what was there.
  34. br.BaseStream.Seek(start, SeekOrigin.Begin);
  35. var offsets = new uint[br.ReadUInt16()];
  36. for (ushort i = 0; i < offsets.Length; i++)
  37. {
  38. var offset = br.ReadUInt16();
  39. if (offset == 0)
  40. continue;
  41. offsets[i] = offset + start;
  42. }
  43. IReadOnlyList<uint> orderedEnumerable = offsets.Where(x => x > 0).Distinct().OrderBy(x => x).ToList().AsReadOnly();
  44. return orderedEnumerable.Select((x, i) => new AnimationSequence(br, x, orderedEnumerable.Count > i + 1 ? orderedEnumerable[i + 1] : end, i))
  45. .ToList().AsReadOnly();
  46. }
  47. public IEnumerator<byte> GetEnumerator() => _data.GetEnumerator();
  48. IEnumerator IEnumerable.GetEnumerator() => _data.GetEnumerator();
  49. #endregion Methods
  50. }
  51. }