Meido.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Xml.Linq;
  8. using UnityEngine;
  9. using static TBody;
  10. using Object = UnityEngine.Object;
  11. namespace MeidoPhotoStudio.Plugin;
  12. public class Meido
  13. {
  14. public static readonly string DefaultFaceBlendSet = "通常";
  15. public static readonly string[] FaceKeys =
  16. new string[24]
  17. {
  18. "eyeclose", "eyeclose2", "eyeclose3", "eyebig", "eyeclose6", "eyeclose5", "hitomih",
  19. "hitomis", "mayuha", "mayuw", "mayuup", "mayuv", "mayuvhalf", "moutha", "mouths",
  20. "mouthc", "mouthi", "mouthup", "mouthdw", "mouthhe", "mouthuphalf", "tangout",
  21. "tangup", "tangopen",
  22. };
  23. public static readonly string[] FaceToggleKeys =
  24. 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. #pragma warning disable SA1308
  32. // TODO: Refactor reflection to using private members directly
  33. private readonly FieldInfo m_eMaskMode = Utility.GetFieldInfo<TBody>("m_eMaskMode");
  34. #pragma warning restore SA1308
  35. private bool initialized;
  36. private float[] blendSetValueBackup;
  37. private bool freeLook;
  38. public Meido(int stockMaidIndex)
  39. {
  40. StockNo = stockMaidIndex;
  41. Maid = GameMain.Instance.CharacterMgr.GetStockMaid(stockMaidIndex);
  42. IKManager = new(this);
  43. IKManager.SelectMaid += (_, args) =>
  44. OnUpdateMeido(args);
  45. }
  46. public event EventHandler<GravityEventArgs> GravityMove;
  47. public event EventHandler<MeidoUpdateEventArgs> UpdateMeido;
  48. public enum Curl
  49. {
  50. Front,
  51. Back,
  52. Shift,
  53. }
  54. public enum Mask
  55. {
  56. All,
  57. Underwear,
  58. Nude,
  59. }
  60. public MaskMode CurrentMaskMode =>
  61. !Body.isLoadedBody ? default : (MaskMode)m_eMaskMode.GetValue(Body);
  62. public DragPointGravity HairGravityControl { get; private set; }
  63. public DragPointGravity SkirtGravityControl { get; private set; }
  64. public Quaternion DefaultEyeRotL { get; private set; }
  65. public Quaternion DefaultEyeRotR { get; private set; }
  66. public bool Active { get; private set; }
  67. public bool IsEditMaid { get; set; }
  68. public PoseInfo CachedPose { get; private set; } = PoseInfo.DefaultPose;
  69. public string CurrentFaceBlendSet { get; private set; } = DefaultFaceBlendSet;
  70. public int Slot { get; private set; }
  71. public bool Loading { get; private set; }
  72. public int StockNo { get; }
  73. public Maid Maid { get; }
  74. public MeidoDragPointManager IKManager { get; }
  75. public TBody Body =>
  76. Maid.body0;
  77. public Texture2D Portrait =>
  78. Maid.GetThumIcon();
  79. public string FirstName =>
  80. Maid.status.firstName;
  81. public string LastName =>
  82. Maid.status.lastName;
  83. public bool Busy =>
  84. Maid.IsBusy || Loading;
  85. public bool CurlingFront =>
  86. Maid.IsItemChange("skirt", "めくれスカート") || Maid.IsItemChange("onepiece", "めくれスカート");
  87. public bool CurlingBack =>
  88. Maid.IsItemChange("skirt", "めくれスカート後ろ") || Maid.IsItemChange("onepiece", "めくれスカート後ろ");
  89. public bool PantsuShift =>
  90. Maid.IsItemChange("panz", "パンツずらし") || Maid.IsItemChange("mizugi", "パンツずらし");
  91. public bool HairGravityActive
  92. {
  93. get => HairGravityControl.Active;
  94. set
  95. {
  96. if (HairGravityControl.Valid)
  97. HairGravityControl.gameObject.SetActive(value);
  98. }
  99. }
  100. public bool SkirtGravityActive
  101. {
  102. get => SkirtGravityControl.Active;
  103. set
  104. {
  105. if (SkirtGravityControl.Valid)
  106. SkirtGravityControl.gameObject.SetActive(value);
  107. }
  108. }
  109. public bool FreeLook
  110. {
  111. get => freeLook;
  112. set
  113. {
  114. if (freeLook == value)
  115. return;
  116. freeLook = value;
  117. Body.trsLookTarget = freeLook ? null : GameMain.Instance.MainCamera.transform;
  118. OnUpdateMeido();
  119. }
  120. }
  121. public bool HeadToCam
  122. {
  123. get => Body.isLoadedBody && Body.boHeadToCam;
  124. set
  125. {
  126. if (!Body.isLoadedBody || HeadToCam == value)
  127. return;
  128. Body.HeadToCamPer = 0f;
  129. Body.boHeadToCam = value;
  130. if (!HeadToCam && !EyeToCam)
  131. FreeLook = false;
  132. OnUpdateMeido();
  133. }
  134. }
  135. public bool EyeToCam
  136. {
  137. get => Body.isLoadedBody && Body.boEyeToCam;
  138. set
  139. {
  140. if (!Body.isLoadedBody || EyeToCam == value)
  141. return;
  142. Body.boEyeToCam = value;
  143. if (!HeadToCam && !EyeToCam)
  144. FreeLook = false;
  145. OnUpdateMeido();
  146. }
  147. }
  148. public bool Stop
  149. {
  150. get => !Body.isLoadedBody || !Maid.GetAnimation().isPlaying;
  151. set
  152. {
  153. if (!Body.isLoadedBody || value == Stop)
  154. return;
  155. if (value)
  156. {
  157. Maid.GetAnimation().Stop();
  158. }
  159. else
  160. {
  161. Body.boEyeToCam = true;
  162. Body.boHeadToCam = true;
  163. SetPose(CachedPose.Pose);
  164. }
  165. OnUpdateMeido();
  166. }
  167. }
  168. public bool IK
  169. {
  170. get => IKManager.Active;
  171. set
  172. {
  173. if (value == IKManager.Active)
  174. return;
  175. IKManager.Active = value;
  176. }
  177. }
  178. public bool Bone
  179. {
  180. get => IKManager.IsBone;
  181. set
  182. {
  183. if (value == Bone)
  184. return;
  185. IKManager.IsBone = value;
  186. OnUpdateMeido();
  187. }
  188. }
  189. public void Load(int slot)
  190. {
  191. if (Busy)
  192. return;
  193. Slot = slot;
  194. if (Active)
  195. return;
  196. FreeLook = false;
  197. Maid.Visible = true;
  198. Body.boHeadToCam = true;
  199. Body.boEyeToCam = true;
  200. Body.SetBoneHitHeightY(-1000f);
  201. if (!Body.isLoadedBody)
  202. {
  203. Maid.DutPropAll();
  204. Maid.AllProcPropSeqStart();
  205. }
  206. StartLoad(OnBodyLoad);
  207. }
  208. public void Unload()
  209. {
  210. if (Body.isLoadedBody && Maid.Visible)
  211. {
  212. DetachAllMpnAttach();
  213. Body.jbMuneL.enabled = true;
  214. Body.jbMuneR.enabled = true;
  215. Body.quaDefEyeL = DefaultEyeRotL;
  216. Body.quaDefEyeR = DefaultEyeRotR;
  217. if (HairGravityControl)
  218. {
  219. HairGravityControl.Move -= OnGravityEvent;
  220. HairGravityActive = false;
  221. }
  222. if (SkirtGravityControl)
  223. {
  224. SkirtGravityControl.Move -= OnGravityEvent;
  225. SkirtGravityActive = false;
  226. }
  227. ApplyGravity(Vector3.zero, skirt: false);
  228. ApplyGravity(Vector3.zero, skirt: true);
  229. SetFaceBlendSet(DefaultFaceBlendSet);
  230. }
  231. AllProcPropSeqStartPatcher.SequenceStart -= ReinitializeBody;
  232. Body.MuneYureL(1f);
  233. Body.MuneYureR(1f);
  234. Body.SetMaskMode(MaskMode.None);
  235. Body.SetBoneHitHeightY(0f);
  236. Maid.Visible = false;
  237. IKManager.Destroy();
  238. Active = false;
  239. }
  240. public void Deactivate()
  241. {
  242. Unload();
  243. if (HairGravityControl)
  244. Object.Destroy(HairGravityControl.gameObject);
  245. if (SkirtGravityControl)
  246. Object.Destroy(SkirtGravityControl.gameObject);
  247. Maid.SetPos(Vector3.zero);
  248. Maid.SetRot(Vector3.zero);
  249. Maid.SetPosOffset(Vector3.zero);
  250. Body.transform.localScale = Vector3.one;
  251. Maid.ResetAll();
  252. Maid.MabatakiUpdateStop = false;
  253. Maid.ActiveSlotNo = -1;
  254. }
  255. public void SetPose(PoseInfo poseInfo)
  256. {
  257. CachedPose = poseInfo;
  258. SetPose(poseInfo.Pose);
  259. }
  260. public void SetPose(string pose)
  261. {
  262. if (!Body.isLoadedBody)
  263. return;
  264. if (pose.StartsWith(Constants.CustomPosePath))
  265. {
  266. var poseFilename = Path.GetFileNameWithoutExtension(pose);
  267. try
  268. {
  269. var poseBuffer = File.ReadAllBytes(pose);
  270. var hash = Path.GetFileName(pose).GetHashCode().ToString();
  271. Body.CrossFade(hash, poseBuffer, loop: true, fade: 0f);
  272. }
  273. catch (Exception e) when (e is DirectoryNotFoundException or FileNotFoundException)
  274. {
  275. Utility.LogWarning($"{poseFilename}: Could not open because {e.Message}");
  276. Constants.InitializeCustomPoses();
  277. SetPose(PoseInfo.DefaultPose);
  278. OnUpdateMeido();
  279. return;
  280. }
  281. catch (Exception e)
  282. {
  283. Utility.LogWarning($"{poseFilename}: Could not apply pose because {e.Message}");
  284. SetPose(PoseInfo.DefaultPose);
  285. OnUpdateMeido();
  286. return;
  287. }
  288. SetMune(true, left: true);
  289. SetMune(true, left: false);
  290. }
  291. else
  292. {
  293. var poseComponents = pose.Split(',');
  294. pose = poseComponents[0] + ".anm";
  295. Maid.CrossFade(pose, loop: true, val: 0f);
  296. Maid.GetAnimation().Play();
  297. if (poseComponents.Length > 1)
  298. {
  299. Maid.GetAnimation()[pose].time = float.Parse(poseComponents[1]);
  300. Maid.GetAnimation()[pose].speed = 0f;
  301. }
  302. SetPoseMune();
  303. }
  304. Maid.SetAutoTwistAll(true);
  305. }
  306. public KeyValuePair<bool, bool> SetFrameBinary(byte[] poseBuffer) =>
  307. GetCacheBoneData().SetFrameBinary(poseBuffer);
  308. public void CopyPose(Meido fromMeido)
  309. {
  310. Stop = true;
  311. SetFrameBinary(fromMeido.SerializePose(frameBinary: true));
  312. SetMune(fromMeido.Body.GetMuneYureL() is not 0f, left: true);
  313. SetMune(fromMeido.Body.GetMuneYureR() is not 0f, left: false);
  314. }
  315. public void SetMune(bool enabled, bool left = false)
  316. {
  317. var value = enabled ? 1f : 0f;
  318. if (left)
  319. {
  320. Body.MuneYureL(value);
  321. Body.jbMuneL.enabled = enabled;
  322. }
  323. else
  324. {
  325. Body.MuneYureR(value);
  326. Body.jbMuneR.enabled = enabled;
  327. }
  328. }
  329. public void SetHandPreset(string filename, bool right)
  330. {
  331. var faceFilename = Path.GetFileNameWithoutExtension(filename);
  332. try
  333. {
  334. var handDocument = XDocument.Load(filename);
  335. var handElement = handDocument.Element("FingerData");
  336. if (handElement?.Elements().Any(element => element?.IsEmpty ?? true) ?? true)
  337. {
  338. Utility.LogWarning($"{faceFilename}: Could not apply hand preset because it is invalid.");
  339. return;
  340. }
  341. Stop = true;
  342. var rightData = bool.Parse(handElement.Element("RightData").Value);
  343. var base64Data = handElement.Element("BinaryData").Value;
  344. var handData = Convert.FromBase64String(base64Data);
  345. IKManager.DeserializeHand(handData, right, rightData != right);
  346. }
  347. catch (System.Xml.XmlException e)
  348. {
  349. Utility.LogWarning($"{faceFilename}: Hand preset data is malformed because {e.Message}");
  350. }
  351. catch (Exception e) when (e is DirectoryNotFoundException or FileNotFoundException)
  352. {
  353. Utility.LogWarning($"{faceFilename}: Could not open hand preset because {e.Message}");
  354. Constants.InitializeHandPresets();
  355. }
  356. catch (Exception e)
  357. {
  358. Utility.LogWarning($"{faceFilename}: Could not parse hand preset because {e.Message}");
  359. }
  360. }
  361. public byte[] SerializePose(bool frameBinary = false)
  362. {
  363. var cache = GetCacheBoneData();
  364. var muneL = Body.GetMuneYureL() is 0f;
  365. var muneR = Body.GetMuneYureR() is 0f;
  366. return frameBinary
  367. ? cache.GetFrameBinary(muneL, muneR)
  368. : cache.GetAnmBinary(true, true);
  369. }
  370. public Dictionary<string, float> SerializeFace()
  371. {
  372. var faceData = new Dictionary<string, float>();
  373. foreach (var hash in FaceKeys.Concat(FaceToggleKeys))
  374. {
  375. try
  376. {
  377. var value = GetFaceBlendValue(hash);
  378. faceData.Add(hash, value);
  379. }
  380. catch
  381. {
  382. // Ignored
  383. }
  384. }
  385. return faceData;
  386. }
  387. public void SetFaceBlendSet(string blendSet)
  388. {
  389. if (blendSet.StartsWith(Constants.CustomFacePath))
  390. {
  391. var blendSetFileName = Path.GetFileNameWithoutExtension(blendSet);
  392. try
  393. {
  394. var faceDocument = XDocument.Load(blendSet, LoadOptions.SetLineInfo);
  395. var faceDataElement = faceDocument.Element("FaceData");
  396. if (faceDataElement?.IsEmpty ?? true)
  397. {
  398. Utility.LogWarning($"{blendSetFileName}: Could not apply face preset because it is invalid.");
  399. return;
  400. }
  401. var hashKeys = new HashSet<string>(FaceKeys.Concat(FaceToggleKeys));
  402. foreach (var element in faceDataElement.Elements())
  403. {
  404. System.Xml.IXmlLineInfo info = element;
  405. var line = info.HasLineInfo() ? info.LineNumber : -1;
  406. string key;
  407. if ((key = (string)element.Attribute("name")) is null)
  408. {
  409. Utility.LogWarning($"{blendSetFileName}: Could not read face blend key at line {line}.");
  410. continue;
  411. }
  412. if (!hashKeys.Contains(key))
  413. {
  414. Utility.LogWarning($"{blendSetFileName}: Invalid face blend key '{key}' at line {line}.");
  415. continue;
  416. }
  417. if (float.TryParse(element.Value, out var value))
  418. {
  419. try
  420. {
  421. SetFaceBlendValue(key, value);
  422. }
  423. catch
  424. {
  425. // Ignored.
  426. }
  427. }
  428. else
  429. {
  430. Utility.LogWarning(
  431. $"{blendSetFileName}: Could not parse value '{element.Value}' of '{key}' at line {line}");
  432. }
  433. }
  434. }
  435. catch (System.Xml.XmlException e)
  436. {
  437. Utility.LogWarning($"{blendSetFileName}: Face preset data is malformed because {e.Message}");
  438. return;
  439. }
  440. catch (Exception e) when (e is DirectoryNotFoundException or FileNotFoundException)
  441. {
  442. Utility.LogWarning($"{blendSetFileName}: Could not open face preset because {e.Message}");
  443. Constants.InitializeCustomFaceBlends();
  444. return;
  445. }
  446. catch (Exception e)
  447. {
  448. Utility.LogWarning($"{blendSetFileName}: Could not parse face preset because {e.Message}");
  449. return;
  450. }
  451. }
  452. else
  453. {
  454. ApplyBackupBlendSet();
  455. CurrentFaceBlendSet = blendSet;
  456. BackupBlendSetValues();
  457. Maid.FaceAnime(blendSet, 0f);
  458. var morph = Body.Face.morph;
  459. foreach (var faceKey in FaceKeys)
  460. {
  461. var hash = Utility.GP01FbFaceHash(morph, faceKey);
  462. if (!morph.Contains(hash))
  463. continue;
  464. var blendIndex = (int)morph.hash[hash];
  465. var value = faceKey is "nosefook"
  466. ? Maid.boNoseFook || morph.boNoseFook ? 1f : 0f
  467. : morph.dicBlendSet[CurrentFaceBlendSet][blendIndex];
  468. morph.SetBlendValues(blendIndex, value);
  469. }
  470. morph.FixBlendValues();
  471. }
  472. StopBlink();
  473. OnUpdateMeido();
  474. }
  475. public void SetFaceBlendValue(string faceKey, float value)
  476. {
  477. var morph = Body.Face.morph;
  478. var hash = Utility.GP01FbFaceHash(morph, faceKey);
  479. if (!morph.Contains(hash))
  480. return;
  481. var blendIndex = (int)morph.hash[hash];
  482. if (faceKey is "nosefook")
  483. Maid.boNoseFook = morph.boNoseFook = value > 0f;
  484. else
  485. morph.dicBlendSet[CurrentFaceBlendSet][blendIndex] = value;
  486. morph.SetBlendValues(blendIndex, value);
  487. morph.FixBlendValues();
  488. }
  489. public float GetFaceBlendValue(string hash)
  490. {
  491. var morph = Body.Face.morph;
  492. if (hash is "nosefook")
  493. return (Maid.boNoseFook || morph.boNoseFook) ? 1f : 0f;
  494. hash = Utility.GP01FbFaceHash(morph, hash);
  495. return morph.dicBlendSet[CurrentFaceBlendSet][(int)morph.hash[hash]];
  496. }
  497. public void StopBlink()
  498. {
  499. Maid.MabatakiUpdateStop = true;
  500. Body.Face.morph.EyeMabataki = 0f;
  501. Utility.SetFieldValue(Maid, "MabatakiVal", 0f);
  502. }
  503. public void SetMaskMode(Mask maskMode) =>
  504. SetMaskMode(maskMode is Mask.Nude ? MaskMode.Nude : (MaskMode)maskMode);
  505. public void SetMaskMode(MaskMode maskMode)
  506. {
  507. var invisibleBody = !Body.GetMask(SlotID.body);
  508. Body.SetMaskMode(maskMode);
  509. if (invisibleBody)
  510. SetBodyMask(false);
  511. }
  512. public void SetBodyMask(bool enabled)
  513. {
  514. var table = Utility.GetFieldValue<TBody, Hashtable>(Body, "m_hFoceHide");
  515. foreach (var bodySlot in MaidDressingPane.BodySlots)
  516. table[bodySlot] = enabled;
  517. Body.FixMaskFlag();
  518. Body.FixVisibleFlag(false);
  519. }
  520. public void SetCurling(Curl curling, bool enabled)
  521. {
  522. var name = curling is Curl.Shift
  523. ? new[] { "panz", "mizugi" }
  524. : new[] { "skirt", "onepiece" };
  525. if (enabled)
  526. {
  527. var action = curling switch
  528. {
  529. Curl.Shift => "パンツずらし",
  530. Curl.Front => "めくれスカート",
  531. _ => "めくれスカート後ろ",
  532. };
  533. Maid.ItemChangeTemp(name[0], action);
  534. Maid.ItemChangeTemp(name[1], action);
  535. }
  536. else
  537. {
  538. Maid.ResetProp(name[0]);
  539. Maid.ResetProp(name[1]);
  540. }
  541. Maid.AllProcProp();
  542. HairGravityControl.Control.OnChangeMekure();
  543. SkirtGravityControl.Control.OnChangeMekure();
  544. }
  545. public void SetMpnProp(MpnAttachProp prop, bool detach)
  546. {
  547. if (detach)
  548. Maid.ResetProp(prop.Tag, false);
  549. else
  550. Maid.SetProp(prop.Tag, prop.MenuFile, 0, true);
  551. Maid.AllProcProp();
  552. }
  553. public void DetachAllMpnAttach()
  554. {
  555. Maid.ResetProp(MPN.kousoku_lower, false);
  556. Maid.ResetProp(MPN.kousoku_upper, false);
  557. Maid.AllProcProp();
  558. }
  559. public void ApplyGravity(Vector3 position, bool skirt = false)
  560. {
  561. var dragPoint = skirt ? SkirtGravityControl : HairGravityControl;
  562. if (dragPoint.Valid)
  563. dragPoint.Control.transform.localPosition = position;
  564. }
  565. private void StartLoad(Action callback)
  566. {
  567. if (Loading)
  568. return;
  569. GameMain.Instance.StartCoroutine(Load(callback));
  570. }
  571. private IEnumerator Load(Action callback)
  572. {
  573. Loading = true;
  574. while (Maid.IsBusy)
  575. yield return null;
  576. yield return new WaitForEndOfFrame();
  577. callback();
  578. Loading = false;
  579. }
  580. private void OnBodyLoad()
  581. {
  582. if (!initialized)
  583. {
  584. DefaultEyeRotL = Body.quaDefEyeL;
  585. DefaultEyeRotR = Body.quaDefEyeR;
  586. initialized = true;
  587. }
  588. if (blendSetValueBackup is null)
  589. BackupBlendSetValues();
  590. if (!HairGravityControl)
  591. InitializeGravityControls();
  592. HairGravityControl.Move += OnGravityEvent;
  593. SkirtGravityControl.Move += OnGravityEvent;
  594. if (MeidoPhotoStudio.EditMode)
  595. AllProcPropSeqStartPatcher.SequenceStart += ReinitializeBody;
  596. IKManager.Initialize();
  597. SetFaceBlendSet(DefaultFaceBlendSet);
  598. IK = true;
  599. Stop = false;
  600. Bone = false;
  601. Active = true;
  602. }
  603. private void ReinitializeBody(object sender, ProcStartEventArgs args)
  604. {
  605. if (Loading || !Body.isLoadedBody)
  606. return;
  607. if (args.Maid.status.guid != Maid.status.guid)
  608. return;
  609. var gravityControlProps =
  610. new[]
  611. {
  612. MPN.skirt, MPN.onepiece, MPN.mizugi, MPN.panz, MPN.set_maidwear, MPN.set_mywear, MPN.set_underwear,
  613. MPN.hairf, MPN.hairr, MPN.hairs, MPN.hairt,
  614. };
  615. Action action = null;
  616. // Change body
  617. if (Maid.GetProp(MPN.body).boDut)
  618. {
  619. IKManager.Destroy();
  620. action += ReinitializeBody;
  621. }
  622. // Change face
  623. if (Maid.GetProp(MPN.head).boDut)
  624. {
  625. SetFaceBlendSet(DefaultFaceBlendSet);
  626. action += ReinitializeFace;
  627. }
  628. // Gravity control clothing/hair change
  629. if (gravityControlProps.Any(prop => Maid.GetProp(prop).boDut))
  630. {
  631. if (HairGravityControl)
  632. Object.Destroy(HairGravityControl.gameObject);
  633. if (SkirtGravityControl)
  634. Object.Destroy(SkirtGravityControl.gameObject);
  635. action += ReinitializeGravity;
  636. }
  637. // Clothing/accessory changes
  638. // Includes null_mpn too but any button click results in null_mpn bodut I think
  639. action ??= Default;
  640. StartLoad(action);
  641. void ReinitializeBody()
  642. {
  643. IKManager.Initialize();
  644. Stop = false;
  645. // Maid animation needs to be set again for custom parts edit
  646. var uiRoot = GameObject.Find("UI Root");
  647. var customPartsWindow = UTY.GetChildObject(uiRoot, "Window/CustomPartsWindow")
  648. .GetComponent<SceneEditWindow.CustomPartsWindow>();
  649. Utility.SetFieldValue(customPartsWindow, "animation", Maid.GetAnimation());
  650. }
  651. void ReinitializeFace()
  652. {
  653. DefaultEyeRotL = Body.quaDefEyeL;
  654. DefaultEyeRotR = Body.quaDefEyeR;
  655. BackupBlendSetValues();
  656. }
  657. void ReinitializeGravity()
  658. {
  659. InitializeGravityControls();
  660. OnUpdateMeido();
  661. }
  662. void Default() =>
  663. OnUpdateMeido();
  664. }
  665. private void SetPoseMune()
  666. {
  667. var momiOrPaizuri = CachedPose.Pose.Contains("_momi") || CachedPose.Pose.Contains("paizuri_");
  668. SetMune(!momiOrPaizuri, left: true);
  669. SetMune(!momiOrPaizuri, left: false);
  670. }
  671. private void BackupBlendSetValues()
  672. {
  673. var values = Body.Face.morph.dicBlendSet[CurrentFaceBlendSet];
  674. blendSetValueBackup = new float[values.Length];
  675. values.CopyTo(blendSetValueBackup, 0);
  676. }
  677. private void ApplyBackupBlendSet()
  678. {
  679. blendSetValueBackup.CopyTo(Body.Face.morph.dicBlendSet[CurrentFaceBlendSet], 0);
  680. Maid.boNoseFook = false;
  681. }
  682. private CacheBoneDataArray GetCacheBoneData()
  683. {
  684. var cache = Maid.gameObject.GetComponent<CacheBoneDataArray>();
  685. if (!cache)
  686. {
  687. cache = Maid.gameObject.AddComponent<CacheBoneDataArray>();
  688. CreateCache();
  689. }
  690. if (!cache.bone_data?.transform)
  691. {
  692. Utility.LogDebug("Cache bone_data is null");
  693. CreateCache();
  694. }
  695. void CreateCache() =>
  696. cache.CreateCache(Body.GetBone("Bip01"));
  697. return cache;
  698. }
  699. private void InitializeGravityControls()
  700. {
  701. HairGravityControl = MakeGravityControl(skirt: false);
  702. SkirtGravityControl = MakeGravityControl(skirt: true);
  703. }
  704. private DragPointGravity MakeGravityControl(bool skirt = false)
  705. {
  706. var gravityDragpoint = DragPoint.Make<DragPointGravity>(PrimitiveType.Cube, Vector3.one * 0.12f);
  707. var control = DragPointGravity.MakeGravityControl(Maid, skirt);
  708. gravityDragpoint.Initialize(() => control.transform.position, () => Vector3.zero);
  709. gravityDragpoint.Set(control.transform);
  710. gravityDragpoint.gameObject.SetActive(false);
  711. return gravityDragpoint;
  712. }
  713. private void OnUpdateMeido(MeidoUpdateEventArgs args = null) =>
  714. UpdateMeido?.Invoke(this, args ?? MeidoUpdateEventArgs.Empty);
  715. private void OnGravityEvent(object sender, EventArgs args) =>
  716. OnGravityChange((DragPointGravity)sender);
  717. private void OnGravityChange(DragPointGravity dragPoint)
  718. {
  719. var args =
  720. new GravityEventArgs(dragPoint == SkirtGravityControl, dragPoint.MyObject.transform.localPosition);
  721. GravityMove?.Invoke(this, args);
  722. }
  723. }