TypeLoader.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. using BepInEx.Logging;
  8. using Mono.Cecil;
  9. namespace BepInEx.Bootstrap
  10. {
  11. /// <summary>
  12. /// Provides methods for loading specified types from an assembly.
  13. /// </summary>
  14. public static class TypeLoader
  15. {
  16. private static DefaultAssemblyResolver resolver;
  17. private static ReaderParameters readerParameters;
  18. static TypeLoader()
  19. {
  20. resolver = new DefaultAssemblyResolver();
  21. readerParameters = new ReaderParameters { AssemblyResolver = resolver };
  22. resolver.ResolveFailure += (sender, reference) =>
  23. {
  24. var name = new AssemblyName(reference.FullName);
  25. if (Utility.TryResolveDllAssembly(name, Paths.BepInExAssemblyDirectory, readerParameters, out AssemblyDefinition assembly) ||
  26. Utility.TryResolveDllAssembly(name, Paths.PluginPath, readerParameters, out assembly) ||
  27. Utility.TryResolveDllAssembly(name, Paths.ManagedPath, readerParameters, out assembly))
  28. return assembly;
  29. return null;
  30. };
  31. }
  32. /// <summary>
  33. /// Loads a list of types from a directory containing assemblies, that derive from a base type.
  34. /// </summary>
  35. /// <typeparam name="T">The specific base type to search for.</typeparam>
  36. /// <param name="directory">The directory to search for assemblies.</param>
  37. /// <returns>Returns a list of found derivative types.</returns>
  38. public static Dictionary<AssemblyDefinition, List<T>> FindPluginTypes<T>(string directory, Func<TypeDefinition, T> typeSelector) where T : class
  39. {
  40. var result = new Dictionary<AssemblyDefinition, List<T>>();
  41. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll", SearchOption.AllDirectories))
  42. {
  43. try
  44. {
  45. var ass = AssemblyDefinition.ReadAssembly(dll, readerParameters);
  46. var matches = ass.MainModule.Types.Select(typeSelector).Where(t => t != null).ToList();
  47. if (matches.Count == 0)
  48. {
  49. ass.Dispose();
  50. continue;
  51. }
  52. result[ass] = matches;
  53. }
  54. catch (Exception e)
  55. {
  56. Logger.LogError(e.ToString());
  57. }
  58. }
  59. return result;
  60. }
  61. public static string TypeLoadExceptionToString(ReflectionTypeLoadException ex)
  62. {
  63. StringBuilder sb = new StringBuilder();
  64. foreach (Exception exSub in ex.LoaderExceptions)
  65. {
  66. sb.AppendLine(exSub.Message);
  67. if (exSub is FileNotFoundException exFileNotFound)
  68. {
  69. if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
  70. {
  71. sb.AppendLine("Fusion Log:");
  72. sb.AppendLine(exFileNotFound.FusionLog);
  73. }
  74. }
  75. else if (exSub is FileLoadException exLoad)
  76. {
  77. if (!string.IsNullOrEmpty(exLoad.FusionLog))
  78. {
  79. sb.AppendLine("Fusion Log:");
  80. sb.AppendLine(exLoad.FusionLog);
  81. }
  82. }
  83. sb.AppendLine();
  84. }
  85. return sb.ToString();
  86. }
  87. }
  88. }