PropManager.cs 14 KB

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