ExampleDocumentParser.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using PixiEditor.Parser;
  2. using PixiEditor.SDK.FileParsers;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Text;
  6. namespace PixiEditor.ExtensionExample
  7. {
  8. [FileParser(".nopixi")]
  9. [Description("A example file type called .noPixi.\n Can only save layer images, name and is heavier than a regular .pixi file so you shouldn't actually use it")]
  10. public class ExampleDocumentParser : DocumentParser
  11. {
  12. public override bool UseBigEndian { get; } = true;
  13. public override Encoding Encoding { get; } = Encoding.UTF8;
  14. public override SerializableDocument Parse()
  15. {
  16. int width = ReadInt32();
  17. int height = ReadInt32();
  18. SerializableDocument document = new SerializableDocument(width, height);
  19. for (int i = 0; true; i++)
  20. {
  21. try
  22. {
  23. int layerWidth = ReadInt32();
  24. int layerHeight = ReadInt32();
  25. int offsetX = ReadInt32();
  26. int offsetY = ReadInt32();
  27. string layerName = ReadString();
  28. SerializableLayer layer = new SerializableLayer()
  29. {
  30. Width = layerWidth,
  31. Height = layerHeight,
  32. OffsetX = offsetX,
  33. OffsetY = offsetY,
  34. Name = layerName,
  35. BitmapBytes = ReadBytes(ReadInt32())
  36. };
  37. document.Layers.Add(layer);
  38. }
  39. catch (EndOfStreamException)
  40. {
  41. break;
  42. }
  43. }
  44. return document;
  45. }
  46. public override void Save(SerializableDocument document)
  47. {
  48. WriteInt32(document.Width);
  49. WriteInt32(document.Height);
  50. foreach (SerializableLayer layer in document)
  51. {
  52. WriteInt32(layer.Width);
  53. WriteInt32(layer.Height);
  54. WriteInt32(layer.OffsetX);
  55. WriteInt32(layer.OffsetY);
  56. WriteString(layer.Name, true);
  57. WriteInt32(layer.BitmapBytes.Length);
  58. WriteBytes(layer.BitmapBytes);
  59. }
  60. }
  61. }
  62. }