Meido.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4. using System.Linq;
  5. using System.Xml.Linq;
  6. using UnityEngine;
  7. using static TBody;
  8. using System.Collections.Generic;
  9. namespace COM3D2.MeidoPhotoStudio.Plugin
  10. {
  11. internal class Meido : ISerializable
  12. {
  13. private bool initialized;
  14. private DragPointGravity hairGravityDragPoint;
  15. private GravityTransformControl hairGravityControl;
  16. public bool HairGravityValid => hairGravityControl != null;
  17. private DragPointGravity skirtGravityDragPoint;
  18. private GravityTransformControl skirtGravityControl;
  19. public bool SkirtGravityValid => skirtGravityControl != null;
  20. private float[] BlendSetValueBackup;
  21. public const int meidoDataVersion = 1000;
  22. public static readonly PoseInfo DefaultPose =
  23. new PoseInfo(Constants.PoseGroupList[0], Constants.PoseDict[Constants.PoseGroupList[0]][0]);
  24. public static readonly string defaultFaceBlendSet = "通常";
  25. public static readonly string[] faceKeys = new string[24]
  26. {
  27. "eyeclose", "eyeclose2", "eyeclose3", "eyebig", "eyeclose6", "eyeclose5", "hitomih",
  28. "hitomis", "mayuha", "mayuw", "mayuup", "mayuv", "mayuvhalf", "moutha", "mouths",
  29. "mouthc", "mouthi", "mouthup", "mouthdw", "mouthhe", "mouthuphalf", "tangout",
  30. "tangup", "tangopen"
  31. };
  32. public static readonly string[] faceToggleKeys = new string[12]
  33. {
  34. // blush, shade, nose up, tears, drool, teeth
  35. "hoho2", "shock", "nosefook", "namida", "yodare", "toothoff",
  36. // cry 1, cry 2, cry 3, blush 1, blush 2, blush 3
  37. "tear1", "tear2", "tear3", "hohos", "hoho", "hohol"
  38. };
  39. public enum Curl
  40. {
  41. front, back, shift
  42. }
  43. public event EventHandler<MeidoUpdateEventArgs> UpdateMeido;
  44. public int StockNo { get; }
  45. public Maid Maid { get; }
  46. public TBody Body => Maid.body0;
  47. public MeidoDragPointManager IKManager { get; }
  48. public Texture2D Portrait { get; }
  49. public PoseInfo CachedPose { get; private set; } = DefaultPose;
  50. public string CurrentFaceBlendSet { get; private set; } = defaultFaceBlendSet;
  51. public int Slot { get; private set; }
  52. public bool Loading { get; private set; }
  53. public string FirstName => Maid.status.firstName;
  54. public string LastName => Maid.status.lastName;
  55. public bool Busy => Maid.IsBusy && Loading;
  56. public bool CurlingFront => Maid.IsItemChange("skirt", "めくれスカート")
  57. || Maid.IsItemChange("onepiece", "めくれスカート");
  58. public bool CurlingBack => Maid.IsItemChange("skirt", "めくれスカート後ろ")
  59. || Maid.IsItemChange("onepiece", "めくれスカート後ろ");
  60. public bool PantsuShift => Maid.IsItemChange("panz", "パンツずらし")
  61. || Maid.IsItemChange("mizugi", "パンツずらし");
  62. private bool freeLook;
  63. public bool FreeLook
  64. {
  65. get => freeLook;
  66. set
  67. {
  68. if (freeLook == value) return;
  69. freeLook = value;
  70. Body.trsLookTarget = freeLook ? null : GameMain.Instance.MainCamera.transform;
  71. OnUpdateMeido();
  72. }
  73. }
  74. public bool HeadToCam
  75. {
  76. get => Body.isLoadedBody && Body.boHeadToCam;
  77. set
  78. {
  79. if (!Body.isLoadedBody || HeadToCam == value) return;
  80. Body.HeadToCamPer = 0f;
  81. Body.boHeadToCam = value;
  82. if (!HeadToCam && !EyeToCam) FreeLook = false;
  83. OnUpdateMeido();
  84. }
  85. }
  86. public bool EyeToCam
  87. {
  88. get => Body.isLoadedBody && Body.boEyeToCam;
  89. set
  90. {
  91. if (!Body.isLoadedBody || EyeToCam == value) return;
  92. Body.boEyeToCam = value;
  93. if (!HeadToCam && !EyeToCam) FreeLook = false;
  94. OnUpdateMeido();
  95. }
  96. }
  97. public bool Stop
  98. {
  99. get => !Body.isLoadedBody || !Maid.GetAnimation().isPlaying;
  100. set
  101. {
  102. if (!Body.isLoadedBody || value == Stop) return;
  103. if (value) Maid.GetAnimation().Stop();
  104. else
  105. {
  106. Body.boEyeToCam = true;
  107. Body.boHeadToCam = true;
  108. SetPose(CachedPose.Pose);
  109. }
  110. OnUpdateMeido();
  111. }
  112. }
  113. public bool IK
  114. {
  115. get => IKManager.Active;
  116. set
  117. {
  118. if (value == IKManager.Active) return;
  119. IKManager.Active = value;
  120. }
  121. }
  122. public bool Bone
  123. {
  124. get => IKManager.IsBone;
  125. set
  126. {
  127. if (value == Bone) return;
  128. IKManager.IsBone = value;
  129. OnUpdateMeido();
  130. }
  131. }
  132. public bool HairGravityActive
  133. {
  134. get => HairGravityValid && hairGravityDragPoint.gameObject.activeSelf;
  135. set
  136. {
  137. if (HairGravityValid && value != HairGravityActive)
  138. {
  139. hairGravityDragPoint.gameObject.SetActive(value);
  140. hairGravityControl.isEnabled = value;
  141. }
  142. }
  143. }
  144. public bool SkirtGravityActive
  145. {
  146. get => SkirtGravityValid && skirtGravityDragPoint.gameObject.activeSelf;
  147. set
  148. {
  149. if (SkirtGravityValid && value != SkirtGravityActive)
  150. {
  151. skirtGravityDragPoint.gameObject.SetActive(value);
  152. skirtGravityControl.isEnabled = value;
  153. }
  154. }
  155. }
  156. public event EventHandler<GravityEventArgs> GravityMove;
  157. public Quaternion DefaultEyeRotL { get; private set; }
  158. public Quaternion DefaultEyeRotR { get; private set; }
  159. public Meido(int stockMaidIndex)
  160. {
  161. StockNo = stockMaidIndex;
  162. Maid = GameMain.Instance.CharacterMgr.GetStockMaid(stockMaidIndex);
  163. Portrait = Maid.GetThumIcon();
  164. IKManager = new MeidoDragPointManager(this);
  165. IKManager.SelectMaid += (s, args) => OnUpdateMeido(args);
  166. }
  167. public void BeginLoad()
  168. {
  169. FreeLook = false;
  170. Maid.Visible = true;
  171. Body.boHeadToCam = true;
  172. Body.boEyeToCam = true;
  173. Body.SetBoneHitHeightY(-1000f);
  174. }
  175. public void Load(int slot)
  176. {
  177. Slot = slot;
  178. Loading = true;
  179. if (!Body.isLoadedBody)
  180. {
  181. Maid.DutPropAll();
  182. Maid.AllProcPropSeqStart();
  183. }
  184. GameMain.Instance.StartCoroutine(Load());
  185. }
  186. private IEnumerator Load()
  187. {
  188. while (Maid.IsBusy) yield return null;
  189. yield return new WaitForEndOfFrame();
  190. OnBodyLoad();
  191. }
  192. public void Unload()
  193. {
  194. if (Body.isLoadedBody)
  195. {
  196. DetachAllMpnAttach();
  197. Body.jbMuneL.enabled = true;
  198. Body.jbMuneR.enabled = true;
  199. Body.quaDefEyeL = DefaultEyeRotL;
  200. Body.quaDefEyeR = DefaultEyeRotR;
  201. HairGravityActive = false;
  202. SkirtGravityActive = false;
  203. if (HairGravityValid) hairGravityDragPoint.Move -= OnGravityEvent;
  204. if (SkirtGravityValid) skirtGravityDragPoint.Move -= OnGravityEvent;
  205. }
  206. Body.MuneYureL(1f);
  207. Body.MuneYureR(1f);
  208. Body.SetMaskMode(MaskMode.None);
  209. Body.SetBoneHitHeightY(0f);
  210. Maid.Visible = false;
  211. IKManager.Destroy();
  212. }
  213. public void Deactivate()
  214. {
  215. if (Body.isLoadedBody) SetFaceBlendSet(defaultFaceBlendSet);
  216. Unload();
  217. DestroyGravityControl(ref hairGravityControl);
  218. DestroyGravityControl(ref skirtGravityControl);
  219. GameObject.Destroy(hairGravityDragPoint?.gameObject);
  220. GameObject.Destroy(skirtGravityDragPoint?.gameObject);
  221. Maid.SetPos(Vector3.zero);
  222. Maid.SetRot(Vector3.zero);
  223. Maid.SetPosOffset(Vector3.zero);
  224. Body.transform.localScale = Vector3.one;
  225. Maid.ResetAll();
  226. Maid.MabatakiUpdateStop = false;
  227. Maid.ActiveSlotNo = -1;
  228. }
  229. public void SetPose(PoseInfo poseInfo)
  230. {
  231. CachedPose = poseInfo;
  232. SetPose(poseInfo.Pose);
  233. }
  234. public void SetPose(string pose)
  235. {
  236. if (!Body.isLoadedBody) return;
  237. if (pose.StartsWith(Constants.customPosePath))
  238. {
  239. byte[] poseBuffer = File.ReadAllBytes(pose);
  240. string hash = Path.GetFileName(pose).GetHashCode().ToString();
  241. Body.CrossFade(hash, poseBuffer, loop: true, fade: 0f);
  242. }
  243. else
  244. {
  245. string[] poseComponents = pose.Split(',');
  246. pose = poseComponents[0] + ".anm";
  247. Maid.CrossFade(pose, loop: true, val: 0f);
  248. Maid.GetAnimation().Play();
  249. if (poseComponents.Length > 1)
  250. {
  251. Maid.GetAnimation()[pose].time = float.Parse(poseComponents[1]);
  252. Maid.GetAnimation()[pose].speed = 0f;
  253. }
  254. }
  255. Maid.SetAutoTwistAll(true);
  256. SetMune();
  257. }
  258. public void CopyPose(Meido fromMeido)
  259. {
  260. byte[] poseBinary = fromMeido.SerializePose();
  261. string tag = $"copy_{fromMeido.Maid.status.guid}";
  262. Body.CrossFade(tag, poseBinary, false, true, false, 0f);
  263. Maid.SetAutoTwistAll(true);
  264. Maid.transform.rotation = fromMeido.Maid.transform.rotation;
  265. SetMune();
  266. }
  267. public void SetMune(bool drag = false)
  268. {
  269. bool momiOrPaizuri = CachedPose.Pose.Contains("_momi") || CachedPose.Pose.Contains("paizuri_");
  270. float onL = (drag || momiOrPaizuri) ? 0f : 1f;
  271. Body.MuneYureL(onL);
  272. Body.MuneYureR(onL);
  273. Body.jbMuneL.enabled = !drag;
  274. Body.jbMuneR.enabled = !drag;
  275. }
  276. public void SetHandPreset(string filename, bool right)
  277. {
  278. XDocument handDocument = XDocument.Load(filename);
  279. XElement handElement = handDocument.Element("FingerData");
  280. if (handElement.IsEmpty || handElement.Element("GameVersion").IsEmpty
  281. || handElement.Element("RightData").IsEmpty || handElement.Element("BinaryData").IsEmpty
  282. ) return;
  283. Stop = true;
  284. bool rightData = bool.Parse(handElement.Element("RightData").Value);
  285. string base64Data = handElement.Element("BinaryData").Value;
  286. byte[] handData = Convert.FromBase64String(base64Data);
  287. IKManager.DeserializeHand(handData, right, rightData != right);
  288. }
  289. public byte[] SerializePose(bool frameBinary = false)
  290. {
  291. CacheBoneDataArray cache = GetCacheBoneData();
  292. return frameBinary ? cache.GetFrameBinary(true, true) : cache.GetAnmBinary(true, true);
  293. }
  294. public Dictionary<string, float> SerializeFace()
  295. {
  296. Dictionary<string, float> faceData = new Dictionary<string, float>();
  297. foreach (string hash in faceKeys.Concat(faceToggleKeys))
  298. {
  299. try
  300. {
  301. float value = GetFaceBlendValue(hash);
  302. faceData.Add(hash, value);
  303. }
  304. catch { }
  305. }
  306. return faceData;
  307. }
  308. public void SetFaceBlendSet(string blendSet, bool custom = false)
  309. {
  310. if (custom)
  311. {
  312. XDocument faceDocument = XDocument.Load(blendSet);
  313. XElement faceDataElement = faceDocument.Element("FaceData");
  314. if (faceDataElement.IsEmpty) return;
  315. HashSet<string> hashKeys = new HashSet<string>(faceKeys.Concat(faceToggleKeys));
  316. foreach (XElement element in faceDataElement.Elements())
  317. {
  318. string key;
  319. if ((key = (string)element.Attribute("name")) != null && !hashKeys.Contains(key)) continue;
  320. if (float.TryParse(element.Value, out float value))
  321. {
  322. try
  323. {
  324. SetFaceBlendValue(key, value);
  325. }
  326. catch { }
  327. }
  328. }
  329. }
  330. else
  331. {
  332. ApplyBackupBlendSet();
  333. CurrentFaceBlendSet = blendSet;
  334. BackupBlendSetValuess();
  335. Maid.FaceAnime(blendSet, 0f);
  336. }
  337. StopBlink();
  338. OnUpdateMeido();
  339. }
  340. public void SetFaceBlendValue(string hash, float value)
  341. {
  342. TMorph morph = Body.Face.morph;
  343. if (hash == "nosefook") Maid.boNoseFook = morph.boNoseFook = value > 0f;
  344. else
  345. {
  346. hash = Utility.GP01FbFaceHash(morph, hash);
  347. try
  348. {
  349. morph.dicBlendSet[CurrentFaceBlendSet][(int)morph.hash[hash]] = value;
  350. }
  351. catch { }
  352. }
  353. }
  354. public float GetFaceBlendValue(string hash)
  355. {
  356. TMorph morph = Body.Face.morph;
  357. if (hash == "nosefook") return (Maid.boNoseFook || morph.boNoseFook) ? 1f : 0f;
  358. hash = Utility.GP01FbFaceHash(morph, hash);
  359. return morph.dicBlendSet[CurrentFaceBlendSet][(int)morph.hash[hash]];
  360. }
  361. public void StopBlink()
  362. {
  363. Maid.MabatakiUpdateStop = true;
  364. Body.Face.morph.EyeMabataki = 0f;
  365. Utility.SetFieldValue(Maid, "MabatakiVal", 0f);
  366. }
  367. public void SetMaskMode(MaskMode maskMode)
  368. {
  369. bool invisibleBody = !Body.GetMask(SlotID.body);
  370. Body.SetMaskMode(maskMode);
  371. if (invisibleBody) SetBodyMask(false);
  372. }
  373. public void SetBodyMask(bool enabled)
  374. {
  375. Hashtable table = Utility.GetFieldValue<TBody, Hashtable>(Body, "m_hFoceHide");
  376. foreach (SlotID bodySlot in MaidDressingPane.bodySlots)
  377. {
  378. table[bodySlot] = enabled;
  379. }
  380. if (Body.goSlot[19].m_strModelFileName.Contains("melala_body"))
  381. {
  382. table[SlotID.accHana] = enabled;
  383. }
  384. Body.FixMaskFlag();
  385. Body.FixVisibleFlag(false);
  386. }
  387. public void SetCurling(Curl curling, bool enabled)
  388. {
  389. string[] name = curling == Curl.shift
  390. ? new[] { "panz", "mizugi" }
  391. : new[] { "skirt", "onepiece" };
  392. if (enabled)
  393. {
  394. string action = curling == Curl.shift
  395. ? "パンツずらし" : curling == Curl.front
  396. ? "めくれスカート" : "めくれスカート後ろ";
  397. Maid.ItemChangeTemp(name[0], action);
  398. Maid.ItemChangeTemp(name[1], action);
  399. }
  400. else
  401. {
  402. Maid.ResetProp(name[0]);
  403. Maid.ResetProp(name[1]);
  404. }
  405. Maid.AllProcProp();
  406. hairGravityControl?.OnChangeMekure();
  407. skirtGravityControl?.OnChangeMekure();
  408. }
  409. public void SetMpnProp(MpnAttachProp prop, bool detach)
  410. {
  411. if (detach) Maid.ResetProp(prop.Tag, false);
  412. else Maid.SetProp(prop.Tag, prop.MenuFile, 0, true);
  413. Maid.AllProcProp();
  414. }
  415. public void DetachAllMpnAttach()
  416. {
  417. Maid.ResetProp(MPN.kousoku_lower, false);
  418. Maid.ResetProp(MPN.kousoku_upper, false);
  419. Maid.AllProcProp();
  420. }
  421. public void ApplyGravity(Vector3 position, bool skirt = false)
  422. {
  423. DragPointGravity dragPoint = skirt ? skirtGravityDragPoint : hairGravityDragPoint;
  424. if (dragPoint != null) dragPoint.MyObject.localPosition = position;
  425. }
  426. private void BackupBlendSetValuess()
  427. {
  428. float[] values = Body.Face.morph.dicBlendSet[CurrentFaceBlendSet];
  429. BlendSetValueBackup = new float[values.Length];
  430. values.CopyTo(BlendSetValueBackup, 0);
  431. }
  432. private void ApplyBackupBlendSet()
  433. {
  434. BlendSetValueBackup.CopyTo(Body.Face.morph.dicBlendSet[CurrentFaceBlendSet], 0);
  435. Maid.boNoseFook = false;
  436. }
  437. private CacheBoneDataArray GetCacheBoneData()
  438. {
  439. CacheBoneDataArray cache = Maid.gameObject.GetComponent<CacheBoneDataArray>();
  440. if (cache == null)
  441. {
  442. cache = Maid.gameObject.AddComponent<CacheBoneDataArray>();
  443. cache.CreateCache(Body.GetBone("Bip01"));
  444. }
  445. return cache;
  446. }
  447. private void OnBodyLoad()
  448. {
  449. if (!initialized)
  450. {
  451. TMorph faceMorph = Body.Face.morph;
  452. DefaultEyeRotL = Body.quaDefEyeL;
  453. DefaultEyeRotR = Body.quaDefEyeR;
  454. InitializeGravityControls();
  455. initialized = true;
  456. }
  457. if (BlendSetValueBackup == null) BackupBlendSetValuess();
  458. if (HairGravityValid) hairGravityDragPoint.Move += OnGravityEvent;
  459. if (SkirtGravityValid) skirtGravityDragPoint.Move += OnGravityEvent;
  460. IKManager.Initialize();
  461. IK = true;
  462. Stop = false;
  463. Bone = false;
  464. Loading = false;
  465. }
  466. private void InitializeGravityControls()
  467. {
  468. hairGravityControl = InitializeGravityControl("hair");
  469. if (hairGravityControl.isValid)
  470. {
  471. hairGravityDragPoint = MakeGravityDragPoint(hairGravityControl);
  472. HairGravityActive = false;
  473. }
  474. else DestroyGravityControl(ref hairGravityControl);
  475. skirtGravityControl = InitializeGravityControl("skirt");
  476. if (skirtGravityControl.isValid)
  477. {
  478. skirtGravityDragPoint = MakeGravityDragPoint(skirtGravityControl);
  479. SkirtGravityActive = false;
  480. }
  481. else DestroyGravityControl(ref skirtGravityControl);
  482. }
  483. private DragPointGravity MakeGravityDragPoint(GravityTransformControl control)
  484. {
  485. DragPointGravity gravityDragpoint = DragPoint.Make<DragPointGravity>(
  486. PrimitiveType.Cube, Vector3.one * 0.12f
  487. );
  488. gravityDragpoint.Initialize(() => control.transform.position, () => Vector3.zero);
  489. gravityDragpoint.Set(control.transform);
  490. return gravityDragpoint;
  491. }
  492. private GravityTransformControl InitializeGravityControl(string category)
  493. {
  494. Transform bone = Body.GetBone("Bip01");
  495. string gravityGoName = $"GravityDatas_{Maid.status.guid}_{category}";
  496. Transform gravityTransform = Maid.gameObject.transform.Find(gravityGoName);
  497. if (gravityTransform == null)
  498. {
  499. GameObject go = new GameObject(gravityGoName);
  500. go.transform.SetParent(bone, false);
  501. go.transform.SetParent(Maid.transform, true);
  502. go.transform.localScale = Vector3.one;
  503. go.transform.rotation = Quaternion.identity;
  504. GameObject go2 = new GameObject(gravityGoName);
  505. go2.transform.SetParent(go.transform, false);
  506. gravityTransform = go2.transform;
  507. }
  508. else
  509. {
  510. gravityTransform = gravityTransform.GetChild(0);
  511. GravityTransformControl control = gravityTransform.GetComponent<GravityTransformControl>();
  512. if (control != null) GameObject.Destroy(control);
  513. }
  514. GravityTransformControl gravityControl = gravityTransform.gameObject.AddComponent<GravityTransformControl>();
  515. SlotID[] slots = category == "skirt"
  516. ? new[] { SlotID.skirt, SlotID.onepiece, SlotID.mizugi, SlotID.panz }
  517. : new[] { SlotID.hairF, SlotID.hairR, SlotID.hairS, SlotID.hairT };
  518. gravityControl.SetTargetSlods(slots);
  519. gravityControl.forceRate = 0.1f;
  520. return gravityControl;
  521. }
  522. private void DestroyGravityControl(ref GravityTransformControl control)
  523. {
  524. if (control != null)
  525. {
  526. GameObject.Destroy(control.transform.parent.gameObject);
  527. control = null;
  528. }
  529. }
  530. private void OnUpdateMeido(MeidoUpdateEventArgs args = null)
  531. {
  532. UpdateMeido?.Invoke(this, args ?? MeidoUpdateEventArgs.Empty);
  533. }
  534. private void OnGravityEvent(object sender, EventArgs args) => OnGravityChange((DragPointGravity)sender);
  535. private void OnGravityChange(DragPointGravity dragPoint)
  536. {
  537. GravityEventArgs args = new GravityEventArgs(
  538. dragPoint == skirtGravityDragPoint, dragPoint.MyObject.transform.localPosition
  539. );
  540. GravityMove?.Invoke(this, args);
  541. }
  542. public void Serialize(BinaryWriter binaryWriter)
  543. {
  544. using (MemoryStream memoryStream = new MemoryStream())
  545. using (BinaryWriter tempWriter = new BinaryWriter(memoryStream))
  546. {
  547. // transform
  548. tempWriter.WriteVector3(Maid.transform.position);
  549. tempWriter.WriteQuaternion(Maid.transform.rotation);
  550. tempWriter.WriteVector3(Maid.transform.localScale);
  551. // pose
  552. byte[] poseBuffer = SerializePose(true);
  553. tempWriter.Write(poseBuffer.Length);
  554. tempWriter.Write(poseBuffer);
  555. CachedPose.Serialize(tempWriter);
  556. // eye direction
  557. tempWriter.WriteQuaternion(Body.quaDefEyeL * Quaternion.Inverse(DefaultEyeRotL));
  558. tempWriter.WriteQuaternion(Body.quaDefEyeR * Quaternion.Inverse(DefaultEyeRotR));
  559. // free look
  560. tempWriter.Write(FreeLook);
  561. if (FreeLook)
  562. {
  563. tempWriter.WriteVector3(Body.offsetLookTarget);
  564. tempWriter.WriteVector3(Utility.GetFieldValue<TBody, Vector3>(Body, "HeadEulerAngle"));
  565. }
  566. // Head/eye to camera
  567. tempWriter.Write(HeadToCam);
  568. tempWriter.Write(EyeToCam);
  569. // face
  570. SerializeFace(tempWriter);
  571. // body visible
  572. tempWriter.Write(Body.GetMask(SlotID.body));
  573. // clothing
  574. foreach (SlotID clothingSlot in MaidDressingPane.clothingSlots)
  575. {
  576. bool value = true;
  577. if (clothingSlot == SlotID.wear)
  578. {
  579. if (MaidDressingPane.wearSlots.Any(slot => Body.GetSlotLoaded(slot)))
  580. {
  581. value = MaidDressingPane.wearSlots.Any(slot => Body.GetMask(slot));
  582. }
  583. }
  584. else if (clothingSlot == SlotID.megane)
  585. {
  586. SlotID[] slots = new[] { SlotID.megane, SlotID.accHead };
  587. if (slots.Any(slot => Body.GetSlotLoaded(slot)))
  588. {
  589. value = slots.Any(slot => Body.GetMask(slot));
  590. }
  591. }
  592. else if (Body.GetSlotLoaded(clothingSlot))
  593. {
  594. value = Body.GetMask(clothingSlot);
  595. }
  596. tempWriter.Write(value);
  597. }
  598. // zurashi and mekure
  599. tempWriter.Write(CurlingFront);
  600. tempWriter.Write(CurlingBack);
  601. tempWriter.Write(PantsuShift);
  602. bool hasKousokuUpper = Body.GetSlotLoaded(SlotID.kousoku_upper);
  603. tempWriter.Write(hasKousokuUpper);
  604. if (hasKousokuUpper) tempWriter.Write(Maid.GetProp(MPN.kousoku_upper).strTempFileName);
  605. bool hasKousokuLower = Body.GetSlotLoaded(SlotID.kousoku_lower);
  606. tempWriter.Write(hasKousokuLower);
  607. if (hasKousokuLower) tempWriter.Write(Maid.GetProp(MPN.kousoku_lower).strTempFileName);
  608. binaryWriter.Write(memoryStream.Length);
  609. binaryWriter.Write(memoryStream.ToArray());
  610. }
  611. }
  612. private void SerializeFace(BinaryWriter binaryWriter)
  613. {
  614. binaryWriter.Write("MPS_FACE");
  615. foreach (string hash in faceKeys.Concat(faceToggleKeys))
  616. {
  617. try
  618. {
  619. float value = GetFaceBlendValue(hash);
  620. binaryWriter.Write(hash);
  621. binaryWriter.Write(value);
  622. }
  623. catch { }
  624. }
  625. binaryWriter.Write("END_FACE");
  626. }
  627. public void Deserialize(BinaryReader binaryReader) => Deserialize(binaryReader, meidoDataVersion, false);
  628. public void Deserialize(BinaryReader binaryReader, int dataVersion, bool mmScene)
  629. {
  630. Maid.GetAnimation().Stop();
  631. DetachAllMpnAttach();
  632. binaryReader.ReadInt64(); // meido buffer length
  633. // transform
  634. Maid.transform.position = binaryReader.ReadVector3();
  635. Maid.transform.rotation = binaryReader.ReadQuaternion();
  636. Maid.transform.localScale = binaryReader.ReadVector3();
  637. // pose
  638. if (mmScene) IKManager.Deserialize(binaryReader);
  639. else
  640. {
  641. int poseBufferLength = binaryReader.ReadInt32();
  642. byte[] poseBuffer = binaryReader.ReadBytes(poseBufferLength);
  643. GetCacheBoneData().SetFrameBinary(poseBuffer);
  644. }
  645. Body.MuneYureL(0f);
  646. Body.MuneYureR(0f);
  647. Body.jbMuneL.enabled = false;
  648. Body.jbMuneR.enabled = false;
  649. CachedPose = PoseInfo.Deserialize(binaryReader);
  650. // eye direction
  651. Body.quaDefEyeL = binaryReader.ReadQuaternion() * DefaultEyeRotL;
  652. Body.quaDefEyeR = binaryReader.ReadQuaternion() * DefaultEyeRotR;
  653. // free look
  654. FreeLook = binaryReader.ReadBoolean();
  655. if (FreeLook)
  656. {
  657. Body.offsetLookTarget = binaryReader.ReadVector3();
  658. // Head angle cannot be resolved with just the offsetLookTarget
  659. if (!mmScene)
  660. {
  661. Utility.SetFieldValue(Body, "HeadEulerAngleG", Vector3.zero);
  662. Utility.SetFieldValue(Body, "HeadEulerAngle", binaryReader.ReadVector3());
  663. }
  664. }
  665. // Head/eye to camera
  666. HeadToCam = binaryReader.ReadBoolean();
  667. EyeToCam = binaryReader.ReadBoolean();
  668. // face
  669. DeserializeFace(binaryReader);
  670. // body visible
  671. SetBodyMask(binaryReader.ReadBoolean());
  672. // clothing
  673. foreach (SlotID clothingSlot in MaidDressingPane.clothingSlots)
  674. {
  675. bool value = binaryReader.ReadBoolean();
  676. if (mmScene) continue;
  677. if (clothingSlot == SlotID.wear)
  678. {
  679. Body.SetMask(SlotID.wear, value);
  680. Body.SetMask(SlotID.mizugi, value);
  681. Body.SetMask(SlotID.onepiece, value);
  682. }
  683. else if (clothingSlot == SlotID.megane)
  684. {
  685. Body.SetMask(SlotID.megane, value);
  686. Body.SetMask(SlotID.accHead, value);
  687. }
  688. else if (Body.GetSlotLoaded(clothingSlot))
  689. {
  690. Body.SetMask(clothingSlot, value);
  691. }
  692. }
  693. // zurashi and mekure
  694. bool curlingFront = binaryReader.ReadBoolean();
  695. bool curlingBack = binaryReader.ReadBoolean();
  696. bool curlingPantsu = binaryReader.ReadBoolean();
  697. if (!mmScene)
  698. {
  699. if (CurlingFront != curlingFront) SetCurling(Curl.front, curlingFront);
  700. if (CurlingBack != curlingBack) SetCurling(Curl.back, curlingBack);
  701. SetCurling(Curl.shift, curlingPantsu);
  702. }
  703. bool hasKousokuUpper = binaryReader.ReadBoolean();
  704. if (hasKousokuUpper)
  705. {
  706. try
  707. {
  708. SetMpnProp(new MpnAttachProp(MPN.kousoku_upper, binaryReader.ReadString()), false);
  709. }
  710. catch { }
  711. }
  712. bool hasKousokuLower = binaryReader.ReadBoolean();
  713. if (hasKousokuLower)
  714. {
  715. try
  716. {
  717. SetMpnProp(new MpnAttachProp(MPN.kousoku_lower, binaryReader.ReadString()), false);
  718. }
  719. catch { }
  720. }
  721. // OnUpdateMeido();
  722. }
  723. private void DeserializeFace(BinaryReader binaryReader)
  724. {
  725. StopBlink();
  726. binaryReader.ReadString(); // read face header
  727. string header;
  728. while ((header = binaryReader.ReadString()) != "END_FACE")
  729. {
  730. SetFaceBlendValue(header, binaryReader.ReadSingle());
  731. }
  732. }
  733. }
  734. public class GravityEventArgs : EventArgs
  735. {
  736. public Vector3 LocalPosition { get; }
  737. public bool IsSkirt { get; }
  738. public GravityEventArgs(bool isSkirt, Vector3 localPosition)
  739. {
  740. LocalPosition = localPosition;
  741. IsSkirt = isSkirt;
  742. }
  743. }
  744. public struct PoseInfo
  745. {
  746. public string PoseGroup { get; }
  747. public string Pose { get; }
  748. public bool CustomPose { get; }
  749. public PoseInfo(string poseGroup, string pose, bool customPose = false)
  750. {
  751. PoseGroup = poseGroup;
  752. Pose = pose;
  753. CustomPose = customPose;
  754. }
  755. public void Serialize(BinaryWriter binaryWriter)
  756. {
  757. binaryWriter.Write(PoseGroup);
  758. binaryWriter.Write(Pose);
  759. binaryWriter.Write(CustomPose);
  760. }
  761. public static PoseInfo Deserialize(BinaryReader binaryReader)
  762. {
  763. return new PoseInfo
  764. (
  765. binaryReader.ReadString(),
  766. binaryReader.ReadString(),
  767. binaryReader.ReadBoolean()
  768. );
  769. }
  770. }
  771. }