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.

155 lines
4.6 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using IPA.Loader;
  7. using IPA.Loader.Composite;
  8. using IPA.Logging;
  9. namespace IPA.Loader
  10. {
  11. internal class PluginComponent : MonoBehaviour
  12. {
  13. private CompositeBSPlugin bsPlugins;
  14. private CompositeIPAPlugin ipaPlugins;
  15. private bool quitting = false;
  16. internal static PluginComponent Create()
  17. {
  18. return new GameObject("IPA_PluginManager").AddComponent<PluginComponent>();
  19. }
  20. void Awake()
  21. {
  22. DontDestroyOnLoad(gameObject);
  23. bsPlugins = new CompositeBSPlugin(PluginManager.BSPlugins);
  24. #pragma warning disable CS0618 // Type or member is obsolete
  25. ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins);
  26. #pragma warning restore CS0618 // Type or member is obsolete
  27. gameObject.AddComponent<Updating.ModsaberML.Updater>();
  28. bsPlugins.OnApplicationStart();
  29. ipaPlugins.OnApplicationStart();
  30. SceneManager.activeSceneChanged += OnActiveSceneChanged;
  31. SceneManager.sceneLoaded += OnSceneLoaded;
  32. SceneManager.sceneUnloaded += OnSceneUnloaded;
  33. foreach (var provider in PluginManager.configProviders)
  34. if (provider.Key.HasChanged)
  35. try
  36. {
  37. provider.Key.Save();
  38. }
  39. catch (Exception e)
  40. {
  41. Logging.Logger.log.Error("Error when trying to save config");
  42. Logging.Logger.log.Error(e);
  43. }
  44. }
  45. void Update()
  46. {
  47. bsPlugins.OnUpdate();
  48. ipaPlugins.OnUpdate();
  49. }
  50. void LateUpdate()
  51. {
  52. bsPlugins.OnLateUpdate();
  53. ipaPlugins.OnLateUpdate();
  54. foreach (var provider in PluginManager.configProviders)
  55. {
  56. if (provider.Key.HasChanged)
  57. try
  58. {
  59. provider.Key.Save();
  60. }
  61. catch (Exception e)
  62. {
  63. Logging.Logger.log.Error("Error when trying to save config");
  64. Logging.Logger.log.Error(e);
  65. }
  66. else if (provider.Key.LastModified > provider.Value.Value)
  67. {
  68. try
  69. {
  70. provider.Key.Load(); // auto reload if it changes
  71. provider.Value.Value = provider.Key.LastModified;
  72. }
  73. catch (Exception e)
  74. {
  75. Logging.Logger.log.Error("Error when trying to load config");
  76. Logging.Logger.log.Error(e);
  77. }
  78. }
  79. }
  80. }
  81. void FixedUpdate()
  82. {
  83. bsPlugins.OnFixedUpdate();
  84. ipaPlugins.OnFixedUpdate();
  85. }
  86. void OnDestroy()
  87. {
  88. if (!quitting)
  89. {
  90. Create();
  91. }
  92. }
  93. void OnApplicationQuit()
  94. {
  95. SceneManager.activeSceneChanged -= OnActiveSceneChanged;
  96. SceneManager.sceneLoaded -= OnSceneLoaded;
  97. SceneManager.sceneUnloaded -= OnSceneUnloaded;
  98. bsPlugins.OnApplicationQuit();
  99. ipaPlugins.OnApplicationQuit();
  100. foreach (var provider in PluginManager.configProviders)
  101. if (provider.Key.HasChanged)
  102. try
  103. {
  104. provider.Key.Save();
  105. }
  106. catch (Exception e)
  107. {
  108. Logging.Logger.log.Error("Error when trying to save config");
  109. Logging.Logger.log.Error(e);
  110. }
  111. quitting = true;
  112. }
  113. void OnLevelWasLoaded(int level)
  114. {
  115. ipaPlugins.OnLevelWasLoaded(level);
  116. }
  117. void OnLevelWasInitialized(int level)
  118. {
  119. ipaPlugins.OnLevelWasInitialized(level);
  120. }
  121. void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  122. {
  123. bsPlugins.OnSceneLoaded(scene, sceneMode);
  124. }
  125. private void OnSceneUnloaded(Scene scene) {
  126. bsPlugins.OnSceneUnloaded(scene);
  127. }
  128. private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  129. bsPlugins.OnActiveSceneChanged(prevScene, nextScene);
  130. }
  131. }
  132. }