static List <TestCase> Monoc(string filename, List <TestCase> lis)
        {
            List <TestCase> methods = new List <TestCase>();

            string   exactPath = Path.GetFullPath(filename);
            Assembly testdll   = Assembly.LoadFile(exactPath);

            Mono.Cecil.ReaderParameters readerParameters =
                new Mono.Cecil.ReaderParameters {
                ReadSymbols = true
            };
            Mono.Cecil.AssemblyDefinition assemblyDefinition;
            try {
                assemblyDefinition =
                    Mono.Cecil.AssemblyDefinition.ReadAssembly(filename, readerParameters);
            } catch (Exception) {
                readerParameters = new Mono.Cecil.ReaderParameters {
                    ReadSymbols = false
                };
                assemblyDefinition =
                    Mono.Cecil.AssemblyDefinition.ReadAssembly(filename, readerParameters);
            }

            Mono.Cecil.ModuleDefinition module = assemblyDefinition.MainModule;
            methods = ProcessTypes(module.Types, methods, lis);
            methods.Sort();

            return(methods);
        }
Exemple #2
0
        public static WriterParameters GetWriterParameters(Mono.Cecil.ReaderParameters readParams)
        {
            WriterParameters parameters = new WriterParameters();

            if (readParams.SymbolReaderProvider is PdbReaderProvider)
            {
                parameters.SymbolWriterProvider = new PdbWriterProvider();
                return(parameters);
            }
            if (readParams.SymbolReaderProvider is MdbReaderProvider)
            {
                parameters.SymbolWriterProvider = new MdbWriterProvider();
            }
            return(parameters);
        }
Exemple #3
0
        public static Mono.Cecil.ReaderParameters ReaderParameters(string assemblyPath, IEnumerable <string> extraPaths, IAssemblyResolver assemblyResolver, string unityEngineDLLPath, string unityUNetDLLPath)
        {
            Mono.Cecil.ReaderParameters parameters = new Mono.Cecil.ReaderParameters();
            if (assemblyResolver == null)
            {
                assemblyResolver = new DefaultAssemblyResolver();
            }
            AddSearchDirectoryHelper helper = new AddSearchDirectoryHelper(assemblyResolver);

            helper.AddSearchDirectory(Path.GetDirectoryName(assemblyPath));
            helper.AddSearchDirectory(UnityEngineDLLDirectoryName());
            helper.AddSearchDirectory(Path.GetDirectoryName(unityEngineDLLPath));
            helper.AddSearchDirectory(Path.GetDirectoryName(unityUNetDLLPath));
            if (extraPaths != null)
            {
                foreach (string str in extraPaths)
                {
                    helper.AddSearchDirectory(str);
                }
            }
            parameters.AssemblyResolver     = assemblyResolver;
            parameters.SymbolReaderProvider = GetSymbolReaderProvider(assemblyPath);
            return(parameters);
        }
        AssemblyDefinition SearchDirectory(AssemblyNameReference name, IEnumerable <string> directories, ReaderParameters parameters)
        {
            var extensions = 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))
                    {
                        return(GetAssembly(file, parameters));
                    }
                }
            }

            return(null);
        }
        public virtual AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (parameters == null)
            {
                parameters = new ReaderParameters();
            }

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

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

