MPSScene.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System.IO;
  2. using System.Text;
  3. using UnityEngine;
  4. namespace MeidoPhotoStudio.Plugin;
  5. public class MPSScene
  6. {
  7. private byte[] data;
  8. public MPSScene(string path, Texture2D thumbnail = null)
  9. {
  10. FileInfo = new(path);
  11. if (!thumbnail)
  12. {
  13. thumbnail = new(1, 1, TextureFormat.ARGB32, false);
  14. thumbnail.LoadImage(File.ReadAllBytes(FileInfo.FullName));
  15. }
  16. Thumbnail = thumbnail;
  17. }
  18. public Texture2D Thumbnail { get; }
  19. public FileInfo FileInfo { get; }
  20. public bool Environment { get; private set; }
  21. public int NumberOfMaids { get; private set; }
  22. public byte[] Data
  23. {
  24. get
  25. {
  26. if (data is null)
  27. Preload();
  28. return data;
  29. }
  30. private set => data = value;
  31. }
  32. public void Preload()
  33. {
  34. if (data is not null)
  35. return;
  36. using var fileStream = FileInfo.OpenRead();
  37. Utility.SeekPngEnd(fileStream);
  38. using var memoryStream = new MemoryStream();
  39. fileStream.CopyTo(memoryStream);
  40. memoryStream.Position = 0L;
  41. using var binaryReader = new BinaryReader(memoryStream, Encoding.UTF8);
  42. var sceneHeader = MeidoPhotoStudio.SceneHeader;
  43. if (!Utility.BytesEqual(binaryReader.ReadBytes(sceneHeader.Length), sceneHeader))
  44. {
  45. Utility.LogWarning($"'{FileInfo.FullName}' is not a MPS Scene");
  46. return;
  47. }
  48. (_, Environment, NumberOfMaids, _) = SceneMetadata.ReadMetadata(binaryReader);
  49. Data = memoryStream.ToArray();
  50. }
  51. public void Destroy()
  52. {
  53. if (Thumbnail)
  54. Object.DestroyImmediate(Thumbnail);
  55. }
  56. }