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.

132 lines
3.6 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. private static bool initialized = false;
  18. internal static PluginComponent Create()
  19. {
  20. return new GameObject("IPA_PluginManager").AddComponent<PluginComponent>();
  21. }
  22. void Awake()
  23. {
  24. DontDestroyOnLoad(gameObject);
  25. if (!initialized)
  26. {
  27. UnityGame.SetMainThread();
  28. UnityGame.EnsureRuntimeGameVersion();
  29. PluginManager.Load();
  30. bsPlugins = new CompositeBSPlugin(PluginManager.BSMetas);
  31. #pragma warning disable 618
  32. ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins);
  33. #pragma warning restore 618
  34. #if BeatSaber
  35. gameObject.AddComponent<Updating.BeatMods.Updater>();
  36. #endif
  37. bsPlugins.OnEnable();
  38. ipaPlugins.OnApplicationStart();
  39. SceneManager.activeSceneChanged += OnActiveSceneChanged;
  40. SceneManager.sceneLoaded += OnSceneLoaded;
  41. SceneManager.sceneUnloaded += OnSceneUnloaded;
  42. var unitySched = UnityMainThreadTaskScheduler.Default as UnityMainThreadTaskScheduler;
  43. if (!unitySched.IsRunning)
  44. StartCoroutine(unitySched.Coroutine());
  45. initialized = true;
  46. #if DEBUG
  47. Config.Stores.GeneratedStoreImpl.DebugSaveAssembly("GeneratedAssembly.dll");
  48. #endif
  49. }
  50. }
  51. void Update()
  52. {
  53. bsPlugins.OnUpdate();
  54. ipaPlugins.OnUpdate();
  55. var unitySched = UnityMainThreadTaskScheduler.Default as UnityMainThreadTaskScheduler;
  56. if (!unitySched.IsRunning)
  57. StartCoroutine(unitySched.Coroutine());
  58. }
  59. void LateUpdate()
  60. {
  61. bsPlugins.OnLateUpdate();
  62. ipaPlugins.OnLateUpdate();
  63. }
  64. void FixedUpdate()
  65. {
  66. bsPlugins.OnFixedUpdate();
  67. ipaPlugins.OnFixedUpdate();
  68. }
  69. void OnDestroy()
  70. {
  71. if (!quitting)
  72. {
  73. Create();
  74. }
  75. }
  76. void OnApplicationQuit()
  77. {
  78. SceneManager.activeSceneChanged -= OnActiveSceneChanged;
  79. SceneManager.sceneLoaded -= OnSceneLoaded;
  80. SceneManager.sceneUnloaded -= OnSceneUnloaded;
  81. bsPlugins.OnApplicationQuit();
  82. ipaPlugins.OnApplicationQuit();
  83. ConfigRuntime.ShutdownRuntime(); // this seems to be needed
  84. quitting = true;
  85. }
  86. void OnLevelWasLoaded(int level)
  87. {
  88. ipaPlugins.OnLevelWasLoaded(level);
  89. }
  90. void OnLevelWasInitialized(int level)
  91. {
  92. ipaPlugins.OnLevelWasInitialized(level);
  93. }
  94. void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  95. {
  96. bsPlugins.OnSceneLoaded(scene, sceneMode);
  97. }
  98. private void OnSceneUnloaded(Scene scene) {
  99. bsPlugins.OnSceneUnloaded(scene);
  100. }
  101. private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  102. bsPlugins.OnActiveSceneChanged(prevScene, nextScene);
  103. }
  104. }
  105. }