MeidoManager.cs 8.4 KB

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