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.

147 lines
5.3 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. String[] files = Directory.GetFiles(pluginDirectory, "*.dll");
  52. foreach (string s in files)
  53. {
  54. string pluginCopy = pluginDirectory + "\\.cache" + s.Substring(s.LastIndexOf('\\'));
  55. File.Copy(Path.Combine(pluginDirectory, s), pluginCopy);
  56. _Plugins.AddRange(LoadPluginsFromFile(pluginCopy, exeName));
  57. }
  58. // DEBUG
  59. debugLogger.Log($"Running on Unity {UnityEngine.Application.unityVersion}");
  60. debugLogger.Log("-----------------------------");
  61. debugLogger.Log($"Loading plugins from {pluginDirectory} and found {_Plugins.Count}");
  62. debugLogger.Log("-----------------------------");
  63. foreach (var plugin in _Plugins)
  64. {
  65. debugLogger.Log($"{plugin.Name}: {plugin.Version}");
  66. if (!(plugin is IPluginNew)) {
  67. debugLogger.Warning($"{plugin.Name} uses a Deprecated Interface! This may cause errors!");
  68. }
  69. }
  70. debugLogger.Log("-----------------------------");
  71. }
  72. private static IEnumerable<IPlugin> LoadPluginsFromFile(string file, string exeName)
  73. {
  74. List<IPlugin> plugins = new List<IPlugin>();
  75. if (!File.Exists(file) || !file.EndsWith(".dll", true, null))
  76. return plugins;
  77. try
  78. {
  79. Assembly assembly = Assembly.LoadFrom(file);
  80. foreach (Type t in assembly.GetTypes())
  81. {
  82. if (t.GetInterface("IPlugin") != null)
  83. {
  84. try
  85. {
  86. IPlugin pluginInstance = Activator.CreateInstance(t) as IPlugin;
  87. string[] filter = null;
  88. if (pluginInstance is IEnhancedPlugin)
  89. {
  90. filter = ((IEnhancedPlugin)pluginInstance).Filter;
  91. }
  92. if(filter == null || filter.Contains(exeName, StringComparer.OrdinalIgnoreCase))
  93. plugins.Add(pluginInstance);
  94. }
  95. catch (Exception e)
  96. {
  97. debugLogger.Exception($"Could not load plugin {t.FullName} in {Path.GetFileName(file)}! {e}");
  98. }
  99. }
  100. }
  101. }
  102. catch (Exception e)
  103. {
  104. debugLogger.Error($"Could not load {Path.GetFileName(file)}! {e}");
  105. }
  106. return plugins;
  107. }
  108. public class AppInfo
  109. {
  110. [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
  111. private static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
  112. private static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
  113. public static string StartupPath
  114. {
  115. get
  116. {
  117. StringBuilder stringBuilder = new StringBuilder(260);
  118. GetModuleFileName(NullHandleRef, stringBuilder, stringBuilder.Capacity);
  119. return stringBuilder.ToString();
  120. }
  121. }
  122. }
  123. }
  124. }