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.

151 lines
5.2 KiB

  1. using IllusionPlugin;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Runtime.CompilerServices;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. namespace IllusionInjector
  12. {
  13. public static class PluginManager
  14. {
  15. private static List<IPlugin> _Plugins = null;
  16. internal static readonly Logger debugLogger = new Logger("IllusionInjector");
  17. /// <summary>
  18. /// Gets the list of loaded plugins and loads them if necessary.
  19. /// </summary>
  20. public static IEnumerable<IPlugin> Plugins
  21. {
  22. get
  23. {
  24. if(_Plugins == null)
  25. {
  26. LoadPlugins();
  27. }
  28. return _Plugins;
  29. }
  30. }
  31. private static void LoadPlugins()
  32. {
  33. string pluginDirectory = Path.Combine(Environment.CurrentDirectory, "Plugins");
  34. // Process.GetCurrentProcess().MainModule crashes the game and Assembly.GetEntryAssembly() is NULL,
  35. // so we need to resort to P/Invoke
  36. string exeName = Path.GetFileNameWithoutExtension(AppInfo.StartupPath);
  37. debugLogger.Log(exeName);
  38. _Plugins = new List<IPlugin>();
  39. if (!Directory.Exists(pluginDirectory)) return;
  40. if (!Directory.Exists(Path.Combine(pluginDirectory, ".cache")))
  41. {
  42. Directory.CreateDirectory(Path.Combine(pluginDirectory, ".cache"));
  43. }
  44. else
  45. {
  46. foreach (string plugin in Directory.GetFiles(Path.Combine(pluginDirectory, ".cache"), "*"))
  47. {
  48. File.Delete(plugin);
  49. }
  50. }
  51. //Copy plugins to .cache
  52. string[] originalPlugins = Directory.GetFiles(pluginDirectory, "*.dll");
  53. foreach (string s in originalPlugins)
  54. {
  55. string pluginCopy = pluginDirectory + "\\.cache" + s.Substring(s.LastIndexOf('\\'));
  56. File.Copy(Path.Combine(pluginDirectory, s), pluginCopy);
  57. }
  58. //Load copied plugins
  59. string copiedPluginsDirectory = pluginDirectory + "\\.cache";
  60. string[] copiedPlugins = Directory.GetFiles(copiedPluginsDirectory, "*.dll");
  61. foreach (string s in copiedPlugins)
  62. {
  63. _Plugins.AddRange(LoadPluginsFromFile(s, exeName));
  64. }
  65. // DEBUG
  66. debugLogger.Log($"Running on Unity {UnityEngine.Application.unityVersion}");
  67. debugLogger.Log("-----------------------------");
  68. debugLogger.Log($"Loading plugins from {pluginDirectory} and found {_Plugins.Count}");
  69. debugLogger.Log("-----------------------------");
  70. foreach (var plugin in _Plugins)
  71. {
  72. debugLogger.Log($"{plugin.Name}: {plugin.Version}");
  73. }
  74. debugLogger.Log("-----------------------------");
  75. }
  76. private static IEnumerable<IPlugin> LoadPluginsFromFile(string file, string exeName)
  77. {
  78. List<IPlugin> plugins = new List<IPlugin>();
  79. if (!File.Exists(file) || !file.EndsWith(".dll", true, null))
  80. return plugins;
  81. try
  82. {
  83. Assembly assembly = Assembly.LoadFrom(file);
  84. foreach (Type t in assembly.GetTypes())
  85. {
  86. if (t.GetInterface("IPlugin") != null)
  87. {
  88. try
  89. {
  90. IPlugin pluginInstance = Activator.CreateInstance(t) as IPlugin;
  91. string[] filter = null;
  92. if (pluginInstance is IEnhancedPlugin)
  93. {
  94. filter = ((IEnhancedPlugin)pluginInstance).Filter;
  95. }
  96. if(filter == null || filter.Contains(exeName, StringComparer.OrdinalIgnoreCase))
  97. plugins.Add(pluginInstance);
  98. }
  99. catch (Exception e)
  100. {
  101. debugLogger.Exception($"Could not load plugin {t.FullName} in {Path.GetFileName(file)}! {e}");
  102. }
  103. }
  104. }
  105. }
  106. catch (Exception e)
  107. {
  108. debugLogger.Error($"Could not load {Path.GetFileName(file)}! {e}");
  109. }
  110. return plugins;
  111. }
  112. public class AppInfo
  113. {
  114. [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
  115. private static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
  116. private static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
  117. public static string StartupPath
  118. {
  119. get
  120. {
  121. StringBuilder stringBuilder = new StringBuilder(260);
  122. GetModuleFileName(NullHandleRef, stringBuilder, stringBuilder.Capacity);
  123. return stringBuilder.ToString();
  124. }
  125. }
  126. }
  127. }
  128. }