A fork of Eusth's IPA
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.

126 lines
4.0KB

  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. /// <summary>
  17. /// Gets the list of loaded plugins and loads them if necessary.
  18. /// </summary>
  19. public static IEnumerable<IPlugin> Plugins
  20. {
  21. get
  22. {
  23. if(_Plugins == null)
  24. {
  25. LoadPlugins();
  26. }
  27. return _Plugins;
  28. }
  29. }
  30. private static void LoadPlugins()
  31. {
  32. string pluginDirectory = Path.Combine(Environment.CurrentDirectory, "Plugins");
  33. // Process.GetCurrentProcess().MainModule crashes the game and Assembly.GetEntryAssembly() is NULL,
  34. // so we need to resort to P/Invoke
  35. string exeName = Path.GetFileNameWithoutExtension(AppInfo.StartupPath);
  36. Console.WriteLine(exeName);
  37. _Plugins = new List<IPlugin>();
  38. if (!Directory.Exists(pluginDirectory)) return;
  39. String[] files = Directory.GetFiles(pluginDirectory, "*.dll");
  40. foreach (var s in files)
  41. {
  42. _Plugins.AddRange(LoadPluginsFromFile(Path.Combine(pluginDirectory, s), exeName));
  43. }
  44. // DEBUG
  45. Console.WriteLine("-----------------------------");
  46. Console.WriteLine("Loading plugins from {0} and found {1}", pluginDirectory, _Plugins.Count);
  47. Console.WriteLine("-----------------------------");
  48. foreach (var plugin in _Plugins)
  49. {
  50. Console.WriteLine(" {0}: {1}", plugin.Name, plugin.Version);
  51. }
  52. Console.WriteLine("-----------------------------");
  53. }
  54. private static IEnumerable<IPlugin> LoadPluginsFromFile(string file, string exeName)
  55. {
  56. List<IPlugin> plugins = new List<IPlugin>();
  57. if (!File.Exists(file) || !file.EndsWith(".dll", true, null))
  58. return plugins;
  59. try
  60. {
  61. Assembly assembly = Assembly.LoadFrom(file);
  62. foreach (Type t in assembly.GetTypes())
  63. {
  64. if (t.GetInterface("IPlugin") != null)
  65. {
  66. try
  67. {
  68. IPlugin pluginInstance = Activator.CreateInstance(t) as IPlugin;
  69. string[] filter = null;
  70. if (pluginInstance is IEnhancedPlugin)
  71. {
  72. filter = ((IEnhancedPlugin)pluginInstance).Filter;
  73. }
  74. if(filter == null || Enumerable.Contains(filter, exeName, StringComparer.OrdinalIgnoreCase))
  75. plugins.Add(pluginInstance);
  76. }
  77. catch (Exception)
  78. {
  79. }
  80. }
  81. }
  82. }
  83. catch (Exception)
  84. {
  85. }
  86. return plugins;
  87. }
  88. public class AppInfo
  89. {
  90. [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
  91. private static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
  92. private static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
  93. public static string StartupPath
  94. {
  95. get
  96. {
  97. StringBuilder stringBuilder = new StringBuilder(260);
  98. GetModuleFileName(NullHandleRef, stringBuilder, stringBuilder.Capacity);
  99. return stringBuilder.ToString();
  100. }
  101. }
  102. }
  103. }
  104. }