PropManagerSerializer.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections.Generic;
  2. using System.IO;
  3. namespace COM3D2.MeidoPhotoStudio.Plugin
  4. {
  5. public class PropManagerSerializer : Serializer<PropManager>
  6. {
  7. private const short version = 1;
  8. private static SimpleSerializer<DragPointPropDTO> DragPointDtoSerializer
  9. => Serialization.GetSimple<DragPointPropDTO>();
  10. public override void Serialize(PropManager manager, BinaryWriter writer)
  11. {
  12. writer.Write(PropManager.header);
  13. writer.WriteVersion(version);
  14. List<DragPointProp> propList = GetPropList(manager);
  15. writer.Write(propList.Count);
  16. foreach (var prop in propList) DragPointDtoSerializer.Serialize(new DragPointPropDTO(prop), writer);
  17. }
  18. public override void Deserialize(PropManager manager, BinaryReader reader, SceneMetadata metadata)
  19. {
  20. _ = reader.ReadVersion();
  21. manager.DeleteAllProps();
  22. List<DragPointProp> propList = GetPropList(manager);
  23. var propCount = reader.ReadInt32();
  24. var propIndex = 0;
  25. for (var i = 0; i < propCount; i++)
  26. {
  27. var dragPointPropDto = DragPointDtoSerializer.Deserialize(reader, metadata);
  28. if (!manager.AddFromPropInfo(dragPointPropDto.PropInfo)) continue;
  29. Apply(manager, propList[propIndex], dragPointPropDto);
  30. propIndex++;
  31. }
  32. }
  33. private static void Apply(PropManager manager, DragPointProp prop, DragPointPropDTO dto)
  34. {
  35. var (transformDto, attachPointInfo, shadowCasting) = dto;
  36. prop.ShadowCasting = shadowCasting;
  37. var transform = prop.MyObject;
  38. if (attachPointInfo.AttachPoint != AttachPoint.None)
  39. {
  40. manager.AttachProp(prop, attachPointInfo.AttachPoint, attachPointInfo.MaidIndex);
  41. transform.localPosition = transformDto.LocalPosition;
  42. transform.localRotation = transformDto.LocalRotation;
  43. }
  44. transform.position = transformDto.Position;
  45. transform.rotation = transformDto.Rotation;
  46. transform.localScale = transformDto.LocalScale;
  47. }
  48. private static List<DragPointProp> GetPropList(PropManager manager)
  49. => Utility.GetFieldValue<PropManager, List<DragPointProp>>(manager, "propList");
  50. }
  51. }