MeidoManager.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. if (MeidoPhotoStudio.EditMode)
  63. {
  64. Maid editMaid = GameMain.Instance.CharacterMgr.GetMaid(0);
  65. EditMaidIndex = Array.FindIndex(Meidos, meido => meido.Maid.status.guid == editMaid.status.guid);
  66. }
  67. ClearSelectList();
  68. }
  69. public void Deactivate()
  70. {
  71. foreach (Meido meido in Meidos)
  72. {
  73. meido.UpdateMeido -= OnUpdateMeido;
  74. meido.GravityMove -= OnGravityMove;
  75. meido.Deactivate();
  76. }
  77. ActiveMeidoList.Clear();
  78. ClearSelectList();
  79. if (MeidoPhotoStudio.EditMode)
  80. {
  81. Meido meido = Meidos[EditMaidIndex];
  82. meido.Maid.Visible = true;
  83. meido.Stop = false;
  84. meido.EyeToCam = true;
  85. }
  86. }
  87. public void Update()
  88. {
  89. if (InputManager.GetKeyDown(MpsKey.MeidoUndressing)) UndressAll();
  90. }
  91. public void Serialize(System.IO.BinaryWriter binaryWriter)
  92. {
  93. binaryWriter.Write(header);
  94. // Only true for MM scenes converted to MPS scenes
  95. binaryWriter.Write(false);
  96. binaryWriter.Write(Meido.meidoDataVersion);
  97. binaryWriter.Write(ActiveMeidoList.Count);
  98. foreach (Meido meido in ActiveMeidoList)
  99. {
  100. meido.Serialize(binaryWriter);
  101. }
  102. // Global hair/skirt gravity
  103. binaryWriter.Write(GlobalGravity);
  104. }
  105. public void Deserialize(System.IO.BinaryReader binaryReader)
  106. {
  107. bool isMMScene = binaryReader.ReadBoolean();
  108. int dataVersion = binaryReader.ReadInt32();
  109. int numberOfMaids = binaryReader.ReadInt32();
  110. for (int i = 0; i < numberOfMaids; i++)
  111. {
  112. if (i >= ActiveMeidoList.Count)
  113. {
  114. long skip = binaryReader.ReadInt64(); // meido buffer length
  115. binaryReader.BaseStream.Seek(skip, System.IO.SeekOrigin.Current);
  116. continue;
  117. }
  118. Meido meido = ActiveMeidoList[i];
  119. meido.Deserialize(binaryReader, dataVersion, isMMScene);
  120. }
  121. // Global hair/skirt gravity
  122. GlobalGravity = binaryReader.ReadBoolean();
  123. }
  124. private void UnloadMeidos()
  125. {
  126. SelectedMeido = 0;
  127. foreach (Meido meido in ActiveMeidoList)
  128. {
  129. meido.UpdateMeido -= OnUpdateMeido;
  130. meido.GravityMove -= OnGravityMove;
  131. meido.Unload();
  132. }
  133. ActiveMeidoList.Clear();
  134. }
  135. public void CallMeidos()
  136. {
  137. BeginCallMeidos?.Invoke(this, EventArgs.Empty);
  138. bool moreThanEditMaid = ActiveMeidoList.Count > 1;
  139. UnloadMeidos();
  140. if (SelectMeidoList.Count == 0)
  141. {
  142. OnEndCallMeidos(this, EventArgs.Empty);
  143. return;
  144. }
  145. void callMeidos() => GameMain.Instance.StartCoroutine(LoadMeidos());
  146. if (MeidoPhotoStudio.EditMode && !moreThanEditMaid && SelectMeidoList.Count == 1) callMeidos();
  147. else GameMain.Instance.MainCamera.FadeOut(0.01f, f_bSkipable: false, f_dg: callMeidos);
  148. }
  149. private System.Collections.IEnumerator LoadMeidos()
  150. {
  151. foreach (int slot in SelectMeidoList) ActiveMeidoList.Add(Meidos[slot]);
  152. for (int i = 0; i < ActiveMeidoList.Count; i++) ActiveMeidoList[i].Load(i);
  153. while (Busy) yield return null;
  154. yield return new WaitForEndOfFrame();
  155. OnEndCallMeidos(this, EventArgs.Empty);
  156. }
  157. public void SelectMeido(int index)
  158. {
  159. if (SelectedMeidoSet.Contains(index))
  160. {
  161. if (!MeidoPhotoStudio.EditMode || index != EditMaidIndex)
  162. {
  163. SelectedMeidoSet.Remove(index);
  164. SelectMeidoList.Remove(index);
  165. }
  166. }
  167. else
  168. {
  169. SelectedMeidoSet.Add(index);
  170. SelectMeidoList.Add(index);
  171. }
  172. }
  173. public void ClearSelectList()
  174. {
  175. SelectedMeidoSet.Clear();
  176. SelectMeidoList.Clear();
  177. if (MeidoPhotoStudio.EditMode)
  178. {
  179. SelectedMeidoSet.Add(EditMaidIndex);
  180. SelectMeidoList.Add(EditMaidIndex);
  181. }
  182. }
  183. public Meido GetMeido(string guid)
  184. {
  185. return string.IsNullOrEmpty(guid) ? null : ActiveMeidoList.Find(meido => meido.Maid.status.guid == guid);
  186. }
  187. public Meido GetMeido(int activeIndex)
  188. {
  189. return activeIndex >= 0 && activeIndex < ActiveMeidoList.Count ? ActiveMeidoList[activeIndex] : null;
  190. }
  191. public void PlaceMeidos(string placementType)
  192. {
  193. MaidPlacementUtility.ApplyPlacement(placementType, ActiveMeidoList);
  194. }
  195. private void UndressAll()
  196. {
  197. if (!HasActiveMeido) return;
  198. undress = Utility.Wrap(undress + 1, 0, 3);
  199. TBody.MaskMode maskMode = TBody.MaskMode.None;
  200. switch (undress)
  201. {
  202. case 0: maskMode = TBody.MaskMode.None; break;
  203. case 1: maskMode = TBody.MaskMode.Underwear; break;
  204. case 2: maskMode = TBody.MaskMode.Nude; break;
  205. }
  206. foreach (Meido activeMeido in ActiveMeidoList)
  207. {
  208. activeMeido.SetMaskMode(maskMode);
  209. }
  210. UpdateMeido?.Invoke(ActiveMeido, new MeidoUpdateEventArgs(SelectedMeido));
  211. }
  212. private void OnUpdateMeido(object sender, MeidoUpdateEventArgs args)
  213. {
  214. if (!args.IsEmpty) SelectedMeido = args.SelectedMeido;
  215. UpdateMeido?.Invoke(ActiveMeido, args);
  216. }
  217. private void OnEndCallMeidos(object sender, EventArgs args)
  218. {
  219. GameMain.Instance.MainCamera.FadeIn(1f);
  220. EndCallMeidos?.Invoke(this, EventArgs.Empty);
  221. foreach (Meido meido in ActiveMeidoList)
  222. {
  223. meido.UpdateMeido += OnUpdateMeido;
  224. meido.GravityMove += OnGravityMove;
  225. }
  226. }
  227. private void OnGravityMove(object sender, GravityEventArgs args)
  228. {
  229. if (!GlobalGravity) return;
  230. foreach (Meido meido in ActiveMeidoList)
  231. {
  232. meido.ApplyGravity(args.LocalPosition, args.IsSkirt);
  233. }
  234. }
  235. }
  236. public class MeidoUpdateEventArgs : EventArgs
  237. {
  238. public static new MeidoUpdateEventArgs Empty { get; } = new MeidoUpdateEventArgs(-1);
  239. public bool IsEmpty => (this == Empty) || (SelectedMeido == -1 && !FromMeido && IsBody);
  240. public int SelectedMeido { get; }
  241. public bool IsBody { get; }
  242. public bool FromMeido { get; }
  243. public MeidoUpdateEventArgs(int meidoIndex = -1, bool fromMaid = false, bool isBody = true)
  244. {
  245. SelectedMeido = meidoIndex;
  246. IsBody = isBody;
  247. FromMeido = fromMaid;
  248. }
  249. }
  250. }