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.

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