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.

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