PropManager.cs 22 KB

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