Meido.cs 31 KB

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