Example #1
0
 IDsDocument Resolve(IAssembly assembly, ModuleDef sourceModule)
 {
     if (assembly.IsContentTypeWindowsRuntime)
     {
         if (failedAssemblyResolveCache.IsFailed(assembly))
         {
             return(null);
         }
         var document = ResolveWinMD(assembly, sourceModule);
         if (document == null)
         {
             failedAssemblyResolveCache.MarkFailed(assembly);
         }
         return(document);
     }
     else
     {
         if (failedAssemblyResolveCache.IsFailed(assembly))
         {
             return(null);
         }
         var document = ResolveNormal(assembly, sourceModule);
         if (document == null)
         {
             failedAssemblyResolveCache.MarkFailed(assembly);
         }
         return(document);
     }
 }
Example #2
0
        public IDsDocument Resolve(IAssembly assembly, ModuleDef sourceModule = null)
        {
            var tempAsm = assembly;

            FrameworkRedirect.ApplyFrameworkRedirect(ref tempAsm, sourceModule);
            // OK : System.Runtime 4.0.20.0 => 4.0.0.0
            // BAD: System 4.0.0.0 => 2.0.0.0
            if (tempAsm.Version.Major >= assembly.Version.Major)
            {
                assembly = tempAsm;
            }

            // Most people don't have the old mscrolib 1.x files, but there could still be references
            // to them, eg. some of the older VS SDK Interop assemblies have refs to them.
            if (assembly.Version.Major == 1 && assembly.Name == mscorlibName && PublicKeyBase.TokenEquals(assembly.PublicKeyOrToken, mscorlibPublicKeyToken))
            {
                assembly = mscorlibRef40;
            }

            if (assembly.IsContentTypeWindowsRuntime)
            {
                if (failedAssemblyResolveCache.IsFailed(assembly))
                {
                    return(null);
                }
                var document = ResolveWinMD(assembly, sourceModule);
                if (document == null)
                {
                    failedAssemblyResolveCache.MarkFailed(assembly);
                }
                return(document);
            }
            else
            {
                // WinMD files have a reference to mscorlib but its version is always 255.255.255.255
                // since mscorlib isn't really loaded. The resolver only loads exact versions, so
                // we must change the version or the resolve will fail.
                if (assembly.Name == mscorlibName && assembly.Version == invalidMscorlibVersion)
                {
                    assembly = new AssemblyNameInfo(assembly)
                    {
                        Version = newMscorlibVersion
                    }
                }
                ;

                if (failedAssemblyResolveCache.IsFailed(assembly))
                {
                    return(null);
                }
                var document = ResolveNormal(assembly, sourceModule);
                if (document == null)
                {
                    failedAssemblyResolveCache.MarkFailed(assembly);
                }
                return(document);
            }
        }
Example #3
0
        public IDnSpyFile Resolve(IAssembly assembly, ModuleDef sourceModule = null)
        {
            var tempAsm = assembly;

            FrameworkRedirect.ApplyFrameworkRedirect(ref tempAsm, sourceModule);
            // OK : System.Runtime 4.0.20.0 => 4.0.0.0
            // BAD: System 4.0.0.0 => 2.0.0.0
            if (tempAsm.Version.Major >= assembly.Version.Major)
            {
                assembly = tempAsm;
            }

            if (assembly.IsContentTypeWindowsRuntime)
            {
                if (failedAssemblyResolveCache.IsFailed(assembly))
                {
                    return(null);
                }
                var file = ResolveWinMD(assembly, sourceModule);
                if (file == null)
                {
                    failedAssemblyResolveCache.MarkFailed(assembly);
                }
                return(file);
            }
            else
            {
                // WinMD files have a reference to mscorlib but its version is always 255.255.255.255
                // since mscorlib isn't really loaded. The resolver only loads exact versions, so
                // we must change the version or the resolve will fail.
                if (assembly.Name == mscorlibName && assembly.Version == invalidMscorlibVersion)
                {
                    assembly = new AssemblyNameInfo(assembly)
                    {
                        Version = newMscorlibVersion
                    }
                }
                ;

                if (failedAssemblyResolveCache.IsFailed(assembly))
                {
                    return(null);
                }
                var file = ResolveNormal(assembly, sourceModule);
                if (file == null)
                {
                    failedAssemblyResolveCache.MarkFailed(assembly);
                }
                return(file);
            }
        }
