MeidoManager.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace COM3D2.MeidoPhotoStudio.Plugin
  6. {
  7. internal class MeidoManager : IManager, ISerializable
  8. {
  9. public const string header = "MEIDO";
  10. private static CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
  11. private int undress = 0;
  12. private int numberOfMeidos;
  13. public Meido[] meidos { get; private set; }
  14. public List<int> SelectMeidoList { get; private set; } = new List<int>();
  15. public List<Meido> ActiveMeidoList { get; private set; } = new List<Meido>();
  16. public Meido ActiveMeido => ActiveMeidoList.Count > 0 ? ActiveMeidoList[SelectedMeido] : null;
  17. public bool HasActiveMeido => ActiveMeido != null;
  18. public event EventHandler<MeidoUpdateEventArgs> UpdateMeido;
  19. public event EventHandler EndCallMeidos;
  20. public event EventHandler BeginCallMeidos;
  21. private int selectedMeido = 0;
  22. public int SelectedMeido
  23. {
  24. get => selectedMeido;
  25. private set => selectedMeido = Utility.Bound(value, 0, ActiveMeidoList.Count - 1);
  26. }
  27. public bool Busy => ActiveMeidoList.Any(meido => meido.Busy);
  28. public void ChangeMaid(int index)
  29. {
  30. OnUpdateMeido(null, new MeidoUpdateEventArgs(index));
  31. }
  32. public void Activate()
  33. {
  34. GameMain.Instance.CharacterMgr.ResetCharaPosAll();
  35. numberOfMeidos = characterMgr.GetStockMaidCount();
  36. meidos = new Meido[numberOfMeidos];
  37. for (int stockMaidIndex = 0; stockMaidIndex < numberOfMeidos; stockMaidIndex++)
  38. {
  39. meidos[stockMaidIndex] = new Meido(stockMaidIndex);
  40. }
  41. }
  42. public void Deactivate()
  43. {
  44. foreach (Meido meido in meidos)
  45. {
  46. meido.UpdateMeido -= OnUpdateMeido;
  47. meido.Deactivate();
  48. }
  49. SelectMeidoList.Clear();
  50. ActiveMeidoList.Clear();
  51. }
  52. public void Update()
  53. {
  54. if (Input.GetKeyDown(KeyCode.H)) UndressAll();
  55. }
  56. public void Serialize(System.IO.BinaryWriter binaryWriter)
  57. {
  58. binaryWriter.Write(header);
  59. // Only true for MM scenes converted to MPS scenes
  60. binaryWriter.Write(false);
  61. binaryWriter.Write(Meido.meidoDataVersion);
  62. binaryWriter.Write(ActiveMeidoList.Count);
  63. foreach (Meido meido in ActiveMeidoList)
  64. {
  65. meido.Serialize(binaryWriter);
  66. }
  67. }
  68. public void Deserialize(System.IO.BinaryReader binaryReader)
  69. {
  70. bool isMMScene = binaryReader.ReadBoolean();
  71. int dataVersion = binaryReader.ReadInt32();
  72. int numberOfMaids = binaryReader.ReadInt32();
  73. for (int i = 0; i < numberOfMaids; i++)
  74. {
  75. if (i >= ActiveMeidoList.Count)
  76. {
  77. Int64 skip = binaryReader.ReadInt64(); // meido buffer length
  78. binaryReader.BaseStream.Seek(skip, System.IO.SeekOrigin.Current);
  79. continue;
  80. }
  81. Meido meido = ActiveMeidoList[i];
  82. meido.Deserialize(binaryReader, dataVersion, isMMScene);
  83. }
  84. }
  85. private void UnloadMeidos()
  86. {
  87. SelectedMeido = 0;
  88. foreach (Meido meido in ActiveMeidoList)
  89. {
  90. meido.UpdateMeido -= OnUpdateMeido;
  91. meido.Unload();
  92. }
  93. ActiveMeidoList.Clear();
  94. }
  95. public void CallMeidos()
  96. {
  97. this.BeginCallMeidos?.Invoke(this, EventArgs.Empty);
  98. bool hadActiveMeidos = HasActiveMeido;
  99. UnloadMeidos();
  100. if (SelectMeidoList.Count == 0)
  101. {
  102. OnEndCallMeidos(this, EventArgs.Empty);
  103. return;
  104. }
  105. GameMain.Instance.MainCamera.FadeOut(0.01f, f_bSkipable: false, f_dg: () =>
  106. {
  107. GameMain.Instance.StartCoroutine(LoadMeidos());
  108. });
  109. }
  110. private System.Collections.IEnumerator LoadMeidos()
  111. {
  112. foreach (int slot in this.SelectMeidoList)
  113. {
  114. Meido meido = meidos[slot];
  115. ActiveMeidoList.Add(meido);
  116. meido.BeginLoad();
  117. }
  118. for (int i = 0; i < ActiveMeidoList.Count; i++)
  119. {
  120. ActiveMeidoList[i].Load(i);
  121. }
  122. while (Busy) yield return null;
  123. yield return new WaitForEndOfFrame();
  124. OnEndCallMeidos(this, EventArgs.Empty);
  125. }
  126. public Meido GetMeido(string guid)
  127. {
  128. if (string.IsNullOrEmpty(guid)) return null;
  129. else return ActiveMeidoList.FirstOrDefault(meido => meido.Maid.status.guid == guid);
  130. }
  131. public Meido GetMeido(int activeIndex)
  132. {
  133. if (activeIndex >= 0 && activeIndex < ActiveMeidoList.Count) return ActiveMeidoList[activeIndex];
  134. else return null;
  135. }
  136. public void PlaceMeidos(string placementType)
  137. {
  138. MaidPlacementUtility.ApplyPlacement(placementType, ActiveMeidoList);
  139. }
  140. private void UndressAll()
  141. {
  142. if (!HasActiveMeido) return;
  143. undress = Utility.Wrap(undress + 1, 0, 3);
  144. TBody.MaskMode maskMode = TBody.MaskMode.None;
  145. switch (undress)
  146. {
  147. case 0: maskMode = TBody.MaskMode.None; break;
  148. case 1: maskMode = TBody.MaskMode.Underwear; break;
  149. case 2: maskMode = TBody.MaskMode.Nude; break;
  150. }
  151. foreach (Meido activeMeido in ActiveMeidoList)
  152. {
  153. activeMeido.SetMaskMode(maskMode);
  154. }
  155. this.UpdateMeido?.Invoke(ActiveMeido, new MeidoUpdateEventArgs(SelectedMeido));
  156. }
  157. private void OnUpdateMeido(object sender, MeidoUpdateEventArgs args)
  158. {
  159. if (!args.IsEmpty) this.SelectedMeido = args.SelectedMeido;
  160. this.UpdateMeido?.Invoke(ActiveMeido, args);
  161. }
  162. private void OnEndCallMeidos(object sender, EventArgs args)
  163. {
  164. GameMain.Instance.MainCamera.FadeIn(1f);
  165. EndCallMeidos?.Invoke(this, EventArgs.Empty);
  166. foreach (Meido meido in ActiveMeidoList)
  167. {
  168. meido.UpdateMeido += OnUpdateMeido;
  169. }
  170. }
  171. }
  172. public class MeidoUpdateEventArgs : EventArgs
  173. {
  174. public static new MeidoUpdateEventArgs Empty { get; } = new MeidoUpdateEventArgs(-1);
  175. public bool IsEmpty
  176. {
  177. get
  178. {
  179. return (this == MeidoUpdateEventArgs.Empty) ||
  180. (this.SelectedMeido == -1 && !this.FromMeido && this.IsBody);
  181. }
  182. }
  183. public int SelectedMeido { get; }
  184. public bool IsBody { get; }
  185. public bool FromMeido { get; }
  186. public MeidoUpdateEventArgs(int meidoIndex = -1, bool fromMaid = false, bool isBody = true)
  187. {
  188. this.SelectedMeido = meidoIndex;
  189. this.IsBody = isBody;
  190. this.FromMeido = fromMaid;
  191. }
  192. }
  193. }