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.

167 lines
5.8KB

  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} using {1}", UnityEngine.Application.unityVersion, GetFrameworkVersion());
  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 (IsValidPlugin(t))
  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. private static bool IsValidPlugin(Type type)
  92. {
  93. return typeof(IPlugin).IsAssignableFrom(type)
  94. && !type.IsAbstract
  95. && !type.IsInterface
  96. && type.GetConstructor(Type.EmptyTypes) != null;
  97. }
  98. private static string GetFrameworkVersion()
  99. {
  100. var version = Environment.Version.ToString();
  101. try
  102. {
  103. switch (version)
  104. {
  105. case "2.0.50727.1433":
  106. return ".NET 3.5 Equivalent";
  107. case "4.0.30319.17020":
  108. // For reasons unknown, switching to netstandard seems to set this back to .NET 4.0
  109. var netstandard = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == "netstandard");
  110. if (netstandard != null)
  111. return $".NET Standard {netstandard.GetName().Version.ToString(2)}";
  112. goto default;
  113. case "4.0.30319.42000":
  114. return ".NET 4.x";
  115. default:
  116. return $".NET Framework {version}";
  117. }
  118. }
  119. catch
  120. {
  121. // In case something goes wrong, return the best we can guess
  122. return version;
  123. }
  124. }
  125. public class AppInfo
  126. {
  127. [DllImport("kernel32.dll", CharSet = CharSet.Auto, ExactSpelling = false)]
  128. private static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer, int length);
  129. private static HandleRef NullHandleRef = new HandleRef(null, IntPtr.Zero);
  130. public static string StartupPath
  131. {
  132. get
  133. {
  134. StringBuilder stringBuilder = new StringBuilder(260);
  135. GetModuleFileName(NullHandleRef, stringBuilder, stringBuilder.Capacity);
  136. return stringBuilder.ToString();
  137. }
  138. }
  139. }
  140. }
  141. }