MeidoManager.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 CharacterMgr characterMgr = GameMain.Instance.CharacterMgr;
  11. private int undress = 0;
  12. private int numberOfMeidos;
  13. public Meido[] meidos { get; private set; }
  14. public List<int> SelectMeidoList { get; private set; } = new List<int>();
  15. public List<Meido> ActiveMeidoList { get; private set; } = 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 = 0;
  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 = false;
  29. public bool GlobalGravity
  30. {
  31. get => globalGravity;
  32. set
  33. {
  34. this.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 : false;
  42. meido.SkirtGravityActive = value ? activeMeido.SkirtGravityActive : false;
  43. }
  44. }
  45. }
  46. }
  47. public void ChangeMaid(int index)
  48. {
  49. OnUpdateMeido(null, new MeidoUpdateEventArgs(index));
  50. }
  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 (Input.GetKeyDown(KeyCode.H)) 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. }
  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. Int64 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. }
  105. private void UnloadMeidos()
  106. {
  107. SelectedMeido = 0;
  108. foreach (Meido meido in ActiveMeidoList)
  109. {
  110. meido.UpdateMeido -= OnUpdateMeido;
  111. meido.GravityMove -= OnGravityMove;
  112. meido.Unload();
  113. }
  114. ActiveMeidoList.Clear();
  115. }
  116. public void CallMeidos()
  117. {
  118. this.BeginCallMeidos?.Invoke(this, EventArgs.Empty);
  119. bool hadActiveMeidos = HasActiveMeido;
  120. UnloadMeidos();
  121. if (SelectMeidoList.Count == 0)
  122. {
  123. OnEndCallMeidos(this, EventArgs.Empty);
  124. return;
  125. }
  126. GameMain.Instance.MainCamera.FadeOut(0.01f, f_bSkipable: false, f_dg: () =>
  127. {
  128. GameMain.Instance.StartCoroutine(LoadMeidos());
  129. });
  130. }
  131. private System.Collections.IEnumerator LoadMeidos()
  132. {
  133. foreach (int slot in this.SelectMeidoList)
  134. {
  135. Meido meido = meidos[slot];
  136. ActiveMeidoList.Add(meido);
  137. meido.BeginLoad();
  138. }
  139. for (int i = 0; i < ActiveMeidoList.Count; i++)
  140. {
  141. ActiveMeidoList[i].Load(i);
  142. }
  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. if (string.IsNullOrEmpty(guid)) return null;
  150. else return ActiveMeidoList.FirstOrDefault(meido => meido.Maid.status.guid == guid);
  151. }
  152. public Meido GetMeido(int activeIndex)
  153. {
  154. if (activeIndex >= 0 && activeIndex < ActiveMeidoList.Count) return ActiveMeidoList[activeIndex];
  155. else return null;
  156. }
  157. public void PlaceMeidos(string placementType)
  158. {
  159. MaidPlacementUtility.ApplyPlacement(placementType, ActiveMeidoList);
  160. }
  161. private void UndressAll()
  162. {
  163. if (!HasActiveMeido) return;
  164. undress = Utility.Wrap(undress + 1, 0, 3);
  165. TBody.MaskMode maskMode = TBody.MaskMode.None;
  166. switch (undress)
  167. {
  168. case 0: maskMode = TBody.MaskMode.None; break;
  169. case 1: maskMode = TBody.MaskMode.Underwear; break;
  170. case 2: maskMode = TBody.MaskMode.Nude; break;
  171. }
  172. foreach (Meido activeMeido in ActiveMeidoList)
  173. {
  174. activeMeido.SetMaskMode(maskMode);
  175. }
  176. this.UpdateMeido?.Invoke(ActiveMeido, new MeidoUpdateEventArgs(SelectedMeido));
  177. }
  178. private void OnUpdateMeido(object sender, MeidoUpdateEventArgs args)
  179. {
  180. if (!args.IsEmpty) this.SelectedMeido = args.SelectedMeido;
  181. this.UpdateMeido?.Invoke(ActiveMeido, args);
  182. }
  183. private void OnEndCallMeidos(object sender, EventArgs args)
  184. {
  185. GameMain.Instance.MainCamera.FadeIn(1f);
  186. EndCallMeidos?.Invoke(this, EventArgs.Empty);
  187. foreach (Meido meido in ActiveMeidoList)
  188. {
  189. meido.UpdateMeido += OnUpdateMeido;
  190. meido.GravityMove += OnGravityMove;
  191. }
  192. }
  193. private void OnGravityMove(object sender, GravityEventArgs args)
  194. {
  195. if (!GlobalGravity) return;
  196. foreach (Meido meido in ActiveMeidoList)
  197. {
  198. meido.ApplyGravity(args.LocalPosition, args.IsSkirt);
  199. }
  200. }
  201. }
  202. public class MeidoUpdateEventArgs : EventArgs
  203. {
  204. public static new MeidoUpdateEventArgs Empty { get; } = new MeidoUpdateEventArgs(-1);
  205. public bool IsEmpty
  206. {
  207. get
  208. {
  209. return (this == MeidoUpdateEventArgs.Empty) ||
  210. (this.SelectedMeido == -1 && !this.FromMeido && this.IsBody);
  211. }
  212. }
  213. public int SelectedMeido { get; }
  214. public bool IsBody { get; }
  215. public bool FromMeido { get; }
  216. public MeidoUpdateEventArgs(int meidoIndex = -1, bool fromMaid = false, bool isBody = true)
  217. {
  218. this.SelectedMeido = meidoIndex;
  219. this.IsBody = isBody;
  220. this.FromMeido = fromMaid;
  221. }
  222. }
  223. }