You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.0 KiB

  1. using IPA.Config;
  2. using IPA.Loader.Composite;
  3. using System.Diagnostics.CodeAnalysis;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. // ReSharper disable UnusedMember.Local
  7. namespace IPA.Loader
  8. {
  9. [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
  10. internal class PluginComponent : MonoBehaviour
  11. {
  12. private CompositeBSPlugin bsPlugins;
  13. private CompositeIPAPlugin ipaPlugins;
  14. private bool quitting;
  15. internal static PluginComponent Create()
  16. {
  17. return new GameObject("IPA_PluginManager").AddComponent<PluginComponent>();
  18. }
  19. void Awake()
  20. {
  21. DontDestroyOnLoad(gameObject);
  22. PluginManager.Load();
  23. bsPlugins = new CompositeBSPlugin(PluginManager.BSMetas);
  24. #pragma warning disable 618
  25. ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins);
  26. #pragma warning restore 618
  27. #if NET4
  28. gameObject.AddComponent<Updating.BeatMods.Updater>();
  29. #endif
  30. #pragma warning disable CS0612 // Type or member is obsolete
  31. bsPlugins.OnApplicationStart();
  32. #pragma warning restore CS0612 // Type or member is obsolete
  33. ipaPlugins.OnApplicationStart();
  34. SceneManager.activeSceneChanged += OnActiveSceneChanged;
  35. SceneManager.sceneLoaded += OnSceneLoaded;
  36. SceneManager.sceneUnloaded += OnSceneUnloaded;
  37. Config.Config.Save();
  38. }
  39. void Update()
  40. {
  41. bsPlugins.OnUpdate();
  42. ipaPlugins.OnUpdate();
  43. Config.Config.Update();
  44. }
  45. void LateUpdate()
  46. {
  47. bsPlugins.OnLateUpdate();
  48. ipaPlugins.OnLateUpdate();
  49. //Config.Config.Update();
  50. }
  51. void FixedUpdate()
  52. {
  53. bsPlugins.OnFixedUpdate();
  54. ipaPlugins.OnFixedUpdate();
  55. }
  56. void OnDestroy()
  57. {
  58. if (!quitting)
  59. {
  60. Create();
  61. }
  62. }
  63. void OnApplicationQuit()
  64. {
  65. SceneManager.activeSceneChanged -= OnActiveSceneChanged;
  66. SceneManager.sceneLoaded -= OnSceneLoaded;
  67. SceneManager.sceneUnloaded -= OnSceneUnloaded;
  68. bsPlugins.OnApplicationQuit();
  69. ipaPlugins.OnApplicationQuit();
  70. Config.Config.Save();
  71. quitting = true;
  72. }
  73. void OnLevelWasLoaded(int level)
  74. {
  75. ipaPlugins.OnLevelWasLoaded(level);
  76. }
  77. void OnLevelWasInitialized(int level)
  78. {
  79. ipaPlugins.OnLevelWasInitialized(level);
  80. }
  81. void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  82. {
  83. bsPlugins.OnSceneLoaded(scene, sceneMode);
  84. }
  85. private void OnSceneUnloaded(Scene scene) {
  86. bsPlugins.OnSceneUnloaded(scene);
  87. }
  88. private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  89. bsPlugins.OnActiveSceneChanged(prevScene, nextScene);
  90. }
  91. }
  92. }