MPSScene.cs 1.9 KB

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