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.1 KiB

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. namespace IllusionInjector
  8. {
  9. public class PluginComponent : MonoBehaviour
  10. {
  11. private CompositeBSPlugin bsPlugins;
  12. private CompositeIPAPlugin ipaPlugins;
  13. private bool quitting = false;
  14. public static PluginComponent Create()
  15. {
  16. Application.logMessageReceived += delegate (string condition, string stackTrace, LogType type)
  17. {
  18. var level = UnityLogInterceptor.LogTypeToLevel(type);
  19. UnityLogInterceptor.Unitylogger.Log(level, $"{condition.Trim()}");
  20. UnityLogInterceptor.Unitylogger.Log(level, $"{stackTrace.Trim()}");
  21. };
  22. return new GameObject("IPA_PluginManager").AddComponent<PluginComponent>();
  23. }
  24. void Awake()
  25. {
  26. DontDestroyOnLoad(gameObject);
  27. bsPlugins = new CompositeBSPlugin(PluginManager.BSPlugins);
  28. ipaPlugins = new CompositeIPAPlugin(PluginManager.Plugins);
  29. // this has no relevance since there is a new mod updater system
  30. //gameObject.AddComponent<ModUpdater>(); // AFTER plugins are loaded, but before most things
  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. }