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.

110 lines
3.1 KiB

6 years ago
6 years ago
  1. using IllusionInjector.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.SceneManagement;
  7. using IPA.Loader;
  8. namespace IllusionInjector
  9. {
  10. public class PluginComponent : MonoBehaviour
  11. {
  12. private CompositeBSPlugin bsPlugins;
  13. private CompositeIPAPlugin ipaPlugins;
  14. private bool quitting = false;
  15. public static PluginComponent Create()
  16. {
  17. Application.logMessageReceived += delegate (string condition, string stackTrace, LogType type)
  18. {
  19. var level = UnityLogInterceptor.LogTypeToLevel(type);
  20. UnityLogInterceptor.Unitylogger.Log(level, $"{condition.Trim()}");
  21. UnityLogInterceptor.Unitylogger.Log(level, $"{stackTrace.Trim()}");
  22. };
  23. return new GameObject("IPA_PluginManager").AddComponent<PluginComponent>();
  24. }
  25. void Awake()
  26. {
  27. DontDestroyOnLoad(gameObject);
  28. bsPlugins = new CompositeBSPlugin(PluginManager.BSPlugins);
  29. ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins);
  30. // this has no relevance since there is a new mod updater system
  31. //gameObject.AddComponent<ModUpdater>(); // AFTER plugins are loaded, but before most things
  32. gameObject.AddComponent<Updating.ModsaberML.Updater>();
  33. bsPlugins.OnApplicationStart();
  34. ipaPlugins.OnApplicationStart();
  35. SceneManager.activeSceneChanged += OnActiveSceneChanged;
  36. SceneManager.sceneLoaded += OnSceneLoaded;
  37. SceneManager.sceneUnloaded += OnSceneUnloaded;
  38. }
  39. void Update()
  40. {
  41. bsPlugins.OnUpdate();
  42. ipaPlugins.OnUpdate();
  43. }
  44. void LateUpdate()
  45. {
  46. bsPlugins.OnLateUpdate();
  47. ipaPlugins.OnLateUpdate();
  48. }
  49. void FixedUpdate()
  50. {
  51. bsPlugins.OnFixedUpdate();
  52. ipaPlugins.OnFixedUpdate();
  53. }
  54. void OnDestroy()
  55. {
  56. if (!quitting)
  57. {
  58. Create();
  59. }
  60. }
  61. void OnApplicationQuit()
  62. {
  63. SceneManager.activeSceneChanged -= OnActiveSceneChanged;
  64. SceneManager.sceneLoaded -= OnSceneLoaded;
  65. SceneManager.sceneUnloaded -= OnSceneUnloaded;
  66. bsPlugins.OnApplicationQuit();
  67. ipaPlugins.OnApplicationQuit();
  68. quitting = true;
  69. }
  70. void OnLevelWasLoaded(int level)
  71. {
  72. ipaPlugins.OnLevelWasLoaded(level);
  73. }
  74. public void OnLevelWasInitialized(int level)
  75. {
  76. ipaPlugins.OnLevelWasInitialized(level);
  77. }
  78. void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  79. {
  80. bsPlugins.OnSceneLoaded(scene, sceneMode);
  81. }
  82. private void OnSceneUnloaded(Scene scene) {
  83. bsPlugins.OnSceneUnloaded(scene);
  84. }
  85. private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  86. bsPlugins.OnActiveSceneChanged(prevScene, nextScene);
  87. }
  88. }
  89. }