Ver código fonte

Don't chainload assemblies that don't reference bepinex

ghorsington 5 anos atrás
pai
commit
d91e6558e6
2 arquivos alterados com 20 adições e 2 exclusões
  1. 13 1
      BepInEx/Bootstrap/Chainloader.cs
  2. 7 1
      BepInEx/Bootstrap/TypeLoader.cs

+ 13 - 1
BepInEx/Bootstrap/Chainloader.cs

@@ -141,6 +141,18 @@ namespace BepInEx.Bootstrap
 			};
 		}
 
+		private static readonly string CurrentAssemblyName = Assembly.GetExecutingAssembly().GetName().Name;
+
+		private static bool HasBepinPlugins(AssemblyDefinition ass)
+		{
+			if (ass.MainModule.AssemblyReferences.All(r => r.Name != CurrentAssemblyName))
+				return false;
+			if (ass.MainModule.GetTypeReferences().All(r => r.FullName != typeof(BaseUnityPlugin).FullName))
+				return false;
+
+			return true;
+		}
+
 		/// <summary>
 		/// The entrypoint for the BepInEx plugin system.
 		/// </summary>
@@ -170,7 +182,7 @@ namespace BepInEx.Bootstrap
 
 				UnityEngine.Object.DontDestroyOnLoad(ManagerObject);
 
-				var pluginsToLoad = TypeLoader.FindPluginTypes(Paths.PluginPath, ToPluginInfo);
+				var pluginsToLoad = TypeLoader.FindPluginTypes(Paths.PluginPath, ToPluginInfo, HasBepinPlugins);
 
 				var pluginInfos = pluginsToLoad.SelectMany(p => p.Value).ToList();
 

+ 7 - 1
BepInEx/Bootstrap/TypeLoader.cs

@@ -43,7 +43,7 @@ namespace BepInEx.Bootstrap
 		/// <typeparam name="T">The specific base type to search for.</typeparam>
 		/// <param name="directory">The directory to search for assemblies.</param>
 		/// <returns>Returns a list of found derivative types.</returns>
-		public static Dictionary<AssemblyDefinition, List<T>> FindPluginTypes<T>(string directory, Func<TypeDefinition, T> typeSelector) where T : class
+		public static Dictionary<AssemblyDefinition, List<T>> FindPluginTypes<T>(string directory, Func<TypeDefinition, T> typeSelector, Func<AssemblyDefinition, bool> assemblyFilter = null) where T : class
 		{
 			var result = new Dictionary<AssemblyDefinition, List<T>>();
 
@@ -53,6 +53,12 @@ namespace BepInEx.Bootstrap
 				{
 					var ass = AssemblyDefinition.ReadAssembly(dll, readerParameters);
 
+					if (!assemblyFilter?.Invoke(ass) ?? false)
+					{
+						ass.Dispose();
+						continue;
+					}
+
 					var matches = ass.MainModule.Types.Select(typeSelector).Where(t => t != null).ToList();
 
 					if (matches.Count == 0)