TypeLoader.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. 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. }