BepInComponent.cs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using TMPro;
  8. using UnityEngine;
  9. using UnityEngine.SceneManagement;
  10. namespace BepInEx
  11. {
  12. //Adapted from https://github.com/Eusth/IPA/blob/0df8b1ecb87fdfc9e169365cb4a8fd5a909a2ad6/IllusionInjector/PluginComponent.cs
  13. public class BepInComponent : MonoBehaviour
  14. {
  15. IEnumerable<IUnityPlugin> Plugins;
  16. private bool quitting = false;
  17. public static BepInComponent Create()
  18. {
  19. return new GameObject("BepInEx_Manager").AddComponent<BepInComponent>();
  20. }
  21. void Awake()
  22. {
  23. DontDestroyOnLoad(gameObject);
  24. Plugins = Chainloader.Plugins;
  25. }
  26. void Start()
  27. {
  28. Console.WriteLine("Component ready");
  29. foreach (IUnityPlugin plugin in Plugins)
  30. plugin.OnStart();
  31. }
  32. void OnEnable()
  33. {
  34. SceneManager.sceneLoaded += LevelFinishedLoading;
  35. }
  36. void OnDisable()
  37. {
  38. SceneManager.sceneLoaded -= LevelFinishedLoading;
  39. }
  40. void Update()
  41. {
  42. foreach (IUnityPlugin plugin in Plugins)
  43. plugin.OnUpdate();
  44. }
  45. void LateUpdate()
  46. {
  47. foreach (IUnityPlugin plugin in Plugins)
  48. plugin.OnLateUpdate();
  49. }
  50. void FixedUpdate()
  51. {
  52. foreach (IUnityPlugin plugin in Plugins)
  53. plugin.OnFixedUpdate();
  54. }
  55. void LevelFinishedLoading(Scene scene, LoadSceneMode mode)
  56. {
  57. foreach (IUnityPlugin plugin in Plugins)
  58. plugin.OnLevelFinishedLoading(scene, mode);
  59. }
  60. void OnDestroy()
  61. {
  62. if (!quitting)
  63. {
  64. Create();
  65. }
  66. }
  67. void OnApplicationQuit()
  68. {
  69. quitting = true;
  70. }
  71. }
  72. }