Chainloader.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using BepInEx.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. using UnityEngine;
  7. namespace BepInEx
  8. {
  9. /// <summary>
  10. /// The manager and loader for all plugins, and the entry point for BepInEx.
  11. /// </summary>
  12. public class Chainloader
  13. {
  14. /// <summary>
  15. /// The loaded and initialized list of plugins.
  16. /// </summary>
  17. public static List<BaseUnityPlugin> Plugins { get; protected set; } = new List<BaseUnityPlugin>();
  18. /// <summary>
  19. /// The GameObject that all plugins are attached to as components.
  20. /// </summary>
  21. public static GameObject ManagerObject { get; protected set; } = new GameObject("BepInEx_Manager");
  22. static bool loaded = false;
  23. /// <summary>
  24. /// The entry point for BepInEx, called on the very first LoadScene() from UnityEngine.
  25. /// </summary>
  26. public static void Initialize()
  27. {
  28. if (loaded)
  29. return;
  30. try
  31. {
  32. UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
  33. if (Directory.Exists(Utility.PluginsDirectory))
  34. {
  35. var pluginTypes = LoadTypes<BaseUnityPlugin>(Utility.PluginsDirectory);
  36. //Log($"{pluginTypes.Count()} plugins found");
  37. foreach (Type t in pluginTypes)
  38. {
  39. var plugin = (BaseUnityPlugin)ManagerObject.AddComponent(t);
  40. Plugins.Add(plugin);
  41. //Log($"Loaded [{plugin.Name}]");
  42. }
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  48. //UnityInjector.ConsoleUtil.ConsoleEncoding.ConsoleCodePage = 932;
  49. Console.WriteLine("Error occurred starting the game");
  50. Console.WriteLine(ex.ToString());
  51. }
  52. loaded = true;
  53. }
  54. /// <summary>
  55. /// Checks all plugins to see if a plugin with a certain ID is loaded.
  56. /// </summary>
  57. /// <param name="ID">The ID to check for.</param>
  58. /// <returns></returns>
  59. public static bool IsIDLoaded(string ID)
  60. {
  61. foreach (var plugin in Plugins)
  62. if (plugin != null && plugin.enabled && plugin.ID == ID)
  63. return true;
  64. return false;
  65. }
  66. /// <summary>
  67. /// Loads a list of types from a directory containing assemblies, that derive from a base type.
  68. /// </summary>
  69. /// <typeparam name="T">The specfiic base type to search for.</typeparam>
  70. /// <param name="directory">The directory to search for assemblies.</param>
  71. /// <returns>Returns a list of found derivative types.</returns>
  72. public static List<Type> LoadTypes<T>(string directory)
  73. {
  74. List<Type> types = new List<Type>();
  75. Type pluginType = typeof(T);
  76. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
  77. {
  78. try
  79. {
  80. AssemblyName an = AssemblyName.GetAssemblyName(dll);
  81. Assembly assembly = Assembly.Load(an);
  82. foreach (Type type in assembly.GetTypes())
  83. {
  84. if (type.IsInterface || type.IsAbstract)
  85. {
  86. continue;
  87. }
  88. else
  89. {
  90. if (type.BaseType == pluginType)
  91. types.Add(type);
  92. }
  93. }
  94. }
  95. catch (BadImageFormatException)
  96. {
  97. }
  98. }
  99. return types;
  100. }
  101. }
  102. }