Example #4
0
        public IDnSpyFile Resolve(IAssembly assembly, ModuleDef sourceModule = null)
        {
            if (assembly.IsContentTypeWindowsRuntime)
            {
                if (failedAssemblyResolveCache.IsFailed(assembly))
                {
                    return(null);
                }
                var file = ResolveWinMD(assembly, sourceModule);
                if (file == null)
                {
                    failedAssemblyResolveCache.MarkFailed(assembly);
                }
                return(file);
            }
            else
            {
                // WinMD files have a reference to mscorlib but its version is always 255.255.255.255
                // since mscorlib isn't really loaded. The resolver only loads exact versions, so
                // we must change the version or the resolve will fail.
                if (assembly.Name == "mscorlib" && assembly.Version == invalidMscorlibVersion)
                {
                    assembly = new AssemblyNameInfo(assembly)
                    {
                        Version = newMscorlibVersion
                    }
                }
                ;

                if (failedAssemblyResolveCache.IsFailed(assembly))
                {
                    return(null);
                }
                var file = ResolveNormal(assembly, sourceModule);
                if (file == null)
                {
                    failedAssemblyResolveCache.MarkFailed(assembly);
                }
                return(file);
            }
        }

        IDnSpyFile ResolveNormal(IAssembly assembly, ModuleDef sourceModule)
        {
            var existingFile = fileManager.FindAssembly(assembly);

            if (existingFile != null)
            {
                return(existingFile);
            }

            var file = LookupFromSearchPaths(assembly, sourceModule, true);

            if (file != null)
            {
                return(fileManager.GetOrAddCanDispose(file));
            }

            if (fileManager.Settings.UseGAC)
            {
                var gacFile = GacInfo.FindInGac(assembly);
                if (gacFile != null)
                {
                    return(fileManager.TryGetOrCreateInternal(DnSpyFileInfo.CreateFile(gacFile), true, true));
                }
                foreach (var path in GacInfo.OtherGacPaths)
                {
                    file = TryLoadFromDir(assembly, true, path);
                    if (file != null)
                    {
                        return(fileManager.GetOrAddCanDispose(file));
                    }
                }
            }

            file = LookupFromSearchPaths(assembly, sourceModule, false);
            if (file != null)
            {
                return(fileManager.GetOrAddCanDispose(file));
            }

            return(null);
        }

        IDnSpyFile LookupFromSearchPaths(IAssembly asmName, ModuleDef sourceModule, bool exactCheck)
        {
            IDnSpyFile file;
            string     sourceModuleDir = null;

            if (sourceModule != null && File.Exists(sourceModule.Location))
            {
                sourceModuleDir = Path.GetDirectoryName(sourceModule.Location);
                file            = TryLoadFromDir(asmName, exactCheck, sourceModuleDir);
                if (file != null)
                {
                    return(file);
                }
            }
            var ary = asmSearchPathsArray;

            foreach (var path in ary)
            {
                file = TryLoadFromDir(asmName, exactCheck, path);
                if (file != null)
                {
                    return(file);
                }
            }

            return(null);
        }

        IDnSpyFile TryLoadFromDir(IAssembly asmName, bool exactCheck, string dirPath)
        {
            string baseName;

            try {
                baseName = Path.Combine(dirPath, asmName.Name);
            }
            catch (ArgumentException) {             // eg. invalid chars in asmName.Name
                return(null);
            }
            return(TryLoadFromDir2(asmName, exactCheck, baseName + ".dll") ??
                   TryLoadFromDir2(asmName, exactCheck, baseName + ".exe"));
        }

        IDnSpyFile TryLoadFromDir2(IAssembly asmName, bool exactCheck, string filename)
        {
            if (!File.Exists(filename))
            {
                return(null);
            }

            IDnSpyFile file  = null;
            bool       error = true;

            try {
                file = fileManager.TryCreateDnSpyFile(DnSpyFileInfo.CreateFile(filename));
                if (file == null)
                {
                    return(null);
                }
                file.IsAutoLoaded = true;
                var asm = file.AssemblyDef;
                if (asm == null)
                {
                    return(null);
                }
                bool b = exactCheck ?
                         AssemblyNameComparer.CompareAll.Equals(asmName, asm) :
                         AssemblyNameComparer.NameAndPublicKeyTokenOnly.Equals(asmName, asm);
                if (!b)
                {
                    return(null);
                }

                error = false;
                return(file);
            }
            finally {
                if (error)
                {
                    if (file is IDisposable)
                    {
                        ((IDisposable)file).Dispose();
                    }
                }
            }
        }

        IDnSpyFile ResolveWinMD(IAssembly assembly, ModuleDef sourceModule)
        {
            var existingFile = fileManager.FindAssembly(assembly);

            if (existingFile != null)
            {
                return(existingFile);
            }

            foreach (var winmdPath in GacInfo.WinmdPaths)
            {
                string file;
                try {
                    file = Path.Combine(winmdPath, assembly.Name + ".winmd");
                }
                catch (ArgumentException) {
                    continue;
                }
                if (File.Exists(file))
                {
                    return(fileManager.TryGetOrCreateInternal(DnSpyFileInfo.CreateFile(file), true, true));
                }
            }
            return(null);
        }
    }