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.

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