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.

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