Meido.cs 14 KB

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