Parcourir la source

Use TryParseAssemblyName wherever possible
Rebase of 911375a

Bepis il y a 4 ans
Parent
commit
eda196fda8

+ 3 - 2
BepInEx.Core/Bootstrap/TypeLoader.cs

@@ -73,7 +73,8 @@ namespace BepInEx.Bootstrap
 
 		public static AssemblyDefinition CecilResolveOnFailure(object sender, AssemblyNameReference reference)
 		{
-			var name = new AssemblyName(reference.FullName);
+			if (!Utility.TryParseAssemblyName(reference.FullName, out var name))
+				return null;
 
 			if (Utility.TryResolveDllAssembly(name, Paths.BepInExAssemblyDirectory, ReaderParameters, out var assembly) ||
 				Utility.TryResolveDllAssembly(name, Paths.PluginPath, ReaderParameters, out assembly))
@@ -81,7 +82,7 @@ namespace BepInEx.Bootstrap
 
 			foreach (var dir in SearchDirectories)
 			{
-				if (Utility.TryResolveDllAssembly(name, Paths.BepInExAssemblyDirectory, ReaderParameters, out assembly))
+				if (Utility.TryResolveDllAssembly(name, dir, ReaderParameters, out assembly))
 					return assembly;
 			}
 

+ 6 - 1
BepInEx.Core/Utility.cs

@@ -308,13 +308,18 @@ namespace BepInEx
 
 			return builder.ToString();
 		}
-		
+
 		/// <summary>
 		/// Try to parse given string as an assembly name
 		/// </summary>
 		/// <param name="fullName">Fully qualified assembly name</param>
 		/// <param name="assemblyName">Resulting <see cref="AssemblyName"/> instance</param>
 		/// <returns><c>true</c>, if parsing was successful, otherwise <c>false</c></returns>
+		/// <remarks>
+		/// On some versions of mono, using <see cref="Assembly.GetName()"/> fails because it runs on unmanaged side
+		/// which has problems with encoding.
+		/// Using <see cref="AssemblyName"/> solves this by doing parsing on managed side instead.
+		/// </remarks>
 		public static bool TryParseAssemblyName(string fullName, out AssemblyName assemblyName)
 		{
 			try

+ 2 - 14
BepInEx.Preloader.Core/Patching/AssemblyPatcher.cs

@@ -250,19 +250,6 @@ namespace BepInEx.Preloader.Core
 			PatcherPlugins.Clear();
 		}
 
-		private static string GetAssemblyName(string fullName)
-		{
-			// We need to manually parse full name to avoid issues with encoding on mono
-			try
-			{
-				return new AssemblyName(fullName).Name;
-			}
-			catch (Exception)
-			{
-				return fullName;
-			}
-		}
-
 		/// <summary>
 		///     Applies patchers to all assemblies in the given directory and loads patched assemblies into memory.
 		/// </summary>
@@ -316,7 +303,8 @@ namespace BepInEx.Preloader.Core
 
 						foreach (var resolvedAss in AppDomain.CurrentDomain.GetAssemblies())
 						{
-							var name = GetAssemblyName(resolvedAss.FullName);
+							var name = Utility.TryParseAssemblyName(resolvedAss.FullName, out var assName) ? assName.Name : resolvedAss.FullName;
+
 							// Report only the first type that caused the assembly to load, because any subsequent ones can be false positives
 							if (!resolvedAssemblies.ContainsKey(name))
 								resolvedAssemblies[name] = assemblyPatcher.TypeName;

+ 1 - 0
BepInEx.Preloader.Unity/DoorstopEntrypoint.cs

@@ -91,6 +91,7 @@ namespace BepInEx.Preloader.Unity
 
 		private static Assembly ResolveCurrentDirectory(object sender, ResolveEventArgs args)
 		{
+			// Can't use Utils here because it's not yet resolved
 			var name = new AssemblyName(args.Name);
 
 			try