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.

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