BepInComponent.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. List<BaseUnityPlugin> Plugins;
  16. private bool quitting = false;
  17. public static GameObject Create()
  18. {
  19. var obj = new GameObject("BepInEx_Manager");
  20. var manager = obj.AddComponent<BepInComponent>();
  21. manager.Plugins = new List<BaseUnityPlugin>();
  22. foreach (Type t in Chainloader.Plugins)
  23. {
  24. var plugin = (BaseUnityPlugin)obj.AddComponent(t);
  25. manager.Plugins.Add(plugin);
  26. Console.WriteLine($"Loaded [{plugin.Name}]");
  27. }
  28. return obj;
  29. }
  30. void Awake()
  31. {
  32. DontDestroyOnLoad(gameObject);
  33. }
  34. void Start()
  35. {
  36. Console.WriteLine("Component ready");
  37. }
  38. void OnDestroy()
  39. {
  40. if (!quitting)
  41. {
  42. Create();
  43. }
  44. }
  45. void OnApplicationQuit()
  46. {
  47. quitting = true;
  48. }
  49. }
  50. }