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.

81 lines
2.0 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. namespace IllusionInjector
  7. {
  8. public class PluginComponent : MonoBehaviour
  9. {
  10. private CompositePlugin plugins;
  11. private bool freshlyLoaded = false;
  12. private bool quitting = false;
  13. public static PluginComponent Create()
  14. {
  15. return new GameObject("IPA_PluginManager").AddComponent<PluginComponent>();
  16. }
  17. void Awake()
  18. {
  19. DontDestroyOnLoad(gameObject);
  20. plugins = new CompositePlugin(PluginManager.Plugins);
  21. plugins.OnApplicationStart();
  22. SceneManager.activeSceneChanged += OnActiveSceneChanged;
  23. SceneManager.sceneLoaded += OnSceneLoaded;
  24. SceneManager.sceneUnloaded += OnSceneUnloaded;
  25. }
  26. void Update()
  27. {
  28. if (freshlyLoaded)
  29. {
  30. freshlyLoaded = false;
  31. }
  32. plugins.OnUpdate();
  33. }
  34. void LateUpdate()
  35. {
  36. plugins.OnLateUpdate();
  37. }
  38. void FixedUpdate()
  39. {
  40. plugins.OnFixedUpdate();
  41. }
  42. void OnDestroy()
  43. {
  44. if (!quitting)
  45. {
  46. Create();
  47. }
  48. }
  49. void OnApplicationQuit()
  50. {
  51. plugins.OnApplicationQuit();
  52. quitting = true;
  53. }
  54. void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
  55. {
  56. plugins.OnSceneLoaded(scene, sceneMode);
  57. freshlyLoaded = true;
  58. }
  59. private void OnSceneUnloaded(Scene scene) {
  60. plugins.OnSceneUnloaded(scene);
  61. }
  62. private void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  63. plugins.OnActiveSceneChanged(prevScene, nextScene);
  64. }
  65. }
  66. }