MeidoManagerSerializer.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEngine;
  4. namespace COM3D2.MeidoPhotoStudio.Plugin
  5. {
  6. public class MeidoManagerSerializer : Serializer<MeidoManager>
  7. {
  8. private const short version = 1;
  9. private static Serializer<Meido> MeidoSerializer => Serialization.Get<Meido>();
  10. public override void Serialize(MeidoManager manager, BinaryWriter writer)
  11. {
  12. writer.Write(MeidoManager.header);
  13. writer.WriteVersion(version);
  14. List<Meido> meidoList = manager.ActiveMeidoList;
  15. var meidoCount = meidoList.Count;
  16. var hairPosition = Vector3.zero;
  17. var skirtPosition = Vector3.zero;
  18. var hairMeidoFound = false;
  19. var skirtMeidoFound = false;
  20. var globalGravity = manager.GlobalGravity;
  21. writer.Write(meidoCount);
  22. foreach (var meido in meidoList)
  23. {
  24. MeidoSerializer.Serialize(meido, writer);
  25. if (!globalGravity || meidoCount <= 0) continue;
  26. // Get gravity and skirt control positions to apply to meidos past the meido count
  27. if (!hairMeidoFound && meido.HairGravityControl.Valid)
  28. {
  29. hairPosition = meido.HairGravityControl.Control.transform.localPosition;
  30. hairMeidoFound = true;
  31. }
  32. else if (!skirtMeidoFound && meido.SkirtGravityControl.Valid)
  33. {
  34. skirtPosition = meido.SkirtGravityControl.Control.transform.localPosition;
  35. skirtMeidoFound = true;
  36. }
  37. }
  38. writer.Write(globalGravity);
  39. writer.Write(hairPosition);
  40. writer.Write(skirtPosition);
  41. }
  42. public override void Deserialize(MeidoManager manager, BinaryReader reader, SceneMetadata metadata)
  43. {
  44. _ = reader.ReadVersion();
  45. var meidoCount = reader.ReadInt32();
  46. for (var i = 0; i < meidoCount; i++)
  47. {
  48. if (i >= manager.ActiveMeidoList.Count)
  49. {
  50. reader.BaseStream.Seek(reader.ReadInt64(), SeekOrigin.Current);
  51. continue;
  52. }
  53. MeidoSerializer.Deserialize(manager.ActiveMeidoList[i], reader, metadata);
  54. }
  55. var globalGravity = reader.ReadBoolean();
  56. var hairPosition = reader.ReadVector3();
  57. var skirtPosition = reader.ReadVector3();
  58. Utility.SetFieldValue(manager, "globalGravity", globalGravity);
  59. if (!globalGravity) return;
  60. foreach (var meido in manager.ActiveMeidoList)
  61. {
  62. meido.HairGravityActive = true;
  63. meido.SkirtGravityActive = true;
  64. meido.ApplyGravity(hairPosition);
  65. meido.ApplyGravity(skirtPosition, true);
  66. }
  67. }
  68. }
  69. }