PropManager.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.Rendering;
  7. using BepInEx.Configuration;
  8. namespace COM3D2.MeidoPhotoStudio.Plugin
  9. {
  10. using static MenuFileUtility;
  11. internal class PropManager : IManager, ISerializable
  12. {
  13. public const string header = "PROP";
  14. private static readonly ConfigEntry<bool> modItemsOnly;
  15. public static bool ModItemsOnly => modItemsOnly.Value;
  16. private MeidoManager meidoManager;
  17. private static bool cubeActive = true;
  18. public static bool CubeActive
  19. {
  20. get => cubeActive;
  21. set
  22. {
  23. if (value != cubeActive)
  24. {
  25. cubeActive = value;
  26. CubeActiveChange?.Invoke(null, EventArgs.Empty);
  27. }
  28. }
  29. }
  30. private static bool cubeSmall;
  31. public static bool CubeSmall
  32. {
  33. get => cubeSmall;
  34. set
  35. {
  36. if (value != cubeSmall)
  37. {
  38. cubeSmall = value;
  39. CubeSmallChange?.Invoke(null, EventArgs.Empty);
  40. }
  41. }
  42. }
  43. private static event EventHandler CubeActiveChange;
  44. private static event EventHandler CubeSmallChange;
  45. private List<DragPointDogu> doguList = new List<DragPointDogu>();
  46. public int DoguCount => doguList.Count;
  47. public event EventHandler DoguListChange;
  48. public event EventHandler DoguSelectChange;
  49. public string[] PropNameList
  50. {
  51. get
  52. {
  53. return doguList.Count == 0
  54. ? new[] { Translation.Get("systemMessage", "noProps") }
  55. : doguList.Select(dogu => dogu.Name).ToArray();
  56. }
  57. }
  58. public int CurrentDoguIndex { get; private set; } = 0;
  59. public DragPointDogu CurrentDogu => DoguCount == 0 ? null : doguList[CurrentDoguIndex];
  60. static PropManager()
  61. {
  62. modItemsOnly = Configuration.Config.Bind<bool>(
  63. "Prop", "ModItemsOnly",
  64. false,
  65. "Disable waiting for and loading base game clothing"
  66. );
  67. }
  68. public PropManager(MeidoManager meidoManager)
  69. {
  70. this.meidoManager = meidoManager;
  71. this.meidoManager.BeginCallMeidos += DetachProps;
  72. this.meidoManager.EndCallMeidos += OnEndCall;
  73. }
  74. public void Serialize(BinaryWriter binaryWriter)
  75. {
  76. binaryWriter.Write(header);
  77. binaryWriter.Write(doguList.Count);
  78. foreach (DragPointDogu dogu in doguList)
  79. {
  80. binaryWriter.Write(dogu.assetName);
  81. AttachPointInfo info = dogu.attachPointInfo;
  82. info.Serialize(binaryWriter);
  83. binaryWriter.WriteVector3(dogu.MyObject.position);
  84. binaryWriter.WriteQuaternion(dogu.MyObject.rotation);
  85. binaryWriter.WriteVector3(dogu.MyObject.localScale);
  86. }
  87. }
  88. public void Deserialize(BinaryReader binaryReader)
  89. {
  90. Dictionary<string, string> modToModPath = null;
  91. ClearDogu();
  92. int numberOfProps = binaryReader.ReadInt32();
  93. for (int i = 0; i < numberOfProps; i++)
  94. {
  95. string assetName = binaryReader.ReadString();
  96. bool result = false;
  97. if (assetName.EndsWith(".menu") && assetName.Contains('#') && modToModPath == null)
  98. {
  99. modToModPath = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
  100. foreach (string mod in Menu.GetModFiles()) modToModPath.Add(Path.GetFileName(mod), mod);
  101. }
  102. result = SpawnFromAssetString(assetName, modToModPath);
  103. AttachPointInfo info = AttachPointInfo.Deserialize(binaryReader);
  104. Vector3 position = binaryReader.ReadVector3();
  105. Quaternion rotation = binaryReader.ReadQuaternion();
  106. Vector3 scale = binaryReader.ReadVector3();
  107. if (result)
  108. {
  109. DragPointDogu dogu = doguList[i];
  110. Transform obj = dogu.MyObject;
  111. obj.position = position;
  112. obj.rotation = rotation;
  113. obj.localScale = scale;
  114. dogu.attachPointInfo = info;
  115. }
  116. }
  117. CurrentDoguIndex = 0;
  118. GameMain.Instance.StartCoroutine(DeserializeAttach());
  119. }
  120. private System.Collections.IEnumerator DeserializeAttach()
  121. {
  122. yield return new WaitForEndOfFrame();
  123. foreach (DragPointDogu dogu in doguList)
  124. {
  125. AttachPointInfo info = dogu.attachPointInfo;
  126. if (info.AttachPoint != AttachPoint.None)
  127. {
  128. Meido parent = meidoManager.GetMeido(info.MaidIndex);
  129. if (parent != null)
  130. {
  131. Transform obj = dogu.MyObject;
  132. Vector3 position = obj.position;
  133. Vector3 scale = obj.localScale;
  134. Quaternion rotation = obj.rotation;
  135. Transform point = parent.IKManager.GetAttachPointTransform(info.AttachPoint);
  136. dogu.MyObject.SetParent(point, true);
  137. info = new AttachPointInfo(
  138. info.AttachPoint,
  139. parent.Maid.status.guid,
  140. parent.Slot
  141. );
  142. dogu.attachPointInfo = info;
  143. obj.position = position;
  144. obj.localScale = scale;
  145. obj.rotation = rotation;
  146. }
  147. }
  148. }
  149. }
  150. public void Activate()
  151. {
  152. CubeSmallChange += OnCubeSmall;
  153. CubeActiveChange += OnCubeActive;
  154. }
  155. public void Deactivate()
  156. {
  157. ClearDogu();
  158. CubeSmallChange -= OnCubeSmall;
  159. CubeActiveChange -= OnCubeActive;
  160. }
  161. public void Update() { }
  162. private GameObject GetDeploymentObject()
  163. {
  164. return GameObject.Find("Deployment Object Parent")
  165. ?? new GameObject("Deployment Object Parent");
  166. }
  167. public bool SpawnModItemProp(ModItem modItem)
  168. {
  169. GameObject dogu = MenuFileUtility.LoadModel(modItem);
  170. string name = modItem.MenuFile;
  171. if (modItem.IsOfficialMod) name = Path.GetFileName(name);
  172. if (dogu != null) AttachDragPoint(dogu, modItem.ToString(), name, new Vector3(0f, 0f, 0.5f));
  173. return dogu != null;
  174. }
  175. public bool SpawnMyRoomProp(MyRoomItem item)
  176. {
  177. MyRoomCustom.PlacementData.Data data = MyRoomCustom.PlacementData.GetData(item.ID);
  178. GameObject dogu = GameObject.Instantiate(data.GetPrefab());
  179. string name = Translation.Get("myRoomPropNames", item.PrefabName);
  180. if (dogu != null)
  181. {
  182. GameObject finalDogu = new GameObject();
  183. dogu.transform.SetParent(finalDogu.transform, true);
  184. finalDogu.transform.SetParent(GetDeploymentObject().transform, false);
  185. AttachDragPoint(finalDogu, item.ToString(), name, new Vector3(0f, 0f, 0.5f));
  186. }
  187. else Utility.LogInfo($"Could not load MyRoomCreative prop '{item.PrefabName}'");
  188. return dogu != null;
  189. }
  190. public bool SpawnBG(string assetName)
  191. {
  192. if (assetName.StartsWith("BG_")) assetName = assetName.Substring(3);
  193. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName)
  194. ?? Resources.Load<GameObject>("BG/" + assetName)
  195. ?? Resources.Load<GameObject>("BG/2_0/" + assetName);
  196. if (obj != null)
  197. {
  198. GameObject dogu = GameObject.Instantiate(obj);
  199. string name = Translation.Get("bgNames", assetName);
  200. dogu.transform.localScale = Vector3.one * 0.1f;
  201. AttachDragPoint(dogu, $"BG_{assetName}", name, new Vector3(0f, 0f, 0.5f));
  202. }
  203. return obj != null;
  204. }
  205. public bool SpawnObject(string assetName)
  206. {
  207. // TODO: Add a couple more things to ignore list
  208. GameObject dogu = null;
  209. string doguName = Translation.Get("propNames", assetName, false);
  210. Vector3 doguPosition = new Vector3(0f, 0f, 0.5f);
  211. if (assetName.EndsWith(".menu"))
  212. {
  213. dogu = MenuFileUtility.LoadModel(assetName);
  214. string handItem = Utility.HandItemToOdogu(assetName);
  215. if (Translation.Has("propNames", handItem)) doguName = Translation.Get("propNames", handItem);
  216. }
  217. else if (assetName.StartsWith("mirror"))
  218. {
  219. Material mirrorMaterial = new Material(Shader.Find("Mirror"));
  220. dogu = GameObject.CreatePrimitive(PrimitiveType.Plane);
  221. Renderer mirrorRenderer = dogu.GetComponent<Renderer>();
  222. mirrorRenderer.material = mirrorMaterial;
  223. mirrorRenderer.enabled = true;
  224. MirrorReflection2 mirrorReflection = dogu.AddComponent<MirrorReflection2>();
  225. mirrorReflection.m_TextureSize = 2048;
  226. Vector3 localPosition = new Vector3(0f, 0.96f, 0f);
  227. dogu.transform.Rotate(dogu.transform.right, 90f);
  228. switch (assetName)
  229. {
  230. case "mirror1":
  231. dogu.transform.localScale = new Vector3(0.2f, 0.4f, 0.2f);
  232. break;
  233. case "mirror2":
  234. dogu.transform.localScale = new Vector3(0.1f, 0.4f, 0.2f);
  235. break;
  236. case "mirror3":
  237. localPosition.y = 0.85f;
  238. dogu.transform.localScale = new Vector3(0.03f, 0.18f, 0.124f);
  239. break;
  240. }
  241. dogu.transform.localPosition = localPosition;
  242. }
  243. else if (assetName.IndexOf(':') >= 0)
  244. {
  245. string[] assetParts = assetName.Split(':');
  246. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetParts[0])
  247. ?? Resources.Load<GameObject>("BG/" + assetParts[0]);
  248. try
  249. {
  250. GameObject bg = GameObject.Instantiate(obj);
  251. int num = int.Parse(assetParts[1]);
  252. dogu = bg.transform.GetChild(num).gameObject;
  253. dogu.transform.SetParent(null);
  254. GameObject.Destroy(bg);
  255. }
  256. catch { }
  257. }
  258. else
  259. {
  260. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName)
  261. ?? Resources.Load<GameObject>("Prefab/" + assetName);
  262. try
  263. {
  264. dogu = GameObject.Instantiate<GameObject>(obj);
  265. dogu.transform.localPosition = Vector3.zero;
  266. MeshRenderer[] meshRenderers = dogu.GetComponentsInChildren<MeshRenderer>();
  267. for (int i = 0; i < meshRenderers.Length; i++)
  268. {
  269. if (meshRenderers[i] != null
  270. && meshRenderers[i].gameObject.name.ToLower().IndexOf("castshadow") < 0
  271. ) meshRenderers[i].shadowCastingMode = ShadowCastingMode.Off;
  272. }
  273. Collider collider = dogu.transform.GetComponent<Collider>();
  274. if (collider != null) collider.enabled = false;
  275. foreach (Transform transform in dogu.transform)
  276. {
  277. collider = transform.GetComponent<Collider>();
  278. if (collider != null)
  279. {
  280. collider.enabled = false;
  281. }
  282. }
  283. }
  284. catch { }
  285. #region particle system experiment
  286. // if (asset.StartsWith("Particle/"))
  287. // {
  288. // ParticleSystem particleSystem = go.GetComponent<ParticleSystem>();
  289. // if (particleSystem != null)
  290. // {
  291. // ParticleSystem.MainModule main;
  292. // main = particleSystem.main;
  293. // main.loop = true;
  294. // main.duration = Mathf.Infinity;
  295. // ParticleSystem[] particleSystems = particleSystem.GetComponents<ParticleSystem>();
  296. // foreach (ParticleSystem part in particleSystems)
  297. // {
  298. // ParticleSystem.EmissionModule emissionModule = part.emission;
  299. // ParticleSystem.Burst[] bursts = new ParticleSystem.Burst[emissionModule.burstCount];
  300. // emissionModule.GetBursts(bursts);
  301. // for (int i = 0; i < bursts.Length; i++)
  302. // {
  303. // bursts[i].cycleCount = Int32.MaxValue;
  304. // }
  305. // emissionModule.SetBursts(bursts);
  306. // main = part.main;
  307. // main.loop = true;
  308. // main.duration = Mathf.Infinity;
  309. // }
  310. // }
  311. // }
  312. #endregion
  313. }
  314. if (dogu != null)
  315. {
  316. AttachDragPoint(dogu, assetName, doguName, doguPosition);
  317. return true;
  318. }
  319. else
  320. {
  321. Utility.LogInfo($"Could not spawn object '{assetName}'");
  322. }
  323. return false;
  324. }
  325. private bool SpawnFromAssetString(string assetName, Dictionary<string, string> modDict = null)
  326. {
  327. bool result = false;
  328. if (assetName.EndsWith(".menu"))
  329. {
  330. if (assetName.Contains('#'))
  331. {
  332. string[] assetParts = assetName.Split('#');
  333. string menuFile = modDict == null ? Menu.GetModPathFileName(assetParts[0]) : modDict[assetParts[0]];
  334. ModItem item = ModItem.OfficialMod(menuFile);
  335. item.BaseMenuFile = assetParts[1];
  336. result = SpawnModItemProp(item);
  337. }
  338. else if (assetName.StartsWith("handitem")) result = SpawnObject(assetName);
  339. else result = SpawnModItemProp(ModItem.Mod(assetName));
  340. }
  341. else if (assetName.StartsWith("MYR_"))
  342. {
  343. string[] assetParts = assetName.Split('#');
  344. int id = int.Parse(assetParts[0].Substring(4));
  345. string prefabName;
  346. if (assetParts.Length == 2 && !string.IsNullOrEmpty(assetParts[1])) prefabName = assetParts[1];
  347. else
  348. {
  349. // deserialize modifiedMM and maybe MM 23.0+.
  350. MyRoomCustom.PlacementData.Data data = MyRoomCustom.PlacementData.GetData(id);
  351. prefabName = !string.IsNullOrEmpty(data.resourceName) ? data.resourceName : data.assetName;
  352. }
  353. result = SpawnMyRoomProp(new MyRoomItem() { ID = id, PrefabName = prefabName });
  354. }
  355. else if (assetName.StartsWith("BG_")) result = SpawnBG(assetName);
  356. else result = SpawnObject(assetName);
  357. return result;
  358. }
  359. private void AttachDragPoint(GameObject dogu, string assetName, string name, Vector3 position)
  360. {
  361. // TODO: Figure out why some props aren't centred properly
  362. // Doesn't happen in MM but even after copy pasting the code, it doesn't work :/
  363. dogu.name = name;
  364. dogu.transform.position = position;
  365. DragPointDogu dragDogu = DragPoint.Make<DragPointDogu>(
  366. PrimitiveType.Cube, Vector3.one * 0.12f, DragPoint.LightBlue
  367. );
  368. dragDogu.Initialize(() => dogu.transform.position, () => Vector3.zero);
  369. dragDogu.Set(dogu.transform);
  370. dragDogu.AddGizmo(scale: 0.45f, mode: CustomGizmo.GizmoMode.World);
  371. dragDogu.ConstantScale = true;
  372. dragDogu.Delete += DeleteDogu;
  373. dragDogu.Select += SelectDogu;
  374. dragDogu.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
  375. dragDogu.assetName = assetName;
  376. doguList.Add(dragDogu);
  377. OnDoguListChange();
  378. }
  379. public void SetCurrentDogu(int doguIndex)
  380. {
  381. if (doguIndex >= 0 && doguIndex < DoguCount)
  382. {
  383. this.CurrentDoguIndex = doguIndex;
  384. this.DoguSelectChange?.Invoke(this, EventArgs.Empty);
  385. }
  386. }
  387. public void RemoveDogu(int doguIndex)
  388. {
  389. if (doguIndex >= 0 && doguIndex < DoguCount)
  390. {
  391. DestroyDogu(doguList[doguIndex]);
  392. doguList.RemoveAt(doguIndex);
  393. CurrentDoguIndex = Utility.Bound(CurrentDoguIndex, 0, DoguCount - 1);
  394. OnDoguListChange();
  395. }
  396. }
  397. public void CopyDogu(int doguIndex)
  398. {
  399. if (doguIndex >= 0 && doguIndex < DoguCount)
  400. {
  401. SpawnFromAssetString(doguList[doguIndex].assetName);
  402. }
  403. }
  404. public void AttachProp(
  405. int doguIndex, AttachPoint attachPoint, Meido meido, bool worldPositionStays = true
  406. )
  407. {
  408. if (doguList.Count == 0 || doguIndex >= doguList.Count || doguIndex < 0) return;
  409. AttachProp(doguList[doguIndex], attachPoint, meido, worldPositionStays);
  410. }
  411. private void AttachProp(
  412. DragPointDogu dragDogu, AttachPoint attachPoint, Meido meido, bool worldPositionStays = true
  413. )
  414. {
  415. GameObject dogu = dragDogu.MyGameObject;
  416. Transform attachPointTransform = meido?.IKManager.GetAttachPointTransform(attachPoint);
  417. // ?? GetDeploymentObject().transform;
  418. dragDogu.attachPointInfo = new AttachPointInfo(
  419. attachPoint: meido == null ? AttachPoint.None : attachPoint,
  420. maidGuid: meido == null ? String.Empty : meido.Maid.status.guid,
  421. maidIndex: meido == null ? -1 : meido.Slot
  422. );
  423. Vector3 position = dogu.transform.position;
  424. Quaternion rotation = dogu.transform.rotation;
  425. Vector3 scale = dogu.transform.localScale;
  426. dogu.transform.SetParent(attachPointTransform, worldPositionStays);
  427. if (worldPositionStays)
  428. {
  429. dogu.transform.position = position;
  430. dogu.transform.rotation = rotation;
  431. }
  432. else
  433. {
  434. dogu.transform.localPosition = Vector3.zero;
  435. dogu.transform.rotation = Quaternion.identity;
  436. }
  437. dogu.transform.localScale = scale;
  438. if (meido == null) Utility.FixGameObjectScale(dogu);
  439. }
  440. private void DetachProps(object sender, EventArgs args)
  441. {
  442. foreach (DragPointDogu dogu in doguList)
  443. {
  444. if (dogu.attachPointInfo.AttachPoint != AttachPoint.None)
  445. {
  446. dogu.MyObject.SetParent(null, /*GetDeploymentObject().transform*/ true);
  447. }
  448. }
  449. }
  450. private void ClearDogu()
  451. {
  452. for (int i = DoguCount - 1; i >= 0; i--)
  453. {
  454. DestroyDogu(doguList[i]);
  455. }
  456. doguList.Clear();
  457. CurrentDoguIndex = 0;
  458. }
  459. private void OnEndCall(object sender, EventArgs args) => ReattachProps(useGuid: true);
  460. private void ReattachProps(bool useGuid, bool forceStay = false)
  461. {
  462. foreach (DragPointDogu dragDogu in doguList)
  463. {
  464. AttachPointInfo info = dragDogu.attachPointInfo;
  465. Meido meido = useGuid
  466. ? this.meidoManager.GetMeido(info.MaidGuid)
  467. : this.meidoManager.GetMeido(info.MaidIndex);
  468. bool worldPositionStays = forceStay || meido == null;
  469. AttachProp(dragDogu, dragDogu.attachPointInfo.AttachPoint, meido, worldPositionStays);
  470. }
  471. }
  472. private void DeleteDogu(object sender, EventArgs args)
  473. {
  474. DragPointDogu dogu = (DragPointDogu)sender;
  475. RemoveDogu(doguList.FindIndex(dragDogu => dragDogu == dogu));
  476. }
  477. private void DestroyDogu(DragPointDogu dogu)
  478. {
  479. if (dogu == null) return;
  480. dogu.Delete -= DeleteDogu;
  481. dogu.Select -= SelectDogu;
  482. GameObject.Destroy(dogu.gameObject);
  483. }
  484. private void SelectDogu(object sender, EventArgs args)
  485. {
  486. DragPointDogu dogu = (DragPointDogu)sender;
  487. SetCurrentDogu(doguList.IndexOf(dogu));
  488. }
  489. private void OnCubeSmall(object sender, EventArgs args)
  490. {
  491. foreach (DragPointDogu dogu in doguList)
  492. {
  493. dogu.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
  494. }
  495. }
  496. private void OnCubeActive(object sender, EventArgs args)
  497. {
  498. foreach (DragPointDogu dragPoint in doguList)
  499. {
  500. dragPoint.gameObject.SetActive(CubeActive);
  501. }
  502. }
  503. private void OnDoguListChange()
  504. {
  505. this.DoguListChange?.Invoke(this, EventArgs.Empty);
  506. }
  507. }
  508. }