CameraManagerSerializer.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using System.IO;
  2. namespace COM3D2.MeidoPhotoStudio.Plugin
  3. {
  4. public class CameraManagerSerializer : Serializer<CameraManager>
  5. {
  6. private const short version = 1;
  7. private static Serializer<CameraInfo> InfoSerializer => Serialization.Get<CameraInfo>();
  8. private static readonly CameraInfo dummyInfo = new();
  9. public override void Serialize(CameraManager manager, BinaryWriter writer)
  10. {
  11. writer.Write(CameraManager.header);
  12. writer.WriteVersion(version);
  13. CameraInfo[] cameraInfos = GetCameraInfos(manager);
  14. cameraInfos[manager.CurrentCameraIndex].UpdateInfo(CameraUtility.MainCamera);
  15. writer.Write(manager.CurrentCameraIndex);
  16. writer.Write(manager.CameraCount);
  17. foreach (var info in cameraInfos) InfoSerializer.Serialize(info, writer);
  18. }
  19. public override void Deserialize(CameraManager manager, BinaryReader reader, SceneMetadata metadata)
  20. {
  21. _ = reader.ReadVersion();
  22. var camera = CameraUtility.MainCamera;
  23. manager.CurrentCameraIndex = reader.ReadInt32();
  24. var cameraCount = reader.ReadInt32();
  25. CameraInfo[] cameraInfos = GetCameraInfos(manager);
  26. for (var i = 0; i < cameraCount; i++)
  27. InfoSerializer.Deserialize(i >= manager.CameraCount ? dummyInfo : cameraInfos[i], reader, metadata);
  28. if (metadata.Environment) return;
  29. cameraInfos[manager.CurrentCameraIndex].Apply(camera);
  30. }
  31. private static CameraInfo[] GetCameraInfos(CameraManager manager)
  32. => Utility.GetFieldValue<CameraManager, CameraInfo[]>(manager, "cameraInfos");
  33. }
  34. }