TypeLoader.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 && type.BaseType == pluginType)
  30. types.Add(type);
  31. }
  32. }
  33. catch (BadImageFormatException) { } //unmanaged DLL
  34. catch (ReflectionTypeLoadException)
  35. {
  36. BepInLogger.Log($"ERROR! Could not load \"{Path.GetFileName(dll)}\" as a plugin!");
  37. }
  38. }
  39. return types;
  40. }
  41. public static BepInPlugin GetMetadata(object plugin)
  42. {
  43. return GetMetadata(plugin.GetType());
  44. }
  45. public static BepInPlugin GetMetadata(Type pluginType)
  46. {
  47. object[] attributes = pluginType.GetCustomAttributes(typeof(BepInPlugin), false);
  48. if (attributes.Length == 0)
  49. return null;
  50. return (BepInPlugin)attributes[0];
  51. }
  52. public static IEnumerable<T> GetAttributes<T>(object plugin) where T : Attribute
  53. {
  54. return GetAttributes<T>(plugin.GetType());
  55. }
  56. public static IEnumerable<T> GetAttributes<T>(Type pluginType) where T : Attribute
  57. {
  58. return pluginType.GetCustomAttributes(typeof(T), true).Cast<T>();
  59. }
  60. public static IEnumerable<Type> GetDependencies(Type Plugin, IEnumerable<Type> AllPlugins)
  61. {
  62. object[] attributes = Plugin.GetCustomAttributes(typeof(BepInDependency), true);
  63. List<Type> dependencyTypes = new List<Type>();
  64. foreach (BepInDependency dependency in attributes)
  65. {
  66. Type dependencyType = AllPlugins.FirstOrDefault(x => GetMetadata(x)?.GUID == dependency.DependencyGUID);
  67. if (dependencyType == null)
  68. {
  69. if ((dependency.Flags & BepInDependency.DependencyFlags.SoftDependency) != 0)
  70. continue; //skip on soft dependencies
  71. throw new MissingDependencyException("Cannot find dependency type.");
  72. }
  73. dependencyTypes.Add(dependencyType);
  74. }
  75. return dependencyTypes;
  76. }
  77. }
  78. public class MissingDependencyException : Exception
  79. {
  80. public MissingDependencyException() : base()
  81. {
  82. }
  83. public MissingDependencyException(string message) : base(message)
  84. {
  85. }
  86. }
  87. }