#if !SILVERLIGHT && !CF
            if (name.IsRetargetable)
            {
                // if the reference is retargetable, zero it
                name = new AssemblyNameReference(name.Name, new Version(0, 0, 0, 0))
                {
                    PublicKeyToken = Empty <byte> .Array,
                };
            }

            var framework_dir = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);

            if (IsZero(name.Version))
            {
                assembly = SearchDirectory(name, new [] { framework_dir }, 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, new [] { framework_dir }, parameters);
            if (assembly != null)
            {
                return(assembly);
            }
#endif

            if (ResolveFailure != null)
            {
                assembly = ResolveFailure(this, name);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            throw new AssemblyResolutionException(name);
        }
Exemple #6
0
        public virtual AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (parameters == null)
            {
                parameters = new ReaderParameters();
            }

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

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

#if !SILVERLIGHT && !CF
            var framework_dir = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);

            if (IsZero(name.Version))
            {
                assembly = SearchDirectory(name, new [] { framework_dir }, 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, new [] { framework_dir }, parameters);
            if (assembly != null)
            {
                return(assembly);
            }
#endif

            if (ResolveFailure != null)
            {
                assembly = ResolveFailure(this, name);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            //throw new AssemblyResolutionException (name);
            return(null);
        }
 public Mono.Cecil.AssemblyDefinition Resolve(string assemblyName, Mono.Cecil.ReaderParameters parameters)
 {
     return(ResolveAssemblyByLocation(GetReferenceLocation(assemblyName), parameters));
 }
        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,
                };
            }

            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);
            }

            if (ResolveFailure != null)
            {
                assembly = ResolveFailure(this, name);
                if (assembly != null)
                {
                    return(assembly);
                }
            }

            throw new AssemblyResolutionException(name);
        }
        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.0.5000.0");
                    }
                    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));
            }

            return(null);
        }
        static ModuleDefinition ReadModule(Disposable <Stream> stream, string fileName, ReaderParameters parameters)
        {
            Mixin.CheckParameters(parameters);

            return(ModuleReader.CreateModule(
                       ImageReader.ReadImage(stream, fileName),
                       parameters));
        }
Exemple #11
0
 public static AssemblyDefinition ReadAssembly(Stream stream, ReaderParameters parameters)
 {
     return(ReadAssembly(ModuleDefinition.ReadModule(stream, parameters)));
 }
Exemple #12
0
 public static Mono.Cecil.ReaderParameters ReaderParameters(string assemblyPath, IEnumerable<string> extraPaths, IAssemblyResolver assemblyResolver, string unityEngineDLLPath, string unityUNetDLLPath)
 {
     Mono.Cecil.ReaderParameters parameters = new Mono.Cecil.ReaderParameters();
     if (assemblyResolver == null)
     {
         assemblyResolver = new DefaultAssemblyResolver();
     }
     AddSearchDirectoryHelper helper = new AddSearchDirectoryHelper(assemblyResolver);
     helper.AddSearchDirectory(Path.GetDirectoryName(assemblyPath));
     helper.AddSearchDirectory(UnityEngineDLLDirectoryName());
     helper.AddSearchDirectory(Path.GetDirectoryName(unityEngineDLLPath));
     helper.AddSearchDirectory(Path.GetDirectoryName(unityUNetDLLPath));
     if (extraPaths != null)
     {
         foreach (string str in extraPaths)
         {
             helper.AddSearchDirectory(str);
         }
     }
     parameters.AssemblyResolver = assemblyResolver;
     parameters.SymbolReaderProvider = GetSymbolReaderProvider(assemblyPath);
     return parameters;
 }
Exemple #13
0
 public static ModuleDefinition ReadModule(Stream stream, ReaderParameters parameters)
 {
     Mixin.CheckStream(stream);
     Mixin.CheckReadSeek(stream);
     return(ReadModule(Disposable.NotOwned(stream), stream.GetFileName(), parameters));
 }
Exemple #14
0
 private AssemblyDefinition GetAssemblyInNetGac(AssemblyNameReference reference, ReaderParameters parameters)
 {
     string[] strArrays = new string[] { "GAC_MSIL", "GAC_32", "GAC_64", "GAC" };
     string[] empty     = new string[] { string.Empty, "v4.0_" };
     for (int i = 0; i < 2; i++)
     {
         for (int j = 0; j < (int)strArrays.Length; j++)
         {
             string str          = Path.Combine(this.gac_paths[i], strArrays[j]);
             string assemblyFile = BaseAssemblyResolver.GetAssemblyFile(reference, empty[i], str);
             if (Directory.Exists(str) && File.Exists(assemblyFile))
             {
                 return(this.GetAssembly(assemblyFile, parameters));
             }
         }
     }
     return(null);
 }
