PropManager.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. namespace COM3D2.MeidoPhotoStudio.Plugin
  7. {
  8. internal class PropManager
  9. {
  10. private MeidoManager meidoManager;
  11. private static bool cubeActive = true;
  12. public static bool CubeActive
  13. {
  14. get => cubeActive;
  15. set
  16. {
  17. if (value != cubeActive)
  18. {
  19. cubeActive = value;
  20. CubeActiveChange?.Invoke(null, EventArgs.Empty);
  21. }
  22. }
  23. }
  24. private static bool cubeSmall;
  25. public static bool CubeSmall
  26. {
  27. get => cubeSmall;
  28. set
  29. {
  30. if (value != cubeSmall)
  31. {
  32. cubeSmall = value;
  33. CubeSmallChange?.Invoke(null, EventArgs.Empty);
  34. }
  35. }
  36. }
  37. private static event EventHandler CubeActiveChange;
  38. private static event EventHandler CubeSmallChange;
  39. private List<DragPointDogu> doguList = new List<DragPointDogu>();
  40. public int DoguCount => doguList.Count;
  41. public event EventHandler DoguListChange;
  42. public string[] PropNameList
  43. {
  44. get
  45. {
  46. return doguList.Count == 0
  47. ? new[] { Translation.Get("systemMessage", "noProps") }
  48. : doguList.Select(dogu => dogu.Name).ToArray();
  49. }
  50. }
  51. public PropManager(MeidoManager meidoManager)
  52. {
  53. this.meidoManager = meidoManager;
  54. this.meidoManager.BeginCallMeidos += DetachProps;
  55. this.meidoManager.EndCallMeidos += ReattachProps;
  56. }
  57. public void Activate()
  58. {
  59. CubeSmallChange += OnCubeSmall;
  60. }
  61. public void Deactivate()
  62. {
  63. foreach (DragPointDogu dogu in doguList)
  64. {
  65. dogu.Delete -= DeleteDogu;
  66. GameObject.Destroy(dogu.gameObject);
  67. }
  68. doguList.Clear();
  69. CubeSmallChange -= OnCubeSmall;
  70. }
  71. private GameObject GetDeploymentObject()
  72. {
  73. return GameObject.Find("Deployment Object Parent")
  74. ?? new GameObject("Deployment Object Parent");
  75. }
  76. public void SpawnModItemProp(MenuFileUtility.ModItem modItem)
  77. {
  78. GameObject dogu = MenuFileUtility.LoadModel(modItem);
  79. string name = modItem.Name;
  80. if (dogu != null) AttachDragPoint(dogu, name, new Vector3(0f, 0f, 0.5f));
  81. // else Debug.Log($"Could not load mod item prop '{modItem.MenuFile}'");
  82. }
  83. public void SpawnMyRoomProp(MenuFileUtility.MyRoomItem item)
  84. {
  85. MyRoomCustom.PlacementData.Data data = MyRoomCustom.PlacementData.GetData(item.ID);
  86. GameObject dogu = GameObject.Instantiate(data.GetPrefab());
  87. string name = Translation.Get("myRoomPropNames", item.PrefabName);
  88. if (dogu != null) AttachDragPoint(dogu, name, new Vector3(0f, 0f, 0.5f));
  89. else Debug.Log($"Could not load MyRoomCreative prop '{item.PrefabName}'");
  90. }
  91. public void SpawnBG(string assetName)
  92. {
  93. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName)
  94. ?? Resources.Load<GameObject>("BG/" + assetName)
  95. ?? Resources.Load<GameObject>("BG/2_0/" + assetName);
  96. if (obj != null)
  97. {
  98. GameObject dogu = GameObject.Instantiate(obj);
  99. string name = Translation.Get("bgNames", assetName);
  100. dogu.transform.localScale = Vector3.one * 0.1f;
  101. AttachDragPoint(dogu, name, Vector3.zero);
  102. }
  103. }
  104. public void SpawnObject(string assetName)
  105. {
  106. // TODO: Add a couple more things to ignore list
  107. GameObject dogu = null;
  108. string doguName = Translation.Get("propNames", assetName, false);
  109. Vector3 doguPosition = new Vector3(0f, 0f, 0.5f);
  110. if (assetName.EndsWith(".menu"))
  111. {
  112. dogu = MenuFileUtility.LoadModel(assetName);
  113. string handItem = Utility.HandItemToOdogu(assetName);
  114. if (Translation.Has("propNames", handItem))
  115. {
  116. doguName = Translation.Get("propNames", handItem);
  117. }
  118. }
  119. else if (assetName.StartsWith("mirror"))
  120. {
  121. Material mirrorMaterial = new Material(Shader.Find("Mirror"));
  122. dogu = GameObject.CreatePrimitive(PrimitiveType.Plane);
  123. Renderer mirrorRenderer = dogu.GetComponent<Renderer>();
  124. mirrorRenderer.material = mirrorMaterial;
  125. mirrorRenderer.enabled = true;
  126. MirrorReflection2 mirrorReflection = dogu.AddComponent<MirrorReflection2>();
  127. mirrorReflection.m_TextureSize = 2048;
  128. Vector3 localPosition = new Vector3(0f, 0.96f, 0f);
  129. dogu.transform.Rotate(dogu.transform.right, 90f);
  130. switch (assetName)
  131. {
  132. case "mirror1":
  133. dogu.transform.localScale = new Vector3(0.2f, 0.4f, 0.2f);
  134. break;
  135. case "mirror2":
  136. dogu.transform.localScale = new Vector3(0.1f, 0.4f, 0.2f);
  137. break;
  138. case "mirror3":
  139. localPosition.y = 0.85f;
  140. dogu.transform.localScale = new Vector3(0.03f, 0.18f, 0.124f);
  141. break;
  142. }
  143. dogu.transform.localPosition = localPosition;
  144. }
  145. else if (assetName.IndexOf(':') >= 0)
  146. {
  147. string[] assetParts = assetName.Split(':');
  148. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetParts[0])
  149. ?? Resources.Load<GameObject>("BG/" + assetParts[0]);
  150. GameObject bg = GameObject.Instantiate(obj);
  151. int num = int.Parse(assetParts[1]);
  152. dogu = bg.transform.GetChild(num).gameObject;
  153. dogu.transform.SetParent(null);
  154. GameObject.Destroy(bg);
  155. bg.SetActive(false);
  156. }
  157. else
  158. {
  159. GameObject obj = GameMain.Instance.BgMgr.CreateAssetBundle(assetName)
  160. ?? Resources.Load<GameObject>("Prefab/" + assetName);
  161. dogu = GameObject.Instantiate<GameObject>(obj);
  162. dogu.transform.localPosition = Vector3.zero;
  163. MeshRenderer[] meshRenderers = dogu.GetComponentsInChildren<MeshRenderer>();
  164. for (int i = 0; i < meshRenderers.Length; i++)
  165. {
  166. if (meshRenderers[i] != null
  167. && meshRenderers[i].gameObject.name.ToLower().IndexOf("castshadow") < 0
  168. )
  169. {
  170. meshRenderers[i].shadowCastingMode = ShadowCastingMode.Off;
  171. }
  172. }
  173. Collider collider = dogu.transform.GetComponent<Collider>();
  174. if (collider != null) collider.enabled = false;
  175. foreach (Transform transform in dogu.transform)
  176. {
  177. collider = transform.GetComponent<Collider>();
  178. if (collider != null)
  179. {
  180. collider.enabled = false;
  181. }
  182. }
  183. #region particle system experiment
  184. // if (asset.StartsWith("Particle/"))
  185. // {
  186. // ParticleSystem particleSystem = go.GetComponent<ParticleSystem>();
  187. // if (particleSystem != null)
  188. // {
  189. // ParticleSystem.MainModule main;
  190. // main = particleSystem.main;
  191. // main.loop = true;
  192. // main.duration = Mathf.Infinity;
  193. // ParticleSystem[] particleSystems = particleSystem.GetComponents<ParticleSystem>();
  194. // foreach (ParticleSystem part in particleSystems)
  195. // {
  196. // ParticleSystem.EmissionModule emissionModule = part.emission;
  197. // ParticleSystem.Burst[] bursts = new ParticleSystem.Burst[emissionModule.burstCount];
  198. // emissionModule.GetBursts(bursts);
  199. // for (int i = 0; i < bursts.Length; i++)
  200. // {
  201. // bursts[i].cycleCount = Int32.MaxValue;
  202. // }
  203. // emissionModule.SetBursts(bursts);
  204. // main = part.main;
  205. // main.loop = true;
  206. // main.duration = Mathf.Infinity;
  207. // }
  208. // }
  209. // }
  210. #endregion
  211. }
  212. if (dogu != null)
  213. {
  214. AttachDragPoint(dogu, doguName, doguPosition);
  215. }
  216. else
  217. {
  218. Debug.LogError($"Could not spawn object '{assetName}'");
  219. }
  220. }
  221. private void AttachDragPoint(GameObject dogu, string name, Vector3 position)
  222. {
  223. // TODO: Figure out why some props aren't centred properly
  224. // Doesn't happen in MM but even after copy pasting the code, it doesn't work :/
  225. GameObject deploymentObject = GetDeploymentObject();
  226. GameObject finalDogu = new GameObject(name);
  227. dogu.transform.SetParent(finalDogu.transform, true);
  228. finalDogu.transform.SetParent(deploymentObject.transform, false);
  229. finalDogu.transform.position = position;
  230. DragPointDogu dragDogu = DragPoint.Make<DragPointDogu>(
  231. PrimitiveType.Cube, Vector3.one * 0.12f, DragPoint.LightBlue
  232. );
  233. dragDogu.Initialize(() => finalDogu.transform.position, () => Vector3.zero);
  234. dragDogu.Set(finalDogu.transform);
  235. dragDogu.AddGizmo(scale: 0.45f, mode: CustomGizmo.GizmoMode.World);
  236. dragDogu.ConstantScale = true;
  237. dragDogu.Delete += DeleteDogu;
  238. dragDogu.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
  239. doguList.Add(dragDogu);
  240. OnDoguListChange();
  241. }
  242. public DragPointDogu GetDogu(int doguIndex)
  243. {
  244. if (doguList.Count == 0 || doguIndex >= doguList.Count || doguIndex < 0) return null;
  245. return doguList[doguIndex];
  246. }
  247. public void AttachProp(
  248. int doguIndex, AttachPoint attachPoint, Meido meido, bool worldPositionStays = true
  249. )
  250. {
  251. if (doguList.Count == 0 || doguIndex >= doguList.Count || doguIndex < 0) return;
  252. AttachProp(doguList[doguIndex], attachPoint, meido, worldPositionStays);
  253. }
  254. private void AttachProp(
  255. DragPointDogu dragDogu, AttachPoint attachPoint, Meido meido, bool worldPositionStays = true
  256. )
  257. {
  258. GameObject dogu = dragDogu.MyGameObject;
  259. Transform attachPointTransform = meido?.GetBoneTransform(attachPoint) ?? GetDeploymentObject().transform;
  260. dragDogu.attachPointInfo = new AttachPointInfo(
  261. attachPoint: meido == null ? AttachPoint.None : attachPoint,
  262. maidGuid: meido == null ? String.Empty : meido.Maid.status.guid,
  263. maidIndex: meido == null ? -1 : meido.ActiveSlot
  264. );
  265. worldPositionStays = meido == null ? true : worldPositionStays;
  266. Vector3 position = dogu.transform.position;
  267. Quaternion rotation = dogu.transform.rotation;
  268. dogu.transform.SetParent(attachPointTransform, worldPositionStays);
  269. if (worldPositionStays)
  270. {
  271. dogu.transform.position = position;
  272. dogu.transform.rotation = rotation;
  273. }
  274. else
  275. {
  276. dogu.transform.localPosition = Vector3.zero;
  277. dogu.transform.rotation = Quaternion.identity;
  278. }
  279. if (meido == null) Utility.FixGameObjectScale(dogu);
  280. }
  281. private void DetachProps(object sender, EventArgs args)
  282. {
  283. foreach (DragPointDogu dogu in doguList)
  284. {
  285. if (dogu.attachPointInfo.AttachPoint != AttachPoint.None)
  286. {
  287. dogu.MyObject.SetParent(GetDeploymentObject().transform, true);
  288. }
  289. }
  290. }
  291. private void ReattachProps(object sender, EventArgs args)
  292. {
  293. foreach (DragPointDogu dragDogu in doguList)
  294. {
  295. Meido meido = this.meidoManager.GetMeido(dragDogu.attachPointInfo.MaidGuid);
  296. bool worldPositionStays = meido == null;
  297. AttachProp(dragDogu, dragDogu.attachPointInfo.AttachPoint, meido, worldPositionStays);
  298. }
  299. }
  300. private void DeleteDogu(object sender, EventArgs args)
  301. {
  302. DragPointDogu dogu = (DragPointDogu)sender;
  303. doguList.RemoveAll(dragDogu =>
  304. {
  305. if (dragDogu == dogu)
  306. {
  307. GameObject.Destroy(dragDogu.gameObject);
  308. return true;
  309. }
  310. return false;
  311. }
  312. );
  313. OnDoguListChange();
  314. }
  315. private void OnCubeSmall(object sender, EventArgs args)
  316. {
  317. foreach (DragPointDogu dogu in doguList)
  318. {
  319. dogu.DragPointScale = CubeSmall ? DragPointGeneral.smallCube : 1f;
  320. }
  321. }
  322. private void OnDoguListChange()
  323. {
  324. this.DoguListChange?.Invoke(this, EventArgs.Empty);
  325. }
  326. }
  327. }