MeidoManager.cs 7.1 KB

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