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.

109 lines
3.0 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. public class PluginComponent : MonoBehaviour
  12. {
  13. private CompositeBSPlugin bsPlugins;
  14. private CompositeIPAPlugin ipaPlugins;
  15. private bool quitting = false;
  16. public 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. }
  38. void Update()
  39. {
  40. bsPlugins.OnUpdate();
  41. ipaPlugins.OnUpdate();
  42. }
  43. void LateUpdate()
  44. {
  45. bsPlugins.OnLateUpdate();
  46. ipaPlugins.OnLateUpdate();
  47. }
  48. void FixedUpdate()
  49. {
  50. bsPlugins.OnFixedUpdate();
  51. ipaPlugins.OnFixedUpdate();
  52. }
  53. void OnDestroy()
  54. {
  55. if (!quitting)
  56. {
  57. Create();
  58. }
  59. }
  60. void OnApplicationQuit()
  61. {
  62. SceneManager.activeSceneChanged -= OnActiveSceneChanged;
  63. SceneManager.sceneLoaded -= OnSceneLoaded;
  64. SceneManager.sceneUnloaded -= OnSceneUnloaded;
  65. bsPlugins.OnApplicationQuit();
  66. ipaPlugins.OnApplicationQuit();
  67. quitting = true;
  68. }
  69. void OnLevelWasLoaded(int level)
  70. {
  71. ipaPlugins.OnLevelWasLoaded(level);
  72. }
  73. public void OnLevelWasInitialized(int level)
  74. {
  75. ipaPlugins.OnLevelWasInitialized(level);
  76. }
  77. void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  78. {
  79. bsPlugins.OnSceneLoaded(scene, sceneMode);
  80. }
  81. private void OnSceneUnloaded(Scene scene) {
  82. bsPlugins.OnSceneUnloaded(scene);
  83. }
  84. private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  85. bsPlugins.OnActiveSceneChanged(prevScene, nextScene);
  86. }
  87. }
  88. }