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.

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