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.

207 lines
7.2 KiB

  1. using IllusionInjector.Logging;
  2. using IllusionPlugin;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Runtime.CompilerServices;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. namespace IllusionInjector
  13. {
  14. public static class PluginManager
  15. {
  16. #pragma warning disable CS0618 // Type or member is obsolete (IPlugin)
  17. public class BSPluginMeta
  18. {
  19. public IBeatSaberPlugin Plugin { get; internal set; }
  20. public string Filename { get; internal set; }
  21. }
  22. public static IEnumerable<IBeatSaberPlugin> BSPlugins
  23. {
  24. get
  25. {
  26. if(_bsPlugins == null)
  27. {
  28. LoadPlugins();
  29. }
  30. return _bsPlugins.Select(p => p.Plugin);
  31. }
  32. }
  33. private static List<BSPluginMeta> _bsPlugins = null;
  34. internal static IEnumerable<BSPluginMeta> BSMetas
  35. {
  36. get
  37. {
  38. if (_bsPlugins == null)
  39. {
  40. LoadPlugins();
  41. }
  42. return _bsPlugins;
  43. }
  44. }
  45. public static IEnumerable<IPlugin> IPAPlugins
  46. {
  47. get
  48. {
  49. if (_ipaPlugins == null)
  50. {
  51. LoadPlugins();
  52. }
  53. return _ipaPlugins;
  54. }
  55. }
  56. private static List<IPlugin> _ipaPlugins = null;
  57. private static void LoadPlugins()
  58. {
  59. string pluginDirectory = Path.Combine(Environment.CurrentDirectory, "Plugins");
  60. // Process.GetCurrentProcess().MainModule crashes the game and Assembly.GetEntryAssembly() is NULL,
  61. // so we need to resort to P/Invoke
  62. string exeName = Path.GetFileNameWithoutExtension(AppInfo.StartupPath);
  63. Logger.log.Info(exeName);
  64. _bsPlugins = new List<BSPluginMeta>();
  65. _ipaPlugins = new List<IPlugin>();
  66. if (!Directory.Exists(pluginDirectory)) return;
  67. string cacheDir = Path.Combine(pluginDirectory, ".cache");
  68. if (!Directory.Exists(cacheDir))
  69. {
  70. Directory.CreateDirectory(cacheDir);
  71. }
  72. else
  73. {
  74. foreach (string plugin in Directory.GetFiles(cacheDir, "*"))
  75. {
  76. File.Delete(plugin);
  77. }
  78. }
  79. //Copy plugins to .cache
  80. string[] originalPlugins = Directory.GetFiles(pluginDirectory, "*.dll");
  81. foreach (string s in originalPlugins)
  82. {
  83. string pluginCopy = Path.Combine(cacheDir, Path.GetFileName(s));
  84. File.Copy(Path.Combine(pluginDirectory, s), pluginCopy);
  85. }
  86. //Load copied plugins
  87. string[] copiedPlugins = Directory.GetFiles(cacheDir, "*.dll");
  88. foreach (string s in copiedPlugins)
  89. {
  90. var result = LoadPluginsFromFile(s, exeName);
  91. _bsPlugins.AddRange(result.Item1.Select(p => new BSPluginMeta { Plugin = p, Filename = s }));
  92. _ipaPlugins.AddRange(result.Item2);
  93. }
  94. // DEBUG
  95. Logger.log.Info($"Running on Unity {UnityEngine.Application.unityVersion}");
  96. Logger.log.Info("-----------------------------");
  97. Logger.log.Info($"Loading plugins from {pluginDirectory} and found {_bsPlugins.Count}");
  98. Logger.log.Info("-----------------------------");
  99. foreach (var plugin in _bsPlugins)
  100. {
  101. Logger.log.Info($"{plugin.Plugin.Name}: {plugin.Plugin.Version}");
  102. }
  103. Logger.log.Info("-----------------------------");
  104. foreach (var plugin in _ipaPlugins)
  105. {
  106. Logger.log.Info($"{plugin.Name}: {plugin.Version}");
  107. }
  108. Logger.log.Info("-----------------------------");
  109. }
  110. private static Tuple<IEnumerable<IBeatSaberPlugin>, IEnumerable<IPlugin>> LoadPluginsFromFile(string file, string exeName)
  111. {
  112. List<IBeatSaberPlugin> bsPlugins = new List<IBeatSaberPlugin>();
  113. List<IPlugin> ipaPlugins = new List<IPlugin>();
  114. if (!File.Exists(file) || !file.EndsWith(".dll", true, null))
  115. return new Tuple<IEnumerable<IBeatSaberPlugin>, IEnumerable<IPlugin>>(bsPlugins, ipaPlugins);
  116. T OptionalGetPlugin<T>(Type t) where T : class
  117. {
  118. // use typeof() to allow for easier renaming (in an ideal world this compiles to a string, but ¯\_(ツ)_/¯)
  119. if (t.GetInterface(typeof(T).Name) != null)
  120. {
  121. try
  122. {
  123. T pluginInstance = Activator.CreateInstance(t) as T;
  124. string[] filter = null;
  125. if (pluginInstance is IGenericEnhancedPlugin)
  126. {
  127. filter = ((IGenericEnhancedPlugin)pluginInstance).Filter;
  128. }
  129. if (filter == null || filter.Contains(exeName, StringComparer.OrdinalIgnoreCase))
  130. return pluginInstance;
  131. }
  132. catch (Exception e)
  133. {
  134. Logger.log.Error($"Could not load plugin {t.FullName} in {Path.GetFileName(file)}! {e}");
  135. }
  136. }
  137. return null;
  138. }
  139. try
  140. {
  141. Assembly assembly = Assembly.LoadFrom(file);
  142. foreach (Type t in assembly.GetTypes())
  143. {
  144. IBeatSaberPlugin bsPlugin = OptionalGetPlugin<IBeatSaberPlugin>(t);
  145. if (bsPlugin != null)
  146. {
  147. bsPlugins.Add(bsPlugin);
  148. }
  149. else
  150. {
  151. IPlugin ipaPlugin = OptionalGetPlugin<IPlugin>(t);
  152. if (ipaPlugin != null)
  153. {
  154. ipaPlugins.Add(ipaPlugin);
  155. }
  156. }
  157. }
  158. }
  159. catch (Exception e)
  160. {
  161. Logger.log.Error($"Could not load {Path.GetFileName(file)}! {e}");
  162. }
  163. return new Tuple<IEnumerable<IBeatSaberPlugin>, IEnumerable<IPlugin>>(bsPlugins, ipaPlugins);
  164. }
  165. public class AppInfo
  166. {
  167. [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
  168. private static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
  169. private static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
  170. public static string StartupPath
  171. {
  172. get
  173. {
  174. StringBuilder stringBuilder = new StringBuilder(260);
  175. GetModuleFileName(NullHandleRef, stringBuilder, stringBuilder.Capacity);
  176. return stringBuilder.ToString();
  177. }
  178. }
  179. }
  180. #pragma warning restore CS0618 // Type or member is obsolete (IPlugin)
  181. }
  182. }