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.

1049 lines
46 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. #nullable enable
  2. using IPA.Config;
  3. using IPA.Loader.Features;
  4. using IPA.Logging;
  5. using IPA.Utilities;
  6. using Mono.Cecil;
  7. using Newtonsoft.Json;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text.RegularExpressions;
  14. using System.Diagnostics.CodeAnalysis;
  15. using System.Diagnostics;
  16. using IPA.AntiMalware;
  17. using Hive.Versioning;
  18. #if NET4
  19. using Task = System.Threading.Tasks.Task;
  20. using TaskEx = System.Threading.Tasks.Task;
  21. #endif
  22. #if NET3
  23. using Net3_Proxy;
  24. using Path = Net3_Proxy.Path;
  25. using File = Net3_Proxy.File;
  26. using Directory = Net3_Proxy.Directory;
  27. #endif
  28. namespace IPA.Loader
  29. {
  30. /// <summary>
  31. /// A type to manage the loading of plugins.
  32. /// </summary>
  33. internal partial class PluginLoader
  34. {
  35. internal static PluginMetadata SelfMeta = null!;
  36. internal static Task LoadTask() =>
  37. TaskEx.Run(() =>
  38. {
  39. YeetIfNeeded();
  40. var sw = Stopwatch.StartNew();
  41. LoadMetadata();
  42. sw.Stop();
  43. Logger.Loader.Info($"Loading metadata took {sw.Elapsed}");
  44. sw.Reset();
  45. sw.Start();
  46. // Features contribute to load order considerations
  47. InitFeatures();
  48. DoOrderResolution();
  49. sw.Stop();
  50. Logger.Loader.Info($"Calculating load order took {sw.Elapsed}");
  51. });
  52. internal static void YeetIfNeeded()
  53. {
  54. string pluginDir = UnityGame.PluginsPath;
  55. if (SelfConfig.YeetMods_ && UnityGame.IsGameVersionBoundary)
  56. {
  57. var oldPluginsName = Path.Combine(UnityGame.InstallPath, $"Old {UnityGame.OldVersion} Plugins");
  58. var newPluginsName = Path.Combine(UnityGame.InstallPath, $"Old {UnityGame.GameVersion} Plugins");
  59. if (Directory.Exists(oldPluginsName))
  60. Directory.Delete(oldPluginsName, true);
  61. Directory.Move(pluginDir, oldPluginsName);
  62. if (Directory.Exists(newPluginsName))
  63. Directory.Move(newPluginsName, pluginDir);
  64. else
  65. _ = Directory.CreateDirectory(pluginDir);
  66. }
  67. }
  68. internal static List<PluginMetadata> PluginsMetadata = new();
  69. internal static List<PluginMetadata> DisabledPlugins = new();
  70. private static readonly Regex embeddedTextDescriptionPattern = new(@"#!\[(.+)\]", RegexOptions.Compiled | RegexOptions.Singleline);
  71. internal static void LoadMetadata()
  72. {
  73. string[] plugins = Directory.GetFiles(UnityGame.PluginsPath, "*.dll", SearchOption.AllDirectories);
  74. try
  75. {
  76. var selfMeta = new PluginMetadata
  77. {
  78. Assembly = Assembly.GetExecutingAssembly(),
  79. File = new FileInfo(Path.Combine(UnityGame.InstallPath, "IPA.exe")),
  80. PluginType = null,
  81. IsSelf = true
  82. };
  83. string manifest;
  84. using (var manifestReader =
  85. new StreamReader(
  86. selfMeta.Assembly.GetManifestResourceStream(typeof(PluginLoader), "manifest.json") ??
  87. throw new InvalidOperationException()))
  88. manifest = manifestReader.ReadToEnd();
  89. var manifestObj = JsonConvert.DeserializeObject<PluginManifest>(manifest);
  90. selfMeta.Manifest = manifestObj ?? throw new InvalidOperationException("Deserialized manifest was null");
  91. PluginsMetadata.Add(selfMeta);
  92. SelfMeta = selfMeta;
  93. }
  94. catch (Exception e)
  95. {
  96. Logger.Loader.Critical("Error loading own manifest");
  97. Logger.Loader.Critical(e);
  98. }
  99. using var resolver = new CecilLibLoader();
  100. foreach (var libDirectory in Directory.GetDirectories(UnityGame.LibraryPath, "*", SearchOption.AllDirectories).Where((x) => !x.Contains("Native")).Prepend(UnityGame.LibraryPath))
  101. {
  102. Logger.Loader.Debug($"Adding lib search directory: {libDirectory.Replace(UnityGame.InstallPath, "").TrimStart(Path.DirectorySeparatorChar)}");
  103. resolver.AddSearchDirectory(libDirectory);
  104. }
  105. foreach (var pluginDirectory in Directory.GetDirectories(UnityGame.PluginsPath, "*", SearchOption.AllDirectories).Where((x) => !x.Contains(".cache")).Prepend(UnityGame.PluginsPath))
  106. {
  107. Logger.Loader.Debug($"Adding plugin search directory: {pluginDirectory.Replace(UnityGame.InstallPath, "").TrimStart(Path.DirectorySeparatorChar)}");
  108. resolver.AddSearchDirectory(pluginDirectory);
  109. }
  110. foreach (var plugin in plugins)
  111. {
  112. var metadata = new PluginMetadata
  113. {
  114. File = new FileInfo(plugin),
  115. IsSelf = false
  116. };
  117. var pluginRelative = plugin.Replace(UnityGame.InstallPath, "").TrimStart(Path.DirectorySeparatorChar);
  118. try
  119. {
  120. var scanResult = AntiMalwareEngine.Engine.ScanFile(metadata.File);
  121. if (scanResult is ScanResult.Detected)
  122. {
  123. Logger.Loader.Warn($"Scan of {plugin} found malware; not loading");
  124. continue;
  125. }
  126. if (!SelfConfig.AntiMalware_.RunPartialThreatCode_ && scanResult is not ScanResult.KnownSafe and not ScanResult.NotDetected)
  127. {
  128. Logger.Loader.Warn($"Scan of {plugin} found partial threat; not loading. To load this, set AntiMalware.RunPartialThreatCode in the config.");
  129. continue;
  130. }
  131. var pluginModule = AssemblyDefinition.ReadAssembly(metadata.File.FullName, new ReaderParameters
  132. {
  133. ReadingMode = ReadingMode.Immediate,
  134. ReadWrite = false,
  135. AssemblyResolver = resolver
  136. }).MainModule;
  137. string pluginNs = "";
  138. PluginManifest? pluginManifest = null;
  139. foreach (var resource in pluginModule.Resources)
  140. {
  141. const string manifestSuffix = ".manifest.json";
  142. if (resource is not EmbeddedResource embedded ||
  143. !embedded.Name.EndsWith(manifestSuffix, StringComparison.Ordinal)) continue;
  144. pluginNs = embedded.Name.Substring(0, embedded.Name.Length - manifestSuffix.Length);
  145. string manifest;
  146. using (var manifestReader = new StreamReader(embedded.GetResourceStream()))
  147. manifest = manifestReader.ReadToEnd();
  148. pluginManifest = JsonConvert.DeserializeObject<PluginManifest?>(manifest);
  149. break;
  150. }
  151. if (pluginManifest == null)
  152. {
  153. #if DIRE_LOADER_WARNINGS
  154. Logger.loader.Error($"Could not find manifest.json for {pluginRelative}");
  155. #else
  156. Logger.Loader.Notice($"No manifest.json in {pluginRelative}");
  157. #endif
  158. continue;
  159. }
  160. if (pluginManifest.Id == null)
  161. {
  162. Logger.Loader.Warn($"Plugin '{pluginManifest.Name}' does not have a listed ID, using name");
  163. pluginManifest.Id = pluginManifest.Name;
  164. }
  165. metadata.Manifest = pluginManifest;
  166. bool TryPopulatePluginType(TypeDefinition type, PluginMetadata meta)
  167. {
  168. if (!type.HasCustomAttributes)
  169. return false;
  170. var attr = type.CustomAttributes.FirstOrDefault(a => a.Constructor.DeclaringType.FullName == typeof(PluginAttribute).FullName);
  171. if (attr is null)
  172. return false;
  173. if (!attr.HasConstructorArguments)
  174. {
  175. Logger.Loader.Warn($"Attribute plugin found in {type.FullName}, but attribute has no arguments");
  176. return false;
  177. }
  178. var args = attr.ConstructorArguments;
  179. if (args.Count != 1)
  180. {
  181. Logger.Loader.Warn($"Attribute plugin found in {type.FullName}, but attribute has unexpected number of arguments");
  182. return false;
  183. }
  184. var rtOptionsArg = args[0];
  185. if (rtOptionsArg.Type.FullName != typeof(RuntimeOptions).FullName)
  186. {
  187. Logger.Loader.Warn($"Attribute plugin found in {type.FullName}, but first argument is of unexpected type {rtOptionsArg.Type.FullName}");
  188. return false;
  189. }
  190. var rtOptionsValInt = (int)rtOptionsArg.Value; // `int` is the underlying type of RuntimeOptions
  191. meta.RuntimeOptions = (RuntimeOptions)rtOptionsValInt;
  192. meta.PluginType = type;
  193. return true;
  194. }
  195. void TryGetNamespacedPluginType(string ns, PluginMetadata meta)
  196. {
  197. foreach (var type in pluginModule.Types)
  198. {
  199. if (type.Namespace != ns) continue;
  200. if (TryPopulatePluginType(type, meta))
  201. return;
  202. }
  203. }
  204. var hint = metadata.Manifest.Misc?.PluginMainHint;
  205. if (hint != null)
  206. {
  207. var type = pluginModule.GetType(hint);
  208. if (type == null || !TryPopulatePluginType(type, metadata))
  209. TryGetNamespacedPluginType(hint, metadata);
  210. }
  211. if (metadata.PluginType == null)
  212. TryGetNamespacedPluginType(pluginNs, metadata);
  213. if (metadata.PluginType == null)
  214. {
  215. Logger.Loader.Error($"No plugin found in the manifest {(hint != null ? $"hint path ({hint}) or " : "")}namespace ({pluginNs}) in {pluginRelative}");
  216. continue;
  217. }
  218. Logger.Loader.Debug($"Adding info for {pluginRelative}");
  219. PluginsMetadata.Add(metadata);
  220. }
  221. catch (Exception e)
  222. {
  223. Logger.Loader.Error($"Could not load data for plugin {pluginRelative}");
  224. Logger.Loader.Error(e);
  225. ignoredPlugins.Add(metadata, new IgnoreReason(Reason.Error)
  226. {
  227. ReasonText = "An error occurred loading the data",
  228. Error = e
  229. });
  230. }
  231. }
  232. IEnumerable<string> bareManifests = Directory.GetFiles(UnityGame.PluginsPath, "*.json", SearchOption.AllDirectories);
  233. bareManifests = bareManifests.Concat(Directory.GetFiles(UnityGame.PluginsPath, "*.manifest", SearchOption.AllDirectories));
  234. foreach (var manifest in bareManifests)
  235. {
  236. try
  237. {
  238. var metadata = new PluginMetadata
  239. {
  240. File = new FileInfo(manifest),
  241. IsSelf = false,
  242. IsBare = true,
  243. };
  244. var manifestRelative = manifest.Replace(UnityGame.InstallPath, "").TrimStart(Path.DirectorySeparatorChar);
  245. var manifestObj = JsonConvert.DeserializeObject<PluginManifest>(File.ReadAllText(manifest));
  246. if (manifestObj is null)
  247. {
  248. Logger.Loader.Error($"Bare manifest {manifestRelative} deserialized to null");
  249. continue;
  250. }
  251. metadata.Manifest = manifestObj;
  252. if (metadata.Manifest.Files.Length < 1)
  253. Logger.Loader.Warn($"Bare manifest {manifestRelative} does not declare any files. " +
  254. $"Dependency resolution and verification cannot be completed.");
  255. Logger.Loader.Debug($"Adding info for bare manifest {manifestRelative}");
  256. PluginsMetadata.Add(metadata);
  257. }
  258. catch (Exception e)
  259. {
  260. Logger.Loader.Error($"Could not load data for bare manifest {Path.GetFileName(manifest)}");
  261. Logger.Loader.Error(e);
  262. }
  263. }
  264. foreach (var meta in PluginsMetadata)
  265. { // process description include
  266. var lines = meta.Manifest.Description.Split('\n');
  267. var m = embeddedTextDescriptionPattern.Match(lines[0]);
  268. if (m.Success)
  269. {
  270. if (meta.IsBare)
  271. {
  272. Logger.Loader.Warn($"Bare manifest cannot specify description file");
  273. meta.Manifest.Description = string.Join("\n", lines.Skip(1).StrJP()); // ignore first line
  274. continue;
  275. }
  276. var name = m.Groups[1].Value;
  277. string description;
  278. if (!meta.IsSelf)
  279. {
  280. // plugin type must be non-null for non-self plugins
  281. var resc = meta.PluginType!.Module.Resources.Select(r => r as EmbeddedResource)
  282. .NonNull()
  283. .FirstOrDefault(r => r.Name == name);
  284. if (resc == null)
  285. {
  286. Logger.Loader.Warn($"Could not find description file for plugin {meta.Name} ({name}); ignoring include");
  287. meta.Manifest.Description = string.Join("\n", lines.Skip(1).StrJP()); // ignore first line
  288. continue;
  289. }
  290. using var reader = new StreamReader(resc.GetResourceStream());
  291. description = reader.ReadToEnd();
  292. }
  293. else
  294. {
  295. using var descriptionReader = new StreamReader(meta.Assembly.GetManifestResourceStream(name));
  296. description = descriptionReader.ReadToEnd();
  297. }
  298. meta.Manifest.Description = description;
  299. }
  300. }
  301. }
  302. }
  303. #region Ignore stuff
  304. /// <summary>
  305. /// An enum that represents several categories of ignore reasons that the loader may encounter.
  306. /// </summary>
  307. /// <seealso cref="IgnoreReason"/>
  308. public enum Reason
  309. {
  310. /// <summary>
  311. /// An error was thrown either loading plugin information from disk, or when initializing the plugin.
  312. /// </summary>
  313. /// <remarks>
  314. /// When this is the set <see cref="Reason"/> in an <see cref="IgnoreReason"/> structure, the member
  315. /// <see cref="IgnoreReason.Error"/> will contain the thrown exception.
  316. /// </remarks>
  317. Error,
  318. /// <summary>
  319. /// The plugin this reason is associated with has the same ID as another plugin whose information was
  320. /// already loaded.
  321. /// </summary>
  322. /// <remarks>
  323. /// When this is the set <see cref="Reason"/> in an <see cref="IgnoreReason"/> structure, the member
  324. /// <see cref="IgnoreReason.RelatedTo"/> will contain the metadata of the already loaded plugin.
  325. /// </remarks>
  326. Duplicate,
  327. /// <summary>
  328. /// The plugin this reason is associated with conflicts with another already loaded plugin.
  329. /// </summary>
  330. /// <remarks>
  331. /// When this is the set <see cref="Reason"/> in an <see cref="IgnoreReason"/> structure, the member
  332. /// <see cref="IgnoreReason.RelatedTo"/> will contain the metadata of the plugin it conflicts with.
  333. /// </remarks>
  334. Conflict,
  335. /// <summary>
  336. /// The plugin this reason is associated with is missing a dependency.
  337. /// </summary>
  338. /// <remarks>
  339. /// Since this is only given when a dependency is missing, <see cref="IgnoreReason.RelatedTo"/> will
  340. /// not be set.
  341. /// </remarks>
  342. Dependency,
  343. /// <summary>
  344. /// The plugin this reason is associated with was released for a game update, but is still considered
  345. /// present for the purposes of updating.
  346. /// </summary>
  347. Released,
  348. /// <summary>
  349. /// The plugin this reason is associated with was denied from loading by a <see cref="Features.Feature"/>
  350. /// that it marks.
  351. /// </summary>
  352. Feature,
  353. /// <summary>
  354. /// The plugin this reason is associated with is unsupported.
  355. /// </summary>
  356. /// <remarks>
  357. /// Currently, there is no path in the loader that emits this <see cref="Reason"/>, however there may
  358. /// be in the future.
  359. /// </remarks>
  360. Unsupported,
  361. /// <summary>
  362. /// One of the files that a plugin declared in its manifest is missing.
  363. /// </summary>
  364. MissingFiles
  365. }
  366. /// <summary>
  367. /// A structure describing the reason that a plugin was ignored.
  368. /// </summary>
  369. public struct IgnoreReason : IEquatable<IgnoreReason>
  370. {
  371. /// <summary>
  372. /// Gets the ignore reason, as represented by the <see cref="Loader.Reason"/> enum.
  373. /// </summary>
  374. public Reason Reason { get; }
  375. /// <summary>
  376. /// Gets the textual description of the particular ignore reason. This will typically
  377. /// include details about why the plugin was ignored, if it is present.
  378. /// </summary>
  379. public string? ReasonText { get; internal set; }
  380. /// <summary>
  381. /// Gets the <see cref="Exception"/> that caused this plugin to be ignored, if any.
  382. /// </summary>
  383. public Exception? Error { get; internal set; }
  384. /// <summary>
  385. /// Gets the metadata of the plugin that this ignore was related to, if any.
  386. /// </summary>
  387. public PluginMetadata? RelatedTo { get; internal set; }
  388. /// <summary>
  389. /// Initializes an <see cref="IgnoreReason"/> with the provided data.
  390. /// </summary>
  391. /// <param name="reason">the <see cref="Loader.Reason"/> enum value that describes this reason</param>
  392. /// <param name="reasonText">the textual description of this ignore reason, if any</param>
  393. /// <param name="error">the <see cref="Exception"/> that caused this <see cref="IgnoreReason"/>, if any</param>
  394. /// <param name="relatedTo">the <see cref="PluginMetadata"/> this reason is related to, if any</param>
  395. public IgnoreReason(Reason reason, string? reasonText = null, Exception? error = null, PluginMetadata? relatedTo = null)
  396. {
  397. Reason = reason;
  398. ReasonText = reasonText;
  399. Error = error;
  400. RelatedTo = relatedTo;
  401. }
  402. /// <inheritdoc/>
  403. public override bool Equals(object obj)
  404. => obj is IgnoreReason ir && Equals(ir);
  405. /// <summary>
  406. /// Compares this <see cref="IgnoreReason"/> with <paramref name="other"/> for equality.
  407. /// </summary>
  408. /// <param name="other">the reason to compare to</param>
  409. /// <returns><see langword="true"/> if the two reasons compare equal, <see langword="false"/> otherwise</returns>
  410. public bool Equals(IgnoreReason other)
  411. => Reason == other.Reason && ReasonText == other.ReasonText
  412. && Error == other.Error && RelatedTo == other.RelatedTo;
  413. /// <inheritdoc/>
  414. public override int GetHashCode()
  415. {
  416. int hashCode = 778404373;
  417. hashCode = (hashCode * -1521134295) + Reason.GetHashCode();
  418. hashCode = (hashCode * -1521134295) + ReasonText?.GetHashCode() ?? 0;
  419. hashCode = (hashCode * -1521134295) + Error?.GetHashCode() ?? 0;
  420. hashCode = (hashCode * -1521134295) + RelatedTo?.GetHashCode() ?? 0;
  421. return hashCode;
  422. }
  423. /// <summary>
  424. /// Checks if two <see cref="IgnoreReason"/>s are equal.
  425. /// </summary>
  426. /// <param name="left">the first <see cref="IgnoreReason"/> to compare</param>
  427. /// <param name="right">the second <see cref="IgnoreReason"/> to compare</param>
  428. /// <returns><see langword="true"/> if the two reasons compare equal, <see langword="false"/> otherwise</returns>
  429. public static bool operator ==(IgnoreReason left, IgnoreReason right)
  430. => left.Equals(right);
  431. /// <summary>
  432. /// Checks if two <see cref="IgnoreReason"/>s are not equal.
  433. /// </summary>
  434. /// <param name="left">the first <see cref="IgnoreReason"/> to compare</param>
  435. /// <param name="right">the second <see cref="IgnoreReason"/> to compare</param>
  436. /// <returns><see langword="true"/> if the two reasons are not equal, <see langword="false"/> otherwise</returns>
  437. public static bool operator !=(IgnoreReason left, IgnoreReason right)
  438. => !(left == right);
  439. }
  440. #endregion
  441. internal partial class PluginLoader
  442. {
  443. // keep track of these for the updater; it should still be able to update mods not loaded
  444. // the thing -> the reason
  445. internal static Dictionary<PluginMetadata, IgnoreReason> ignoredPlugins = new();
  446. internal static void DoOrderResolution()
  447. {
  448. #if DEBUG
  449. // print starting order
  450. Logger.Loader.Debug(string.Join(", ", PluginsMetadata.StrJP()));
  451. #endif
  452. PluginsMetadata.Sort((a, b) => b.HVersion.CompareTo(a.HVersion));
  453. #if DEBUG
  454. // print base resolution order
  455. Logger.Loader.Debug(string.Join(", ", PluginsMetadata.StrJP()));
  456. #endif
  457. var metadataCache = new Dictionary<string, (PluginMetadata Meta, bool Enabled)>(PluginsMetadata.Count);
  458. var pluginsToProcess = new List<PluginMetadata>(PluginsMetadata.Count);
  459. var disabledIds = DisabledConfig.Instance.DisabledModIds;
  460. var disabledPlugins = new List<PluginMetadata>();
  461. // build metadata cache
  462. foreach (var meta in PluginsMetadata)
  463. {
  464. if (!metadataCache.TryGetValue(meta.Id, out var existing))
  465. {
  466. if (disabledIds.Contains(meta.Id))
  467. {
  468. metadataCache.Add(meta.Id, (meta, false));
  469. disabledPlugins.Add(meta);
  470. }
  471. else
  472. {
  473. metadataCache.Add(meta.Id, (meta, true));
  474. pluginsToProcess.Add(meta);
  475. }
  476. }
  477. else
  478. {
  479. Logger.Loader.Warn(
  480. $"{meta.Name}@{meta.HVersion} ({meta.File.ToString().Replace(UnityGame.InstallPath, "").TrimStart(Path.DirectorySeparatorChar)}) --- " +
  481. $"Used plugin: {existing.Meta.File.ToString().Replace(UnityGame.InstallPath, "").TrimStart(Path.DirectorySeparatorChar)}@{existing.Meta.HVersion}");
  482. ignoredPlugins.Add(meta, new(Reason.Duplicate)
  483. {
  484. ReasonText = $"Duplicate entry of same ID ({meta.Id})",
  485. RelatedTo = existing.Meta
  486. });
  487. }
  488. }
  489. // preprocess LoadBefore into LoadAfter
  490. foreach (var (_, (meta, _)) in metadataCache)
  491. { // we iterate the metadata cache because it contains both disabled and enabled plugins
  492. var loadBefore = meta.Manifest.LoadBefore;
  493. foreach (var id in loadBefore)
  494. {
  495. if (metadataCache.TryGetValue(id, out var plugin))
  496. {
  497. // if the id exists in our metadata cache, make sure it knows to load after the plugin in kvp
  498. _ = plugin.Meta.LoadsAfter.Add(meta);
  499. }
  500. }
  501. }
  502. // preprocess conflicts to be mutual
  503. foreach (var (_, (meta, _)) in metadataCache)
  504. {
  505. foreach (var (id, range) in meta.Manifest.Conflicts)
  506. {
  507. if (metadataCache.TryGetValue(id, out var plugin)
  508. && range.Matches(plugin.Meta.HVersion))
  509. {
  510. // make sure that there's a mutual dependency
  511. var targetRange = VersionRange.ForVersion(meta.HVersion);
  512. var targetConflicts = plugin.Meta.Manifest.Conflicts;
  513. if (!targetConflicts.TryGetValue(meta.Id, out var realRange))
  514. {
  515. // there's not already a listed conflict
  516. targetConflicts.Add(meta.Id, targetRange);
  517. }
  518. else if (!realRange.Matches(meta.HVersion))
  519. {
  520. // there is already a listed conflict that isn't mutual
  521. targetRange = realRange | targetRange;
  522. targetConflicts[meta.Id] = targetRange;
  523. }
  524. }
  525. }
  526. }
  527. var loadedPlugins = new Dictionary<string, (PluginMetadata Meta, bool Disabled, bool Ignored)>();
  528. var outputOrder = new List<PluginMetadata>(PluginsMetadata.Count);
  529. var isProcessing = new HashSet<PluginMetadata>();
  530. {
  531. bool TryResolveId(string id, [MaybeNullWhen(false)] out PluginMetadata meta, out bool disabled, out bool ignored, bool partial = false)
  532. {
  533. meta = null;
  534. disabled = false;
  535. ignored = true;
  536. Logger.Loader.Trace($"Trying to resolve plugin '{id}' partial:{partial}");
  537. if (loadedPlugins.TryGetValue(id, out var foundMeta))
  538. {
  539. meta = foundMeta.Meta;
  540. disabled = foundMeta.Disabled;
  541. ignored = foundMeta.Ignored;
  542. Logger.Loader.Trace($"- Found already processed");
  543. return true;
  544. }
  545. if (metadataCache.TryGetValue(id, out var plugin))
  546. {
  547. Logger.Loader.Trace($"- In metadata cache");
  548. if (partial)
  549. {
  550. Logger.Loader.Trace($" - but requested in a partial lookup");
  551. return false;
  552. }
  553. disabled = !plugin.Enabled;
  554. meta = plugin.Meta;
  555. ignored = false;
  556. if (!disabled)
  557. {
  558. try
  559. {
  560. Resolve(plugin.Meta, ref disabled, out ignored);
  561. }
  562. catch (Exception e)
  563. {
  564. if (e is not DependencyResolutionLoopException)
  565. {
  566. Logger.Loader.Error($"While performing load order resolution for {id}:");
  567. Logger.Loader.Error(e);
  568. }
  569. if (!ignored)
  570. {
  571. ignoredPlugins.Add(plugin.Meta, new(Reason.Error)
  572. {
  573. Error = e
  574. });
  575. }
  576. ignored = true;
  577. }
  578. }
  579. if (!loadedPlugins.ContainsKey(id))
  580. {
  581. // this condition is specifically for when we fail resolution because of a graph loop
  582. Logger.Loader.Trace($"- '{id}' resolved as ignored:{ignored},disabled:{disabled}");
  583. loadedPlugins.Add(id, (plugin.Meta, disabled, ignored));
  584. }
  585. return true;
  586. }
  587. Logger.Loader.Trace($"- Not found");
  588. return false;
  589. }
  590. void Resolve(PluginMetadata plugin, ref bool disabled, out bool ignored)
  591. {
  592. Logger.Loader.Trace($">Resolving '{plugin.Name}'");
  593. // first we need to check for loops in the resolution graph to prevent stack overflows
  594. if (isProcessing.Contains(plugin))
  595. {
  596. Logger.Loader.Error($"Loop detected while processing '{plugin.Name}'; flagging as ignored");
  597. throw new DependencyResolutionLoopException();
  598. }
  599. isProcessing.Add(plugin);
  600. using var _removeProcessing = Utils.ScopeGuard(() => isProcessing.Remove(plugin));
  601. // if this method is being called, this is the first and only time that it has been called for this plugin.
  602. ignored = false;
  603. // perform file existence check before attempting to load dependencies
  604. foreach (var file in plugin.AssociatedFiles)
  605. {
  606. if (!file.Exists)
  607. {
  608. ignoredPlugins.Add(plugin, new IgnoreReason(Reason.MissingFiles)
  609. {
  610. ReasonText = $"File {Utils.GetRelativePath(file.FullName, UnityGame.InstallPath)} does not exist"
  611. });
  612. Logger.Loader.Warn($"File {Utils.GetRelativePath(file.FullName, UnityGame.InstallPath)}" +
  613. $" (declared by '{plugin.Name}') does not exist! Mod installation is incomplete, not loading it.");
  614. ignored = true;
  615. return;
  616. }
  617. }
  618. // first load dependencies
  619. var dependsOnSelf = false;
  620. foreach (var (id, range) in plugin.Manifest.Dependencies)
  621. {
  622. if (id == SelfMeta.Id)
  623. dependsOnSelf = true;
  624. if (!TryResolveId(id, out var depMeta, out var depDisabled, out var depIgnored)
  625. || !range.Matches(depMeta.HVersion))
  626. {
  627. Logger.Loader.Warn($"'{plugin.Id}' is missing dependency '{id}@{range}'; ignoring");
  628. ignoredPlugins.Add(plugin, new(Reason.Dependency)
  629. {
  630. ReasonText = $"Dependency '{id}@{range}' not found",
  631. });
  632. ignored = true;
  633. return;
  634. }
  635. // make a point to propagate ignored
  636. if (depIgnored)
  637. {
  638. Logger.Loader.Warn($"Dependency '{id}' for '{plugin.Id}' previously ignored; ignoring '{plugin.Id}'");
  639. ignoredPlugins.Add(plugin, new(Reason.Dependency)
  640. {
  641. ReasonText = $"Dependency '{id}' ignored",
  642. RelatedTo = depMeta
  643. });
  644. ignored = true;
  645. return;
  646. }
  647. // make a point to propagate disabled
  648. if (depDisabled)
  649. {
  650. Logger.Loader.Warn($"Dependency '{id}' for '{plugin.Id}' disabled; disabling");
  651. disabledPlugins.Add(plugin);
  652. disabled = true;
  653. }
  654. // we found our dep, lets save the metadata and keep going
  655. _ = plugin.Dependencies.Add(depMeta);
  656. }
  657. // make sure the plugin depends on the loader (assuming it actually needs to)
  658. if (!dependsOnSelf && !plugin.IsSelf && !plugin.IsBare)
  659. {
  660. Logger.Loader.Warn($"Plugin '{plugin.Id}' does not depend on any particular loader version; assuming its incompatible");
  661. ignoredPlugins.Add(plugin, new(Reason.Dependency)
  662. {
  663. ReasonText = "Does not depend on any loader version, so it is assumed to be incompatible",
  664. RelatedTo = SelfMeta
  665. });
  666. ignored = true;
  667. return;
  668. }
  669. // exit early if we've decided we need to be disabled
  670. if (disabled)
  671. return;
  672. // handle LoadsAfter populated by Features processing
  673. foreach (var loadAfter in plugin.LoadsAfter)
  674. {
  675. if (TryResolveId(loadAfter.Id, out _, out _, out _))
  676. {
  677. // do nothing, because the plugin is already in the LoadsAfter set
  678. }
  679. }
  680. // then handle loadafters
  681. foreach (var id in plugin.Manifest.LoadAfter)
  682. {
  683. if (TryResolveId(id, out var meta, out var depDisabled, out var depIgnored))
  684. {
  685. // we only want to make sure to loadafter if its not ignored
  686. // if its disabled, we still wanna track it where possible
  687. _ = plugin.LoadsAfter.Add(meta);
  688. }
  689. }
  690. // after we handle dependencies and loadafters, then check conflicts
  691. foreach (var (id, range) in plugin.Manifest.Conflicts)
  692. {
  693. Logger.Loader.Trace($">- Checking conflict '{id}' {range}");
  694. // this lookup must be partial to prevent loadBefore/conflictsWith from creating a recursion loop
  695. if (TryResolveId(id, out var meta, out var conflDisabled, out var conflIgnored, partial: true)
  696. && range.Matches(meta.HVersion)
  697. && !conflIgnored && !conflDisabled) // the conflict is only *actually* a problem if it is both not ignored and not disabled
  698. {
  699. Logger.Loader.Warn($"Plugin '{plugin.Id}' conflicts with {meta.Id}@{meta.HVersion}; ignoring '{plugin.Id}'");
  700. ignoredPlugins.Add(plugin, new(Reason.Conflict)
  701. {
  702. ReasonText = $"Conflicts with {meta.Id}@{meta.HVersion}",
  703. RelatedTo = meta
  704. });
  705. ignored = true;
  706. return;
  707. }
  708. }
  709. // specifically check if some strange stuff happened (like graph loops) causing this to be ignored
  710. // from some other invocation
  711. if (!ignoredPlugins.ContainsKey(plugin))
  712. {
  713. // we can now load the current plugin
  714. Logger.Loader.Trace($"->'{plugin.Name}' loads here");
  715. outputOrder!.Add(plugin);
  716. }
  717. // loadbefores have already been preprocessed into loadafters
  718. Logger.Loader.Trace($">Processed '{plugin.Name}'");
  719. }
  720. // run TryResolveId over every plugin, which recursively calculates load order
  721. foreach (var plugin in pluginsToProcess)
  722. {
  723. _ = TryResolveId(plugin.Id, out _, out _, out _);
  724. }
  725. // by this point, outputOrder contains the full load order
  726. }
  727. DisabledConfig.Instance.Changed();
  728. DisabledPlugins = disabledPlugins;
  729. PluginsMetadata = outputOrder;
  730. }
  731. internal static void InitFeatures()
  732. {
  733. foreach (var meta in PluginsMetadata)
  734. {
  735. foreach (var feature in meta.Manifest.Features
  736. .SelectMany(f => f.Value.Select(o => (f.Key, o)))
  737. .Select(t => new Feature.Instance(meta, t.Key, t.o)))
  738. {
  739. if (feature.TryGetDefiningPlugin(out var plugin) && plugin == null)
  740. { // this is a DefineFeature, so we want to initialize it early
  741. if (!feature.TryCreate(out var inst))
  742. {
  743. Logger.Features.Error($"Error evaluating {feature.Name}: {inst.InvalidMessage}");
  744. }
  745. else
  746. {
  747. meta.InternalFeatures.Add(inst);
  748. }
  749. }
  750. else
  751. { // this is literally any other feature, so we want to delay its initialization
  752. _ = meta.UnloadedFeatures.Add(feature);
  753. }
  754. }
  755. }
  756. // at this point we have pre-initialized all features, so we can go ahead and use them to add stuff to the dep resolver
  757. foreach (var meta in PluginsMetadata)
  758. {
  759. foreach (var feature in meta.UnloadedFeatures)
  760. {
  761. if (feature.TryGetDefiningPlugin(out var plugin))
  762. {
  763. if (plugin != meta && plugin != null)
  764. { // if the feature is not applied to the defining feature
  765. _ = meta.LoadsAfter.Add(plugin);
  766. }
  767. if (plugin != null)
  768. {
  769. plugin.CreateFeaturesWhenLoaded.Add(feature);
  770. }
  771. }
  772. else
  773. {
  774. Logger.Features.Warn($"No such feature {feature.Name}");
  775. }
  776. }
  777. }
  778. }
  779. internal static void ReleaseAll(bool full = false)
  780. {
  781. if (full)
  782. {
  783. ignoredPlugins = new();
  784. }
  785. else
  786. {
  787. foreach (var m in PluginsMetadata)
  788. ignoredPlugins.Add(m, new IgnoreReason(Reason.Released));
  789. foreach (var m in ignoredPlugins.Keys)
  790. { // clean them up so we can still use the metadata for updates
  791. m.InternalFeatures.Clear();
  792. m.PluginType = null;
  793. m.Assembly = null!;
  794. }
  795. }
  796. PluginsMetadata = new List<PluginMetadata>();
  797. DisabledPlugins = new List<PluginMetadata>();
  798. Feature.Reset();
  799. GC.Collect();
  800. GC.WaitForPendingFinalizers();
  801. }
  802. internal static void Load(PluginMetadata meta)
  803. {
  804. if (meta is { Assembly: null, PluginType: not null })
  805. meta.Assembly = Assembly.LoadFrom(meta.File.FullName);
  806. }
  807. internal static PluginExecutor? InitPlugin(PluginMetadata meta, IEnumerable<PluginMetadata> alreadyLoaded)
  808. {
  809. if (meta.Manifest.GameVersion is { } gv && gv != UnityGame.GameVersion)
  810. Logger.Loader.Warn($"Mod {meta.Name} developed for game version {gv}, so it may not work properly.");
  811. if (meta.IsSelf)
  812. return new PluginExecutor(meta, PluginExecutor.Special.Self);
  813. foreach (var dep in meta.Dependencies)
  814. {
  815. if (alreadyLoaded.Contains(dep)) continue;
  816. // otherwise...
  817. if (ignoredPlugins.TryGetValue(dep, out var reason))
  818. { // was added to the ignore list
  819. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Dependency)
  820. {
  821. ReasonText = $"Dependency was ignored at load time: {reason.ReasonText}",
  822. RelatedTo = dep
  823. });
  824. }
  825. else
  826. { // was not added to ignore list
  827. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Dependency)
  828. {
  829. ReasonText = $"Dependency was not already loaded at load time, but was also not ignored",
  830. RelatedTo = dep
  831. });
  832. }
  833. return null;
  834. }
  835. if (meta.IsBare)
  836. return new PluginExecutor(meta, PluginExecutor.Special.Bare);
  837. Load(meta);
  838. PluginExecutor exec;
  839. try
  840. {
  841. exec = new PluginExecutor(meta);
  842. }
  843. catch (Exception e)
  844. {
  845. Logger.Loader.Error($"Error creating executor for {meta.Name}");
  846. Logger.Loader.Error(e);
  847. return null;
  848. }
  849. foreach (var feature in meta.Features)
  850. {
  851. try
  852. {
  853. feature.BeforeInit(meta);
  854. }
  855. catch (Exception e)
  856. {
  857. Logger.Loader.Critical($"Feature errored in {nameof(Feature.BeforeInit)}:");
  858. Logger.Loader.Critical(e);
  859. }
  860. }
  861. try
  862. {
  863. exec.Create();
  864. }
  865. catch (Exception e)
  866. {
  867. Logger.Loader.Error($"Could not init plugin {meta.Name}");
  868. Logger.Loader.Error(e);
  869. ignoredPlugins.Add(meta, new IgnoreReason(Reason.Error)
  870. {
  871. ReasonText = "Error occurred while initializing",
  872. Error = e
  873. });
  874. return null;
  875. }
  876. // TODO: make this new features system behave better wrt DynamicInit plugins
  877. foreach (var feature in meta.CreateFeaturesWhenLoaded)
  878. {
  879. if (!feature.TryCreate(out var inst))
  880. {
  881. Logger.Features.Warn($"Could not create instance of feature {feature.Name}: {inst.InvalidMessage}");
  882. }
  883. else
  884. {
  885. feature.AppliedTo.InternalFeatures.Add(inst);
  886. _ = feature.AppliedTo.UnloadedFeatures.Remove(feature);
  887. }
  888. }
  889. meta.CreateFeaturesWhenLoaded.Clear(); // if a plugin is loaded twice, for the moment, we don't want to create the feature twice
  890. foreach (var feature in meta.Features)
  891. try
  892. {
  893. feature.AfterInit(meta, exec.Instance);
  894. }
  895. catch (Exception e)
  896. {
  897. Logger.Loader.Critical($"Feature errored in {nameof(Feature.AfterInit)}:");
  898. Logger.Loader.Critical(e);
  899. }
  900. return exec;
  901. }
  902. internal static bool IsFirstLoadComplete { get; private set; }
  903. internal static List<PluginExecutor> LoadPlugins()
  904. {
  905. DisabledPlugins.ForEach(Load); // make sure they get loaded into memory so their metadata and stuff can be read more easily
  906. var list = new List<PluginExecutor>();
  907. var loaded = new HashSet<PluginMetadata>();
  908. foreach (var meta in PluginsMetadata)
  909. {
  910. try
  911. {
  912. var exec = InitPlugin(meta, loaded);
  913. if (exec != null)
  914. {
  915. list.Add(exec);
  916. _ = loaded.Add(meta);
  917. }
  918. }
  919. catch (Exception e)
  920. {
  921. Logger.Default.Critical($"Uncaught exception while loading plugin {meta.Name}:");
  922. Logger.Default.Critical(e);
  923. }
  924. }
  925. // TODO: should this be somewhere else?
  926. IsFirstLoadComplete = true;
  927. return list;
  928. }
  929. }
  930. }