MeidoManager.cs 8.3 KB

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