Meido.cs 9.5 KB

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