Meido.cs 9.8 KB

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