Meido.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using UnityEngine;
  5. namespace COM3D2.MeidoPhotoStudio.Plugin
  6. {
  7. internal class Meido
  8. {
  9. private const int MAX_MAIDS = 12;
  10. private static CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
  11. public readonly int stockNo;
  12. public static readonly PoseInfo defaultPose
  13. = new PoseInfo(Constants.PoseGroupList[0], Constants.PoseDict[Constants.PoseGroupList[0]][0]);
  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.Pose);
  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 byte[] SerializePose()
  101. {
  102. CacheBoneDataArray cache = this.Maid.gameObject.GetComponent<CacheBoneDataArray>();
  103. if (cache == null)
  104. {
  105. cache = this.Maid.gameObject.AddComponent<CacheBoneDataArray>();
  106. cache.CreateCache(this.Maid.body0.GetBone("Bip01"));
  107. }
  108. return cache.GetAnmBinary(true, true);
  109. }
  110. public Maid Load(int slot, int activeSlot)
  111. {
  112. isLoading = true;
  113. this.ActiveSlot = slot;
  114. Maid.Visible = true;
  115. if (!Maid.body0.isLoadedBody)
  116. {
  117. if (activeSlot >= MAX_MAIDS)
  118. {
  119. Maid.DutPropAll();
  120. Maid.AllProcPropSeqStart();
  121. }
  122. else
  123. {
  124. GameMain.Instance.CharacterMgr.Activate(activeSlot, activeSlot, false, false);
  125. GameMain.Instance.CharacterMgr.CharaVisible(activeSlot, true, false);
  126. }
  127. }
  128. else
  129. {
  130. SetPose(defaultPose);
  131. }
  132. dragPointManager = new MeidoDragPointManager(this);
  133. dragPointManager.SelectMaid += OnMeidoSelect;
  134. this.IsFreeLook = false;
  135. Maid.body0.boHeadToCam = true;
  136. Maid.body0.boEyeToCam = true;
  137. Maid.body0.SetBoneHitHeightY(-1000f);
  138. return Maid;
  139. }
  140. public void Unload()
  141. {
  142. if (Maid.body0.isLoadedBody)
  143. {
  144. Maid.body0.MuneYureL(1f);
  145. Maid.body0.MuneYureR(1f);
  146. Maid.body0.jbMuneL.enabled = true;
  147. Maid.body0.jbMuneR.enabled = true;
  148. }
  149. Maid.body0.SetMaskMode(TBody.MaskMode.None);
  150. Maid.body0.trsLookTarget = GameMain.Instance.MainCamera.transform;
  151. Maid.Visible = false;
  152. if (dragPointManager != null)
  153. {
  154. dragPointManager.Destroy();
  155. dragPointManager.SelectMaid -= OnMeidoSelect;
  156. dragPointManager = null;
  157. }
  158. }
  159. public void Deactivate()
  160. {
  161. Unload();
  162. Maid.SetPos(Vector3.zero);
  163. Maid.SetRot(Vector3.zero);
  164. Maid.SetPosOffset(Vector3.zero);
  165. Maid.body0.SetBoneHitHeightY(0f);
  166. Maid.DelPrefabAll();
  167. Maid.ActiveSlotNo = -1;
  168. }
  169. public void SetPose(PoseInfo poseInfo)
  170. {
  171. this.CachedPose = poseInfo;
  172. SetPose(poseInfo.Pose);
  173. }
  174. public void SetPose(string pose)
  175. {
  176. if (!Maid.body0.isLoadedBody) return;
  177. if (pose.StartsWith(Constants.customPosePath))
  178. {
  179. SetPoseCustom(pose);
  180. return;
  181. }
  182. string[] poseComponents = pose.Split(',');
  183. pose = poseComponents[0] + ".anm";
  184. Maid.CrossFade(pose, false, true, false, 0f);
  185. Maid.SetAutoTwistAll(true);
  186. Maid.GetAnimation().Play();
  187. if (poseComponents.Length > 1)
  188. {
  189. Maid.GetAnimation()[pose].time = float.Parse(poseComponents[1]);
  190. Maid.GetAnimation()[pose].speed = 0f;
  191. }
  192. SetMune();
  193. }
  194. public void SetPoseCustom(string path)
  195. {
  196. if (!Maid.body0.isLoadedBody) return;
  197. byte[] bytes = File.ReadAllBytes(path);
  198. string hash = Path.GetFileName(path).GetHashCode().ToString();
  199. Maid.body0.CrossFade(hash, bytes, false, true, false, 0f);
  200. Maid.SetAutoTwistAll(true);
  201. SetMune();
  202. }
  203. public void SetMune(bool drag = false)
  204. {
  205. bool isMomiOrPaizuri = CachedPose.Pose.Contains("_momi") || CachedPose.Pose.Contains("paizuri_");
  206. float onL = (drag || isMomiOrPaizuri) ? 0f : 1f;
  207. Maid.body0.MuneYureL(onL);
  208. Maid.body0.MuneYureR(onL);
  209. Maid.body0.jbMuneL.enabled = !drag;
  210. Maid.body0.jbMuneR.enabled = !drag;
  211. }
  212. public void SetFaceBlendSet(string blendSet)
  213. {
  214. FaceBlendSet = blendSet;
  215. Maid.boMabataki = false;
  216. TMorph morph = Maid.body0.Face.morph;
  217. morph.EyeMabataki = 0f;
  218. morph.MulBlendValues(blendSet, 1f);
  219. morph.FixBlendValues_Face();
  220. OnUpdateMeido();
  221. }
  222. public void SetFaceBlendValue(string hash, float value)
  223. {
  224. TMorph morph = Maid.body0.Face.morph;
  225. if (hash == "nosefook")
  226. {
  227. morph.boNoseFook = value > 0f;
  228. Maid.boNoseFook = morph.boNoseFook;
  229. }
  230. else
  231. {
  232. float[] blendValues = hash.StartsWith("eyeclose")
  233. ? Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValuesBackup")
  234. : Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValues");
  235. try
  236. {
  237. blendValues[(int)morph.hash[hash]] = value;
  238. }
  239. catch { }
  240. }
  241. Maid.boMabataki = false;
  242. morph.EyeMabataki = 0f;
  243. morph.FixBlendValues_Face();
  244. }
  245. public void SetMaskMode(TBody.MaskMode maskMode)
  246. {
  247. TBody body = Maid.body0;
  248. bool invisibleBody = !body.GetMask(TBody.SlotID.body);
  249. body.SetMaskMode(maskMode);
  250. if (invisibleBody) SetBodyMask(false);
  251. }
  252. public void SetBodyMask(bool enabled)
  253. {
  254. TBody body = Maid.body0;
  255. Hashtable table = Utility.GetFieldValue<TBody, Hashtable>(body, "m_hFoceHide");
  256. foreach (TBody.SlotID bodySlot in MaidDressingPane.bodySlots)
  257. {
  258. table[bodySlot] = enabled;
  259. }
  260. if (body.goSlot[19].m_strModelFileName.Contains("melala_body"))
  261. {
  262. table[TBody.SlotID.accHana] = enabled;
  263. }
  264. body.FixMaskFlag();
  265. body.FixVisibleFlag(false);
  266. }
  267. public Transform GetBoneTransform(AttachPoint point) => this.dragPointManager?.GetAttachPointTransform(point);
  268. private void OnBodyLoad()
  269. {
  270. BodyLoad?.Invoke(this, EventArgs.Empty);
  271. this.IsIK = true;
  272. this.IsStop = false;
  273. this.IsBone = false;
  274. }
  275. private void OnUpdateMeido(MeidoUpdateEventArgs args = null)
  276. {
  277. this.UpdateMeido?.Invoke(this, args ?? MeidoUpdateEventArgs.Empty);
  278. }
  279. private void OnMeidoSelect(object sender, MeidoUpdateEventArgs args)
  280. {
  281. UpdateMeido?.Invoke(this, args);
  282. }
  283. }
  284. public struct PoseInfo
  285. {
  286. public string PoseGroup { get; }
  287. public string Pose { get; }
  288. public bool CustomPose { get; }
  289. public PoseInfo(string poseGroup, string pose, bool customPose = false)
  290. {
  291. this.PoseGroup = poseGroup;
  292. this.Pose = pose;
  293. this.CustomPose = customPose;
  294. }
  295. }
  296. }