Meido.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 activeSlot, int maidSlot)
  111. {
  112. isLoading = true;
  113. this.ActiveSlot = activeSlot;
  114. Maid.Visible = true;
  115. if (!Maid.body0.isLoadedBody)
  116. {
  117. if (maidSlot >= MAX_MAIDS)
  118. {
  119. Maid.DutPropAll();
  120. Maid.AllProcPropSeqStart();
  121. }
  122. else
  123. {
  124. GameMain.Instance.CharacterMgr.Activate(maidSlot, maidSlot, false, false);
  125. GameMain.Instance.CharacterMgr.CharaVisible(maidSlot, 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 CopyPose(Meido fromMeido)
  195. {
  196. byte[] poseBinary = fromMeido.SerializePose();
  197. string tag = $"copy_{fromMeido.Maid.status.guid}";
  198. Maid.body0.CrossFade(tag, poseBinary, false, true, false, 0f);
  199. Maid.SetAutoTwistAll(true);
  200. Maid.transform.rotation = fromMeido.Maid.transform.rotation;
  201. SetMune();
  202. }
  203. public void SetPoseCustom(string path)
  204. {
  205. if (!Maid.body0.isLoadedBody) return;
  206. byte[] bytes = File.ReadAllBytes(path);
  207. string hash = Path.GetFileName(path).GetHashCode().ToString();
  208. Maid.body0.CrossFade(hash, bytes, false, true, false, 0f);
  209. Maid.SetAutoTwistAll(true);
  210. SetMune();
  211. }
  212. public void SetMune(bool drag = false)
  213. {
  214. bool isMomiOrPaizuri = CachedPose.Pose.Contains("_momi") || CachedPose.Pose.Contains("paizuri_");
  215. float onL = (drag || isMomiOrPaizuri) ? 0f : 1f;
  216. Maid.body0.MuneYureL(onL);
  217. Maid.body0.MuneYureR(onL);
  218. Maid.body0.jbMuneL.enabled = !drag;
  219. Maid.body0.jbMuneR.enabled = !drag;
  220. }
  221. public void SetFaceBlendSet(string blendSet)
  222. {
  223. FaceBlendSet = blendSet;
  224. Maid.boMabataki = false;
  225. TMorph morph = Maid.body0.Face.morph;
  226. morph.EyeMabataki = 0f;
  227. morph.MulBlendValues(blendSet, 1f);
  228. morph.FixBlendValues_Face();
  229. OnUpdateMeido();
  230. }
  231. public void SetFaceBlendValue(string hash, float value)
  232. {
  233. TMorph morph = Maid.body0.Face.morph;
  234. if (hash == "nosefook")
  235. {
  236. morph.boNoseFook = value > 0f;
  237. Maid.boNoseFook = morph.boNoseFook;
  238. }
  239. else
  240. {
  241. float[] blendValues = hash.StartsWith("eyeclose")
  242. ? Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValuesBackup")
  243. : Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValues");
  244. try
  245. {
  246. blendValues[(int)morph.hash[hash]] = value;
  247. }
  248. catch { }
  249. }
  250. Maid.boMabataki = false;
  251. morph.EyeMabataki = 0f;
  252. morph.FixBlendValues_Face();
  253. }
  254. public void SetMaskMode(TBody.MaskMode maskMode)
  255. {
  256. TBody body = Maid.body0;
  257. bool invisibleBody = !body.GetMask(TBody.SlotID.body);
  258. body.SetMaskMode(maskMode);
  259. if (invisibleBody) SetBodyMask(false);
  260. }
  261. public void SetBodyMask(bool enabled)
  262. {
  263. TBody body = Maid.body0;
  264. Hashtable table = Utility.GetFieldValue<TBody, Hashtable>(body, "m_hFoceHide");
  265. foreach (TBody.SlotID bodySlot in MaidDressingPane.bodySlots)
  266. {
  267. table[bodySlot] = enabled;
  268. }
  269. if (body.goSlot[19].m_strModelFileName.Contains("melala_body"))
  270. {
  271. table[TBody.SlotID.accHana] = enabled;
  272. }
  273. body.FixMaskFlag();
  274. body.FixVisibleFlag(false);
  275. }
  276. public Transform GetBoneTransform(AttachPoint point) => this.dragPointManager?.GetAttachPointTransform(point);
  277. private void OnBodyLoad()
  278. {
  279. BodyLoad?.Invoke(this, EventArgs.Empty);
  280. this.IsIK = true;
  281. this.IsStop = false;
  282. this.IsBone = false;
  283. }
  284. private void OnUpdateMeido(MeidoUpdateEventArgs args = null)
  285. {
  286. this.UpdateMeido?.Invoke(this, args ?? MeidoUpdateEventArgs.Empty);
  287. }
  288. private void OnMeidoSelect(object sender, MeidoUpdateEventArgs args)
  289. {
  290. UpdateMeido?.Invoke(this, args);
  291. }
  292. }
  293. public struct PoseInfo
  294. {
  295. public string PoseGroup { get; }
  296. public string Pose { get; }
  297. public bool CustomPose { get; }
  298. public PoseInfo(string poseGroup, string pose, bool customPose = false)
  299. {
  300. this.PoseGroup = poseGroup;
  301. this.Pose = pose;
  302. this.CustomPose = customPose;
  303. }
  304. }
  305. }