MeidoManager.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // Global hair/skirt gravity
  86. binaryWriter.Write(GlobalGravity);
  87. }
  88. public void Deserialize(System.IO.BinaryReader binaryReader)
  89. {
  90. bool isMMScene = binaryReader.ReadBoolean();
  91. int dataVersion = binaryReader.ReadInt32();
  92. int numberOfMaids = binaryReader.ReadInt32();
  93. for (int i = 0; i < numberOfMaids; i++)
  94. {
  95. if (i >= ActiveMeidoList.Count)
  96. {
  97. long skip = binaryReader.ReadInt64(); // meido buffer length
  98. binaryReader.BaseStream.Seek(skip, System.IO.SeekOrigin.Current);
  99. continue;
  100. }
  101. Meido meido = ActiveMeidoList[i];
  102. meido.Deserialize(binaryReader, dataVersion, isMMScene);
  103. }
  104. // Global hair/skirt gravity
  105. GlobalGravity = binaryReader.ReadBoolean();
  106. }
  107. private void UnloadMeidos()
  108. {
  109. SelectedMeido = 0;
  110. foreach (Meido meido in ActiveMeidoList)
  111. {
  112. meido.UpdateMeido -= OnUpdateMeido;
  113. meido.GravityMove -= OnGravityMove;
  114. meido.Unload();
  115. }
  116. ActiveMeidoList.Clear();
  117. }
  118. public void CallMeidos()
  119. {
  120. BeginCallMeidos?.Invoke(this, EventArgs.Empty);
  121. bool hadActiveMeidos = HasActiveMeido;
  122. UnloadMeidos();
  123. if (SelectMeidoList.Count == 0)
  124. {
  125. OnEndCallMeidos(this, EventArgs.Empty);
  126. return;
  127. }
  128. GameMain.Instance.MainCamera.FadeOut(
  129. 0.01f, f_bSkipable: false, f_dg: () => GameMain.Instance.StartCoroutine(LoadMeidos())
  130. );
  131. }
  132. private System.Collections.IEnumerator LoadMeidos()
  133. {
  134. foreach (int slot in SelectMeidoList)
  135. {
  136. Meido meido = Meidos[slot];
  137. ActiveMeidoList.Add(meido);
  138. meido.BeginLoad();
  139. }
  140. for (int i = 0; i < ActiveMeidoList.Count; i++) ActiveMeidoList[i].Load(i);
  141. while (Busy) yield return null;
  142. yield return new WaitForEndOfFrame();
  143. OnEndCallMeidos(this, EventArgs.Empty);
  144. }
  145. public Meido GetMeido(string guid)
  146. {
  147. return string.IsNullOrEmpty(guid) ? null : ActiveMeidoList.Find(meido => meido.Maid.status.guid == guid);
  148. }
  149. public Meido GetMeido(int activeIndex)
  150. {
  151. return activeIndex >= 0 && activeIndex < ActiveMeidoList.Count ? ActiveMeidoList[activeIndex] : null;
  152. }
  153. public void PlaceMeidos(string placementType)
  154. {
  155. MaidPlacementUtility.ApplyPlacement(placementType, ActiveMeidoList);
  156. }
  157. private void UndressAll()
  158. {
  159. if (!HasActiveMeido) return;
  160. undress = Utility.Wrap(undress + 1, 0, 3);
  161. TBody.MaskMode maskMode = TBody.MaskMode.None;
  162. switch (undress)
  163. {
  164. case 0: maskMode = TBody.MaskMode.None; break;
  165. case 1: maskMode = TBody.MaskMode.Underwear; break;
  166. case 2: maskMode = TBody.MaskMode.Nude; break;
  167. }
  168. foreach (Meido activeMeido in ActiveMeidoList)
  169. {
  170. activeMeido.SetMaskMode(maskMode);
  171. }
  172. UpdateMeido?.Invoke(ActiveMeido, new MeidoUpdateEventArgs(SelectedMeido));
  173. }
  174. private void OnUpdateMeido(object sender, MeidoUpdateEventArgs args)
  175. {
  176. if (!args.IsEmpty) SelectedMeido = args.SelectedMeido;
  177. UpdateMeido?.Invoke(ActiveMeido, args);
  178. }
  179. private void OnEndCallMeidos(object sender, EventArgs args)
  180. {
  181. GameMain.Instance.MainCamera.FadeIn(1f);
  182. EndCallMeidos?.Invoke(this, EventArgs.Empty);
  183. foreach (Meido meido in ActiveMeidoList)
  184. {
  185. meido.UpdateMeido += OnUpdateMeido;
  186. meido.GravityMove += OnGravityMove;
  187. }
  188. }
  189. private void OnGravityMove(object sender, GravityEventArgs args)
  190. {
  191. if (!GlobalGravity) return;
  192. foreach (Meido meido in ActiveMeidoList)
  193. {
  194. meido.ApplyGravity(args.LocalPosition, args.IsSkirt);
  195. }
  196. }
  197. }
  198. public class MeidoUpdateEventArgs : EventArgs
  199. {
  200. public static new MeidoUpdateEventArgs Empty { get; } = new MeidoUpdateEventArgs(-1);
  201. public bool IsEmpty => (this == Empty) || (SelectedMeido == -1 && !FromMeido && IsBody);
  202. public int SelectedMeido { get; }
  203. public bool IsBody { get; }
  204. public bool FromMeido { get; }
  205. public MeidoUpdateEventArgs(int meidoIndex = -1, bool fromMaid = false, bool isBody = true)
  206. {
  207. SelectedMeido = meidoIndex;
  208. IsBody = isBody;
  209. FromMeido = fromMaid;
  210. }
  211. }
  212. }