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.

129 lines
4.4KB

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