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.

587 lines
26 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using IPA.Config;
  10. using IPA.Old;
  11. using IPA.Utilities;
  12. using Mono.Cecil;
  13. using UnityEngine;
  14. using Logger = IPA.Logging.Logger;
  15. using System.Threading.Tasks;
  16. #if NET4
  17. using TaskEx = System.Threading.Tasks.Task;
  18. using TaskEx6 = System.Threading.Tasks.Task;
  19. using Task = System.Threading.Tasks.Task;
  20. #endif
  21. #if NET3
  22. using Net3_Proxy;
  23. using Path = Net3_Proxy.Path;
  24. using File = Net3_Proxy.File;
  25. using Directory = Net3_Proxy.Directory;
  26. #endif
  27. namespace IPA.Loader
  28. {
  29. /// <summary>
  30. /// The manager class for all plugins.
  31. /// </summary>
  32. public static class PluginManager
  33. {
  34. #pragma warning disable CS0618 // Type or member is obsolete (IPlugin)
  35. private static List<PluginExecutor> _bsPlugins;
  36. internal static IEnumerable<PluginExecutor> BSMetas => _bsPlugins;
  37. /// <summary>
  38. /// Gets info about the enabled plugin with the specified name.
  39. /// </summary>
  40. /// <param name="name">the name of the plugin to get (must be an exact match)</param>
  41. /// <returns>the plugin metadata for the requested plugin or <see langword="null"/> if it doesn't exist or is disabled</returns>
  42. public static PluginMetadata GetPlugin(string name)
  43. => BSMetas.Select(p => p.Metadata).FirstOrDefault(p => p.Name == name);
  44. /// <summary>
  45. /// Gets info about the enabled plugin with the specified ID.
  46. /// </summary>
  47. /// <param name="name">the ID name of the plugin to get (must be an exact match)</param>
  48. /// <returns>the plugin metadata for the requested plugin or <see langword="null"/> if it doesn't exist or is disabled</returns>
  49. public static PluginMetadata GetPluginFromId(string name)
  50. => BSMetas.Select(p => p.Metadata).FirstOrDefault(p => p.Id == name);
  51. /// <summary>
  52. /// Gets a disabled plugin's metadata by its name.
  53. /// </summary>
  54. /// <param name="name">the name of the disabled plugin to get</param>
  55. /// <returns>the metadata for the corresponding plugin</returns>
  56. public static PluginMetadata GetDisabledPlugin(string name) =>
  57. DisabledPlugins.FirstOrDefault(p => p.Name == name);
  58. /// <summary>
  59. /// Gets a disabled plugin's metadata by its ID.
  60. /// </summary>
  61. /// <param name="name">the ID of the disabled plugin to get</param>
  62. /// <returns>the metadata for the corresponding plugin</returns>
  63. public static PluginMetadata GetDisabledPluginFromId(string name) =>
  64. DisabledPlugins.FirstOrDefault(p => p.Id == name);
  65. /// <summary>
  66. /// Creates a new transaction for mod enabling and disabling mods simultaneously.
  67. /// </summary>
  68. /// <returns>a new <see cref="StateTransitionTransaction"/> that captures the current state of loaded mods</returns>
  69. public static StateTransitionTransaction PluginStateTransaction()
  70. => new StateTransitionTransaction(AllPlugins, DisabledPlugins);
  71. private static readonly object commitTransactionLockObject = new object();
  72. internal static Task CommitTransaction(StateTransitionTransaction transaction)
  73. {
  74. lock (commitTransactionLockObject)
  75. {
  76. if (transaction.CurrentlyEnabled.Except(AllPlugins)
  77. .Concat(AllPlugins.Except(transaction.CurrentlyEnabled)).Any()
  78. || transaction.CurrentlyDisabled.Except(DisabledPlugins)
  79. .Concat(DisabledPlugins.Except(transaction.DisabledPlugins)).Any())
  80. { // ensure that the transaction's base state reflects the current state, otherwise throw
  81. throw new InvalidOperationException("Transaction no longer resembles the current state of plugins");
  82. }
  83. var toEnable = transaction.ToEnable;
  84. var toDisable = transaction.ToDisable;
  85. transaction.Dispose();
  86. {
  87. // first enable the mods that need to be
  88. void DeTree(List<PluginMetadata> into, IEnumerable<PluginMetadata> tree)
  89. {
  90. foreach (var st in tree)
  91. if (toEnable.Contains(st) && !into.Contains(st))
  92. {
  93. DeTree(into, st.Dependencies);
  94. into.Add(st);
  95. }
  96. }
  97. var enableOrder = new List<PluginMetadata>();
  98. DeTree(enableOrder, toEnable);
  99. foreach (var meta in enableOrder)
  100. {
  101. var executor = runtimeDisabledPlugins.FirstOrDefault(e => e.Metadata == meta);
  102. if (meta.RuntimeOptions == RuntimeOptions.DynamicInit)
  103. {
  104. if (executor != null)
  105. runtimeDisabledPlugins.Remove(executor);
  106. else
  107. executor = PluginLoader.InitPlugin(meta, AllPlugins);
  108. if (executor == null) continue; // couldn't initialize, skip to next
  109. }
  110. PluginLoader.DisabledPlugins.Remove(meta);
  111. DisabledConfig.Instance.DisabledModIds.Remove(meta.Id ?? meta.Name);
  112. PluginEnabled(meta, meta.RuntimeOptions != RuntimeOptions.DynamicInit);
  113. if (meta.RuntimeOptions == RuntimeOptions.DynamicInit)
  114. {
  115. _bsPlugins.Add(executor);
  116. try
  117. {
  118. executor.Enable();
  119. }
  120. catch (Exception e)
  121. {
  122. Logger.loader.Error($"Error while enabling {meta.Id}:");
  123. Logger.loader.Error(e);
  124. // this should still be considered enabled, hence its position
  125. }
  126. }
  127. }
  128. }
  129. Task result;
  130. {
  131. // then disable the mods that need to be
  132. static DisableExecutor MakeDisableExec(PluginExecutor e)
  133. => new DisableExecutor
  134. {
  135. Executor = e,
  136. Dependents = BSMetas.Where(f => f.Metadata.Dependencies.Contains(e.Metadata)).Select(MakeDisableExec)
  137. };
  138. var disableExecs = toDisable.Select(m => BSMetas.FirstOrDefault(e => e.Metadata == m)).NonNull().ToArray(); // eagerly evaluate once
  139. foreach (var exec in disableExecs)
  140. {
  141. PluginLoader.DisabledPlugins.Add(exec.Metadata);
  142. DisabledConfig.Instance.DisabledModIds.Add(exec.Metadata.Id ?? exec.Metadata.Name);
  143. if (exec.Metadata.RuntimeOptions == RuntimeOptions.DynamicInit)
  144. {
  145. runtimeDisabledPlugins.Add(exec);
  146. _bsPlugins.Remove(exec);
  147. }
  148. PluginDisabled(exec.Metadata, exec.Metadata.RuntimeOptions != RuntimeOptions.DynamicInit);
  149. }
  150. var disableStructure = disableExecs.Select(MakeDisableExec);
  151. static Task Disable(DisableExecutor exec, Dictionary<PluginExecutor, Task> alreadyDisabled)
  152. {
  153. if (alreadyDisabled.TryGetValue(exec.Executor, out var task))
  154. return task;
  155. else
  156. {
  157. if (exec.Executor.Metadata.RuntimeOptions != RuntimeOptions.DynamicInit)
  158. return TaskEx6.FromException(new CannotRuntimeDisableException(exec.Executor.Metadata));
  159. var res = TaskEx.WhenAll(exec.Dependents.Select(d => Disable(d, alreadyDisabled)))
  160. .ContinueWith(t => TaskEx.WhenAll(t, exec.Executor.Disable())).Unwrap();
  161. // The WhenAll above allows us to wait for the executor to disable, but still propagate errors
  162. alreadyDisabled.Add(exec.Executor, res);
  163. return res;
  164. }
  165. }
  166. var disabled = new Dictionary<PluginExecutor, Task>();
  167. result = TaskEx.WhenAll(disableStructure.Select(d => Disable(d, disabled)));
  168. }
  169. DisabledConfig.Instance.Changed();
  170. return result;
  171. }
  172. }
  173. private struct DisableExecutor
  174. {
  175. public PluginExecutor Executor;
  176. public IEnumerable<DisableExecutor> Dependents;
  177. }
  178. // TODO: rewrite below
  179. /*
  180. /// <summary>
  181. /// Disables a plugin, and all dependents.
  182. /// </summary>
  183. /// <param name="plugin">the plugin to disable</param>
  184. /// <returns>whether or not it needs a restart to enable</returns>
  185. public static bool DisablePlugin(PluginInfo plugin)
  186. {
  187. if (plugin == null) return false;
  188. if (plugin.Metadata.IsBare)
  189. {
  190. Logger.loader.Warn($"Trying to disable bare manifest");
  191. return false;
  192. }
  193. if (IsDisabled(plugin.Metadata)) return false;
  194. var needsRestart = false;
  195. Logger.loader.Info($"Disabling {plugin.Metadata.Name}");
  196. var dependents = BSMetas.Where(m => m.Metadata.Dependencies.Contains(plugin.Metadata)).ToList();
  197. needsRestart = dependents.Aggregate(needsRestart, (b, p) => DisablePlugin(p) || b);
  198. DisabledConfig.Instance.DisabledModIds.Add(plugin.Metadata.Id ?? plugin.Metadata.Name);
  199. if (!needsRestart && plugin.Plugin is IDisablablePlugin disable)
  200. {
  201. try
  202. {
  203. disable.OnDisable();
  204. }
  205. catch (Exception e)
  206. {
  207. Logger.loader.Error($"Error occurred trying to disable {plugin.Metadata.Name}");
  208. Logger.loader.Error(e);
  209. }
  210. if (needsRestart)
  211. Logger.loader.Warn($"Disablable plugin has non-disablable dependents; some things may not work properly");
  212. }
  213. else needsRestart = true;
  214. runtimeDisabled.Add(plugin);
  215. _bsPlugins.Remove(plugin);
  216. try
  217. {
  218. PluginDisabled?.Invoke(plugin.Metadata, needsRestart);
  219. }
  220. catch (Exception e)
  221. {
  222. Logger.loader.Error($"Error occurred invoking disable event for {plugin.Metadata.Name}");
  223. Logger.loader.Error(e);
  224. }
  225. return needsRestart;
  226. }
  227. /// <summary>
  228. /// Disables a plugin, and all dependents.
  229. /// </summary>
  230. /// <param name="pluginId">the ID, or name if the ID is null, of the plugin to disable</param>
  231. /// <returns>whether a restart is needed to activate</returns>
  232. public static bool DisablePlugin(string pluginId) => DisablePlugin(GetPluginFromId(pluginId) ?? GetPlugin(pluginId));
  233. /// <summary>
  234. /// Enables a plugin that had been previously disabled.
  235. /// </summary>
  236. /// <param name="plugin">the plugin to enable</param>
  237. /// <returns>whether a restart is needed to activate</returns>
  238. public static bool EnablePlugin(PluginMetadata plugin)
  239. { // TODO: fix some of this behaviour, by adding better support for runtime enable/disable of mods
  240. if (plugin == null) return false;
  241. if (plugin.IsBare)
  242. {
  243. Logger.loader.Warn($"Trying to enable bare manifest");
  244. return false;
  245. }
  246. if (!IsDisabled(plugin)) return false;
  247. Logger.loader.Info($"Enabling {plugin.Name}");
  248. DisabledConfig.Instance.DisabledModIds.Remove(plugin.Id ?? plugin.Name);
  249. var needsRestart = true;
  250. var depsNeedRestart = plugin.Dependencies.Aggregate(false, (b, p) => EnablePlugin(p) || b);
  251. var runtimeInfo = runtimeDisabled.FirstOrDefault(p => p.Metadata == plugin);
  252. if (runtimeInfo != null && runtimeInfo.Plugin is IPlugin newPlugin)
  253. {
  254. try
  255. {
  256. newPlugin.OnEnable();
  257. }
  258. catch (Exception e)
  259. {
  260. Logger.loader.Error($"Error occurred trying to enable {plugin.Name}");
  261. Logger.loader.Error(e);
  262. }
  263. needsRestart = false;
  264. }
  265. else
  266. {
  267. PluginLoader.DisabledPlugins.Remove(plugin);
  268. if (runtimeInfo == null)
  269. {
  270. runtimeInfo = InitPlugin(plugin, AllPlugins.Select(i => i.Metadata));
  271. needsRestart = false;
  272. }
  273. }
  274. if (runtimeInfo != null)
  275. runtimeDisabled.Remove(runtimeInfo);
  276. _bsPlugins.Add(runtimeInfo);
  277. try
  278. {
  279. PluginEnabled?.Invoke(runtimeInfo, needsRestart || depsNeedRestart);
  280. }
  281. catch (Exception e)
  282. {
  283. Logger.loader.Error($"Error occurred invoking enable event for {plugin.Name}");
  284. Logger.loader.Error(e);
  285. }
  286. return needsRestart || depsNeedRestart;
  287. }
  288. /// <summary>
  289. /// Enables a plugin that had been previously disabled.
  290. /// </summary>
  291. /// <param name="pluginId">the ID, or name if the ID is null, of the plugin to enable</param>
  292. /// <returns>whether a restart is needed to activate</returns>
  293. public static bool EnablePlugin(string pluginId) =>
  294. EnablePlugin(GetDisabledPluginFromId(pluginId) ?? GetDisabledPlugin(pluginId));*/
  295. /// <summary>
  296. /// Checks if a given plugin is disabled.
  297. /// </summary>
  298. /// <param name="meta">the plugin to check</param>
  299. /// <returns><see langword="true"/> if the plugin is disabled, <see langword="false"/> otherwise.</returns>
  300. public static bool IsDisabled(PluginMetadata meta) => DisabledPlugins.Contains(meta);
  301. /// <summary>
  302. /// Checks if a given plugin is enabled.
  303. /// </summary>
  304. /// <param name="meta">the plugin to check</param>
  305. /// <returns><see langword="true"/> if the plugin is enabled, <see langword="false"/> otherwise.</returns>
  306. public static bool IsEnabled(PluginMetadata meta) => BSMetas.Any(p => p.Metadata == meta);
  307. /// <summary>
  308. /// Gets a list of disabled BSIPA plugins.
  309. /// </summary>
  310. /// <value>a collection of all disabled plugins as <see cref="PluginMetadata"/></value>
  311. public static IEnumerable<PluginMetadata> DisabledPlugins => PluginLoader.DisabledPlugins;
  312. private static readonly HashSet<PluginExecutor> runtimeDisabledPlugins = new HashSet<PluginExecutor>();
  313. /// <summary>
  314. /// An invoker for the <see cref="PluginEnabled"/> event.
  315. /// </summary>
  316. /// <param name="plugin">the plugin that was enabled</param>
  317. /// <param name="needsRestart">whether it needs a restart to take effect</param>
  318. public delegate void PluginEnableDelegate(PluginMetadata plugin, bool needsRestart);
  319. /// <summary>
  320. /// An invoker for the <see cref="PluginDisabled"/> event.
  321. /// </summary>
  322. /// <param name="plugin">the plugin that was disabled</param>
  323. /// <param name="needsRestart">whether it needs a restart to take effect</param>
  324. public delegate void PluginDisableDelegate(PluginMetadata plugin, bool needsRestart);
  325. /// <summary>
  326. /// Called whenever a plugin is enabled.
  327. /// </summary>
  328. public static event PluginEnableDelegate PluginEnabled;
  329. /// <summary>
  330. /// Called whenever a plugin is disabled.
  331. /// </summary>
  332. public static event PluginDisableDelegate PluginDisabled;
  333. /// <summary>
  334. /// Gets a list of all enabled BSIPA plugins.
  335. /// </summary>
  336. /// <value>a collection of all enabled plugins as <see cref="PluginMetadata"/>s</value>
  337. public static IEnumerable<PluginMetadata> AllPlugins => BSMetas.Select(p => p.Metadata);
  338. /// <summary>
  339. /// An <see cref="IEnumerable{T}"/> of old IPA plugins.
  340. /// </summary>
  341. /// <value>all legacy plugin instances</value>
  342. [Obsolete("I mean, IPlugin shouldn't be used, so why should this? Not renaming to extend support for old plugins.")]
  343. public static IEnumerable<Old.IPlugin> Plugins => _ipaPlugins;
  344. private static List<Old.IPlugin> _ipaPlugins;
  345. internal static IConfigProvider SelfConfigProvider { get; set; }
  346. internal static void Load()
  347. {
  348. string pluginDirectory = UnityGame.PluginsPath;
  349. // Process.GetCurrentProcess().MainModule crashes the game and Assembly.GetEntryAssembly() is NULL,
  350. // so we need to resort to P/Invoke
  351. string exeName = Path.GetFileNameWithoutExtension(AppInfo.StartupPath);
  352. _bsPlugins = new List<PluginExecutor>();
  353. _ipaPlugins = new List<Old.IPlugin>();
  354. if (!Directory.Exists(pluginDirectory)) return;
  355. string cacheDir = Path.Combine(pluginDirectory, ".cache");
  356. if (!Directory.Exists(cacheDir))
  357. {
  358. Directory.CreateDirectory(cacheDir);
  359. }
  360. else
  361. {
  362. foreach (string plugin in Directory.GetFiles(cacheDir, "*"))
  363. {
  364. File.Delete(plugin);
  365. }
  366. }
  367. // initialize BSIPA plugins first
  368. _bsPlugins.AddRange(PluginLoader.LoadPlugins());
  369. var metadataPaths = PluginLoader.PluginsMetadata.Select(m => m.File.FullName).ToList();
  370. var ignoredPaths = PluginLoader.ignoredPlugins.Select(m => m.Key.File.FullName).ToList();
  371. var disabledPaths = DisabledPlugins.Select(m => m.File.FullName).ToList();
  372. //Copy plugins to .cache
  373. string[] originalPlugins = Directory.GetFiles(pluginDirectory, "*.dll");
  374. foreach (string s in originalPlugins)
  375. {
  376. if (metadataPaths.Contains(s)) continue;
  377. if (ignoredPaths.Contains(s)) continue;
  378. if (disabledPaths.Contains(s)) continue;
  379. string pluginCopy = Path.Combine(cacheDir, Path.GetFileName(s));
  380. #region Fix assemblies for refactor
  381. var module = ModuleDefinition.ReadModule(Path.Combine(pluginDirectory, s));
  382. foreach (var @ref in module.AssemblyReferences)
  383. { // fix assembly references
  384. if (@ref.Name == "IllusionPlugin" || @ref.Name == "IllusionInjector")
  385. {
  386. @ref.Name = "IPA.Loader";
  387. }
  388. }
  389. foreach (var @ref in module.GetTypeReferences())
  390. { // fix type references
  391. if (@ref.FullName == "IllusionPlugin.IPlugin") @ref.Namespace = "IPA.Old"; //@ref.Name = "";
  392. if (@ref.FullName == "IllusionPlugin.IEnhancedPlugin") @ref.Namespace = "IPA.Old"; //@ref.Name = "";
  393. if (@ref.FullName == "IllusionPlugin.IBeatSaberPlugin") { @ref.Namespace = "IPA"; @ref.Name = nameof(IPlugin); }
  394. if (@ref.FullName == "IllusionPlugin.IEnhancedBeatSaberPlugin") { @ref.Namespace = "IPA"; @ref.Name = nameof(IEnhancedPlugin); }
  395. if (@ref.FullName == "IllusionPlugin.IniFile") @ref.Namespace = "IPA.Config"; //@ref.Name = "";
  396. if (@ref.FullName == "IllusionPlugin.IModPrefs") @ref.Namespace = "IPA.Config"; //@ref.Name = "";
  397. if (@ref.FullName == "IllusionPlugin.ModPrefs") @ref.Namespace = "IPA.Config"; //@ref.Name = "";
  398. if (@ref.FullName == "IllusionPlugin.Utils.ReflectionUtil") @ref.Namespace = "IPA.Utilities"; //@ref.Name = "";
  399. if (@ref.FullName == "IllusionPlugin.Logging.Logger") @ref.Namespace = "IPA.Logging"; //@ref.Name = "";
  400. if (@ref.FullName == "IllusionPlugin.Logging.LogPrinter") @ref.Namespace = "IPA.Logging"; //@ref.Name = "";
  401. if (@ref.FullName == "IllusionInjector.PluginManager") @ref.Namespace = "IPA.Loader"; //@ref.Name = "";
  402. if (@ref.FullName == "IllusionInjector.PluginComponent") @ref.Namespace = "IPA.Loader"; //@ref.Name = "";
  403. if (@ref.FullName == "IllusionInjector.CompositeBSPlugin") @ref.Namespace = "IPA.Loader.Composite"; //@ref.Name = "";
  404. if (@ref.FullName == "IllusionInjector.CompositeIPAPlugin") @ref.Namespace = "IPA.Loader.Composite"; //@ref.Name = "";
  405. if (@ref.FullName == "IllusionInjector.Logging.UnityLogInterceptor") @ref.Namespace = "IPA.Logging"; //@ref.Name = "";
  406. if (@ref.FullName == "IllusionInjector.Logging.StandardLogger") @ref.Namespace = "IPA.Logging"; //@ref.Name = "";
  407. if (@ref.FullName == "IllusionInjector.Updating.SelfPlugin") @ref.Namespace = "IPA.Updating"; //@ref.Name = "";
  408. if (@ref.FullName == "IllusionInjector.Updating.Backup.BackupUnit") @ref.Namespace = "IPA.Updating.Backup"; //@ref.Name = "";
  409. if (@ref.Namespace == "IllusionInjector.Utilities") @ref.Namespace = "IPA.Utilities"; //@ref.Name = "";
  410. if (@ref.Namespace == "IllusionInjector.Logging.Printers") @ref.Namespace = "IPA.Logging.Printers"; //@ref.Name = "";
  411. }
  412. module.Write(pluginCopy);
  413. #endregion
  414. }
  415. //Load copied plugins
  416. string[] copiedPlugins = Directory.GetFiles(cacheDir, "*.dll");
  417. foreach (string s in copiedPlugins)
  418. {
  419. var result = LoadPluginsFromFile(s);
  420. if (result == null) continue;
  421. _ipaPlugins.AddRange(result.NonNull());
  422. }
  423. Logger.log.Info(exeName);
  424. Logger.log.Info($"Running on Unity {Application.unityVersion}");
  425. Logger.log.Info($"Game version {UnityGame.GameVersion}");
  426. Logger.log.Info("-----------------------------");
  427. Logger.log.Info($"Loading plugins from {Utils.GetRelativePath(pluginDirectory, Environment.CurrentDirectory)} and found {_bsPlugins.Count + _ipaPlugins.Count}");
  428. Logger.log.Info("-----------------------------");
  429. foreach (var plugin in _bsPlugins)
  430. {
  431. Logger.log.Info($"{plugin.Metadata.Name} ({plugin.Metadata.Id}): {plugin.Metadata.Version}");
  432. }
  433. Logger.log.Info("-----------------------------");
  434. foreach (var plugin in _ipaPlugins)
  435. {
  436. Logger.log.Info($"{plugin.Name}: {plugin.Version}");
  437. }
  438. Logger.log.Info("-----------------------------");
  439. }
  440. private static IEnumerable<Old.IPlugin> LoadPluginsFromFile(string file)
  441. {
  442. var ipaPlugins = new List<Old.IPlugin>();
  443. if (!File.Exists(file) || !file.EndsWith(".dll", true, null))
  444. return ipaPlugins;
  445. T OptionalGetPlugin<T>(Type t) where T : class
  446. {
  447. if (t.FindInterfaces((t, o) => t == (o as Type), typeof(T)).Length > 0)
  448. {
  449. try
  450. {
  451. T pluginInstance = Activator.CreateInstance(t) as T;
  452. return pluginInstance;
  453. }
  454. catch (Exception e)
  455. {
  456. Logger.loader.Error($"Could not load plugin {t.FullName} in {Path.GetFileName(file)}! {e}");
  457. }
  458. }
  459. return null;
  460. }
  461. try
  462. {
  463. Assembly assembly = Assembly.LoadFrom(file);
  464. foreach (Type t in assembly.GetTypes())
  465. {
  466. var ipaPlugin = OptionalGetPlugin<Old.IPlugin>(t);
  467. if (ipaPlugin != null)
  468. {
  469. ipaPlugins.Add(ipaPlugin);
  470. }
  471. }
  472. }
  473. catch (ReflectionTypeLoadException e)
  474. {
  475. Logger.loader.Error($"Could not load the following types from {Path.GetFileName(file)}:");
  476. Logger.loader.Error($" {string.Join("\n ", e.LoaderExceptions?.Select(e1 => e1?.Message).StrJP() ?? new string[0])}");
  477. }
  478. catch (Exception e)
  479. {
  480. Logger.loader.Error($"Could not load {Path.GetFileName(file)}!");
  481. Logger.loader.Error(e);
  482. }
  483. return ipaPlugins;
  484. }
  485. internal static class AppInfo
  486. {
  487. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = false)]
  488. private static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
  489. private static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
  490. public static string StartupPath
  491. {
  492. get
  493. {
  494. StringBuilder stringBuilder = new StringBuilder(260);
  495. GetModuleFileName(NullHandleRef, stringBuilder, stringBuilder.Capacity);
  496. return stringBuilder.ToString();
  497. }
  498. }
  499. }
  500. #pragma warning restore CS0618 // Type or member is obsolete (IPlugin)
  501. }
  502. }