Exemple #15
0
 private AssemblyDefinition GetAssemblyInMonoGac(AssemblyNameReference reference, ReaderParameters parameters)
 {
     for (int i = 0; i < this.gac_paths.Count; i++)
     {
         string item         = this.gac_paths[i];
         string assemblyFile = BaseAssemblyResolver.GetAssemblyFile(reference, string.Empty, item);
         if (File.Exists(assemblyFile))
         {
             return(this.GetAssembly(assemblyFile, parameters));
         }
     }
     return(null);
 }
Exemple #16
0
        private AssemblyDefinition SearchDirectory(AssemblyNameReference name, IEnumerable <string> directories, ReaderParameters parameters)
        {
            AssemblyDefinition assembly;

            string[] strArrays = new string[] { ".exe", ".dll" };
            using (IEnumerator <string> enumerator = directories.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    string   current    = enumerator.Current;
                    string[] strArrays1 = strArrays;
                    int      num        = 0;
                    while (num < (int)strArrays1.Length)
                    {
                        string str  = strArrays1[num];
                        string str1 = Path.Combine(current, string.Concat(name.Name, str));
                        if (!File.Exists(str1))
                        {
                            num++;
                        }
                        else
                        {
                            assembly = this.GetAssembly(str1, parameters);
                            return(assembly);
                        }
                    }
                }
                return(null);
            }
            return(assembly);
        }
Exemple #17
0
        public virtual AssemblyDefinition Resolve(AssemblyNameReference name, ReaderParameters parameters)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (parameters == null)
            {
                parameters = new ReaderParameters();
            }
            AssemblyDefinition corlib = this.SearchDirectory(name, this.directories, parameters);

            if (corlib != null)
            {
                return(corlib);
            }
            if (name.IsRetargetable)
            {
                name = new AssemblyNameReference(name.Name, new Version(0, 0, 0, 0))
                {
                    PublicKeyToken = Empty <byte> .Array
                };
            }
            string directoryName = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);

            if (BaseAssemblyResolver.IsZero(name.Version))
            {
                corlib = this.SearchDirectory(name, new string[] { directoryName }, parameters);
                if (corlib != null)
                {
                    return(corlib);
                }
            }
            if (name.Name == "mscorlib")
            {
                corlib = this.GetCorlib(name, parameters);
                if (corlib != null)
                {
                    return(corlib);
                }
            }
            corlib = this.GetAssemblyInGac(name, parameters);
            if (corlib != null)
            {
                return(corlib);
            }
            corlib = this.SearchDirectory(name, new string[] { directoryName }, parameters);
            if (corlib != null)
            {
                return(corlib);
            }
            if (this.ResolveFailure != null)
            {
                corlib = this.ResolveFailure(this, name);
                if (corlib != null)
                {
                    return(corlib);
                }
            }
            throw new AssemblyResolutionException(name);
        }
Exemple #18
0
        private AssemblyDefinition GetCorlib(AssemblyNameReference reference, ReaderParameters parameters)
        {
            Version version = reference.Version;

            if (typeof(object).Assembly.GetName().Version == version || BaseAssemblyResolver.IsZero(version))
            {
                return(this.GetAssembly(typeof(object).Module.FullyQualifiedName, parameters));
            }
            string fullName = Directory.GetParent(Directory.GetParent(typeof(object).Module.FullyQualifiedName).FullName).FullName;

            if (!BaseAssemblyResolver.on_mono)
            {
                switch (version.Major)
                {
                case 1:
                {
                    if (version.MajorRevision != 3300)
                    {
                        fullName = Path.Combine(fullName, "v1.0.5000.0");
                        break;
                    }
                    else
                    {
                        fullName = Path.Combine(fullName, "v1.0.3705");
                        break;
                    }
                }

                case 2:
                {
                    fullName = Path.Combine(fullName, "v2.0.50727");
                    break;
                }

                case 3:
                {
                    throw new NotSupportedException(string.Concat("Version not supported: ", version));
                }

                case 4:
                {
                    fullName = Path.Combine(fullName, "v4.0.30319");
                    break;
                }

                default:
                {
                    throw new NotSupportedException(string.Concat("Version not supported: ", version));
                }
                }
            }
            else if (version.Major == 1)
            {
                fullName = Path.Combine(fullName, "1.0");
            }
            else if (version.Major != 2)
            {
                if (version.Major != 4)
                {
                    throw new NotSupportedException(string.Concat("Version not supported: ", version));
                }
                fullName = Path.Combine(fullName, "4.0");
            }
            else
            {
                fullName = (version.MajorRevision != 5 ? Path.Combine(fullName, "2.0") : Path.Combine(fullName, "2.1"));
            }
            string str = Path.Combine(fullName, "mscorlib.dll");

            if (!File.Exists(str))
            {
                return(null);
            }
            return(this.GetAssembly(str, parameters));
        }
