TypeLoader.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Text;
  6. using BepInEx.Logging;
  7. namespace BepInEx.Bootstrap
  8. {
  9. /// <summary>
  10. /// Provides methods for loading specified types from an assembly.
  11. /// </summary>
  12. public static class TypeLoader
  13. {
  14. /// <summary>
  15. /// Loads a list of types from a directory containing assemblies, that derive from a base type.
  16. /// </summary>
  17. /// <typeparam name="T">The specfiic base type to search for.</typeparam>
  18. /// <param name="directory">The directory to search for assemblies.</param>
  19. /// <returns>Returns a list of found derivative types.</returns>
  20. public static IEnumerable<Type> LoadTypes<T>(string directory)
  21. {
  22. List<Type> types = new List<Type>();
  23. Type pluginType = typeof(T);
  24. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll"))
  25. {
  26. try
  27. {
  28. AssemblyName an = AssemblyName.GetAssemblyName(dll);
  29. Assembly assembly = Assembly.Load(an);
  30. foreach (Type type in assembly.GetTypes())
  31. {
  32. if (!type.IsInterface && !type.IsAbstract && type.BaseType == pluginType)
  33. types.Add(type);
  34. }
  35. }
  36. catch (BadImageFormatException) { } //unmanaged DLL
  37. catch (ReflectionTypeLoadException ex)
  38. {
  39. Logger.Log(LogLevel.Error, $"Could not load \"{Path.GetFileName(dll)}\" as a plugin!");
  40. Logger.Log(LogLevel.Debug, TypeLoadExceptionToString(ex));
  41. }
  42. }
  43. return types;
  44. }
  45. private static string TypeLoadExceptionToString(ReflectionTypeLoadException ex)
  46. {
  47. StringBuilder sb = new StringBuilder();
  48. foreach (Exception exSub in ex.LoaderExceptions)
  49. {
  50. sb.AppendLine(exSub.Message);
  51. if (exSub is FileNotFoundException exFileNotFound)
  52. {
  53. if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
  54. {
  55. sb.AppendLine("Fusion Log:");
  56. sb.AppendLine(exFileNotFound.FusionLog);
  57. }
  58. }
  59. sb.AppendLine();
  60. }
  61. return sb.ToString();
  62. }
  63. }
  64. }