MeidoSerializer.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. public class MeidoSerializer : Serializer<Meido>
  9. {
  10. private const short version = 1;
  11. private const short headVersion = 1;
  12. private const short bodyVersion = 1;
  13. private const short clothingVersion = 1;
  14. private static SimpleSerializer<PoseInfo> PoseInfoSerializer => Serialization.GetSimple<PoseInfo>();
  15. private static SimpleSerializer<TransformDTO> TransformDtoSerializer => Serialization.GetSimple<TransformDTO>();
  16. public override void Serialize(Meido meido, BinaryWriter writer)
  17. {
  18. var maid = meido.Maid;
  19. using var memoryStream = new MemoryStream();
  20. using var tempWriter = new BinaryWriter(memoryStream, Encoding.UTF8);
  21. tempWriter.WriteVersion(version);
  22. TransformDtoSerializer.Serialize(new TransformDTO(maid.transform), tempWriter);
  23. SerializeHead(meido, tempWriter);
  24. SerializeBody(meido, tempWriter);
  25. SerializeClothing(meido, tempWriter);
  26. writer.Write(memoryStream.Length);
  27. writer.Write(memoryStream.ToArray());
  28. }
  29. public override void Deserialize(Meido meido, BinaryReader reader, SceneMetadata metadata)
  30. {
  31. var maid = meido.Maid;
  32. maid.GetAnimation().Stop();
  33. meido.DetachAllMpnAttach();
  34. meido.StopBlink();
  35. reader.ReadInt64(); // data length
  36. _ = reader.ReadVersion();
  37. var transformDto = TransformDtoSerializer.Deserialize(reader, metadata);
  38. var maidTransform = maid.transform;
  39. maidTransform.position = transformDto.Position;
  40. maidTransform.rotation = transformDto.Rotation;
  41. maidTransform.localScale = transformDto.LocalScale;
  42. meido.IKManager.SetDragPointScale(maidTransform.localScale.x);
  43. DeserializeHead(meido, reader, metadata);
  44. DeserializeBody(meido, reader, metadata);
  45. DeserializeClothing(meido, reader, metadata);
  46. }
  47. private static void SerializeHead(Meido meido, BinaryWriter writer)
  48. {
  49. var body = meido.Body;
  50. writer.WriteVersion(headVersion);
  51. // eye direction
  52. writer.WriteQuaternion(body.quaDefEyeL * Quaternion.Inverse(meido.DefaultEyeRotL));
  53. writer.WriteQuaternion(body.quaDefEyeR * Quaternion.Inverse(meido.DefaultEyeRotR));
  54. // free look
  55. writer.Write(meido.FreeLook);
  56. writer.WriteVector3(body.offsetLookTarget);
  57. writer.WriteVector3(Utility.GetFieldValue<TBody, Vector3>(body, "HeadEulerAngle"));
  58. // Head/eye to camera
  59. writer.Write(meido.HeadToCam);
  60. writer.Write(meido.EyeToCam);
  61. // face
  62. Dictionary<string, float> faceDict = meido.SerializeFace();
  63. writer.Write(faceDict.Count);
  64. foreach (var (hash, value) in faceDict)
  65. {
  66. writer.Write(hash);
  67. writer.Write(value);
  68. }
  69. }
  70. private static void SerializeBody(Meido meido, BinaryWriter writer)
  71. {
  72. writer.WriteVersion(bodyVersion);
  73. // pose
  74. var poseBuffer = meido.SerializePose(true);
  75. writer.Write(poseBuffer.Length);
  76. writer.Write(poseBuffer);
  77. PoseInfoSerializer.Serialize(meido.CachedPose, writer);
  78. }
  79. private static void SerializeClothing(Meido meido, BinaryWriter writer)
  80. {
  81. var maid = meido.Maid;
  82. var body = meido.Body;
  83. writer.WriteVersion(clothingVersion);
  84. // body visible
  85. writer.Write(body.GetMask(TBody.SlotID.body));
  86. // clothing
  87. foreach (var clothingSlot in MaidDressingPane.ClothingSlots)
  88. {
  89. var value = true;
  90. if (clothingSlot == TBody.SlotID.wear)
  91. {
  92. if (MaidDressingPane.WearSlots.Any(slot => body.GetSlotLoaded(slot)))
  93. {
  94. value = MaidDressingPane.WearSlots.Any(slot => body.GetMask(slot));
  95. }
  96. }
  97. else if (clothingSlot == TBody.SlotID.megane)
  98. {
  99. var slots = new[] { TBody.SlotID.megane, TBody.SlotID.accHead };
  100. if (slots.Any(slot => body.GetSlotLoaded(slot))) { value = slots.Any(slot => body.GetMask(slot)); }
  101. }
  102. else if (body.GetSlotLoaded(clothingSlot)) value = body.GetMask(clothingSlot);
  103. writer.Write(value);
  104. }
  105. // zurashi and mekure
  106. writer.Write(meido.CurlingFront);
  107. writer.Write(meido.CurlingBack);
  108. writer.Write(meido.PantsuShift);
  109. // mpn attach props
  110. var hasKousokuUpper = body.GetSlotLoaded(TBody.SlotID.kousoku_upper);
  111. writer.Write(hasKousokuUpper);
  112. writer.Write(maid.GetProp(MPN.kousoku_upper).strTempFileName);
  113. var hasKousokuLower = body.GetSlotLoaded(TBody.SlotID.kousoku_lower);
  114. writer.Write(hasKousokuLower);
  115. writer.Write(maid.GetProp(MPN.kousoku_lower).strTempFileName);
  116. // hair/skirt gravity
  117. writer.Write(meido.HairGravityActive);
  118. writer.Write(meido.HairGravityControl.Control.transform.localPosition);
  119. writer.Write(meido.SkirtGravityActive);
  120. writer.Write(meido.SkirtGravityControl.Control.transform.localPosition);
  121. }
  122. private static void DeserializeHead(Meido meido, BinaryReader reader, SceneMetadata metadata)
  123. {
  124. var body = meido.Body;
  125. _ = reader.ReadVersion();
  126. body.quaDefEyeL = reader.ReadQuaternion() * meido.DefaultEyeRotL;
  127. body.quaDefEyeR = reader.ReadQuaternion() * meido.DefaultEyeRotR;
  128. var freeLook = meido.FreeLook = reader.ReadBoolean();
  129. var offsetLookTarget = reader.ReadVector3();
  130. var headEulerAngle = reader.ReadVector3();
  131. if (freeLook) body.offsetLookTarget = offsetLookTarget;
  132. if (!metadata.MMConverted)
  133. {
  134. Utility.SetFieldValue(body, "HeadEulerAngleG", Vector3.zero);
  135. Utility.SetFieldValue(body, "HeadEulerAngle", headEulerAngle);
  136. }
  137. meido.HeadToCam = reader.ReadBoolean();
  138. meido.EyeToCam = reader.ReadBoolean();
  139. var faceBlendCount = reader.ReadInt32();
  140. for (var i = 0; i < faceBlendCount; i++)
  141. {
  142. var hash = reader.ReadString();
  143. var value = reader.ReadSingle();
  144. meido.SetFaceBlendValue(hash, value);
  145. }
  146. }
  147. private static void DeserializeBody(Meido meido, BinaryReader reader, SceneMetadata metadata)
  148. {
  149. _ = reader.ReadVersion();
  150. var muneSetting = new KeyValuePair<bool, bool>(true, true);
  151. if (metadata.MMConverted) meido.IKManager.Deserialize(reader);
  152. else
  153. {
  154. var poseBufferLength = reader.ReadInt32();
  155. byte[] poseBuffer = reader.ReadBytes(poseBufferLength);
  156. muneSetting = meido.SetFrameBinary(poseBuffer);
  157. }
  158. var poseInfo = PoseInfoSerializer.Deserialize(reader, metadata);
  159. Utility.SetPropertyValue(meido, nameof(Meido.CachedPose), poseInfo);
  160. meido.SetMune(!muneSetting.Key, true);
  161. meido.SetMune(!muneSetting.Value);
  162. }
  163. private static void DeserializeClothing(Meido meido, BinaryReader reader, SceneMetadata metadata)
  164. {
  165. var body = meido.Body;
  166. _ = reader.ReadVersion();
  167. meido.SetBodyMask(reader.ReadBoolean());
  168. foreach (var clothingSlot in MaidDressingPane.ClothingSlots)
  169. {
  170. var value = reader.ReadBoolean();
  171. if (metadata.MMConverted) continue;
  172. if (clothingSlot == TBody.SlotID.wear)
  173. {
  174. body.SetMask(TBody.SlotID.wear, value);
  175. body.SetMask(TBody.SlotID.mizugi, value);
  176. body.SetMask(TBody.SlotID.onepiece, value);
  177. }
  178. else if (clothingSlot == TBody.SlotID.megane)
  179. {
  180. body.SetMask(TBody.SlotID.megane, value);
  181. body.SetMask(TBody.SlotID.accHead, value);
  182. }
  183. else if (body.GetSlotLoaded(clothingSlot)) body.SetMask(clothingSlot, value);
  184. }
  185. // zurashi and mekure
  186. var curlingFront = reader.ReadBoolean();
  187. var curlingBack = reader.ReadBoolean();
  188. var curlingPantsu = reader.ReadBoolean();
  189. if (!metadata.MMConverted)
  190. {
  191. if (meido.CurlingFront != curlingFront) meido.SetCurling(Meido.Curl.Front, curlingFront);
  192. if (meido.CurlingBack != curlingBack) meido.SetCurling(Meido.Curl.Back, curlingBack);
  193. meido.SetCurling(Meido.Curl.Shift, curlingPantsu);
  194. }
  195. // MPN attach upper prop
  196. var hasKousokuUpper = reader.ReadBoolean();
  197. var upperMenuFile = reader.ReadString();
  198. if (hasKousokuUpper) meido.SetMpnProp(new MpnAttachProp(MPN.kousoku_upper, upperMenuFile), false);
  199. // MPN attach lower prop
  200. var hasKousokuLower = reader.ReadBoolean();
  201. var lowerMenuFile = reader.ReadString();
  202. if (hasKousokuLower) meido.SetMpnProp(new MpnAttachProp(MPN.kousoku_lower, lowerMenuFile), false);
  203. // hair gravity
  204. var hairGravityActive = reader.ReadBoolean();
  205. var hairPosition = reader.ReadVector3();
  206. meido.HairGravityActive = hairGravityActive;
  207. if (meido.HairGravityActive) meido.ApplyGravity(hairPosition);
  208. // skirt gravity
  209. var skirtGravityActive = reader.ReadBoolean();
  210. var skirtPosition = reader.ReadVector3();
  211. meido.SkirtGravityActive = skirtGravityActive;
  212. if (meido.SkirtGravityActive) meido.ApplyGravity(skirtPosition, true);
  213. }
  214. }
  215. }