MeidoManager.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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
  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. if (hadActiveMeidos) 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. private void UndressAll()
  135. {
  136. if (!HasActiveMeido) return;
  137. undress = Utility.Wrap(undress + 1, 0, 3);
  138. TBody.MaskMode maskMode = TBody.MaskMode.None;
  139. switch (undress)
  140. {
  141. case 0: maskMode = TBody.MaskMode.None; break;
  142. case 1: maskMode = TBody.MaskMode.Underwear; break;
  143. case 2: maskMode = TBody.MaskMode.Nude; break;
  144. }
  145. foreach (Meido activeMeido in ActiveMeidoList)
  146. {
  147. activeMeido.SetMaskMode(maskMode);
  148. }
  149. this.UpdateMeido?.Invoke(ActiveMeido, new MeidoUpdateEventArgs(SelectedMeido));
  150. }
  151. private void OnUpdateMeido(object sender, MeidoUpdateEventArgs args)
  152. {
  153. if (!args.IsEmpty) this.SelectedMeido = args.SelectedMeido;
  154. this.UpdateMeido?.Invoke(ActiveMeido, args);
  155. }
  156. private void OnEndCallMeidos(object sender, EventArgs args)
  157. {
  158. GameMain.Instance.MainCamera.FadeIn(1f);
  159. EndCallMeidos?.Invoke(this, EventArgs.Empty);
  160. foreach (Meido meido in ActiveMeidoList)
  161. {
  162. meido.UpdateMeido += OnUpdateMeido;
  163. }
  164. }
  165. }
  166. public class MeidoUpdateEventArgs : EventArgs
  167. {
  168. public static new MeidoUpdateEventArgs Empty { get; } = new MeidoUpdateEventArgs(-1);
  169. public bool IsEmpty
  170. {
  171. get
  172. {
  173. return (this == MeidoUpdateEventArgs.Empty) ||
  174. (this.SelectedMeido == -1 && !this.FromMeido && !this.IsBody);
  175. }
  176. }
  177. public int SelectedMeido { get; }
  178. public bool IsBody { get; }
  179. public bool FromMeido { get; }
  180. public MeidoUpdateEventArgs(int meidoIndex, bool fromMaid = false, bool isBody = true)
  181. {
  182. this.SelectedMeido = meidoIndex;
  183. this.IsBody = isBody;
  184. this.FromMeido = fromMaid;
  185. }
  186. }
  187. }