Meido.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. Maid.SetPos(Vector3.zero);
  128. Maid.SetRot(Vector3.zero);
  129. Maid.SetPosOffset(Vector3.zero);
  130. Maid.body0.SetBoneHitHeightY(0f);
  131. Maid.Visible = false;
  132. Maid.ActiveSlotNo = -1;
  133. Maid.DelPrefabAll();
  134. }
  135. public void SetPose(PoseInfo poseInfo)
  136. {
  137. this.poseInfo = poseInfo;
  138. SetPose(poseInfo.PoseName);
  139. }
  140. public void SetPose(string pose)
  141. {
  142. if (!Maid.body0.isLoadedBody) return;
  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. if (!Maid.body0.isLoadedBody) return;
  172. byte[] bytes = File.ReadAllBytes(path);
  173. string hash = Path.GetFileName(path).GetHashCode().ToString();
  174. Maid.body0.CrossFade(hash, bytes, false, true, false, 0f);
  175. Maid.SetAutoTwistAll(true);
  176. Maid.body0.MuneYureL(1f);
  177. Maid.body0.MuneYureR(1f);
  178. }
  179. public void SetFaceBlend(string blendValue)
  180. {
  181. Maid.boMabataki = false;
  182. TMorph morph = Maid.body0.Face.morph;
  183. morph.EyeMabataki = 0f;
  184. morph.MulBlendValues(blendValue, 1f);
  185. morph.FixBlendValues_Face();
  186. }
  187. public void SetFaceBlendValue(string hash, float value)
  188. {
  189. TMorph morph = Maid.body0.Face.morph;
  190. if (hash == "nosefook")
  191. {
  192. morph.boNoseFook = value > 0f;
  193. Maid.boNoseFook = morph.boNoseFook;
  194. }
  195. else
  196. {
  197. float[] blendValues = hash.StartsWith("eyeclose")
  198. ? Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValuesBackup")
  199. : Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValues");
  200. try
  201. {
  202. blendValues[(int)morph.hash[hash]] = value;
  203. }
  204. catch { }
  205. }
  206. Maid.boMabataki = false;
  207. morph.EyeMabataki = 0f;
  208. morph.FixBlendValues_Face();
  209. }
  210. public void SetMaskMode(TBody.MaskMode maskMode)
  211. {
  212. TBody body = Maid.body0;
  213. bool invisibleBody = !body.GetMask(TBody.SlotID.body);
  214. body.SetMaskMode(maskMode);
  215. if (invisibleBody) SetBodyMask(false);
  216. }
  217. public void SetBodyMask(bool enabled)
  218. {
  219. TBody body = Maid.body0;
  220. Hashtable table = Utility.GetFieldValue<TBody, Hashtable>(body, "m_hFoceHide");
  221. foreach (TBody.SlotID bodySlot in MaidDressingPane.bodySlots)
  222. {
  223. table[bodySlot] = enabled;
  224. }
  225. if (body.goSlot[19].m_strModelFileName.Contains("melala_body"))
  226. {
  227. table[TBody.SlotID.accHana] = enabled;
  228. }
  229. body.FixMaskFlag();
  230. body.FixVisibleFlag(false);
  231. }
  232. public void SetIKActive(bool active)
  233. {
  234. this.IsIK = active;
  235. if (dragPointManager == null) this.IsIK = false;
  236. else
  237. {
  238. if (this.IsIK) dragPointManager.Activate();
  239. else dragPointManager.Deactivate();
  240. }
  241. }
  242. public void IKRelease()
  243. {
  244. if (!Maid.GetAnimation().isPlaying)
  245. {
  246. this.IsStop = false;
  247. this.SetPose(this.poseInfo.PoseName);
  248. }
  249. }
  250. private void OnBodyLoad()
  251. {
  252. BodyLoad?.Invoke(this, EventArgs.Empty);
  253. this.IsIK = true;
  254. this.IsStop = false;
  255. this.IsBone = false;
  256. }
  257. private void OnMeidoSelect(MeidoChangeEventArgs args)
  258. {
  259. SelectMeido?.Invoke(this, args);
  260. }
  261. }
  262. }
  263. public struct PoseInfo
  264. {
  265. public int PoseGroupIndex { get; private set; }
  266. public int PoseIndex { get; private set; }
  267. public string PoseName { get; private set; }
  268. public bool IsCustomPose { get; private set; }
  269. public PoseInfo(int poseGroup, int pose, string poseName, bool isCustomPose = false)
  270. {
  271. this.PoseGroupIndex = poseGroup;
  272. this.PoseIndex = pose;
  273. this.PoseName = poseName;
  274. this.IsCustomPose = isCustomPose;
  275. }
  276. }