Meido.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 slot, int activeSlot)
  75. {
  76. isLoading = true;
  77. this.ActiveSlot = slot;
  78. Maid.Visible = true;
  79. if (!Maid.body0.isLoadedBody)
  80. {
  81. if (activeSlot >= 12)
  82. {
  83. Maid.DutPropAll();
  84. Maid.AllProcPropSeqStart();
  85. }
  86. else
  87. {
  88. GameMain.Instance.CharacterMgr.Activate(activeSlot, activeSlot, false, false);
  89. GameMain.Instance.CharacterMgr.CharaVisible(activeSlot, true, false);
  90. }
  91. }
  92. else
  93. {
  94. SetPose(defaultPose);
  95. }
  96. if (dragPointManager == null)
  97. {
  98. dragPointManager = new DragPointManager(this);
  99. dragPointManager.SelectMaid += (sender, meidoChangeArgs) => OnMeidoSelect(meidoChangeArgs);
  100. }
  101. else
  102. {
  103. dragPointManager.Activate();
  104. this.IsIK = true;
  105. this.IsStop = false;
  106. this.IsBone = false;
  107. }
  108. this.IsFreeLook = false;
  109. Maid.body0.boHeadToCam = true;
  110. Maid.body0.boEyeToCam = true;
  111. Maid.body0.SetBoneHitHeightY(-1000f);
  112. return Maid;
  113. }
  114. public void Unload()
  115. {
  116. if (Maid.body0.isLoadedBody)
  117. {
  118. Maid.body0.MuneYureL(1f);
  119. Maid.body0.MuneYureR(1f);
  120. Maid.body0.jbMuneL.enabled = true;
  121. Maid.body0.jbMuneR.enabled = true;
  122. }
  123. Maid.body0.SetMaskMode(TBody.MaskMode.None);
  124. Maid.body0.trsLookTarget = GameMain.Instance.MainCamera.transform;
  125. Maid.Visible = false;
  126. dragPointManager?.Deactivate();
  127. this.IsIK = false;
  128. this.IsStop = false;
  129. this.IsBone = false;
  130. }
  131. public void Deactivate()
  132. {
  133. Unload();
  134. dragPointManager?.Destroy();
  135. Maid.SetPos(Vector3.zero);
  136. Maid.SetRot(Vector3.zero);
  137. Maid.SetPosOffset(Vector3.zero);
  138. Maid.body0.SetBoneHitHeightY(0f);
  139. Maid.Visible = false;
  140. Maid.ActiveSlotNo = -1;
  141. Maid.DelPrefabAll();
  142. }
  143. public void SetPose(PoseInfo poseInfo)
  144. {
  145. this.poseInfo = poseInfo;
  146. SetPose(poseInfo.PoseName);
  147. }
  148. public void SetPose(string pose)
  149. {
  150. if (!Maid.body0.isLoadedBody) return;
  151. if (pose.StartsWith(Constants.customPosePath))
  152. {
  153. SetPoseCustom(pose);
  154. return;
  155. }
  156. string[] poseComponents = pose.Split(',');
  157. pose = poseComponents[0] + ".anm";
  158. Maid.CrossFade(pose, false, true, false, 0f);
  159. Maid.SetAutoTwistAll(true);
  160. Maid.GetAnimation().Play();
  161. if (poseComponents.Length > 1)
  162. {
  163. Maid.GetAnimation()[pose].time = float.Parse(poseComponents[1]);
  164. Maid.GetAnimation()[pose].speed = 0f;
  165. }
  166. if (pose.Contains("_momi") || pose.Contains("paizuri_"))
  167. {
  168. Maid.body0.MuneYureL(0f);
  169. Maid.body0.MuneYureR(0f);
  170. }
  171. else
  172. {
  173. Maid.body0.MuneYureL(1f);
  174. Maid.body0.MuneYureR(1f);
  175. }
  176. }
  177. public void SetPoseCustom(string path)
  178. {
  179. if (!Maid.body0.isLoadedBody) return;
  180. byte[] bytes = File.ReadAllBytes(path);
  181. string hash = Path.GetFileName(path).GetHashCode().ToString();
  182. Maid.body0.CrossFade(hash, bytes, false, true, false, 0f);
  183. Maid.SetAutoTwistAll(true);
  184. Maid.body0.MuneYureL(1f);
  185. Maid.body0.MuneYureR(1f);
  186. }
  187. public void SetFaceBlend(string blendValue)
  188. {
  189. Maid.boMabataki = false;
  190. TMorph morph = Maid.body0.Face.morph;
  191. morph.EyeMabataki = 0f;
  192. morph.MulBlendValues(blendValue, 1f);
  193. morph.FixBlendValues_Face();
  194. }
  195. public void SetFaceBlendValue(string hash, float value)
  196. {
  197. TMorph morph = Maid.body0.Face.morph;
  198. if (hash == "nosefook")
  199. {
  200. morph.boNoseFook = value > 0f;
  201. Maid.boNoseFook = morph.boNoseFook;
  202. }
  203. else
  204. {
  205. float[] blendValues = hash.StartsWith("eyeclose")
  206. ? Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValuesBackup")
  207. : Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValues");
  208. try
  209. {
  210. blendValues[(int)morph.hash[hash]] = value;
  211. }
  212. catch { }
  213. }
  214. Maid.boMabataki = false;
  215. morph.EyeMabataki = 0f;
  216. morph.FixBlendValues_Face();
  217. }
  218. public void SetMaskMode(TBody.MaskMode maskMode)
  219. {
  220. TBody body = Maid.body0;
  221. bool invisibleBody = !body.GetMask(TBody.SlotID.body);
  222. body.SetMaskMode(maskMode);
  223. if (invisibleBody) SetBodyMask(false);
  224. }
  225. public void SetBodyMask(bool enabled)
  226. {
  227. TBody body = Maid.body0;
  228. Hashtable table = Utility.GetFieldValue<TBody, Hashtable>(body, "m_hFoceHide");
  229. foreach (TBody.SlotID bodySlot in MaidDressingPane.bodySlots)
  230. {
  231. table[bodySlot] = enabled;
  232. }
  233. if (body.goSlot[19].m_strModelFileName.Contains("melala_body"))
  234. {
  235. table[TBody.SlotID.accHana] = enabled;
  236. }
  237. body.FixMaskFlag();
  238. body.FixVisibleFlag(false);
  239. }
  240. public void SetIKActive(bool active)
  241. {
  242. this.IsIK = active;
  243. if (dragPointManager == null) this.IsIK = false;
  244. else
  245. {
  246. if (this.IsIK) dragPointManager.Activate();
  247. else dragPointManager.Deactivate();
  248. }
  249. }
  250. public void IKRelease()
  251. {
  252. if (!Maid.GetAnimation().isPlaying)
  253. {
  254. this.IsStop = false;
  255. this.SetPose(this.poseInfo.PoseName);
  256. }
  257. }
  258. private void OnBodyLoad()
  259. {
  260. BodyLoad?.Invoke(this, EventArgs.Empty);
  261. this.IsIK = true;
  262. this.IsStop = false;
  263. this.IsBone = false;
  264. }
  265. private void OnMeidoSelect(MeidoChangeEventArgs args)
  266. {
  267. SelectMeido?.Invoke(this, args);
  268. }
  269. }
  270. }
  271. public struct PoseInfo
  272. {
  273. public int PoseGroupIndex { get; private set; }
  274. public int PoseIndex { get; private set; }
  275. public string PoseName { get; private set; }
  276. public bool IsCustomPose { get; private set; }
  277. public PoseInfo(int poseGroup, int pose, string poseName, bool isCustomPose = false)
  278. {
  279. this.PoseGroupIndex = poseGroup;
  280. this.PoseIndex = pose;
  281. this.PoseName = poseName;
  282. this.IsCustomPose = isCustomPose;
  283. }
  284. }