Meido.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. public class Meido
  9. {
  10. private static CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
  11. public readonly int stockNo;
  12. public readonly PoseInfo defaultPose = new PoseInfo(0, 0, "pose_taiki_f");
  13. public Maid Maid { get; private set; }
  14. public Texture2D Image { get; private set; }
  15. public string FirstName { get; private set; }
  16. public string LastName { get; private set; }
  17. public string NameJP => $"{LastName}\n{FirstName}";
  18. public string NameEN => $"{FirstName}\n{LastName}";
  19. public int ActiveSlot { get; private set; }
  20. private DragPointManager dragPointManager;
  21. public event EventHandler<MeidoChangeEventArgs> SelectMeido;
  22. public event EventHandler BodyLoad;
  23. public event EventHandler AnimeChange;
  24. private bool isLoading = false;
  25. private bool isIK = false;
  26. public bool IsIK
  27. {
  28. get => this.isIK;
  29. private set => this.isIK = value;
  30. }
  31. private bool isStop = false;
  32. public bool IsStop
  33. {
  34. get => isStop;
  35. set
  36. {
  37. isStop = value;
  38. this.AnimeChange?.Invoke(this, EventArgs.Empty);
  39. if (!isStop) this.SetPose(this.poseInfo.PoseName);
  40. }
  41. }
  42. public bool IsBone { get; set; } = false;
  43. public bool Visible
  44. {
  45. get => Maid.Visible;
  46. set => Maid.Visible = value;
  47. }
  48. public PoseInfo poseInfo;
  49. public Meido(int stockMaidIndex)
  50. {
  51. this.Maid = characterMgr.GetStockMaid(stockMaidIndex);
  52. this.stockNo = stockMaidIndex;
  53. this.Image = Maid.GetThumIcon();
  54. this.FirstName = Maid.status.firstName;
  55. this.LastName = Maid.status.lastName;
  56. this.poseInfo = defaultPose;
  57. // I don't know why I put this here. Must've fixed something with proc loading
  58. Maid.boAllProcPropBUSY = false;
  59. }
  60. public void Update()
  61. {
  62. if (isLoading)
  63. {
  64. if (!Maid.IsBusy)
  65. {
  66. isLoading = false;
  67. OnBodyLoad();
  68. }
  69. return;
  70. }
  71. dragPointManager.Update();
  72. }
  73. public Maid Load(int activeSlot)
  74. {
  75. isLoading = true;
  76. this.ActiveSlot = activeSlot;
  77. Maid.Visible = true;
  78. if (!Maid.body0.isLoadedBody)
  79. {
  80. Maid.DutPropAll();
  81. Maid.AllProcPropSeqStart();
  82. }
  83. else
  84. {
  85. SetPose(defaultPose);
  86. }
  87. if (dragPointManager == null)
  88. {
  89. dragPointManager = new DragPointManager(this);
  90. dragPointManager.SelectMaid += (sender, meidoChangeArgs) => OnMeidoSelect(meidoChangeArgs);
  91. }
  92. else
  93. {
  94. dragPointManager.Activate();
  95. this.IsIK = true;
  96. this.IsStop = false;
  97. this.IsBone = false;
  98. }
  99. Maid.body0.boHeadToCam = true;
  100. Maid.body0.boEyeToCam = true;
  101. Maid.body0.SetBoneHitHeightY(-1000f);
  102. return Maid;
  103. }
  104. public void Unload()
  105. {
  106. if (Maid.body0.isLoadedBody)
  107. {
  108. Maid.body0.MuneYureL(1f);
  109. Maid.body0.MuneYureR(1f);
  110. Maid.body0.jbMuneL.enabled = true;
  111. Maid.body0.jbMuneR.enabled = true;
  112. }
  113. Maid.body0.SetMaskMode(TBody.MaskMode.None);
  114. Maid.body0.trsLookTarget = GameMain.Instance.MainCamera.transform;
  115. Maid.Visible = false;
  116. dragPointManager?.Deactivate();
  117. this.IsIK = false;
  118. this.IsStop = false;
  119. this.IsBone = false;
  120. }
  121. public void Deactivate()
  122. {
  123. Unload();
  124. dragPointManager?.Destroy();
  125. this.IsIK = false;
  126. this.IsStop = false;
  127. this.IsBone = false;
  128. Maid.SetPos(Vector3.zero);
  129. Maid.SetRot(Vector3.zero);
  130. Maid.SetPosOffset(Vector3.zero);
  131. Maid.body0?.SetBoneHitHeightY(0f);
  132. Maid.Visible = false;
  133. Maid.ActiveSlotNo = -1;
  134. Maid.DelPrefabAll();
  135. }
  136. public void SetPose(PoseInfo poseInfo)
  137. {
  138. this.poseInfo = poseInfo;
  139. SetPose(poseInfo.PoseName);
  140. }
  141. public void SetPose(string pose)
  142. {
  143. if (pose.StartsWith(Constants.customPosePath))
  144. {
  145. SetPoseCustom(pose);
  146. return;
  147. }
  148. string[] poseComponents = pose.Split(',');
  149. pose = poseComponents[0] + ".anm";
  150. Maid.CrossFade(pose, false, true, false, 0f);
  151. Maid.SetAutoTwistAll(true);
  152. Maid.GetAnimation().Play();
  153. if (poseComponents.Length > 1)
  154. {
  155. Maid.GetAnimation()[pose].time = float.Parse(poseComponents[1]);
  156. Maid.GetAnimation()[pose].speed = 0f;
  157. }
  158. if (pose.Contains("_momi") || pose.Contains("paizuri_"))
  159. {
  160. Maid.body0.MuneYureL(0f);
  161. Maid.body0.MuneYureR(0f);
  162. }
  163. else
  164. {
  165. Maid.body0.MuneYureL(1f);
  166. Maid.body0.MuneYureR(1f);
  167. }
  168. }
  169. public void SetPoseCustom(string path)
  170. {
  171. byte[] bytes = File.ReadAllBytes(path);
  172. string hash = Path.GetFileName(path).GetHashCode().ToString();
  173. Maid.body0.CrossFade(hash, bytes, false, true, false, 0f);
  174. Maid.SetAutoTwistAll(true);
  175. }
  176. public void SetFaceBlend(string blendValue)
  177. {
  178. Maid.boMabataki = false;
  179. TMorph morph = Maid.body0.Face.morph;
  180. morph.EyeMabataki = 0f;
  181. morph.MulBlendValues(blendValue, 1f);
  182. morph.FixBlendValues_Face();
  183. }
  184. public void SetFaceBlendValue(string hash, float value)
  185. {
  186. TMorph morph = Maid.body0.Face.morph;
  187. if (hash == "nosefook")
  188. {
  189. morph.boNoseFook = value > 0f;
  190. Maid.boNoseFook = morph.boNoseFook;
  191. }
  192. else
  193. {
  194. float[] blendValues = hash.StartsWith("eyeclose")
  195. ? Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValuesBackup")
  196. : Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValues");
  197. try
  198. {
  199. blendValues[(int)morph.hash[hash]] = value;
  200. }
  201. catch { }
  202. }
  203. Maid.boMabataki = false;
  204. morph.EyeMabataki = 0f;
  205. morph.FixBlendValues_Face();
  206. }
  207. public void SetMaskMode(TBody.MaskMode maskMode)
  208. {
  209. TBody body = Maid.body0;
  210. bool invisibleBody = !body.GetMask(TBody.SlotID.body);
  211. body.SetMaskMode(maskMode);
  212. if (invisibleBody) SetBodyMask(false);
  213. }
  214. public void SetBodyMask(bool enabled)
  215. {
  216. TBody body = Maid.body0;
  217. Hashtable table = Utility.GetFieldValue<TBody, Hashtable>(body, "m_hFoceHide");
  218. foreach (TBody.SlotID bodySlot in MaidDressingPane.bodySlots)
  219. {
  220. table[bodySlot] = enabled;
  221. }
  222. if (body.goSlot[19].m_strModelFileName.Contains("melala_body"))
  223. {
  224. table[TBody.SlotID.accHana] = enabled;
  225. }
  226. body.FixMaskFlag();
  227. body.FixVisibleFlag(false);
  228. }
  229. public void SetIKActive(bool active)
  230. {
  231. this.IsIK = active;
  232. if (dragPointManager == null) this.IsIK = false;
  233. else
  234. {
  235. if (this.IsIK) dragPointManager.Activate();
  236. else dragPointManager.Deactivate();
  237. }
  238. }
  239. public void IKRelease()
  240. {
  241. if (!Maid.GetAnimation().isPlaying)
  242. {
  243. this.IsStop = false;
  244. this.SetPose(this.poseInfo.PoseName);
  245. }
  246. }
  247. private void OnBodyLoad()
  248. {
  249. BodyLoad?.Invoke(this, EventArgs.Empty);
  250. this.IsIK = true;
  251. this.IsStop = false;
  252. this.IsBone = false;
  253. }
  254. private void OnMeidoSelect(MeidoChangeEventArgs args)
  255. {
  256. SelectMeido?.Invoke(this, args);
  257. }
  258. }
  259. }
  260. public struct PoseInfo
  261. {
  262. public int PoseGroupIndex { get; private set; }
  263. public int PoseIndex { get; private set; }
  264. public string PoseName { get; private set; }
  265. public bool IsCustomPose { get; private set; }
  266. public PoseInfo(int poseGroup, int pose, string poseName, bool isCustomPose = false)
  267. {
  268. this.PoseGroupIndex = poseGroup;
  269. this.PoseIndex = pose;
  270. this.PoseName = poseName;
  271. this.IsCustomPose = isCustomPose;
  272. }
  273. }