TypeLoader.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. namespace BepInEx
  8. {
  9. public static class TypeLoader
  10. {
  11. /// <summary>
  12. /// Loads a list of types from a directory containing assemblies, that derive from a base type.
  13. /// </summary>
  14. /// <typeparam name="T">The specfiic base type to search for.</typeparam>
  15. /// <param name="directory">The directory to search for assemblies.</param>
  16. /// <returns>Returns a list of found derivative types.</returns>
  17. public static IEnumerable<Type> LoadTypes<T>(string directory)
  18. {
  19. List<Type> types = new List<Type>();
  20. Type pluginType = typeof(T);
  21. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
  22. {
  23. try
  24. {
  25. AssemblyName an = AssemblyName.GetAssemblyName(dll);
  26. Assembly assembly = Assembly.Load(an);
  27. foreach (Type type in assembly.GetTypes())
  28. {
  29. if (type.IsInterface || type.IsAbstract)
  30. {
  31. continue;
  32. }
  33. else
  34. {
  35. if (type.BaseType == pluginType)
  36. types.Add(type);
  37. }
  38. }
  39. }
  40. catch (BadImageFormatException) { } //unmanaged DLL
  41. catch (ReflectionTypeLoadException)
  42. {
  43. BepInLogger.Log($"ERROR! Could not load \"{Path.GetFileName(dll)}\" as a plugin!");
  44. }
  45. }
  46. return types;
  47. }
  48. public static BepInPlugin GetMetadata(object plugin)
  49. {
  50. return GetMetadata(plugin.GetType());
  51. }
  52. public static BepInPlugin GetMetadata(Type pluginType)
  53. {
  54. object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
  55. if (attributes.Length == 0)
  56. return null;
  57. return (BepInPlugin)attributes[0];
  58. }
  59. public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute
  60. {
  61. return GetAttributes<T>(plugin.GetType());
  62. }
  63. public static IEnumerable<T> GetAttributes<T>(Type pluginType) where T : Attribute
  64. {
  65. return pluginType.GetCustomAttributes(typeof(T), true).Cast<T>();
  66. }
  67. public static IEnumerable<Type> GetDependencies(Type Plugin, IEnumerable<Type> AllPlugins)
  68. {
  69. object[] attributes = Plugin.GetCustomAttributes(typeof(BepInDependency), true);
  70. List<Type> dependencyTypes = new List<Type>();
  71. foreach (BepInDependency dependency in attributes)
  72. {
  73. Type dependencyType = AllPlugins.FirstOrDefault(x => GetMetadata(x)?.GUID == dependency.DependencyGUID);
  74. if (dependencyType == null)
  75. {
  76. if ((dependency.Flags & BepInDependency.DependencyFlags.SoftDependency) != 0)
  77. continue; //skip on soft dependencies
  78. throw new MissingDependencyException("Cannot find dependency type.");
  79. }
  80. dependencyTypes.Add(dependencyType);
  81. }
  82. return dependencyTypes;
  83. }
  84. }
  85. public class MissingDependencyException : Exception
  86. {
  87. public MissingDependencyException() : base()
  88. {
  89. }
  90. public MissingDependencyException(string message) : base(message)
  91. {
  92. }
  93. }
  94. }