TypeLoader.cs 2.1 KB

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