Ejemplo n.º 1
0
        AssemblyDefinition GetCorlib(AssemblyNameReference reference, ReaderParameters parameters)
        {
            var version = reference.Version;
            var corlib  = typeof(object).Assembly.GetName();

            if (corlib.Version == version || IsZero(version))
            {
                return(GetAssembly(typeof(object).Module.FullyQualifiedName, parameters));
            }

            var path = Directory.GetParent(
                Directory.GetParent(
                    typeof(object).Module.FullyQualifiedName).FullName
                ).FullName;

            if (on_mono)
            {
                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
                {
                    throw new NotSupportedException("Version not supported: " + version);
                }
            }
            else
            {
                switch (version.Major)
                {
                case 1:
                    if (version.MajorRevision == 3300)
                    {
                        path = Path.Combine(path, "v1.0.3705");
                    }
                    else
                    {
                        path = Path.Combine(path, "v1.1.4322");
                    }
                    break;

                case 2:
                    path = Path.Combine(path, "v2.0.50727");
                    break;

                case 4:
                    path = Path.Combine(path, "v4.0.30319");
                    break;

                default:
                    throw new NotSupportedException("Version not supported: " + version);
                }
            }

            var file = Path.Combine(path, "mscorlib.dll");

            if (File.Exists(file))
            {
                return(GetAssembly(file, parameters));
            }

            if (on_mono && Directory.Exists(path + "-api"))
            {
                file = Path.Combine(path + "-api", "mscorlib.dll");
                if (File.Exists(file))
                {
                    return(GetAssembly(file, parameters));
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        protected virtual AssemblyDefinition SearchDirectory(AssemblyNameReference name, IEnumerable <string> directories, ReaderParameters parameters)
        {
            var extensions = name.IsWindowsRuntime ? new [] { ".winmd", ".dll" } : new [] { ".exe", ".dll" };

            foreach (var directory in directories)
            {
                foreach (var extension in extensions)
                {
                    string file = Path.Combine(directory, name.Name + extension);
                    if (!File.Exists(file))
                    {
                        continue;
                    }
                    try {
                        return(GetAssembly(file, parameters));
                    } catch (System.BadImageFormatException) {
                        continue;
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
        public virtual AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            Mixin.CheckName(name);
            Mixin.CheckParameters(parameters);

            var assembly = SearchDirectory(name, directories, parameters);

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

            if (name.IsRetargetable)
            {
                // if the reference is retargetable, zero it
                name = new AssemblyNameReference(name.Name, Mixin.ZeroVersion)
                {
                    PublicKeyToken = Empty <byte> .Array,
                };
            }

#if NET_CORE
            assembly = SearchTrustedPlatformAssemblies(name, parameters);
            if (assembly != null)
            {
                return(assembly);
            }
#else
            var framework_dir  = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);
            var framework_dirs = on_mono
                                ? new [] { framework_dir, Path.Combine(framework_dir, "Facades") }
                                : new [] { framework_dir };

            if (IsZero(name.Version))
            {
                assembly = SearchDirectory(name, framework_dirs, parameters);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            if (name.Name == "mscorlib")
            {
                assembly = GetCorlib(name, parameters);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            assembly = GetAssemblyInGac(name, parameters);
            if (assembly != null)
            {
                return(assembly);
            }

            assembly = SearchDirectory(name, framework_dirs, parameters);
            if (assembly != null)
            {
                return(assembly);
            }
#endif
            if (ResolveFailure != null)
            {
                assembly = ResolveFailure(this, name);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            throw new AssemblyResolutionException(name);
        }
Ejemplo n.º 4
0
        AssemblyDefinition SearchTrustedPlatformAssemblies(AssemblyNameReference name, ReaderParameters parameters)
        {
            if (name.IsWindowsRuntime)
            {
                return(null);
            }

            if (TrustedPlatformAssemblies.Value.TryGetValue(name.Name, out string path))
            {
                return(GetAssembly(path, parameters));
            }

            return(null);
        }
Ejemplo n.º 5
0
        private static ModuleDefinition ReadModule(Disposable <Stream> stream, string fileName, ReaderParameters parameters)
        {
            Mixin.CheckParameters(parameters);

            return(ModuleReader.CreateModule(
                       ImageReader.ReadImage(stream, fileName),
                       parameters));
        }