Meido.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using System;
  2. using System.Collections;
  3. using System.IO;
  4. using System.Xml.Linq;
  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
  14. = new PoseInfo(Constants.PoseGroupList[0], Constants.PoseDict[Constants.PoseGroupList[0]][0]);
  15. public static readonly string defaultFaceBlendSet = "通常";
  16. public Maid Maid { get; private set; }
  17. public Texture2D Image { get; private set; }
  18. public string FirstName { get; private set; }
  19. public string LastName { get; private set; }
  20. public string NameJP => $"{LastName}\n{FirstName}";
  21. public string NameEN => $"{FirstName}\n{LastName}";
  22. public int ActiveSlot { get; private set; }
  23. private MeidoDragPointManager dragPointManager;
  24. public event EventHandler<MeidoUpdateEventArgs> UpdateMeido;
  25. public event EventHandler BodyLoad;
  26. private bool isLoading = false;
  27. public bool IsIK
  28. {
  29. get => dragPointManager?.Active ?? false;
  30. set
  31. {
  32. if (dragPointManager == null || value == dragPointManager.Active) return;
  33. else dragPointManager.Active = value;
  34. }
  35. }
  36. public bool IsStop
  37. {
  38. get
  39. {
  40. if (!Maid.body0.isLoadedBody) return true;
  41. else return !Maid.GetAnimation().isPlaying;
  42. }
  43. set
  44. {
  45. if (!Maid.body0.isLoadedBody || value == !Maid.GetAnimation().isPlaying) return;
  46. else
  47. {
  48. if (value) Maid.GetAnimation().Stop();
  49. else this.SetPose(this.CachedPose.Pose);
  50. OnUpdateMeido();
  51. }
  52. }
  53. }
  54. // private bool isBone = false;
  55. public bool IsBone
  56. {
  57. get => dragPointManager?.IsBone ?? false;
  58. set
  59. {
  60. if (dragPointManager == null || value == dragPointManager.IsBone) return;
  61. else dragPointManager.IsBone = value;
  62. OnUpdateMeido();
  63. }
  64. }
  65. private bool isFreeLook;
  66. public bool IsFreeLook
  67. {
  68. get => isFreeLook;
  69. set
  70. {
  71. if (this.isFreeLook == value) return;
  72. this.isFreeLook = value;
  73. Maid.body0.trsLookTarget = this.isFreeLook ? null : GameMain.Instance.MainCamera.transform;
  74. OnUpdateMeido();
  75. }
  76. }
  77. public PoseInfo CachedPose { get; private set; }
  78. public string FaceBlendSet { get; private set; } = defaultFaceBlendSet;
  79. public Meido(int stockMaidIndex)
  80. {
  81. this.Maid = characterMgr.GetStockMaid(stockMaidIndex);
  82. this.stockNo = stockMaidIndex;
  83. this.Image = Maid.GetThumIcon();
  84. this.FirstName = Maid.status.firstName;
  85. this.LastName = Maid.status.lastName;
  86. this.CachedPose = defaultPose;
  87. // I don't know why I put this here. Must've fixed something with proc loading
  88. Maid.boAllProcPropBUSY = false;
  89. }
  90. public void Update()
  91. {
  92. if (isLoading)
  93. {
  94. if (!Maid.IsBusy)
  95. {
  96. isLoading = false;
  97. OnBodyLoad();
  98. }
  99. }
  100. }
  101. public byte[] SerializePose()
  102. {
  103. CacheBoneDataArray cache = this.Maid.gameObject.GetComponent<CacheBoneDataArray>();
  104. if (cache == null)
  105. {
  106. cache = this.Maid.gameObject.AddComponent<CacheBoneDataArray>();
  107. cache.CreateCache(this.Maid.body0.GetBone("Bip01"));
  108. }
  109. return cache.GetAnmBinary(true, true);
  110. }
  111. public void SetHandPreset(string filename, bool right)
  112. {
  113. if (this.dragPointManager == null) return;
  114. XDocument handDocument = XDocument.Load(filename);
  115. XElement handElement = handDocument.Element("FingerData");
  116. if (handElement.IsEmpty || handElement.Element("GameVersion").IsEmpty
  117. || handElement.Element("RightData").IsEmpty || handElement.Element("BinaryData").IsEmpty)
  118. {
  119. return;
  120. }
  121. IsStop = true;
  122. bool rightData = bool.Parse(handElement.Element("RightData").Value);
  123. string base64Data = handElement.Element("BinaryData").Value;
  124. byte[] handData = Convert.FromBase64String(base64Data);
  125. this.dragPointManager.DeserializeHand(handData, right, rightData != right);
  126. }
  127. public byte[] SerializeHand(bool right) => this.dragPointManager?.SerializeHand(right);
  128. public Maid Load(int activeSlot, int maidSlot)
  129. {
  130. isLoading = true;
  131. this.ActiveSlot = activeSlot;
  132. Maid.Visible = true;
  133. if (!Maid.body0.isLoadedBody)
  134. {
  135. if (maidSlot >= MAX_MAIDS)
  136. {
  137. Maid.DutPropAll();
  138. Maid.AllProcPropSeqStart();
  139. }
  140. else
  141. {
  142. GameMain.Instance.CharacterMgr.Activate(maidSlot, maidSlot, false, false);
  143. GameMain.Instance.CharacterMgr.CharaVisible(maidSlot, true, false);
  144. }
  145. }
  146. else
  147. {
  148. SetPose(defaultPose);
  149. }
  150. dragPointManager = new MeidoDragPointManager(this);
  151. dragPointManager.SelectMaid += OnMeidoSelect;
  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)
  171. {
  172. dragPointManager.Destroy();
  173. dragPointManager.SelectMaid -= OnMeidoSelect;
  174. dragPointManager = null;
  175. }
  176. }
  177. public void Deactivate()
  178. {
  179. Unload();
  180. Maid.SetPos(Vector3.zero);
  181. Maid.SetRot(Vector3.zero);
  182. Maid.SetPosOffset(Vector3.zero);
  183. Maid.body0.SetBoneHitHeightY(0f);
  184. Maid.DelPrefabAll();
  185. Maid.ActiveSlotNo = -1;
  186. }
  187. public void SetPose(PoseInfo poseInfo)
  188. {
  189. this.CachedPose = poseInfo;
  190. SetPose(poseInfo.Pose);
  191. }
  192. public void SetPose(string pose)
  193. {
  194. if (!Maid.body0.isLoadedBody) return;
  195. if (pose.StartsWith(Constants.customPosePath))
  196. {
  197. SetPoseCustom(pose);
  198. return;
  199. }
  200. string[] poseComponents = pose.Split(',');
  201. pose = poseComponents[0] + ".anm";
  202. Maid.CrossFade(pose, false, true, false, 0f);
  203. Maid.SetAutoTwistAll(true);
  204. Maid.GetAnimation().Play();
  205. if (poseComponents.Length > 1)
  206. {
  207. Maid.GetAnimation()[pose].time = float.Parse(poseComponents[1]);
  208. Maid.GetAnimation()[pose].speed = 0f;
  209. }
  210. SetMune();
  211. }
  212. public void CopyPose(Meido fromMeido)
  213. {
  214. byte[] poseBinary = fromMeido.SerializePose();
  215. string tag = $"copy_{fromMeido.Maid.status.guid}";
  216. Maid.body0.CrossFade(tag, poseBinary, false, true, false, 0f);
  217. Maid.SetAutoTwistAll(true);
  218. Maid.transform.rotation = fromMeido.Maid.transform.rotation;
  219. SetMune();
  220. }
  221. public void SetPoseCustom(string path)
  222. {
  223. if (!Maid.body0.isLoadedBody) return;
  224. byte[] bytes = File.ReadAllBytes(path);
  225. string hash = Path.GetFileName(path).GetHashCode().ToString();
  226. Maid.body0.CrossFade(hash, bytes, false, true, false, 0f);
  227. Maid.SetAutoTwistAll(true);
  228. SetMune();
  229. }
  230. public void SetMune(bool drag = false)
  231. {
  232. bool isMomiOrPaizuri = CachedPose.Pose.Contains("_momi") || CachedPose.Pose.Contains("paizuri_");
  233. float onL = (drag || isMomiOrPaizuri) ? 0f : 1f;
  234. Maid.body0.MuneYureL(onL);
  235. Maid.body0.MuneYureR(onL);
  236. Maid.body0.jbMuneL.enabled = !drag;
  237. Maid.body0.jbMuneR.enabled = !drag;
  238. }
  239. public void SetFaceBlendSet(string blendSet)
  240. {
  241. FaceBlendSet = blendSet;
  242. Maid.boMabataki = false;
  243. TMorph morph = Maid.body0.Face.morph;
  244. morph.EyeMabataki = 0f;
  245. morph.MulBlendValues(blendSet, 1f);
  246. morph.FixBlendValues_Face();
  247. OnUpdateMeido();
  248. }
  249. public void SetFaceBlendValue(string hash, float value)
  250. {
  251. TMorph morph = Maid.body0.Face.morph;
  252. if (hash == "nosefook")
  253. {
  254. morph.boNoseFook = value > 0f;
  255. Maid.boNoseFook = morph.boNoseFook;
  256. }
  257. else
  258. {
  259. float[] blendValues = hash.StartsWith("eyeclose")
  260. ? Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValuesBackup")
  261. : Utility.GetFieldValue<TMorph, float[]>(morph, "BlendValues");
  262. try
  263. {
  264. blendValues[(int)morph.hash[hash]] = value;
  265. }
  266. catch { }
  267. }
  268. Maid.boMabataki = false;
  269. morph.EyeMabataki = 0f;
  270. morph.FixBlendValues_Face();
  271. }
  272. public void SetMaskMode(TBody.MaskMode maskMode)
  273. {
  274. TBody body = Maid.body0;
  275. bool invisibleBody = !body.GetMask(TBody.SlotID.body);
  276. body.SetMaskMode(maskMode);
  277. if (invisibleBody) SetBodyMask(false);
  278. }
  279. public void SetBodyMask(bool enabled)
  280. {
  281. TBody body = Maid.body0;
  282. Hashtable table = Utility.GetFieldValue<TBody, Hashtable>(body, "m_hFoceHide");
  283. foreach (TBody.SlotID bodySlot in MaidDressingPane.bodySlots)
  284. {
  285. table[bodySlot] = enabled;
  286. }
  287. if (body.goSlot[19].m_strModelFileName.Contains("melala_body"))
  288. {
  289. table[TBody.SlotID.accHana] = enabled;
  290. }
  291. body.FixMaskFlag();
  292. body.FixVisibleFlag(false);
  293. }
  294. public Transform GetBoneTransform(AttachPoint point) => this.dragPointManager?.GetAttachPointTransform(point);
  295. private void OnBodyLoad()
  296. {
  297. BodyLoad?.Invoke(this, EventArgs.Empty);
  298. this.IsIK = true;
  299. this.IsStop = false;
  300. this.IsBone = false;
  301. }
  302. private void OnUpdateMeido(MeidoUpdateEventArgs args = null)
  303. {
  304. this.UpdateMeido?.Invoke(this, args ?? MeidoUpdateEventArgs.Empty);
  305. }
  306. private void OnMeidoSelect(object sender, MeidoUpdateEventArgs args)
  307. {
  308. UpdateMeido?.Invoke(this, args);
  309. }
  310. }
  311. public struct PoseInfo
  312. {
  313. public string PoseGroup { get; }
  314. public string Pose { get; }
  315. public bool CustomPose { get; }
  316. public PoseInfo(string poseGroup, string pose, bool customPose = false)
  317. {
  318. this.PoseGroup = poseGroup;
  319. this.Pose = pose;
  320. this.CustomPose = customPose;
  321. }
  322. }
  323. }