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