Meido.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. if (dragPointManager != null) dragPointManager.Deactivate();
  90. }
  91. public void Deactivate()
  92. {
  93. Unload();
  94. if (dragPointManager != null) dragPointManager.Destroy();
  95. Maid.SetPos(Vector3.zero);
  96. Maid.SetRot(Vector3.zero);
  97. Maid.SetPosOffset(Vector3.zero);
  98. if (Maid.body0 != null)
  99. {
  100. Maid.body0.SetBoneHitHeightY(0f);
  101. }
  102. Maid.Visible = false;
  103. Maid.ActiveSlotNo = -1;
  104. Maid.DelPrefabAll();
  105. }
  106. public void SetPose(string pose)
  107. {
  108. if (pose.StartsWith(Constants.customPosePath))
  109. {
  110. SetPoseCustom(pose);
  111. return;
  112. }
  113. string[] poseComponents = pose.Split(',');
  114. pose = poseComponents[0] + ".anm";
  115. Maid.CrossFade(pose, false, true, false, 0f);
  116. Maid.SetAutoTwistAll(true);
  117. Maid.GetAnimation().Play();
  118. if (poseComponents.Length > 1)
  119. {
  120. Maid.GetAnimation()[pose].time = float.Parse(poseComponents[1]);
  121. Maid.GetAnimation()[pose].speed = 0f;
  122. }
  123. if (pose.Contains("_momi") || pose.Contains("paizuri_"))
  124. {
  125. Maid.body0.MuneYureL(0f);
  126. Maid.body0.MuneYureR(0f);
  127. }
  128. else
  129. {
  130. Maid.body0.MuneYureL(1f);
  131. Maid.body0.MuneYureR(1f);
  132. }
  133. }
  134. public void SetPoseCustom(string path)
  135. {
  136. byte[] bytes = File.ReadAllBytes(path);
  137. string hash = Path.GetFileName(path).GetHashCode().ToString();
  138. Maid.body0.CrossFade(hash, bytes, false, true, false, 0f);
  139. Maid.SetAutoTwistAll(true);
  140. }
  141. public void SetFaceBlend(string blendValue)
  142. {
  143. Maid.boMabataki = false;
  144. TMorph morph = Maid.body0.Face.morph;
  145. morph.EyeMabataki = 0f;
  146. morph.MulBlendValues(blendValue, 1f);
  147. morph.FixBlendValues_Face();
  148. }
  149. public void SetFaceBlendValue(string hash, float value)
  150. {
  151. TMorph morph = Maid.body0.Face.morph;
  152. if (hash == "nosefook")
  153. {
  154. morph.boNoseFook = value > 0f;
  155. Maid.boNoseFook = morph.boNoseFook;
  156. }
  157. else
  158. {
  159. float[] blendValues = hash.StartsWith("eyeclose")
  160. ? Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValuesBackup")
  161. : Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValues");
  162. try
  163. {
  164. blendValues[(int)morph.hash[hash]] = value;
  165. }
  166. catch { }
  167. }
  168. Maid.boMabataki = false;
  169. morph.EyeMabataki = 0f;
  170. morph.FixBlendValues_Face();
  171. }
  172. private void OnBodyLoad()
  173. {
  174. EventHandler handler = BodyLoad;
  175. if (handler != null) handler(this, EventArgs.Empty);
  176. }
  177. private void OnMeidoSelect(MeidoChangeEventArgs args)
  178. {
  179. EventHandler<MeidoChangeEventArgs> handler = SelectMeido;
  180. if (handler != null) handler(this, args);
  181. }
  182. }
  183. }