From eda196fda82057047e875f9ebb8a689fece3de3e Mon Sep 17 00:00:00 2001 From: Bepis <36346617+bbepis@users.noreply.github.com> Date: Fri, 6 Nov 2020 11:47:04 +1100 Subject: [PATCH] Use TryParseAssemblyName wherever possible Rebase of 911375a --- BepInEx.Core/Bootstrap/TypeLoader.cs | 5 +++-- BepInEx.Core/Utility.cs | 7 ++++++- .../Patching/AssemblyPatcher.cs | 16 ++-------------- BepInEx.Preloader.Unity/DoorstopEntrypoint.cs | 1 + 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/BepInEx.Core/Bootstrap/TypeLoader.cs b/BepInEx.Core/Bootstrap/TypeLoader.cs index c9bb363..6320acc 100644 --- a/BepInEx.Core/Bootstrap/TypeLoader.cs +++ b/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; } diff --git a/BepInEx.Core/Utility.cs b/BepInEx.Core/Utility.cs index d3adb34..2ae22fd 100644 --- a/BepInEx.Core/Utility.cs +++ b/BepInEx.Core/Utility.cs @@ -308,13 +308,18 @@ namespace BepInEx return builder.ToString(); } - + /// /// Try to parse given string as an assembly name /// /// Fully qualified assembly name /// Resulting instance /// true, if parsing was successful, otherwise false + /// + /// On some versions of mono, using fails because it runs on unmanaged side + /// which has problems with encoding. + /// Using solves this by doing parsing on managed side instead. + /// public static bool TryParseAssemblyName(string fullName, out AssemblyName assemblyName) { try diff --git a/BepInEx.Preloader.Core/Patching/AssemblyPatcher.cs b/BepInEx.Preloader.Core/Patching/AssemblyPatcher.cs index ad40dca..1fde201 100644 --- a/BepInEx.Preloader.Core/Patching/AssemblyPatcher.cs +++ b/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; - } - } - /// /// Applies patchers to all assemblies in the given directory and loads patched assemblies into memory. /// @@ -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; diff --git a/BepInEx.Preloader.Unity/DoorstopEntrypoint.cs b/BepInEx.Preloader.Unity/DoorstopEntrypoint.cs index 42d7db8..01aee65 100644 --- a/BepInEx.Preloader.Unity/DoorstopEntrypoint.cs +++ b/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