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.

115 lines
2.9 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. bsPlugins.OnApplicationStart();
  31. ipaPlugins.OnApplicationStart();
  32. SceneManager.activeSceneChanged += OnActiveSceneChanged;
  33. SceneManager.sceneLoaded += OnSceneLoaded;
  34. SceneManager.sceneUnloaded += OnSceneUnloaded;
  35. Config.Config.Save();
  36. }
  37. void Update()
  38. {
  39. bsPlugins.OnUpdate();
  40. ipaPlugins.OnUpdate();
  41. Config.Config.Update();
  42. }
  43. void LateUpdate()
  44. {
  45. bsPlugins.OnLateUpdate();
  46. ipaPlugins.OnLateUpdate();
  47. //Config.Config.Update();
  48. }
  49. void FixedUpdate()
  50. {
  51. bsPlugins.OnFixedUpdate();
  52. ipaPlugins.OnFixedUpdate();
  53. }
  54. void OnDestroy()
  55. {
  56. if (!quitting)
  57. {
  58. Create();
  59. }
  60. }
  61. void OnApplicationQuit()
  62. {
  63. SceneManager.activeSceneChanged -= OnActiveSceneChanged;
  64. SceneManager.sceneLoaded -= OnSceneLoaded;
  65. SceneManager.sceneUnloaded -= OnSceneUnloaded;
  66. bsPlugins.OnApplicationQuit();
  67. ipaPlugins.OnApplicationQuit();
  68. Config.Config.Save();
  69. quitting = true;
  70. }
  71. void OnLevelWasLoaded(int level)
  72. {
  73. ipaPlugins.OnLevelWasLoaded(level);
  74. }
  75. void OnLevelWasInitialized(int level)
  76. {
  77. ipaPlugins.OnLevelWasInitialized(level);
  78. }
  79. void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  80. {
  81. bsPlugins.OnSceneLoaded(scene, sceneMode);
  82. }
  83. private void OnSceneUnloaded(Scene scene) {
  84. bsPlugins.OnSceneUnloaded(scene);
  85. }
  86. private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  87. bsPlugins.OnActiveSceneChanged(prevScene, nextScene);
  88. }
  89. }
  90. }