설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseChunk.cs 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. namespace UnityEditor.U2D.Aseprite
  5. {
  6. /// <summary>
  7. /// Aseprite Chunk Types.
  8. /// </summary>
  9. public enum ChunkTypes
  10. {
  11. None = 0,
  12. OldPalette = 0x0004,
  13. OldPalette2 = 0x0011,
  14. Layer = 0x2004,
  15. Cell = 0x2005,
  16. CellExtra = 0x2006,
  17. ColorProfile = 0x2007,
  18. ExternalFiles = 0x2008,
  19. Mask = 0x2016,
  20. Path = 0x2017,
  21. Tags = 0x2018,
  22. Palette = 0x2019,
  23. UserData = 0x2020,
  24. Slice = 0x2022,
  25. Tileset = 0x2023
  26. }
  27. /// <summary>
  28. /// The header of each chunk.
  29. /// </summary>
  30. public class ChunkHeader
  31. {
  32. /// <summary>
  33. /// The stride of the chunk header in bytes.
  34. /// </summary>
  35. public const int stride = 6;
  36. /// <summary>
  37. /// The size of the chunk in bytes.
  38. /// </summary>
  39. public uint chunkSize { get; private set; }
  40. /// <summary>
  41. /// The type of the chunk.
  42. /// </summary>
  43. public ChunkTypes chunkType { get; private set; }
  44. internal void Read(BinaryReader reader)
  45. {
  46. chunkSize = reader.ReadUInt32();
  47. chunkType = (ChunkTypes)reader.ReadUInt16();
  48. }
  49. }
  50. public abstract class BaseChunk : IDisposable
  51. {
  52. /// <summary>
  53. /// The type of the chunk.
  54. /// </summary>
  55. public virtual ChunkTypes chunkType => ChunkTypes.None;
  56. protected readonly uint m_ChunkSize;
  57. protected BaseChunk(uint chunkSize)
  58. {
  59. m_ChunkSize = chunkSize;
  60. }
  61. internal bool Read(BinaryReader reader)
  62. {
  63. var bytes = reader.ReadBytes((int)m_ChunkSize - ChunkHeader.stride);
  64. using var memoryStream = new MemoryStream(bytes);
  65. using var chunkReader = new BinaryReader(memoryStream);
  66. try
  67. {
  68. InternalRead(chunkReader);
  69. }
  70. catch (Exception e)
  71. {
  72. Debug.LogError($"Failed to read a chunk of type: {chunkType}. Skipping the chunk. \nException: {e}");
  73. return false;
  74. }
  75. return true;
  76. }
  77. protected abstract void InternalRead(BinaryReader reader);
  78. public virtual void Dispose() { }
  79. }
  80. }