TypeLoader.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 specific 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", SearchOption.AllDirectories))
  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 && pluginType.IsAssignableFrom(type))
  33. types.Add(type);
  34. }
  35. }
  36. catch (BadImageFormatException) { } //unmanaged DLL
  37. catch (ReflectionTypeLoadException ex)
  38. {
  39. Logger.LogError($"Could not load \"{Path.GetFileName(dll)}\" as a plugin!");
  40. Logger.LogDebug(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. else if (exSub is FileLoadException exLoad)
  60. {
  61. if (!string.IsNullOrEmpty(exLoad.FusionLog))
  62. {
  63. sb.AppendLine("Fusion Log:");
  64. sb.AppendLine(exLoad.FusionLog);
  65. }
  66. }
  67. sb.AppendLine();
  68. }
  69. return sb.ToString();
  70. }
  71. }
  72. }