MeidoManager.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. private bool globalGravity = false;
  29. public bool GlobalGravity
  30. {
  31. get => globalGravity;
  32. set
  33. {
  34. this.globalGravity = value;
  35. Meido activeMeido = ActiveMeido;
  36. int activeMeidoSlot = activeMeido.Slot;
  37. foreach (Meido meido in ActiveMeidoList)
  38. {
  39. if (meido.Slot != activeMeidoSlot)
  40. {
  41. meido.HairGravityActive = value ? activeMeido.HairGravityActive : false;
  42. meido.SkirtGravityActive = value ? activeMeido.SkirtGravityActive : false;
  43. }
  44. }
  45. }
  46. }
  47. static MeidoManager() => InputManager.Register(MpsKey.MeidoUndressing, KeyCode.H, "All maid undressing");
  48. public void ChangeMaid(int index)
  49. {
  50. OnUpdateMeido(null, new MeidoUpdateEventArgs(index));
  51. }
  52. public void Activate()
  53. {
  54. GameMain.Instance.CharacterMgr.ResetCharaPosAll();
  55. numberOfMeidos = characterMgr.GetStockMaidCount();
  56. meidos = new Meido[numberOfMeidos];
  57. for (int stockMaidIndex = 0; stockMaidIndex < numberOfMeidos; stockMaidIndex++)
  58. {
  59. meidos[stockMaidIndex] = new Meido(stockMaidIndex);
  60. }
  61. }
  62. public void Deactivate()
  63. {
  64. foreach (Meido meido in meidos)
  65. {
  66. meido.UpdateMeido -= OnUpdateMeido;
  67. meido.GravityMove -= OnGravityMove;
  68. meido.Deactivate();
  69. }
  70. SelectMeidoList.Clear();
  71. ActiveMeidoList.Clear();
  72. }
  73. public void Update()
  74. {
  75. if (InputManager.GetKeyDown(MpsKey.MeidoUndressing)) UndressAll();
  76. }
  77. public void Serialize(System.IO.BinaryWriter binaryWriter)
  78. {
  79. binaryWriter.Write(header);
  80. // Only true for MM scenes converted to MPS scenes
  81. binaryWriter.Write(false);
  82. binaryWriter.Write(Meido.meidoDataVersion);
  83. binaryWriter.Write(ActiveMeidoList.Count);
  84. foreach (Meido meido in ActiveMeidoList)
  85. {
  86. meido.Serialize(binaryWriter);
  87. }
  88. }
  89. public void Deserialize(System.IO.BinaryReader binaryReader)
  90. {
  91. bool isMMScene = binaryReader.ReadBoolean();
  92. int dataVersion = binaryReader.ReadInt32();
  93. int numberOfMaids = binaryReader.ReadInt32();
  94. for (int i = 0; i < numberOfMaids; i++)
  95. {
  96. if (i >= ActiveMeidoList.Count)
  97. {
  98. Int64 skip = binaryReader.ReadInt64(); // meido buffer length
  99. binaryReader.BaseStream.Seek(skip, System.IO.SeekOrigin.Current);
  100. continue;
  101. }
  102. Meido meido = ActiveMeidoList[i];
  103. meido.Deserialize(binaryReader, dataVersion, isMMScene);
  104. }
  105. }
  106. private void UnloadMeidos()
  107. {
  108. SelectedMeido = 0;
  109. foreach (Meido meido in ActiveMeidoList)
  110. {
  111. meido.UpdateMeido -= OnUpdateMeido;
  112. meido.GravityMove -= OnGravityMove;
  113. meido.Unload();
  114. }
  115. ActiveMeidoList.Clear();
  116. }
  117. public void CallMeidos()
  118. {
  119. this.BeginCallMeidos?.Invoke(this, EventArgs.Empty);
  120. bool hadActiveMeidos = HasActiveMeido;
  121. UnloadMeidos();
  122. if (SelectMeidoList.Count == 0)
  123. {
  124. OnEndCallMeidos(this, EventArgs.Empty);
  125. return;
  126. }
  127. GameMain.Instance.MainCamera.FadeOut(0.01f, f_bSkipable: false, f_dg: () =>
  128. {
  129. GameMain.Instance.StartCoroutine(LoadMeidos());
  130. });
  131. }
  132. private System.Collections.IEnumerator LoadMeidos()
  133. {
  134. foreach (int slot in this.SelectMeidoList)
  135. {
  136. Meido meido = meidos[slot];
  137. ActiveMeidoList.Add(meido);
  138. meido.BeginLoad();
  139. }
  140. for (int i = 0; i < ActiveMeidoList.Count; i++)
  141. {
  142. ActiveMeidoList[i].Load(i);
  143. }
  144. while (Busy) yield return null;
  145. yield return new WaitForEndOfFrame();
  146. OnEndCallMeidos(this, EventArgs.Empty);
  147. }
  148. public Meido GetMeido(string guid)
  149. {
  150. if (string.IsNullOrEmpty(guid)) return null;
  151. else return ActiveMeidoList.FirstOrDefault(meido => meido.Maid.status.guid == guid);
  152. }
  153. public Meido GetMeido(int activeIndex)
  154. {
  155. if (activeIndex >= 0 && activeIndex < ActiveMeidoList.Count) return ActiveMeidoList[activeIndex];
  156. else return null;
  157. }
  158. public void PlaceMeidos(string placementType)
  159. {
  160. MaidPlacementUtility.ApplyPlacement(placementType, ActiveMeidoList);
  161. }
  162. private void UndressAll()
  163. {
  164. if (!HasActiveMeido) return;
  165. undress = Utility.Wrap(undress + 1, 0, 3);
  166. TBody.MaskMode maskMode = TBody.MaskMode.None;
  167. switch (undress)
  168. {
  169. case 0: maskMode = TBody.MaskMode.None; break;
  170. case 1: maskMode = TBody.MaskMode.Underwear; break;
  171. case 2: maskMode = TBody.MaskMode.Nude; break;
  172. }
  173. foreach (Meido activeMeido in ActiveMeidoList)
  174. {
  175. activeMeido.SetMaskMode(maskMode);
  176. }
  177. this.UpdateMeido?.Invoke(ActiveMeido, new MeidoUpdateEventArgs(SelectedMeido));
  178. }
  179. private void OnUpdateMeido(object sender, MeidoUpdateEventArgs args)
  180. {
  181. if (!args.IsEmpty) this.SelectedMeido = args.SelectedMeido;
  182. this.UpdateMeido?.Invoke(ActiveMeido, args);
  183. }
  184. private void OnEndCallMeidos(object sender, EventArgs args)
  185. {
  186. GameMain.Instance.MainCamera.FadeIn(1f);
  187. EndCallMeidos?.Invoke(this, EventArgs.Empty);
  188. foreach (Meido meido in ActiveMeidoList)
  189. {
  190. meido.UpdateMeido += OnUpdateMeido;
  191. meido.GravityMove += OnGravityMove;
  192. }
  193. }
  194. private void OnGravityMove(object sender, GravityEventArgs args)
  195. {
  196. if (!GlobalGravity) return;
  197. foreach (Meido meido in ActiveMeidoList)
  198. {
  199. meido.ApplyGravity(args.LocalPosition, args.IsSkirt);
  200. }
  201. }
  202. }
  203. public class MeidoUpdateEventArgs : EventArgs
  204. {
  205. public static new MeidoUpdateEventArgs Empty { get; } = new MeidoUpdateEventArgs(-1);
  206. public bool IsEmpty
  207. {
  208. get
  209. {
  210. return (this == MeidoUpdateEventArgs.Empty) ||
  211. (this.SelectedMeido == -1 && !this.FromMeido && this.IsBody);
  212. }
  213. }
  214. public int SelectedMeido { get; }
  215. public bool IsBody { get; }
  216. public bool FromMeido { get; }
  217. public MeidoUpdateEventArgs(int meidoIndex = -1, bool fromMaid = false, bool isBody = true)
  218. {
  219. this.SelectedMeido = meidoIndex;
  220. this.IsBody = isBody;
  221. this.FromMeido = fromMaid;
  222. }
  223. }
  224. }