Chainloader.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. BepInLogger.Log($"{pluginTypes.Count} plugins found");
  37. foreach (Type t in pluginTypes)
  38. {
  39. try
  40. {
  41. var plugin = (BaseUnityPlugin)ManagerObject.AddComponent(t);
  42. Plugins.Add(plugin);
  43. BepInLogger.Log($"Loaded [{plugin.Name}]");
  44. }
  45. catch (Exception ex)
  46. {
  47. BepInLogger.Log($"Error loading [{t.Name}] : {ex.Message}");
  48. }
  49. }
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. UnityInjector.ConsoleUtil.ConsoleWindow.Attach();
  55. //UnityInjector.ConsoleUtil.ConsoleEncoding.ConsoleCodePage = 932;
  56. Console.WriteLine("Error occurred starting the game");
  57. Console.WriteLine(ex.ToString());
  58. }
  59. loaded = true;
  60. }
  61. /// <summary>
  62. /// Checks all plugins to see if a plugin with a certain ID is loaded.
  63. /// </summary>
  64. /// <param name="ID">The ID to check for.</param>
  65. /// <returns></returns>
  66. public static bool IsIDLoaded(string ID)
  67. {
  68. foreach (var plugin in Plugins)
  69. if (plugin != null && plugin.enabled && plugin.ID == ID)
  70. return true;
  71. return false;
  72. }
  73. /// <summary>
  74. /// Loads a list of types from a directory containing assemblies, that derive from a base type.
  75. /// </summary>
  76. /// <typeparam name="T">The specfiic base type to search for.</typeparam>
  77. /// <param name="directory">The directory to search for assemblies.</param>
  78. /// <returns>Returns a list of found derivative types.</returns>
  79. public static List<Type> LoadTypes<T>(string directory)
  80. {
  81. List<Type> types = new List<Type>();
  82. Type pluginType = typeof(T);
  83. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
  84. {
  85. try
  86. {
  87. AssemblyName an = AssemblyName.GetAssemblyName(dll);
  88. Assembly assembly = Assembly.Load(an);
  89. foreach (Type type in assembly.GetTypes())
  90. {
  91. if (type.IsInterface || type.IsAbstract)
  92. {
  93. continue;
  94. }
  95. else
  96. {
  97. if (type.BaseType == pluginType)
  98. types.Add(type);
  99. }
  100. }
  101. }
  102. catch (BadImageFormatException)
  103. {
  104. }
  105. }
  106. return types;
  107. }
  108. }
  109. }