Meido.cs 10 KB

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