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.

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