Chainloader.cs 4.5 KB

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