PropManager.cs 21 KB

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