Meido.cs 11 KB

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