Meido.cs 9.9 KB

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