Meido.cs 11 KB

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