Chainloader.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /// Loads a list of types from a directory containing assemblies, that derive from a base type.
  56. /// </summary>
  57. /// <typeparam name="T">The specfiic base type to search for.</typeparam>
  58. /// <param name="directory">The directory to search for assemblies.</param>
  59. /// <returns>Returns a list of found derivative types.</returns>
  60. public static List<Type> LoadTypes<T>(string directory)
  61. {
  62. List<Type> types = new List<Type>();
  63. Type pluginType = typeof(T);
  64. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
  65. {
  66. try
  67. {
  68. AssemblyName an = AssemblyName.GetAssemblyName(dll);
  69. Assembly assembly = Assembly.Load(an);
  70. foreach (Type type in assembly.GetTypes())
  71. {
  72. if (type.IsInterface || type.IsAbstract)
  73. {
  74. continue;
  75. }
  76. else
  77. {
  78. if (type.BaseType == pluginType)
  79. types.Add(type);
  80. }
  81. }
  82. }
  83. catch (BadImageFormatException)
  84. {
  85. }
  86. }
  87. return types;
  88. }
  89. }
  90. }