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.

110 lines
2.8 KiB

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