Meido.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace COM3D2.MeidoPhotoStudio.Plugin
  6. {
  7. public class Meido
  8. {
  9. private static CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
  10. public readonly int stockNo;
  11. public Maid Maid { get; private set; }
  12. public Texture2D Image { get; private set; }
  13. public string FirstName { get; private set; }
  14. public string LastName { get; private set; }
  15. public string NameJP => $"{LastName}\n{FirstName}";
  16. public string NameEN => $"{FirstName}\n{LastName}";
  17. public int ActiveSlot { get; private set; }
  18. private DragPointManager dragPointManager;
  19. public event EventHandler<MeidoChangeEventArgs> SelectMeido;
  20. public event EventHandler BodyLoad;
  21. private bool isLoading = false;
  22. public bool Visible
  23. {
  24. get => Maid.Visible;
  25. set => Maid.Visible = value;
  26. }
  27. public Meido(int stockMaidIndex)
  28. {
  29. this.Maid = characterMgr.GetStockMaid(stockMaidIndex);
  30. this.stockNo = stockMaidIndex;
  31. this.Image = Maid.GetThumIcon();
  32. this.FirstName = Maid.status.firstName;
  33. this.LastName = Maid.status.lastName;
  34. Maid.boAllProcPropBUSY = false;
  35. }
  36. public void Update()
  37. {
  38. if (isLoading)
  39. {
  40. if (!Maid.IsBusy)
  41. {
  42. isLoading = false;
  43. OnBodyLoad();
  44. }
  45. return;
  46. }
  47. dragPointManager.Update();
  48. }
  49. public Maid Load(int activeSlot)
  50. {
  51. isLoading = true;
  52. this.ActiveSlot = activeSlot;
  53. Maid.Visible = true;
  54. if (!Maid.body0.isLoadedBody)
  55. {
  56. Maid.DutPropAll();
  57. Maid.AllProcPropSeqStart();
  58. }
  59. else
  60. {
  61. SetPose("pose_taiki_f");
  62. }
  63. if (dragPointManager == null)
  64. {
  65. dragPointManager = new DragPointManager(this);
  66. dragPointManager.SelectMaid += (sender, meidoChangeArgs) => OnMeidoSelect(meidoChangeArgs);
  67. }
  68. else
  69. {
  70. dragPointManager.Activate();
  71. }
  72. Maid.body0.boHeadToCam = true;
  73. Maid.body0.boEyeToCam = true;
  74. Maid.body0.SetBoneHitHeightY(-1000f);
  75. return Maid;
  76. }
  77. public void Unload()
  78. {
  79. if (Maid.body0.isLoadedBody)
  80. {
  81. Maid.body0.MuneYureL(1f);
  82. Maid.body0.MuneYureR(1f);
  83. Maid.body0.jbMuneL.enabled = true;
  84. Maid.body0.jbMuneR.enabled = true;
  85. }
  86. Maid.body0.SetMaskMode(TBody.MaskMode.None);
  87. Maid.body0.trsLookTarget = GameMain.Instance.MainCamera.transform;
  88. Maid.Visible = false;
  89. dragPointManager?.Deactivate();
  90. }
  91. public void Deactivate()
  92. {
  93. Unload();
  94. dragPointManager?.Destroy();
  95. Maid.SetPos(Vector3.zero);
  96. Maid.SetRot(Vector3.zero);
  97. Maid.SetPosOffset(Vector3.zero);
  98. Maid.body0?.SetBoneHitHeightY(0f);
  99. Maid.Visible = false;
  100. Maid.ActiveSlotNo = -1;
  101. Maid.DelPrefabAll();
  102. }
  103. public void SetPose(string pose)
  104. {
  105. if (pose.StartsWith(Constants.customPosePath))
  106. {
  107. SetPoseCustom(pose);
  108. return;
  109. }
  110. string[] poseComponents = pose.Split(',');
  111. pose = poseComponents[0] + ".anm";
  112. Maid.CrossFade(pose, false, true, false, 0f);
  113. Maid.SetAutoTwistAll(true);
  114. Maid.GetAnimation().Play();
  115. if (poseComponents.Length > 1)
  116. {
  117. Maid.GetAnimation()[pose].time = float.Parse(poseComponents[1]);
  118. Maid.GetAnimation()[pose].speed = 0f;
  119. }
  120. if (pose.Contains("_momi") || pose.Contains("paizuri_"))
  121. {
  122. Maid.body0.MuneYureL(0f);
  123. Maid.body0.MuneYureR(0f);
  124. }
  125. else
  126. {
  127. Maid.body0.MuneYureL(1f);
  128. Maid.body0.MuneYureR(1f);
  129. }
  130. }
  131. public void SetPoseCustom(string path)
  132. {
  133. byte[] bytes = File.ReadAllBytes(path);
  134. string hash = Path.GetFileName(path).GetHashCode().ToString();
  135. Maid.body0.CrossFade(hash, bytes, false, true, false, 0f);
  136. Maid.SetAutoTwistAll(true);
  137. }
  138. public void SetFaceBlend(string blendValue)
  139. {
  140. Maid.boMabataki = false;
  141. TMorph morph = Maid.body0.Face.morph;
  142. morph.EyeMabataki = 0f;
  143. morph.MulBlendValues(blendValue, 1f);
  144. morph.FixBlendValues_Face();
  145. }
  146. public void SetFaceBlendValue(string hash, float value)
  147. {
  148. TMorph morph = Maid.body0.Face.morph;
  149. if (hash == "nosefook")
  150. {
  151. morph.boNoseFook = value > 0f;
  152. Maid.boNoseFook = morph.boNoseFook;
  153. }
  154. else
  155. {
  156. float[] blendValues = hash.StartsWith("eyeclose")
  157. ? Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValuesBackup")
  158. : Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValues");
  159. try
  160. {
  161. blendValues[(int)morph.hash[hash]] = value;
  162. }
  163. catch { }
  164. }
  165. Maid.boMabataki = false;
  166. morph.EyeMabataki = 0f;
  167. morph.FixBlendValues_Face();
  168. }
  169. private void OnBodyLoad()
  170. {
  171. BodyLoad?.Invoke(this, EventArgs.Empty);
  172. }
  173. private void OnMeidoSelect(MeidoChangeEventArgs args)
  174. {
  175. SelectMeido?.Invoke(this, args);
  176. }
  177. }
  178. }