From 929e4d0f59006fd188328c762795b4d4d07bcaef Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 27 Jun 2018 23:42:37 +0200 Subject: [PATCH] Fix #977: Fix loading of mscorlib v1.x --- .../DotNetCore/UniversalAssemblyResolver.cs | 96 ++++++++++++------- 1 file changed, 62 insertions(+), 34 deletions(-) diff --git a/ICSharpCode.Decompiler/DotNetCore/UniversalAssemblyResolver.cs b/ICSharpCode.Decompiler/DotNetCore/UniversalAssemblyResolver.cs index 65748125d..3b7fdf365 100644 --- a/ICSharpCode.Decompiler/DotNetCore/UniversalAssemblyResolver.cs +++ b/ICSharpCode.Decompiler/DotNetCore/UniversalAssemblyResolver.cs @@ -254,52 +254,80 @@ namespace ICSharpCode.Decompiler if (corlib.Version == version || IsZero(version)) return typeof(object).Module.FullyQualifiedName; - var path = Directory.GetParent( - Directory.GetParent( - typeof(object).Module.FullyQualifiedName).FullName - ).FullName; - + string path; if (DetectMono()) { - if (version.Major == 1) - path = Path.Combine(path, "1.0"); - else if (version.Major == 2) { - if (version.MajorRevision == 5) - path = Path.Combine(path, "2.1"); - else - path = Path.Combine(path, "2.0"); - } else if (version.Major == 4) - path = Path.Combine(path, "4.0"); - else { - if (throwOnError) - throw new NotSupportedException("Version not supported: " + version); - return null; - } + path = GetMonoMscorlibBasePath(version); } else { + path = GetMscorlibBasePath(version); + } + + if (path == null) + return null; + + var file = Path.Combine(path, "mscorlib.dll"); + if (File.Exists(file)) + return file; + + return null; + } + + string GetMscorlibBasePath(Version version) + { + string rootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Microsoft.NET"); + string[] frameworkPaths = new[] { + Path.Combine(rootPath, "Framework"), + Path.Combine(rootPath, "Framework64") + }; + + string folder = GetSubFolderForVersion(); + + foreach (var path in frameworkPaths) { + var basePath = Path.Combine(path, folder); + if (Directory.Exists(basePath)) + return basePath; + } + + if (throwOnError) + throw new NotSupportedException("Version not supported: " + version); + return null; + + string GetSubFolderForVersion() + { switch (version.Major) { case 1: if (version.MajorRevision == 3300) - path = Path.Combine(path, "v1.0.3705"); - else - path = Path.Combine(path, "v1.0.5000.0"); - break; + return "v1.0.3705"; + return "v1.1.4322"; case 2: - path = Path.Combine(path, "v2.0.50727"); - break; + return "v2.0.50727"; case 4: - path = Path.Combine(path, "v4.0.30319"); - break; + return "v4.0.30319"; default: if (throwOnError) throw new NotSupportedException("Version not supported: " + version); return null; } } + } - var file = Path.Combine(path, "mscorlib.dll"); - if (File.Exists(file)) - return file; - - return null; + string GetMonoMscorlibBasePath(Version version) + { + var path = Directory.GetParent(typeof(object).Module.FullyQualifiedName).Parent.FullName; + if (version.Major == 1) + path = Path.Combine(path, "1.0"); + else if (version.Major == 2) { + if (version.MajorRevision == 5) + path = Path.Combine(path, "2.1"); + else + path = Path.Combine(path, "2.0"); + } else if (version.Major == 4) + path = Path.Combine(path, "4.0"); + else { + if (throwOnError) + throw new NotSupportedException("Version not supported: " + version); + return null; + } + return path; } static List GetGacPaths() @@ -308,12 +336,12 @@ namespace ICSharpCode.Decompiler return GetDefaultMonoGacPaths(); var paths = new List(2); - var windir = Environment.GetEnvironmentVariable("WINDIR"); + var windir = Environment.GetFolderPath(Environment.SpecialFolder.Windows); if (windir == null) return paths; paths.Add(Path.Combine(windir, "assembly")); - paths.Add(Path.Combine(windir, Path.Combine("Microsoft.NET", "assembly"))); + paths.Add(Path.Combine(windir, "Microsoft.NET", "assembly")); return paths; }