Exemple #19
0
 public static AssemblyDefinition ReadAssembly(string fileName, ReaderParameters parameters)
 {
     return(ReadAssembly(ModuleDefinition.ReadModule(fileName, parameters)));
 }
        AssemblyDefinition GetAssembly(string file, ReaderParameters parameters)
        {
            if (parameters.AssemblyResolver == null)
            {
                parameters.AssemblyResolver = this;
            }

            var assemblyDef = ModuleDefinition.ReadModule(file, parameters).Assembly;

            if (assemblyDef == null || parameters.TargetArchitecture == TargetArchitecture.Any)
            {
                return(assemblyDef);
            }

            switch (parameters.TargetArchitecture)
            {
            case TargetArchitecture.ILOnly:
            {
                var is64Bit         = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);
                var is32BitRequired = (assemblyDef.MainModule.Attributes & ModuleAttributes.Required32Bit) == ModuleAttributes.Required32Bit;
                var ilOnly          = (assemblyDef.MainModule.Attributes & ModuleAttributes.ILOnly) == ModuleAttributes.ILOnly;

                if (!(ilOnly && !is32BitRequired && !is64Bit))
                {
                    assemblyDef = null;
                }

                break;
            }

            case TargetArchitecture.x86:
            {
                var is64Bit = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);
                var is32Bit = !is64Bit;

                if (!is32Bit)
                {
                    assemblyDef = null;
                }

                break;
            }

            case TargetArchitecture.x86Only:
            {
                var is64Bit         = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);
                var is32Bit         = !is64Bit;
                var is32BitRequired = (assemblyDef.MainModule.Attributes & ModuleAttributes.Required32Bit) == ModuleAttributes.Required32Bit;

                if (!(is32Bit && is32BitRequired))
                {
                    assemblyDef = null;
                }

                break;
            }

            case TargetArchitecture.x64:
            {
                var is64Bit         = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);
                var is32BitRequired = (assemblyDef.MainModule.Attributes & ModuleAttributes.Required32Bit) == ModuleAttributes.Required32Bit;
                var ilOnly          = (assemblyDef.MainModule.Attributes & ModuleAttributes.ILOnly) == ModuleAttributes.ILOnly;

                if (!(is64Bit || (ilOnly && !is32BitRequired)))
                {
                    assemblyDef = null;
                }

                break;
            }

            case TargetArchitecture.x64Only:
            {
                var is64Bit = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);

                if (!is64Bit)
                {
                    assemblyDef = null;
                }

                break;
            }
            }

            return(assemblyDef);
        }
        AssemblyDefinition SearchDirectory(AssemblyNameReference name, IEnumerable <string> directories, ReaderParameters parameters)
        {
            var extensions = new[] { ".exe", ".dll" };

            {
                // foreach(var directory in directories)
                var __enumerator1 = (directories).GetEnumerator();
                while (__enumerator1.MoveNext())
                {
                    var directory = __enumerator1.Current;
                    {
                        {
                            var __array2       = extensions;
                            var __arrayLength2 = __array2.Length;
                            for (int __i2 = 0; __i2 < __arrayLength2; ++__i2)
                            {
                                var extension = __array2[__i2];
                                {
                                    string file = Path.Combine(directory, name.Name + extension);
                                    if (File.Exists(file))
                                    {
                                        return(GetAssembly(file, parameters));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(null);
        }
        AssemblyDefinition GetAssemblyByTargetArchitecture(List <AssemblyDefinition> assemblies, ReaderParameters parameters)
        {
            if (assemblies.Count == 0)
            {
                return(null);
            }

            var compatibleAssemblies = new List <AssemblyDefinition>();

            switch (parameters.TargetArchitecture)
            {
            case TargetArchitecture.ILOnly:
            {
                for (var i = assemblies.Count - 1; i >= 0; i--)
                {
                    var assemblyDef = assemblies[i];

                    var is64Bit         = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);
                    var is32BitRequired = (assemblyDef.MainModule.Attributes & ModuleAttributes.Required32Bit) == ModuleAttributes.Required32Bit;
                    var ilOnly          = (assemblyDef.MainModule.Attributes & ModuleAttributes.ILOnly) == ModuleAttributes.ILOnly;

                    if (ilOnly && !is32BitRequired && !is64Bit)
                    {
                        compatibleAssemblies.Add(assemblyDef);
                    }
                }

                break;
            }

            case TargetArchitecture.x86:
            {
                for (var i = 0; i < assemblies.Count; i++)
                {
                    var assemblyDef = assemblies[i];

                    var is32Bit = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.I386);

                    if (is32Bit)
                    {
                        compatibleAssemblies.Add(assemblyDef);
                    }
                }

                break;
            }

            case TargetArchitecture.x86Only:
            {
                for (var i = 0; i < assemblies.Count; i++)
                {
                    var assemblyDef = assemblies[i];

                    var is32Bit         = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.I386);
                    var is32BitRequired = (assemblyDef.MainModule.Attributes & ModuleAttributes.Required32Bit) == ModuleAttributes.Required32Bit;

                    if (is32Bit && is32BitRequired)
                    {
                        compatibleAssemblies.Add(assemblyDef);
                    }
                }

                break;
            }

            case TargetArchitecture.x64:
            {
                for (var i = 0; i < assemblies.Count; i++)
                {
                    var assemblyDef = assemblies[i];

                    var is64Bit         = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);
                    var is32BitRequired = (assemblyDef.MainModule.Attributes & ModuleAttributes.Required32Bit) == ModuleAttributes.Required32Bit;
                    var ilOnly          = (assemblyDef.MainModule.Attributes & ModuleAttributes.ILOnly) == ModuleAttributes.ILOnly;

                    if (is64Bit || (ilOnly && !is32BitRequired))
                    {
                        compatibleAssemblies.Add(assemblyDef);
                    }
                }

                break;
            }

            case TargetArchitecture.x64Only:
            {
                for (var i = 0; i < assemblies.Count; i++)
                {
                    var assemblyDef = assemblies[i];

                    var is64Bit = (assemblyDef.MainModule.Architecture == ProcessorArchitecture.AMD64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.IA64 || assemblyDef.MainModule.Architecture == ProcessorArchitecture.ARM64);

                    if (is64Bit)
                    {
                        compatibleAssemblies.Add(assemblyDef);
                    }
                }

                break;
            }

            default:
                compatibleAssemblies.AddRange(assemblies);
                break;
            }

            if (compatibleAssemblies.Count == 0)
            {
                return(null);
            }

            return(compatibleAssemblies[0]);
        }
        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);
        }
Exemple #24
0
        /*Telerik Authorship*/
        private AssemblyDefinition Resolve(AssemblyNameReference name, string defaultPath, ReaderParameters parameters, TargetArchitecture architecture, bool bubbleToUserIfFailed, bool addToFailedCache = true)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (parameters == null)
            {
                parameters = new ReaderParameters(this);
            }
            if (assemblyPathResolver.IsFailedAssembly(name.FullName))
            {
                return(null);
            }

            AssemblyDefinition assembly =
                GetFromResolvedAssemblies(new AssemblyName(name.Name, name.FullName, name.Version, name.PublicKey)
            {
                TargetArchitecture = architecture
            });

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

            /*Telerik Authorship*/
            // This code has been added by Mono.Cecil 0.9.6. It has been commented, because retargetable references should be further
            // researched and handled appropriately across the application. TP item N. 323383
            //if (name.IsRetargetable)
            //{
            //	// if the reference is retargetable, zero it
            //	name = new AssemblyNameReference(name.Name, new Version(0, 0, 0, 0))
            //	{
            //		PublicKeyToken = Empty<byte>.Array,
            //	};
            //}

            assembly = SearchDirectory(name, parameters, architecture, defaultPath) ?? TryGetTargetAssembly(name, parameters, architecture);

            if (assembly != null)
            {
                if (!filePathToAssemblyDefinitionCache.ContainsKey(assembly.MainModule.FilePath))
                {
                    AddToResolvedAssemblies(assembly);
                }
                return(assembly);
            }
            assembly = GetTargetAssembly(name, parameters, architecture);
            if (assembly != null)
            {
                if (!filePathToAssemblyDefinitionCache.ContainsKey(assembly.MainModule.FilePath))
                {
                    AddToResolvedAssemblies(assembly);
                }
                return(assembly);
            }
            if (bubbleToUserIfFailed)
            {
                return(UserSpecifiedAssembly(name, architecture));
            }
            else if (addToFailedCache)
            {
                assemblyPathResolver.AddToUnresolvedCache(name.FullName);
            }
            return(null);
        }
