PropManager.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using BepInEx.Configuration;
  6. using UnityEngine;
  7. using static MeidoPhotoStudio.Plugin.ModelUtility;
  8. using Object = UnityEngine.Object;
  9. namespace MeidoPhotoStudio.Plugin;
  10. public class PropManager : IManager
  11. {
  12. public const string Header = "PROP";
  13. private static readonly ConfigEntry<bool> ModItemsOnlyValue;
  14. private static bool cubeActive = true;
  15. private static Dictionary<string, string> modFileToFullPath;
  16. private static bool cubeSmall;
  17. private readonly List<DragPointProp> propList = new();
  18. private readonly MeidoManager meidoManager;
  19. private int currentPropIndex;
  20. static PropManager() =>
  21. ModItemsOnlyValue = Configuration.Config.Bind(
  22. "Prop", "ModItemsOnly", false, "Disable waiting for and loading base game clothing");
  23. public PropManager(MeidoManager meidoManager)
  24. {
  25. this.meidoManager = meidoManager;
  26. meidoManager.BeginCallMeidos += OnBeginCallMeidos;
  27. meidoManager.EndCallMeidos += OnEndCallMeidos;
  28. Activate();
  29. }
  30. public event EventHandler PropSelectionChange;
  31. public event EventHandler FromPropSelect;
  32. public event EventHandler PropListChange;
  33. private static event EventHandler CubeActiveChange;
  34. private static event EventHandler CubeSmallChange;
  35. public static bool CubeActive
  36. {
  37. get => cubeActive;
  38. set
  39. {
  40. if (value == cubeActive)
  41. return;
  42. cubeActive = value;
  43. CubeActiveChange?.Invoke(null, EventArgs.Empty);
  44. }
  45. }
  46. public static bool CubeSmall
  47. {
  48. get => cubeSmall;
  49. set
  50. {
  51. if (value == cubeSmall)
  52. return;
  53. cubeSmall = value;
  54. CubeSmallChange?.Invoke(null, EventArgs.Empty);
  55. }
  56. }
  57. public static bool ModItemsOnly =>
  58. ModItemsOnlyValue.Value;
  59. public DragPointProp CurrentProp =>
  60. PropCount is 0 ? null : propList[CurrentPropIndex];
  61. public string[] PropNameList =>
  62. propList.Count is 0
  63. ? new[] { Translation.Get("systemMessage", "noProps") }
  64. : propList.Select(prop => prop.Name).ToArray();
  65. public int PropCount =>
  66. propList.Count;
  67. public int CurrentPropIndex
  68. {
  69. get => currentPropIndex;
  70. set
  71. {
  72. if (PropCount is 0)
  73. {
  74. currentPropIndex = 0;
  75. return;
  76. }
  77. if ((uint)value >= (uint)PropCount)
  78. throw new ArgumentOutOfRangeException(nameof(value));
  79. if (currentPropIndex == value)
  80. return;
  81. currentPropIndex = value;
  82. PropSelectionChange?.Invoke(this, EventArgs.Empty);
  83. }
  84. }
  85. private static Dictionary<string, string> ModFileToFullPath
  86. {
  87. get
  88. {
  89. if (modFileToFullPath is not null)
  90. return modFileToFullPath;
  91. var modFiles = Menu.GetModFiles();
  92. modFileToFullPath = new(modFiles.Length, StringComparer.OrdinalIgnoreCase);
  93. foreach (var mod in modFiles)
  94. {
  95. var key = Path.GetFileName(mod);
  96. if (!modFileToFullPath.ContainsKey(key))
  97. modFileToFullPath[key] = mod;
  98. }
  99. return modFileToFullPath;
  100. }
  101. }
  102. public bool AddFromPropInfo(PropInfo propInfo)
  103. {
  104. switch (propInfo.Type)
  105. {
  106. case PropInfo.PropType.Mod:
  107. ModItem modItem;
  108. if (!string.IsNullOrEmpty(propInfo.SubFilename))
  109. {
  110. modItem = ModItem.OfficialMod(ModFileToFullPath[propInfo.Filename]);
  111. modItem.BaseMenuFile = propInfo.SubFilename;
  112. }
  113. else
  114. {
  115. modItem = ModItem.Mod(propInfo.Filename);
  116. }
  117. return AddModProp(modItem);
  118. case PropInfo.PropType.MyRoom:
  119. return AddMyRoomProp(new() { ID = propInfo.MyRoomID, PrefabName = propInfo.Filename });
  120. case PropInfo.PropType.Bg:
  121. return AddBgProp(propInfo.Filename);
  122. case PropInfo.PropType.Odogu:
  123. return AddGameProp(propInfo.Filename);
  124. default:
  125. throw new ArgumentOutOfRangeException();
  126. }
  127. }
  128. public bool AddModProp(ModItem modItem)
  129. {
  130. var model = LoadMenuModel(modItem);
  131. if (!model)
  132. return false;
  133. var name = modItem.MenuFile;
  134. if (modItem.IsOfficialMod)
  135. name = Path.GetFileName(name) + ".menu"; // add '.menu' for partsedit support
  136. model.name = name;
  137. var dragPoint = AttachDragPoint(model);
  138. dragPoint.Info = PropInfo.FromModItem(modItem);
  139. AddProp(dragPoint);
  140. return true;
  141. }
  142. public bool AddMyRoomProp(MyRoomItem myRoomItem)
  143. {
  144. var model = LoadMyRoomModel(myRoomItem);
  145. if (!model)
  146. return false;
  147. model.name = Translation.Get("myRoomPropNames", myRoomItem.PrefabName);
  148. var dragPoint = AttachDragPoint(model);
  149. dragPoint.Info = PropInfo.FromMyRoom(myRoomItem);
  150. AddProp(dragPoint);
  151. return true;
  152. }
  153. public bool AddBgProp(string assetName)
  154. {
  155. var model = LoadBgModel(assetName);
  156. if (!model)
  157. return false;
  158. model.name = Translation.Get("bgNames", assetName);
  159. var dragPoint = AttachDragPoint(model);
  160. dragPoint.Info = PropInfo.FromBg(assetName);
  161. AddProp(dragPoint);
  162. return true;
  163. }
  164. public bool AddGameProp(string assetName)
  165. {
  166. var isMenu = assetName.EndsWith(".menu");
  167. var model = isMenu ? LoadMenuModel(assetName) : LoadGameModel(assetName);
  168. if (!model)
  169. return false;
  170. model.name = Translation.Get("propNames", isMenu ? Utility.HandItemToOdogu(assetName) : assetName, !isMenu);
  171. var dragPoint = AttachDragPoint(model);
  172. dragPoint.Info = PropInfo.FromGameProp(assetName);
  173. AddProp(dragPoint);
  174. return true;
  175. }
  176. public void CopyProp(int propIndex)
  177. {
  178. if ((uint)propIndex >= (uint)PropCount)
  179. throw new ArgumentOutOfRangeException(nameof(propIndex));
  180. AddFromPropInfo(propList[propIndex].Info);
  181. }
  182. public void DeleteAllProps()
  183. {
  184. foreach (var prop in propList)
  185. DestroyProp(prop);
  186. propList.Clear();
  187. CurrentPropIndex = 0;
  188. EmitPropListChange();
  189. }
  190. public void RemoveProp(int index)
  191. {
  192. if ((uint)index >= (uint)PropCount)
  193. throw new ArgumentOutOfRangeException(nameof(index));
  194. DestroyProp(propList[index]);
  195. propList.RemoveAt(index);
  196. CurrentPropIndex = Utility.Bound(CurrentPropIndex, 0, PropCount - 1);
  197. EmitPropListChange();
  198. }
  199. public void AttachProp(DragPointProp prop, AttachPoint point, int index)
  200. {
  201. if ((uint)index >= (uint)meidoManager.ActiveMeidoList.Count)
  202. return;
  203. var meido = meidoManager.ActiveMeidoList[index];
  204. prop.AttachTo(meido, point);
  205. }
  206. public void Update()
  207. {
  208. }
  209. public void Activate()
  210. {
  211. CubeSmallChange += OnCubeSmall;
  212. CubeActiveChange += OnCubeActive;
  213. }
  214. public void Deactivate()
  215. {
  216. DeleteAllProps();
  217. CubeSmallChange -= OnCubeSmall;
  218. CubeActiveChange -= OnCubeActive;
  219. }
  220. private DragPointProp AttachDragPoint(GameObject model)
  221. {
  222. var dragPoint = DragPoint.Make<DragPointProp>(PrimitiveType.Cube, Vector3.one * 0.12f);
  223. dragPoint.Initialize(() => model.transform.position, () => Vector3.zero);
  224. dragPoint.Set(model.transform);
  225. dragPoint.AddGizmo(0.45f, CustomGizmo.GizmoMode.World);
  226. dragPoint.ConstantScale = true;
  227. dragPoint.DragPointScale = CubeSmall ? DragPointGeneral.SmallCube : 1f;
  228. dragPoint.Delete += OnDeleteProp;
  229. dragPoint.Select += OnSelectProp;
  230. return dragPoint;
  231. }
  232. private void AddProp(DragPointProp dragPoint)
  233. {
  234. propList.Add(dragPoint);
  235. EmitPropListChange();
  236. }
  237. private void DestroyProp(DragPointProp prop)
  238. {
  239. if (!prop)
  240. return;
  241. prop.Delete -= OnDeleteProp;
  242. prop.Select -= OnSelectProp;
  243. Object.Destroy(prop.gameObject);
  244. }
  245. private void EmitPropListChange() =>
  246. PropListChange?.Invoke(this, EventArgs.Empty);
  247. private void OnBeginCallMeidos(object sender, EventArgs args)
  248. {
  249. foreach (var prop in propList.Where(p => p.AttachPointInfo.AttachPoint is not AttachPoint.None))
  250. prop.DetachTemporary();
  251. }
  252. private void OnEndCallMeidos(object sender, EventArgs args)
  253. {
  254. foreach (var prop in propList.Where(p => p.AttachPointInfo.AttachPoint is not AttachPoint.None))
  255. {
  256. var info = prop.AttachPointInfo;
  257. var meido = meidoManager.GetMeido(info.MaidGuid);
  258. prop.AttachTo(meido, info.AttachPoint, meido is null);
  259. }
  260. }
  261. private void OnDeleteProp(object sender, EventArgs args) =>
  262. RemoveProp(propList.IndexOf((DragPointProp)sender));
  263. private void OnSelectProp(object sender, EventArgs args)
  264. {
  265. CurrentPropIndex = propList.IndexOf((DragPointProp)sender);
  266. FromPropSelect?.Invoke(this, EventArgs.Empty);
  267. }
  268. private void OnCubeSmall(object sender, EventArgs args)
  269. {
  270. foreach (var dragPoint in propList)
  271. dragPoint.DragPointScale = CubeSmall ? DragPointGeneral.SmallCube : 1f;
  272. }
  273. private void OnCubeActive(object sender, EventArgs args)
  274. {
  275. foreach (var dragPoint in propList)
  276. dragPoint.gameObject.SetActive(CubeActive);
  277. }
  278. }