MeidoManager.cs 8.1 KB

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