From 911375ace5996d245ae11276c4debe3f7196a921 Mon Sep 17 00:00:00 2001 From: ghorsington Date: Fri, 14 Aug 2020 12:14:54 +0300 Subject: [PATCH] Use TryParseAssemblyName wherever possible --- BepInEx.Preloader/Entrypoint.cs | 1 + BepInEx.Preloader/Patching/AssemblyPatcher.cs | 10 +--------- BepInEx/Bootstrap/TypeLoader.cs | 9 ++++----- BepInEx/Utility.cs | 6 ++++++ 4 files changed, 12 insertions(+), 14 deletions(-) diff --git a/BepInEx.Preloader/Entrypoint.cs b/BepInEx.Preloader/Entrypoint.cs index e85651a..30fcbfa 100644 --- a/BepInEx.Preloader/Entrypoint.cs +++ b/BepInEx.Preloader/Entrypoint.cs @@ -91,6 +91,7 @@ namespace BepInEx.Preloader 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 diff --git a/BepInEx.Preloader/Patching/AssemblyPatcher.cs b/BepInEx.Preloader/Patching/AssemblyPatcher.cs index a10a042..70bcd56 100644 --- a/BepInEx.Preloader/Patching/AssemblyPatcher.cs +++ b/BepInEx.Preloader/Patching/AssemblyPatcher.cs @@ -196,15 +196,7 @@ namespace BepInEx.Preloader.Patching 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; - } + return Utility.TryParseAssemblyName(fullName, out var assName) ? assName.Name : fullName; } /// diff --git a/BepInEx/Bootstrap/TypeLoader.cs b/BepInEx/Bootstrap/TypeLoader.cs index 9a16a74..8602a53 100644 --- a/BepInEx/Bootstrap/TypeLoader.cs +++ b/BepInEx/Bootstrap/TypeLoader.cs @@ -68,11 +68,10 @@ namespace BepInEx.Bootstrap Resolver.ResolveFailure += (sender, reference) => { - var name = new AssemblyName(reference.FullName); - - if (Utility.TryResolveDllAssembly(name, Paths.BepInExAssemblyDirectory, ReaderParameters, out var assembly) || - Utility.TryResolveDllAssembly(name, Paths.PluginPath, ReaderParameters, out assembly) || - Utility.TryResolveDllAssembly(name, Paths.ManagedPath, ReaderParameters, out assembly)) + if (Utility.TryParseAssemblyName(reference.FullName, out var name) && + (Utility.TryResolveDllAssembly(name, Paths.BepInExAssemblyDirectory, ReaderParameters, out var assembly) || + Utility.TryResolveDllAssembly(name, Paths.PluginPath, ReaderParameters, out assembly) || + Utility.TryResolveDllAssembly(name, Paths.ManagedPath, ReaderParameters, out assembly))) return assembly; return AssemblyResolve?.Invoke(sender, reference); diff --git a/BepInEx/Utility.cs b/BepInEx/Utility.cs index 1bdd45a..a853a6b 100644 --- a/BepInEx/Utility.cs +++ b/BepInEx/Utility.cs @@ -280,6 +280,11 @@ namespace BepInEx /// 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 @@ -289,6 +294,7 @@ namespace BepInEx } catch (Exception e) { + File.AppendAllText("tryparseerr.log", $"Failed to parse {fullName}: {e}"); assemblyName = null; return false; }