PropManager.cs 20 KB

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