MeidoManager.cs 8.8 KB

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