Exemple #25
0
 public static ModuleDefinition ReadModule(string fileName, ReaderParameters parameters)
 {
     using (var stream = GetFileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read)) {
         return(ReadModule(stream, parameters));
     }
 }
Exemple #26
0
        /*Telerik Authorship*/
        private AssemblyDefinition Resolve(AssemblyNameReference name, string defaultPath, ReaderParameters parameters, TargetArchitecture architecture, bool bubbleToUserIfFailed, bool addToFailedCache = true)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (parameters == null)
            {
                parameters = new ReaderParameters(this);
            }
            if (assemblyPathResolver.IsFailedAssembly(name.FullName))
            {
                return(null);
            }

            AssemblyDefinition assembly =
                GetFromResolvedAssemblies(new AssemblyName(name.Name, name.FullName, name.Version, name.PublicKey)
            {
                TargetArchitecture = architecture
            });

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

            assembly = SearchDirectory(name, parameters, architecture, defaultPath) ?? TryGetTargetAssembly(name, parameters, architecture);

            if (assembly != null)
            {
                if (!filePathToAssemblyDefinitionCache.ContainsKey(assembly.MainModule.FilePath))
                {
                    AddToResolvedAssemblies(assembly);
                }
                return(assembly);
            }
            assembly = GetTargetAssembly(name, parameters, architecture);
            if (assembly != null)
            {
                if (!filePathToAssemblyDefinitionCache.ContainsKey(assembly.MainModule.FilePath))
                {
                    AddToResolvedAssemblies(assembly);
                }
                return(assembly);
            }
            if (bubbleToUserIfFailed)
            {
                return(UserSpecifiedAssembly(name, architecture));
            }
            else if (addToFailedCache)
            {
                assemblyPathResolver.AddToUnresolvedCache(name.FullName);
            }
            return(null);
        }
        public Mono.Cecil.AssemblyDefinition ResolveAssemblyByLocation(string referenceLocation, Mono.Cecil.ReaderParameters parameters)
        {
            if (referenceLocation == null)
            {
                return(null);
            }

            var assemblyName  = Path.GetFileName(referenceLocation);
            var lastTimeWrite = File.GetLastWriteTime(referenceLocation);

            if (_assemblyCache.TryGetValue(assemblyName, out var cacheEntry))
            {
                if (lastTimeWrite == cacheEntry.lastTimeWrite)
                {
                    return(cacheEntry.assemblyDefinition);
                }
            }

            parameters.AssemblyResolver = this;

            using (var stream = new MemoryStream(File.ReadAllBytes(referenceLocation)))
            {
                var assemblyDefinition = Mono.Cecil.AssemblyDefinition.ReadAssembly(stream, parameters);
                cacheEntry.assemblyDefinition?.Dispose();
                cacheEntry.assemblyDefinition = assemblyDefinition;
                cacheEntry.lastTimeWrite      = lastTimeWrite;
                _assemblyCache[assemblyName]  = cacheEntry;
            }

            return(cacheEntry.assemblyDefinition);
        }
        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);
        }