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.

108 lines
2.8 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. }
  35. void Update()
  36. {
  37. bsPlugins.OnUpdate();
  38. ipaPlugins.OnUpdate();
  39. }
  40. void LateUpdate()
  41. {
  42. bsPlugins.OnLateUpdate();
  43. ipaPlugins.OnLateUpdate();
  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. ConfigRuntime.ShutdownRuntime(); // this seems to be needed
  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. }