You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

538 lines
20 KiB

  1. using IPA.Loader.Features;
  2. using IPA.Logging;
  3. using IPA.Utilities;
  4. using Mono.Cecil;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Reflection;
  11. using System.Threading.Tasks;
  12. using Version = SemVer.Version;
  13. namespace IPA.Loader
  14. {
  15. /// <summary>
  16. /// A type to manage the loading of plugins.
  17. /// </summary>
  18. public class PluginLoader
  19. {
  20. internal static Task LoadTask() => Task.Run(() =>
  21. {
  22. LoadMetadata();
  23. Resolve();
  24. ComputeLoadOrder();
  25. InitFeatures();
  26. });
  27. /// <summary>
  28. /// A class which describes a loaded plugin.
  29. /// </summary>
  30. public class PluginMetadata
  31. {
  32. /// <summary>
  33. /// The assembly the plugin was loaded from.
  34. /// </summary>
  35. public Assembly Assembly { get; internal set; }
  36. /// <summary>
  37. /// The TypeDefinition for the main type of the plugin.
  38. /// </summary>
  39. public TypeDefinition PluginType { get; internal set; }
  40. /// <summary>
  41. /// The human readable name of the plugin.
  42. /// </summary>
  43. public string Name { get; internal set; }
  44. /// <summary>
  45. /// The BeatMods ID of the plugin, or null if it doesn't have one.
  46. /// </summary>
  47. public string Id { get; internal set; }
  48. /// <summary>
  49. /// The version of the plugin.
  50. /// </summary>
  51. public Version Version { get; internal set; }
  52. /// <summary>
  53. /// The file the plugin was loaded from.
  54. /// </summary>
  55. public FileInfo File { get; internal set; }
  56. // ReSharper disable once UnusedAutoPropertyAccessor.Global
  57. /// <summary>
  58. /// The features this plugin requests.
  59. /// </summary>
  60. public IReadOnlyList<Feature> Features => InternalFeatures;
  61. internal readonly List<Feature> InternalFeatures = new List<Feature>();
  62. internal bool IsSelf;
  63. internal bool IsBare;
  64. private PluginManifest manifest;
  65. internal HashSet<PluginMetadata> Dependencies { get; } = new HashSet<PluginMetadata>();
  66. internal PluginManifest Manifest
  67. {
  68. get => manifest;
  69. set
  70. {
  71. manifest = value;
  72. Name = value.Name;
  73. Version = value.Version;
  74. Id = value.Id;
  75. }
  76. }
  77. /// <inheritdoc />
  78. public override string ToString() => $"{Name}({Id}@{Version})({PluginType?.FullName}) from '{Utils.GetRelativePath(File?.FullName, BeatSaber.InstallPath)}'";
  79. }
  80. /// <summary>
  81. /// A container object for all the data relating to a plugin.
  82. /// </summary>
  83. public class PluginInfo
  84. {
  85. internal IBeatSaberPlugin Plugin { get; set; }
  86. /// <summary>
  87. /// Metadata for the plugin.
  88. /// </summary>
  89. public PluginMetadata Metadata { get; internal set; } = new PluginMetadata();
  90. }
  91. internal static List<PluginMetadata> PluginsMetadata = new List<PluginMetadata>();
  92. internal static void LoadMetadata()
  93. {
  94. string[] plugins = Directory.GetFiles(BeatSaber.PluginsPath, "*.dll");
  95. try
  96. {
  97. var selfMeta = new PluginMetadata
  98. {
  99. Assembly = Assembly.GetExecutingAssembly(),
  100. File = new FileInfo(Path.Combine(BeatSaber.InstallPath, "IPA.exe")),
  101. PluginType = null,
  102. IsSelf = true
  103. };
  104. string manifest;
  105. using (var manifestReader =
  106. new StreamReader(
  107. selfMeta.Assembly.GetManifestResourceStream(typeof(PluginLoader), "manifest.json") ??
  108. throw new InvalidOperationException()))
  109. manifest = manifestReader.ReadToEnd();
  110. selfMeta.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  111. PluginsMetadata.Add(selfMeta);
  112. }
  113. catch (Exception e)
  114. {
  115. Logger.loader.Critical("Error loading own manifest");
  116. Logger.loader.Critical(e);
  117. }
  118. foreach (var plugin in plugins)
  119. {
  120. try
  121. {
  122. var metadata = new PluginMetadata
  123. {
  124. File = new FileInfo(Path.Combine(BeatSaber.PluginsPath, plugin)),
  125. IsSelf = false
  126. };
  127. var pluginModule = AssemblyDefinition.ReadAssembly(plugin, new ReaderParameters
  128. {
  129. ReadingMode = ReadingMode.Immediate,
  130. ReadWrite = false,
  131. AssemblyResolver = new CecilLibLoader()
  132. }).MainModule;
  133. var iBeatSaberPlugin = pluginModule.ImportReference(typeof(IBeatSaberPlugin));
  134. foreach (var type in pluginModule.Types)
  135. {
  136. foreach (var inter in type.Interfaces)
  137. {
  138. var ifType = inter.InterfaceType;
  139. if (iBeatSaberPlugin.FullName == ifType.FullName)
  140. {
  141. metadata.PluginType = type;
  142. break;
  143. }
  144. }
  145. if (metadata.PluginType != null) break;
  146. }
  147. if (metadata.PluginType == null)
  148. {
  149. Logger.loader.Notice(
  150. #if DIRE_LOADER_WARNINGS
  151. $"Could not find plugin type for {Path.GetFileName(plugin)}"
  152. #else
  153. $"New plugin type not present in {Path.GetFileName(plugin)}; maybe an old plugin?"
  154. #endif
  155. );
  156. continue;
  157. }
  158. foreach (var resource in pluginModule.Resources)
  159. {
  160. if (!(resource is EmbeddedResource embedded) ||
  161. embedded.Name != $"{metadata.PluginType.Namespace}.manifest.json") continue;
  162. string manifest;
  163. using (var manifestReader = new StreamReader(embedded.GetResourceStream()))
  164. manifest = manifestReader.ReadToEnd();
  165. metadata.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  166. break;
  167. }
  168. if (metadata.Manifest == null)
  169. {
  170. Logger.loader.Error("Could not find manifest.json in namespace " +
  171. $"{metadata.PluginType.Namespace} for {Path.GetFileName(plugin)}");
  172. continue;
  173. }
  174. Logger.loader.Debug($"Adding info for {Path.GetFileName(plugin)}");
  175. PluginsMetadata.Add(metadata);
  176. }
  177. catch (Exception e)
  178. {
  179. Logger.loader.Error($"Could not load data for plugin {Path.GetFileName(plugin)}");
  180. Logger.loader.Error(e);
  181. }
  182. }
  183. IEnumerable<string> bareManifests = Directory.GetFiles(BeatSaber.PluginsPath, "*.json");
  184. bareManifests = bareManifests.Concat(Directory.GetFiles(BeatSaber.PluginsPath, "*.manifest"));
  185. foreach (var manifest in bareManifests)
  186. {
  187. try
  188. {
  189. var metadata = new PluginMetadata
  190. {
  191. File = new FileInfo(Path.Combine(BeatSaber.PluginsPath, manifest)),
  192. IsSelf = false,
  193. IsBare = true,
  194. };
  195. metadata.Manifest = JsonConvert.DeserializeObject<PluginManifest>(File.ReadAllText(manifest));
  196. Logger.loader.Debug($"Adding info for bare manifest {Path.GetFileName(manifest)}");
  197. PluginsMetadata.Add(metadata);
  198. }
  199. catch (Exception e)
  200. {
  201. Logger.loader.Error($"Could not load data for bare manifest {Path.GetFileName(manifest)}");
  202. Logger.loader.Error(e);
  203. }
  204. }
  205. }
  206. // keep track of these for the updater; it should still be able to update mods not loaded
  207. internal static HashSet<PluginMetadata> ignoredPlugins = new HashSet<PluginMetadata>();
  208. internal static void Resolve()
  209. { // resolves duplicates and conflicts, etc
  210. PluginsMetadata.Sort((a, b) => b.Version.CompareTo(a.Version));
  211. var ids = new HashSet<string>();
  212. var ignore = new HashSet<PluginMetadata>();
  213. var resolved = new List<PluginMetadata>(PluginsMetadata.Count);
  214. foreach (var meta in PluginsMetadata)
  215. {
  216. if (meta.Id != null)
  217. {
  218. if (ids.Contains(meta.Id))
  219. {
  220. Logger.loader.Warn($"Found duplicates of {meta.Id}, using newest");
  221. ignore.Add(meta);
  222. ignoredPlugins.Add(meta);
  223. continue; // because of sorted order, hightest order will always be the first one
  224. }
  225. bool processedLater = false;
  226. foreach (var meta2 in PluginsMetadata)
  227. {
  228. if (ignore.Contains(meta2)) continue;
  229. if (meta == meta2)
  230. {
  231. processedLater = true;
  232. continue;
  233. }
  234. if (!meta2.Manifest.Conflicts.ContainsKey(meta.Id)) continue;
  235. var range = meta2.Manifest.Conflicts[meta.Id];
  236. if (!range.IsSatisfied(meta.Version)) continue;
  237. Logger.loader.Warn($"{meta.Id}@{meta.Version} conflicts with {meta2.Name}");
  238. if (processedLater)
  239. {
  240. Logger.loader.Warn($"Ignoring {meta2.Name}");
  241. ignore.Add(meta2);
  242. }
  243. else
  244. {
  245. Logger.loader.Warn($"Ignoring {meta.Name}");
  246. ignore.Add(meta);
  247. break;
  248. }
  249. }
  250. }
  251. if (ignore.Contains(meta))
  252. {
  253. ignoredPlugins.Add(meta);
  254. continue;
  255. }
  256. if (meta.Id != null)
  257. ids.Add(meta.Id);
  258. resolved.Add(meta);
  259. }
  260. PluginsMetadata = resolved;
  261. }
  262. internal static void ComputeLoadOrder()
  263. {
  264. #if DEBUG
  265. Logger.loader.Debug(string.Join(", ", PluginsMetadata.Select(p => p.ToString())));
  266. #endif
  267. bool InsertInto(HashSet<PluginMetadata> root, PluginMetadata meta, bool isRoot = false)
  268. { // this is slow, and hella recursive
  269. bool inserted = false;
  270. foreach (var sr in root)
  271. {
  272. inserted = inserted || InsertInto(sr.Dependencies, meta);
  273. if (meta.Id != null)
  274. if (sr.Manifest.Dependencies.ContainsKey(meta.Id) || sr.Manifest.LoadAfter.Contains(meta.Id))
  275. inserted = inserted || sr.Dependencies.Add(meta);
  276. if (sr.Id != null)
  277. if (meta.Manifest.LoadBefore.Contains(sr.Id))
  278. inserted = inserted || sr.Dependencies.Add(meta);
  279. }
  280. if (isRoot)
  281. {
  282. foreach (var sr in root)
  283. {
  284. InsertInto(meta.Dependencies, sr);
  285. if (sr.Id != null)
  286. if (meta.Manifest.Dependencies.ContainsKey(sr.Id) || meta.Manifest.LoadAfter.Contains(sr.Id))
  287. meta.Dependencies.Add(sr);
  288. if (meta.Id != null)
  289. if (sr.Manifest.LoadBefore.Contains(meta.Id))
  290. meta.Dependencies.Add(sr);
  291. }
  292. root.Add(meta);
  293. }
  294. return inserted;
  295. }
  296. var pluginTree = new HashSet<PluginMetadata>();
  297. foreach (var meta in PluginsMetadata)
  298. InsertInto(pluginTree, meta, true);
  299. void DeTree(List<PluginMetadata> into, HashSet<PluginMetadata> tree)
  300. {
  301. foreach (var st in tree)
  302. if (!into.Contains(st))
  303. {
  304. DeTree(into, st.Dependencies);
  305. into.Add(st);
  306. }
  307. }
  308. var deTreed = new List<PluginMetadata>();
  309. DeTree(deTreed, pluginTree);
  310. #if DEBUG
  311. Logger.loader.Debug(string.Join(", ", deTreed.Select(p => p.ToString())));
  312. #endif
  313. var metadata = new List<PluginMetadata>();
  314. var pluginsToLoad = new Dictionary<string, Version>();
  315. foreach (var meta in deTreed)
  316. {
  317. bool load = true;
  318. foreach (var dep in meta.Manifest.Dependencies)
  319. {
  320. #if DEBUG
  321. Logger.loader.Debug($"Looking for dependency {dep.Key} with version range {dep.Value.Intersect(new SemVer.Range("*.*.*"))}");
  322. #endif
  323. if (pluginsToLoad.ContainsKey(dep.Key) && dep.Value.IsSatisfied(pluginsToLoad[dep.Key]))
  324. continue;
  325. load = false;
  326. Logger.loader.Warn($"{meta.Name} is missing dependency {dep.Key}@{dep.Value}");
  327. }
  328. if (load)
  329. {
  330. metadata.Add(meta);
  331. if (meta.Id != null)
  332. pluginsToLoad.Add(meta.Id, meta.Version);
  333. }
  334. else
  335. ignoredPlugins.Add(meta);
  336. }
  337. PluginsMetadata = metadata;
  338. }
  339. internal static void InitFeatures()
  340. {
  341. var parsedFeatures = PluginsMetadata.Select(m =>
  342. Tuple.Create(m,
  343. m.Manifest.Features.Select(f =>
  344. Tuple.Create(f, Ref.Create<Feature.FeatureParse?>(null))
  345. ).ToList()
  346. )
  347. ).ToList();
  348. while (DefineFeature.NewFeature)
  349. {
  350. DefineFeature.NewFeature = false;
  351. foreach (var plugin in parsedFeatures)
  352. for (var i = 0; i < plugin.Item2.Count; i++)
  353. {
  354. var feature = plugin.Item2[i];
  355. var success = Feature.TryParseFeature(feature.Item1, plugin.Item1, out var featureObj,
  356. out var exception, out var valid, out var parsed, feature.Item2.Value);
  357. if (!success && !valid && featureObj == null && exception == null) // no feature of type found
  358. feature.Item2.Value = parsed;
  359. else if (success)
  360. {
  361. if (valid && featureObj.StoreOnPlugin)
  362. plugin.Item1.InternalFeatures.Add(featureObj);
  363. else if (!valid)
  364. Logger.features.Warn(
  365. $"Feature not valid on {plugin.Item1.Name}: {featureObj.InvalidMessage}");
  366. plugin.Item2.RemoveAt(i--);
  367. }
  368. else
  369. {
  370. Logger.features.Error($"Error parsing feature definition on {plugin.Item1.Name}");
  371. Logger.features.Error(exception);
  372. plugin.Item2.RemoveAt(i--);
  373. }
  374. }
  375. foreach (var plugin in PluginsMetadata)
  376. foreach (var feature in plugin.Features)
  377. feature.Evaluate();
  378. }
  379. foreach (var plugin in parsedFeatures)
  380. {
  381. if (plugin.Item2.Count <= 0) continue;
  382. Logger.features.Warn($"On plugin {plugin.Item1.Name}:");
  383. foreach (var feature in plugin.Item2)
  384. Logger.features.Warn($" Feature not found with name {feature.Item1}");
  385. }
  386. }
  387. internal static void Load(PluginMetadata meta)
  388. {
  389. if (meta.Assembly == null && meta.PluginType != null)
  390. meta.Assembly = Assembly.LoadFrom(meta.File.FullName);
  391. }
  392. internal static PluginInfo InitPlugin(PluginMetadata meta)
  393. {
  394. if (meta.PluginType == null)
  395. return new PluginInfo()
  396. {
  397. Metadata = meta,
  398. Plugin = null
  399. };
  400. var info = new PluginInfo();
  401. try
  402. {
  403. Load(meta);
  404. Feature denyingFeature = null;
  405. if (!meta.Features.All(f => (denyingFeature = f).BeforeLoad(meta)))
  406. {
  407. Logger.loader.Warn(
  408. $"Feature {denyingFeature?.GetType()} denied plugin {meta.Name} from loading! {denyingFeature?.InvalidMessage}");
  409. ignoredPlugins.Add(meta);
  410. return null;
  411. }
  412. var type = meta.Assembly.GetType(meta.PluginType.FullName);
  413. var instance = (IBeatSaberPlugin)Activator.CreateInstance(type);
  414. info.Metadata = meta;
  415. info.Plugin = instance;
  416. var init = type.GetMethod("Init", BindingFlags.Instance | BindingFlags.Public);
  417. if (init != null)
  418. {
  419. denyingFeature = null;
  420. if (!meta.Features.All(f => (denyingFeature = f).BeforeInit(info)))
  421. {
  422. Logger.loader.Warn(
  423. $"Feature {denyingFeature?.GetType()} denied plugin {meta.Name} from initializing! {denyingFeature?.InvalidMessage}");
  424. ignoredPlugins.Add(meta);
  425. return null;
  426. }
  427. PluginInitInjector.Inject(init, info);
  428. }
  429. foreach (var feature in meta.Features)
  430. try
  431. {
  432. feature.AfterInit(info);
  433. }
  434. catch (Exception e)
  435. {
  436. Logger.loader.Critical($"Feature errored in {nameof(Feature.AfterInit)}: {e}");
  437. }
  438. }
  439. catch (AmbiguousMatchException)
  440. {
  441. Logger.loader.Error($"Only one Init allowed per plugin (ambiguous match in {meta.Name})");
  442. // not adding to ignoredPlugins here because this should only happen in a development context
  443. // if someone fucks this up on release thats on them
  444. return null;
  445. }
  446. catch (Exception e)
  447. {
  448. Logger.loader.Error($"Could not init plugin {meta.Name}: {e}");
  449. ignoredPlugins.Add(meta);
  450. return null;
  451. }
  452. return info;
  453. }
  454. internal static List<PluginInfo> LoadPlugins() => PluginsMetadata.Select(InitPlugin).Where(p => p != null).ToList();
  455. }
  456. }