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.

153 lines
4.5 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. ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins);
  25. gameObject.AddComponent<Updating.ModsaberML.Updater>();
  26. bsPlugins.OnApplicationStart();
  27. ipaPlugins.OnApplicationStart();
  28. SceneManager.activeSceneChanged += OnActiveSceneChanged;
  29. SceneManager.sceneLoaded += OnSceneLoaded;
  30. SceneManager.sceneUnloaded += OnSceneUnloaded;
  31. foreach (var provider in PluginManager.configProviders)
  32. if (provider.Key.HasChanged)
  33. try
  34. {
  35. provider.Key.Save();
  36. }
  37. catch (Exception e)
  38. {
  39. Logging.Logger.log.Error("Error when trying to save config");
  40. Logging.Logger.log.Error(e);
  41. }
  42. }
  43. void Update()
  44. {
  45. bsPlugins.OnUpdate();
  46. ipaPlugins.OnUpdate();
  47. }
  48. void LateUpdate()
  49. {
  50. bsPlugins.OnLateUpdate();
  51. ipaPlugins.OnLateUpdate();
  52. foreach (var provider in PluginManager.configProviders)
  53. {
  54. if (provider.Key.HasChanged)
  55. try
  56. {
  57. provider.Key.Save();
  58. }
  59. catch (Exception e)
  60. {
  61. Logging.Logger.log.Error("Error when trying to save config");
  62. Logging.Logger.log.Error(e);
  63. }
  64. else if (provider.Key.LastModified > provider.Value.Value)
  65. {
  66. try
  67. {
  68. provider.Key.Load(); // auto reload if it changes
  69. provider.Value.Value = provider.Key.LastModified;
  70. }
  71. catch (Exception e)
  72. {
  73. Logging.Logger.log.Error("Error when trying to load config");
  74. Logging.Logger.log.Error(e);
  75. }
  76. }
  77. }
  78. }
  79. void FixedUpdate()
  80. {
  81. bsPlugins.OnFixedUpdate();
  82. ipaPlugins.OnFixedUpdate();
  83. }
  84. void OnDestroy()
  85. {
  86. if (!quitting)
  87. {
  88. Create();
  89. }
  90. }
  91. void OnApplicationQuit()
  92. {
  93. SceneManager.activeSceneChanged -= OnActiveSceneChanged;
  94. SceneManager.sceneLoaded -= OnSceneLoaded;
  95. SceneManager.sceneUnloaded -= OnSceneUnloaded;
  96. bsPlugins.OnApplicationQuit();
  97. ipaPlugins.OnApplicationQuit();
  98. foreach (var provider in PluginManager.configProviders)
  99. if (provider.Key.HasChanged)
  100. try
  101. {
  102. provider.Key.Save();
  103. }
  104. catch (Exception e)
  105. {
  106. Logging.Logger.log.Error("Error when trying to save config");
  107. Logging.Logger.log.Error(e);
  108. }
  109. quitting = true;
  110. }
  111. void OnLevelWasLoaded(int level)
  112. {
  113. ipaPlugins.OnLevelWasLoaded(level);
  114. }
  115. void OnLevelWasInitialized(int level)
  116. {
  117. ipaPlugins.OnLevelWasInitialized(level);
  118. }
  119. void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  120. {
  121. bsPlugins.OnSceneLoaded(scene, sceneMode);
  122. }
  123. private void OnSceneUnloaded(Scene scene) {
  124. bsPlugins.OnSceneUnloaded(scene);
  125. }
  126. private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  127. bsPlugins.OnActiveSceneChanged(prevScene, nextScene);
  128. }
  129. }
  130. }