TypeLoader.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. /// A cacheable metadata item. Can be used with <see cref="TypeLoader.LoadAssemblyCache{T}"/> and <see cref="TypeLoader.SaveAssemblyCache{T}"/> to cache plugin metadata.
  13. /// </summary>
  14. public interface ICacheable
  15. {
  16. /// <summary>
  17. /// Serialize the object into a binary format.
  18. /// </summary>
  19. /// <param name="bw"></param>
  20. void Save(BinaryWriter bw);
  21. /// <summary>
  22. /// Loads the object from binary format.
  23. /// </summary>
  24. /// <param name="br"></param>
  25. void Load(BinaryReader br);
  26. }
  27. /// <summary>
  28. /// A cached assembly.
  29. /// </summary>
  30. /// <typeparam name="T"></typeparam>
  31. public class CachedAssembly<T> where T : ICacheable
  32. {
  33. /// <summary>
  34. /// List of cached items inside the assembly.
  35. /// </summary>
  36. public List<T> CacheItems { get; set; }
  37. /// <summary>
  38. /// Timestamp of the assembly. Used to check the age of the cache.
  39. /// </summary>
  40. public long Timestamp { get; set; }
  41. }
  42. /// <summary>
  43. /// Provides methods for loading specified types from an assembly.
  44. /// </summary>
  45. public static class TypeLoader
  46. {
  47. private static readonly DefaultAssemblyResolver resolver;
  48. private static readonly ReaderParameters readerParameters;
  49. static TypeLoader()
  50. {
  51. resolver = new DefaultAssemblyResolver();
  52. readerParameters = new ReaderParameters { AssemblyResolver = resolver };
  53. resolver.ResolveFailure += (sender, reference) =>
  54. {
  55. var name = new AssemblyName(reference.FullName);
  56. if (Utility.TryResolveDllAssembly(name, Paths.BepInExAssemblyDirectory, readerParameters, out var assembly) ||
  57. Utility.TryResolveDllAssembly(name, Paths.PluginPath, readerParameters, out assembly) ||
  58. Utility.TryResolveDllAssembly(name, Paths.ManagedPath, readerParameters, out assembly))
  59. return assembly;
  60. return AssemblyResolve?.Invoke(sender, reference);
  61. };
  62. }
  63. public static event AssemblyResolveEventHandler AssemblyResolve;
  64. /// <summary>
  65. /// Looks up assemblies in the given directory and locates all types that can be loaded and collects their metadata.
  66. /// </summary>
  67. /// <typeparam name="T">The specific base type to search for.</typeparam>
  68. /// <param name="directory">The directory to search for assemblies.</param>
  69. /// <param name="typeSelector">A function to check if a type should be selected and to build the type metadata.</param>
  70. /// <param name="assemblyFilter">A filter function to quickly determine if the assembly can be loaded.</param>
  71. /// <param name="cacheName">The name of the cache to get cached types from.</param>
  72. /// <returns>A list of all loadable type metadatas indexed by the full path to the assembly that contains the types.</returns>
  73. public static Dictionary<string, List<T>> FindPluginTypes<T>(string directory, Func<TypeDefinition, T> typeSelector, Func<AssemblyDefinition, bool> assemblyFilter = null, string cacheName = null) where T : ICacheable, new()
  74. {
  75. var result = new Dictionary<string, List<T>>();
  76. Dictionary<string, CachedAssembly<T>> cache = null;
  77. if (cacheName != null)
  78. cache = LoadAssemblyCache<T>(cacheName);
  79. foreach (string dll in Directory.GetFiles(Path.GetFullPath(directory), "*.dll", SearchOption.AllDirectories))
  80. try
  81. {
  82. if (cache != null && cache.TryGetValue(dll, out var cacheEntry))
  83. {
  84. long lastWrite = File.GetLastWriteTimeUtc(dll).Ticks;
  85. if (lastWrite == cacheEntry.Timestamp)
  86. {
  87. result[dll] = cacheEntry.CacheItems;
  88. continue;
  89. }
  90. }
  91. var ass = AssemblyDefinition.ReadAssembly(dll, readerParameters);
  92. if (!assemblyFilter?.Invoke(ass) ?? false)
  93. {
  94. ass.Dispose();
  95. continue;
  96. }
  97. var matches = ass.MainModule.Types.Select(typeSelector).Where(t => t != null).ToList();
  98. if (matches.Count == 0)
  99. {
  100. ass.Dispose();
  101. continue;
  102. }
  103. result[dll] = matches;
  104. ass.Dispose();
  105. }
  106. catch (Exception e)
  107. {
  108. Logger.LogError(e.ToString());
  109. }
  110. if (cacheName != null)
  111. SaveAssemblyCache(cacheName, result);
  112. return result;
  113. }
  114. /// <summary>
  115. /// Loads an index of type metadatas from a cache.
  116. /// </summary>
  117. /// <param name="cacheName">Name of the cache</param>
  118. /// <typeparam name="T">Cacheable item</typeparam>
  119. /// <returns>Cached type metadatas indexed by the path of the assembly that defines the type</returns>
  120. public static Dictionary<string, CachedAssembly<T>> LoadAssemblyCache<T>(string cacheName) where T : ICacheable, new()
  121. {
  122. var result = new Dictionary<string, CachedAssembly<T>>();
  123. try
  124. {
  125. string path = Path.Combine(Paths.CachePath, $"{cacheName}_typeloader.dat");
  126. if (!File.Exists(path))
  127. return null;
  128. using (var br = new BinaryReader(File.OpenRead(path)))
  129. {
  130. int entriesCount = br.ReadInt32();
  131. for (var i = 0; i < entriesCount; i++)
  132. {
  133. string entryIdentifier = br.ReadString();
  134. long entryDate = br.ReadInt64();
  135. int itemsCount = br.ReadInt32();
  136. var items = new List<T>();
  137. for (var j = 0; j < itemsCount; j++)
  138. {
  139. var entry = new T();
  140. entry.Load(br);
  141. items.Add(entry);
  142. }
  143. result[entryIdentifier] = new CachedAssembly<T> { Timestamp = entryDate, CacheItems = items };
  144. }
  145. }
  146. }
  147. catch (Exception e)
  148. {
  149. Logger.LogWarning($"Failed to load cache \"{cacheName}\"; skipping loading cache. Reason: {e.Message}.");
  150. }
  151. return result;
  152. }
  153. /// <summary>
  154. /// Saves indexed type metadata into a cache.
  155. /// </summary>
  156. /// <param name="cacheName">Name of the cache</param>
  157. /// <param name="entries">List of plugin metadatas indexed by the path to the assembly that contains the types</param>
  158. /// <typeparam name="T">Cacheable item</typeparam>
  159. public static void SaveAssemblyCache<T>(string cacheName, Dictionary<string, List<T>> entries) where T : ICacheable
  160. {
  161. try
  162. {
  163. if (!Directory.Exists(Paths.CachePath))
  164. Directory.CreateDirectory(Paths.CachePath);
  165. string path = Path.Combine(Paths.CachePath, $"{cacheName}_typeloader.dat");
  166. using (var bw = new BinaryWriter(File.OpenWrite(path)))
  167. {
  168. bw.Write(entries.Count);
  169. foreach (var kv in entries)
  170. {
  171. bw.Write(kv.Key);
  172. bw.Write(File.GetLastWriteTimeUtc(kv.Key).Ticks);
  173. bw.Write(kv.Value.Count);
  174. foreach (var item in kv.Value)
  175. item.Save(bw);
  176. }
  177. }
  178. }
  179. catch (Exception e)
  180. {
  181. Logger.LogWarning($"Failed to save cache \"{cacheName}\"; skipping saving cache. Reason: {e.Message}.");
  182. }
  183. }
  184. /// <summary>
  185. /// Converts TypeLoadException to a readable string.
  186. /// </summary>
  187. /// <param name="ex">TypeLoadException</param>
  188. /// <returns>Readable representation of the exception</returns>
  189. public static string TypeLoadExceptionToString(ReflectionTypeLoadException ex)
  190. {
  191. var sb = new StringBuilder();
  192. foreach (var exSub in ex.LoaderExceptions)
  193. {
  194. sb.AppendLine(exSub.Message);
  195. if (exSub is FileNotFoundException exFileNotFound)
  196. {
  197. if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
  198. {
  199. sb.AppendLine("Fusion Log:");
  200. sb.AppendLine(exFileNotFound.FusionLog);
  201. }
  202. }
  203. else if (exSub is FileLoadException exLoad)
  204. {
  205. if (!string.IsNullOrEmpty(exLoad.FusionLog))
  206. {
  207. sb.AppendLine("Fusion Log:");
  208. sb.AppendLine(exLoad.FusionLog);
  209. }
  210. }
  211. sb.AppendLine();
  212. }
  213. return sb.ToString();
  214. }
  215. }
  216. }