Meido.cs 9.2 KB

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