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.

133 lines
3.8 KiB

  1. using IPA.Config;
  2. using IPA.Loader.Composite;
  3. using IPA.Utilities;
  4. using IPA.Utilities.Async;
  5. using System.Diagnostics.CodeAnalysis;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. // ReSharper disable UnusedMember.Local
  9. namespace IPA.Loader
  10. {
  11. [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
  12. internal class PluginComponent : MonoBehaviour
  13. {
  14. private CompositeBSPlugin bsPlugins;
  15. private CompositeIPAPlugin ipaPlugins;
  16. private bool quitting;
  17. public static PluginComponent Instance;
  18. private static bool initialized = false;
  19. internal static PluginComponent Create()
  20. {
  21. return Instance = new GameObject("IPA_PluginManager").AddComponent<PluginComponent>();
  22. }
  23. internal void Awake()
  24. {
  25. DontDestroyOnLoad(gameObject);
  26. if (!initialized)
  27. {
  28. UnityGame.SetMainThread();
  29. UnityGame.EnsureRuntimeGameVersion();
  30. PluginManager.Load();
  31. bsPlugins = new CompositeBSPlugin(PluginManager.BSMetas);
  32. #pragma warning disable 618
  33. ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins);
  34. #pragma warning restore 618
  35. #if BeatSaber // TODO: remove this
  36. gameObject.AddComponent<Updating.BeatMods.Updater>();
  37. #endif
  38. bsPlugins.OnEnable();
  39. ipaPlugins.OnApplicationStart();
  40. SceneManager.activeSceneChanged += OnActiveSceneChanged;
  41. SceneManager.sceneLoaded += OnSceneLoaded;
  42. SceneManager.sceneUnloaded += OnSceneUnloaded;
  43. var unitySched = UnityMainThreadTaskScheduler.Default as UnityMainThreadTaskScheduler;
  44. if (!unitySched.IsRunning)
  45. StartCoroutine(unitySched.Coroutine());
  46. initialized = true;
  47. #if DEBUG
  48. Config.Stores.GeneratedStoreImpl.DebugSaveAssembly("GeneratedAssembly.dll");
  49. #endif
  50. }
  51. }
  52. internal void Update()
  53. {
  54. bsPlugins.OnUpdate();
  55. ipaPlugins.OnUpdate();
  56. var unitySched = UnityMainThreadTaskScheduler.Default as UnityMainThreadTaskScheduler;
  57. if (!unitySched.IsRunning)
  58. StartCoroutine(unitySched.Coroutine());
  59. }
  60. internal void LateUpdate()
  61. {
  62. bsPlugins.OnLateUpdate();
  63. ipaPlugins.OnLateUpdate();
  64. }
  65. internal void FixedUpdate()
  66. {
  67. bsPlugins.OnFixedUpdate();
  68. ipaPlugins.OnFixedUpdate();
  69. }
  70. internal void OnDestroy()
  71. {
  72. if (!quitting)
  73. {
  74. Create();
  75. }
  76. }
  77. internal void OnApplicationQuit()
  78. {
  79. SceneManager.activeSceneChanged -= OnActiveSceneChanged;
  80. SceneManager.sceneLoaded -= OnSceneLoaded;
  81. SceneManager.sceneUnloaded -= OnSceneUnloaded;
  82. bsPlugins.OnApplicationQuit();
  83. ipaPlugins.OnApplicationQuit();
  84. ConfigRuntime.ShutdownRuntime(); // this seems to be needed
  85. quitting = true;
  86. }
  87. internal void OnLevelWasLoaded(int level)
  88. {
  89. ipaPlugins.OnLevelWasLoaded(level);
  90. }
  91. internal void OnLevelWasInitialized(int level)
  92. {
  93. ipaPlugins.OnLevelWasInitialized(level);
  94. }
  95. private void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  96. {
  97. bsPlugins.OnSceneLoaded(scene, sceneMode);
  98. }
  99. private void OnSceneUnloaded(Scene scene) {
  100. bsPlugins.OnSceneUnloaded(scene);
  101. }
  102. private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  103. bsPlugins.OnActiveSceneChanged(prevScene, nextScene);
  104. }
  105. }
  106. }