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.

658 lines
25 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.Text.RegularExpressions;
  12. using System.Threading.Tasks;
  13. using Version = SemVer.Version;
  14. namespace IPA.Loader
  15. {
  16. /// <summary>
  17. /// A type to manage the loading of plugins.
  18. /// </summary>
  19. public class PluginLoader
  20. {
  21. internal static Task LoadTask() => Task.Run(() =>
  22. {
  23. LoadMetadata();
  24. Resolve();
  25. FilterDisabled();
  26. ComputeLoadOrder();
  27. });
  28. /// <summary>
  29. /// A class which describes a loaded plugin.
  30. /// </summary>
  31. public class PluginMetadata
  32. {
  33. /// <summary>
  34. /// The assembly the plugin was loaded from.
  35. /// </summary>
  36. public Assembly Assembly { get; internal set; }
  37. /// <summary>
  38. /// The TypeDefinition for the main type of the plugin.
  39. /// </summary>
  40. public TypeDefinition PluginType { get; internal set; }
  41. /// <summary>
  42. /// The human readable name of the plugin.
  43. /// </summary>
  44. public string Name { get; internal set; }
  45. /// <summary>
  46. /// The BeatMods ID of the plugin, or null if it doesn't have one.
  47. /// </summary>
  48. public string Id { get; internal set; }
  49. /// <summary>
  50. /// The version of the plugin.
  51. /// </summary>
  52. public Version Version { get; internal set; }
  53. /// <summary>
  54. /// The file the plugin was loaded from.
  55. /// </summary>
  56. public FileInfo File { get; internal set; }
  57. // ReSharper disable once UnusedAutoPropertyAccessor.Global
  58. /// <summary>
  59. /// The features this plugin requests.
  60. /// </summary>
  61. public IReadOnlyList<Feature> Features => InternalFeatures;
  62. internal readonly List<Feature> InternalFeatures = new List<Feature>();
  63. internal bool IsSelf;
  64. internal bool IsBare;
  65. private PluginManifest manifest;
  66. internal HashSet<PluginMetadata> Dependencies { get; } = new HashSet<PluginMetadata>();
  67. internal PluginManifest Manifest
  68. {
  69. get => manifest;
  70. set
  71. {
  72. manifest = value;
  73. Name = value.Name;
  74. Version = value.Version;
  75. Id = value.Id;
  76. }
  77. }
  78. /// <inheritdoc />
  79. public override string ToString() => $"{Name}({Id}@{Version})({PluginType?.FullName}) from '{Utils.GetRelativePath(File?.FullName, BeatSaber.InstallPath)}'";
  80. }
  81. /// <summary>
  82. /// A container object for all the data relating to a plugin.
  83. /// </summary>
  84. public class PluginInfo
  85. {
  86. internal IBeatSaberPlugin Plugin { get; set; }
  87. /// <summary>
  88. /// Metadata for the plugin.
  89. /// </summary>
  90. public PluginMetadata Metadata { get; internal set; } = new PluginMetadata();
  91. }
  92. internal static List<PluginMetadata> PluginsMetadata = new List<PluginMetadata>();
  93. internal static List<PluginMetadata> DisabledPlugins = new List<PluginMetadata>();
  94. private static readonly Regex embeddedTextDescriptionPattern = new Regex(@"#!\[(.+)\]", RegexOptions.Compiled | RegexOptions.Singleline);
  95. internal static void LoadMetadata()
  96. {
  97. string[] plugins = Directory.GetFiles(BeatSaber.PluginsPath, "*.dll");
  98. try
  99. {
  100. var selfMeta = new PluginMetadata
  101. {
  102. Assembly = Assembly.GetExecutingAssembly(),
  103. File = new FileInfo(Path.Combine(BeatSaber.InstallPath, "IPA.exe")),
  104. PluginType = null,
  105. IsSelf = true
  106. };
  107. string manifest;
  108. using (var manifestReader =
  109. new StreamReader(
  110. selfMeta.Assembly.GetManifestResourceStream(typeof(PluginLoader), "manifest.json") ??
  111. throw new InvalidOperationException()))
  112. manifest = manifestReader.ReadToEnd();
  113. selfMeta.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  114. PluginsMetadata.Add(selfMeta);
  115. }
  116. catch (Exception e)
  117. {
  118. Logger.loader.Critical("Error loading own manifest");
  119. Logger.loader.Critical(e);
  120. }
  121. foreach (var plugin in plugins)
  122. {
  123. try
  124. {
  125. var metadata = new PluginMetadata
  126. {
  127. File = new FileInfo(Path.Combine(BeatSaber.PluginsPath, plugin)),
  128. IsSelf = false
  129. };
  130. var pluginModule = AssemblyDefinition.ReadAssembly(plugin, new ReaderParameters
  131. {
  132. ReadingMode = ReadingMode.Immediate,
  133. ReadWrite = false,
  134. AssemblyResolver = new CecilLibLoader()
  135. }).MainModule;
  136. var iBeatSaberPlugin = pluginModule.ImportReference(typeof(IBeatSaberPlugin));
  137. foreach (var type in pluginModule.Types)
  138. {
  139. foreach (var inter in type.Interfaces)
  140. {
  141. var ifType = inter.InterfaceType;
  142. if (iBeatSaberPlugin.FullName == ifType.FullName)
  143. {
  144. metadata.PluginType = type;
  145. break;
  146. }
  147. }
  148. if (metadata.PluginType != null) break;
  149. }
  150. if (metadata.PluginType == null)
  151. {
  152. Logger.loader.Notice(
  153. #if DIRE_LOADER_WARNINGS
  154. $"Could not find plugin type for {Path.GetFileName(plugin)}"
  155. #else
  156. $"New plugin type not present in {Path.GetFileName(plugin)}; maybe an old plugin?"
  157. #endif
  158. );
  159. continue;
  160. }
  161. foreach (var resource in pluginModule.Resources)
  162. {
  163. if (!(resource is EmbeddedResource embedded) ||
  164. embedded.Name != $"{metadata.PluginType.Namespace}.manifest.json") continue;
  165. string manifest;
  166. using (var manifestReader = new StreamReader(embedded.GetResourceStream()))
  167. manifest = manifestReader.ReadToEnd();
  168. metadata.Manifest = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  169. break;
  170. }
  171. if (metadata.Manifest == null)
  172. {
  173. Logger.loader.Error("Could not find manifest.json in namespace " +
  174. $"{metadata.PluginType.Namespace} for {Path.GetFileName(plugin)}");
  175. continue;
  176. }
  177. Logger.loader.Debug($"Adding info for {Path.GetFileName(plugin)}");
  178. PluginsMetadata.Add(metadata);
  179. }
  180. catch (Exception e)
  181. {
  182. Logger.loader.Error($"Could not load data for plugin {Path.GetFileName(plugin)}");
  183. Logger.loader.Error(e);
  184. }
  185. foreach (var meta in PluginsMetadata)
  186. { // process description include
  187. var lines = meta.Manifest.Description.Split('\n');
  188. var m = embeddedTextDescriptionPattern.Match(lines[0]);
  189. if (m.Success)
  190. {
  191. if (meta.IsBare)
  192. {
  193. Logger.loader.Warn($"Bare manifest cannot specify description file");
  194. meta.Manifest.Description = string.Join("\n", lines.Skip(1)); // ignore first line
  195. continue;
  196. }
  197. var name = m.Groups[1].Value;
  198. string description;
  199. if (!meta.IsSelf)
  200. {
  201. var resc = meta.PluginType.Module.Resources.Select(r => r as EmbeddedResource)
  202. .Where(r => r != null)
  203. .FirstOrDefault(r => r.Name == name);
  204. if (resc == null)
  205. {
  206. Logger.loader.Warn($"Could not find description file for plugin {meta.Name} ({name}); ignoring include");
  207. meta.Manifest.Description = string.Join("\n", lines.Skip(1)); // ignore first line
  208. continue;
  209. }
  210. using (var reader = new StreamReader(resc.GetResourceStream()))
  211. description = reader.ReadToEnd();
  212. }
  213. else
  214. {
  215. using (var descriptionReader =
  216. new StreamReader(
  217. meta.Assembly.GetManifestResourceStream(name) ??
  218. throw new InvalidOperationException()))
  219. description = descriptionReader.ReadToEnd();
  220. }
  221. meta.Manifest.Description = description;
  222. }
  223. }
  224. }
  225. IEnumerable<string> bareManifests = Directory.GetFiles(BeatSaber.PluginsPath, "*.json");
  226. bareManifests = bareManifests.Concat(Directory.GetFiles(BeatSaber.PluginsPath, "*.manifest"));
  227. foreach (var manifest in bareManifests)
  228. {
  229. try
  230. {
  231. var metadata = new PluginMetadata
  232. {
  233. File = new FileInfo(Path.Combine(BeatSaber.PluginsPath, manifest)),
  234. IsSelf = false,
  235. IsBare = true,
  236. };
  237. metadata.Manifest = JsonConvert.DeserializeObject<PluginManifest>(File.ReadAllText(manifest));
  238. Logger.loader.Debug($"Adding info for bare manifest {Path.GetFileName(manifest)}");
  239. PluginsMetadata.Add(metadata);
  240. }
  241. catch (Exception e)
  242. {
  243. Logger.loader.Error($"Could not load data for bare manifest {Path.GetFileName(manifest)}");
  244. Logger.loader.Error(e);
  245. }
  246. }
  247. }
  248. // keep track of these for the updater; it should still be able to update mods not loaded
  249. // TODO: add ignore reason
  250. internal static HashSet<PluginMetadata> ignoredPlugins = new HashSet<PluginMetadata>();
  251. internal static void Resolve()
  252. { // resolves duplicates and conflicts, etc
  253. PluginsMetadata.Sort((a, b) => b.Version.CompareTo(a.Version));
  254. var ids = new HashSet<string>();
  255. var ignore = new HashSet<PluginMetadata>();
  256. var resolved = new List<PluginMetadata>(PluginsMetadata.Count);
  257. foreach (var meta in PluginsMetadata)
  258. {
  259. if (meta.Id != null)
  260. {
  261. if (ids.Contains(meta.Id))
  262. {
  263. Logger.loader.Warn($"Found duplicates of {meta.Id}, using newest");
  264. ignore.Add(meta);
  265. ignoredPlugins.Add(meta);
  266. continue; // because of sorted order, hightest order will always be the first one
  267. }
  268. bool processedLater = false;
  269. foreach (var meta2 in PluginsMetadata)
  270. {
  271. if (ignore.Contains(meta2)) continue;
  272. if (meta == meta2)
  273. {
  274. processedLater = true;
  275. continue;
  276. }
  277. if (!meta2.Manifest.Conflicts.ContainsKey(meta.Id)) continue;
  278. var range = meta2.Manifest.Conflicts[meta.Id];
  279. if (!range.IsSatisfied(meta.Version)) continue;
  280. Logger.loader.Warn($"{meta.Id}@{meta.Version} conflicts with {meta2.Name}");
  281. if (processedLater)
  282. {
  283. Logger.loader.Warn($"Ignoring {meta2.Name}");
  284. ignore.Add(meta2);
  285. }
  286. else
  287. {
  288. Logger.loader.Warn($"Ignoring {meta.Name}");
  289. ignore.Add(meta);
  290. break;
  291. }
  292. }
  293. }
  294. if (ignore.Contains(meta))
  295. {
  296. ignoredPlugins.Add(meta);
  297. continue;
  298. }
  299. if (meta.Id != null)
  300. ids.Add(meta.Id);
  301. resolved.Add(meta);
  302. }
  303. PluginsMetadata = resolved;
  304. }
  305. private static void FilterDisabled()
  306. { // TODO: move disabled to a seperate list from ignored
  307. var enabled = new List<PluginMetadata>(PluginsMetadata.Count);
  308. var disabled = DisabledConfig.Ref.Value.DisabledModIds;
  309. foreach (var meta in PluginsMetadata)
  310. {
  311. if (disabled.Contains(meta.Id ?? meta.Name))
  312. DisabledPlugins.Add(meta);
  313. else
  314. enabled.Add(meta);
  315. }
  316. PluginsMetadata = enabled;
  317. }
  318. internal static void ComputeLoadOrder()
  319. {
  320. #if DEBUG
  321. Logger.loader.Debug(string.Join(", ", PluginsMetadata.Select(p => p.ToString())));
  322. #endif
  323. bool InsertInto(HashSet<PluginMetadata> root, PluginMetadata meta, bool isRoot = false)
  324. { // this is slow, and hella recursive
  325. bool inserted = false;
  326. foreach (var sr in root)
  327. {
  328. inserted = inserted || InsertInto(sr.Dependencies, meta);
  329. if (meta.Id != null)
  330. if (sr.Manifest.Dependencies.ContainsKey(meta.Id) || sr.Manifest.LoadAfter.Contains(meta.Id))
  331. inserted = inserted || sr.Dependencies.Add(meta);
  332. if (sr.Id != null)
  333. if (meta.Manifest.LoadBefore.Contains(sr.Id))
  334. inserted = inserted || sr.Dependencies.Add(meta);
  335. }
  336. if (isRoot)
  337. {
  338. foreach (var sr in root)
  339. {
  340. InsertInto(meta.Dependencies, sr);
  341. if (sr.Id != null)
  342. if (meta.Manifest.Dependencies.ContainsKey(sr.Id) || meta.Manifest.LoadAfter.Contains(sr.Id))
  343. meta.Dependencies.Add(sr);
  344. if (meta.Id != null)
  345. if (sr.Manifest.LoadBefore.Contains(meta.Id))
  346. meta.Dependencies.Add(sr);
  347. }
  348. root.Add(meta);
  349. }
  350. return inserted;
  351. }
  352. var pluginTree = new HashSet<PluginMetadata>();
  353. foreach (var meta in PluginsMetadata)
  354. InsertInto(pluginTree, meta, true);
  355. void DeTree(List<PluginMetadata> into, HashSet<PluginMetadata> tree)
  356. {
  357. foreach (var st in tree)
  358. if (!into.Contains(st))
  359. {
  360. DeTree(into, st.Dependencies);
  361. into.Add(st);
  362. }
  363. }
  364. var deTreed = new List<PluginMetadata>();
  365. DeTree(deTreed, pluginTree);
  366. #if DEBUG
  367. Logger.loader.Debug(string.Join(", ", deTreed.Select(p => p.ToString())));
  368. #endif
  369. var metadata = new List<PluginMetadata>();
  370. var pluginsToLoad = new Dictionary<string, Version>();
  371. var disabledLookup = DisabledPlugins.Where(m => m.Id != null).ToDictionary(m => m.Id, m => m.Version);
  372. foreach (var meta in deTreed)
  373. {
  374. bool load = true;
  375. bool disable = false;
  376. foreach (var dep in meta.Manifest.Dependencies)
  377. {
  378. #if DEBUG
  379. Logger.loader.Debug($"Looking for dependency {dep.Key} with version range {dep.Value.Intersect(new SemVer.Range("*.*.*"))}");
  380. #endif
  381. if (pluginsToLoad.ContainsKey(dep.Key) && dep.Value.IsSatisfied(pluginsToLoad[dep.Key]))
  382. continue;
  383. load = false;
  384. if (disabledLookup.ContainsKey(dep.Key) && dep.Value.IsSatisfied(disabledLookup[dep.Key]))
  385. {
  386. disable = true;
  387. Logger.loader.Warn($"Dependency {dep.Key} was found, but disabled. Disabling {meta.Name} too.");
  388. }
  389. else
  390. Logger.loader.Warn($"{meta.Name} is missing dependency {dep.Key}@{dep.Value}");
  391. break;
  392. }
  393. if (load)
  394. {
  395. metadata.Add(meta);
  396. if (meta.Id != null)
  397. pluginsToLoad.Add(meta.Id, meta.Version);
  398. }
  399. else if (disable)
  400. {
  401. DisabledPlugins.Add(meta);
  402. DisabledConfig.Ref.Value.DisabledModIds.Add(meta.Id ?? meta.Name);
  403. }
  404. else
  405. ignoredPlugins.Add(meta);
  406. }
  407. PluginsMetadata = metadata;
  408. }
  409. internal static void InitFeatures()
  410. {
  411. var parsedFeatures = PluginsMetadata.Select(m =>
  412. Tuple.Create(m,
  413. m.Manifest.Features.Select(f =>
  414. Tuple.Create(f, Ref.Create<Feature.FeatureParse?>(null))
  415. ).ToList()
  416. )
  417. ).ToList();
  418. while (DefineFeature.NewFeature)
  419. {
  420. DefineFeature.NewFeature = false;
  421. foreach (var plugin in parsedFeatures)
  422. for (var i = 0; i < plugin.Item2.Count; i++)
  423. {
  424. var feature = plugin.Item2[i];
  425. var success = Feature.TryParseFeature(feature.Item1, plugin.Item1, out var featureObj,
  426. out var exception, out var valid, out var parsed, feature.Item2.Value);
  427. if (!success && !valid && featureObj == null && exception == null) // no feature of type found
  428. feature.Item2.Value = parsed;
  429. else if (success)
  430. {
  431. if (valid && featureObj.StoreOnPlugin)
  432. plugin.Item1.InternalFeatures.Add(featureObj);
  433. else if (!valid)
  434. Logger.features.Warn(
  435. $"Feature not valid on {plugin.Item1.Name}: {featureObj.InvalidMessage}");
  436. plugin.Item2.RemoveAt(i--);
  437. }
  438. else
  439. {
  440. Logger.features.Error($"Error parsing feature definition on {plugin.Item1.Name}");
  441. Logger.features.Error(exception);
  442. plugin.Item2.RemoveAt(i--);
  443. }
  444. }
  445. foreach (var plugin in PluginsMetadata)
  446. foreach (var feature in plugin.Features)
  447. feature.Evaluate();
  448. }
  449. foreach (var plugin in parsedFeatures)
  450. {
  451. if (plugin.Item2.Count <= 0) continue;
  452. Logger.features.Warn($"On plugin {plugin.Item1.Name}:");
  453. foreach (var feature in plugin.Item2)
  454. Logger.features.Warn($" Feature not found with name {feature.Item1}");
  455. }
  456. }
  457. internal static void ReleaseAll(bool full = false)
  458. {
  459. if (full)
  460. ignoredPlugins = new HashSet<PluginMetadata>();
  461. else
  462. {
  463. foreach (var m in PluginsMetadata)
  464. ignoredPlugins.Add(m);
  465. foreach (var m in ignoredPlugins)
  466. { // clean them up so we can still use the metadata for updates
  467. m.InternalFeatures.Clear();
  468. m.PluginType = null;
  469. m.Assembly = null;
  470. }
  471. }
  472. PluginsMetadata = new List<PluginMetadata>();
  473. DisabledPlugins = new List<PluginMetadata>();
  474. Feature.Reset();
  475. GC.Collect();
  476. }
  477. internal static void Load(PluginMetadata meta)
  478. {
  479. if (meta.Assembly == null && meta.PluginType != null)
  480. meta.Assembly = Assembly.LoadFrom(meta.File.FullName);
  481. }
  482. internal static PluginInfo InitPlugin(PluginMetadata meta)
  483. {
  484. if (meta.PluginType == null)
  485. return new PluginInfo()
  486. {
  487. Metadata = meta,
  488. Plugin = null
  489. };
  490. var info = new PluginInfo();
  491. if (meta.Manifest.GameVersion != BeatSaber.GameVersion)
  492. Logger.loader.Warn($"Mod {meta.Name} developed for game version {meta.Manifest.GameVersion}, so it may not work properly.");
  493. try
  494. {
  495. Load(meta);
  496. Feature denyingFeature = null;
  497. if (!meta.Features.All(f => (denyingFeature = f).BeforeLoad(meta)))
  498. {
  499. Logger.loader.Warn(
  500. $"Feature {denyingFeature?.GetType()} denied plugin {meta.Name} from loading! {denyingFeature?.InvalidMessage}");
  501. ignoredPlugins.Add(meta);
  502. return null;
  503. }
  504. var type = meta.Assembly.GetType(meta.PluginType.FullName);
  505. var instance = (IBeatSaberPlugin)Activator.CreateInstance(type);
  506. info.Metadata = meta;
  507. info.Plugin = instance;
  508. var init = type.GetMethod("Init", BindingFlags.Instance | BindingFlags.Public);
  509. if (init != null)
  510. {
  511. denyingFeature = null;
  512. if (!meta.Features.All(f => (denyingFeature = f).BeforeInit(info)))
  513. {
  514. Logger.loader.Warn(
  515. $"Feature {denyingFeature?.GetType()} denied plugin {meta.Name} from initializing! {denyingFeature?.InvalidMessage}");
  516. ignoredPlugins.Add(meta);
  517. return null;
  518. }
  519. PluginInitInjector.Inject(init, info);
  520. }
  521. foreach (var feature in meta.Features)
  522. try
  523. {
  524. feature.AfterInit(info);
  525. }
  526. catch (Exception e)
  527. {
  528. Logger.loader.Critical($"Feature errored in {nameof(Feature.AfterInit)}: {e}");
  529. }
  530. if (instance is IDisablablePlugin disable)
  531. try
  532. {
  533. disable.OnEnable();
  534. }
  535. catch (Exception e)
  536. {
  537. Logger.loader.Error($"Error occurred trying to enable {meta.Name}");
  538. Logger.loader.Error(e);
  539. }
  540. }
  541. catch (AmbiguousMatchException)
  542. {
  543. Logger.loader.Error($"Only one Init allowed per plugin (ambiguous match in {meta.Name})");
  544. // not adding to ignoredPlugins here because this should only happen in a development context
  545. // if someone fucks this up on release thats on them
  546. return null;
  547. }
  548. catch (Exception e)
  549. {
  550. Logger.loader.Error($"Could not init plugin {meta.Name}: {e}");
  551. ignoredPlugins.Add(meta);
  552. return null;
  553. }
  554. return info;
  555. }
  556. internal static List<PluginInfo> LoadPlugins()
  557. {
  558. InitFeatures();
  559. DisabledPlugins.ForEach(Load); // make sure they get loaded into memory so their metadata and stuff can be read more easily
  560. return PluginsMetadata.Select(InitPlugin).Where(p => p != null).ToList();
  561. }
  562. }
  563. }