/// <inheritdoc/>
        protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
        {
            if (unmanagedAssemblyMap.TryGetValue(unmanagedDllName, out var libraryTuple))
            {
                var library    = libraryTuple.Item1;
                var assetGroup = libraryTuple.Item2;
                var wrapper    = new CompilationLibrary(library.Type, library.Name, library.Version, library.Hash,
                                                        assetGroup.AssetPaths, library.Dependencies, library.Serviceable);

                var assemblies = new List <string>();
                if (assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies))
                {
                    var resolvedAssemblyPath = assemblies.FirstOrDefault(a => string.Equals(unmanagedDllName, Path.GetFileName(a), StringComparison.OrdinalIgnoreCase));
                    if (resolvedAssemblyPath != null)
                    {
                        var assembly = LoadUnmanagedDllFromPath(resolvedAssemblyPath);
                        if (assembly != null)
                        {
                            return(assembly);
                        }
                    }
                }
            }

            return(base.LoadUnmanagedDll(unmanagedDllName));
        }
Exemplo n.º 2
0
        /// <inheritdoc/>
        public IEnumerable <string> GetAssemblyPathsFor(Library library)
        {
            var compilationLibrary = library as CompilationLibrary;
            var libraryPaths       = new List <string>();

            if (compilationLibrary == null && library is RuntimeLibrary)
            {
                compilationLibrary = GetCompilationLibraryFrom(library as RuntimeLibrary);
                _assemblyResolver.TryResolveAssemblyPaths(compilationLibrary, libraryPaths);
            }
            return(libraryPaths);
        }
Exemplo n.º 3
0
        private Assembly AssemblyLoadContextResolving(AssemblyLoadContext context, AssemblyName name)
        {
            // Attempt to resolve the library from one of the dependency contexts.
            var library = _resolverDependencyContext?.RuntimeLibraries?.FirstOrDefault(NamesMatch);

            if (library == null)
            {
                return(null);
            }

            var wrapper = new CompilationLibrary(
                library.Type,
                library.Name,
                library.Version,
                library.Hash,
                library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
                library.Dependencies,
                library.Serviceable);

            var assemblies = new List <string>();

            if (_assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies))
            {
                foreach (var assembly in assemblies.Select(TryLoadAssemblyFromPath).Where(assembly => assembly != null))
                {
                    return(assembly);
                }
            }

            return(null);

            bool NamesMatch(RuntimeLibrary runtime) => string.Equals(runtime.Name, name.Name, StringComparison.OrdinalIgnoreCase);
        }
        private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
        {
            bool NamesMatch(RuntimeLibrary runtime)
            {
                return(string.Equals(runtime.Name, name.Name, StringComparison.OrdinalIgnoreCase));
            }

            _logger.LogInformation("Loading " + name.FullName);

            var library =
                _dependencyContext.RuntimeLibraries.FirstOrDefault(NamesMatch);

            if (library != null)
            {
                var wrapper = new CompilationLibrary(
                    library.Type,
                    library.Name,
                    library.Version,
                    library.Hash,
                    library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
                    library.Dependencies,
                    library.Serviceable);

                var assemblies = new List <string>();
                _assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies);
                if (assemblies.Count > 0)
                {
                    return(_loadContext.LoadFromAssemblyPath(assemblies[0]));
                }
            }
            _logger.LogWarning("Could not load assembly " + name.FullName);

            return(null);
        }
Exemplo n.º 5
0
        private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
        {
            var library = _dependencyContext.RuntimeLibraries.FirstOrDefault((library) =>
            {
                return(string.Equals(library.Name, name.Name, StringComparison.OrdinalIgnoreCase));
            });

            if (library != null)
            {
                var wrapper = new CompilationLibrary(
                    library.Type,
                    library.Name,
                    library.Version,
                    library.Hash,
                    library.RuntimeAssemblyGroups.SelectMany(group => group.AssetPaths),
                    library.Dependencies,
                    library.Serviceable);

                var assemblies = new List <string>();
                _assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies);
                if (assemblies.Count > 0)
                {
                    return(_loadContext.LoadFromAssemblyPath(assemblies[0]));
                }
            }
            return(null);
        }
        private Assembly LoadContextOnResolving(AssemblyLoadContext context, AssemblyName assemblyName)
        {
            bool NamesMatch(RuntimeLibrary runtime)
            {
                return(string.Equals(runtime.Name, assemblyName.Name, StringComparison.OrdinalIgnoreCase));
            }

#if DEBUG
            _trace.WriteLine($"[NetCoreAssemblyRuntimeLoader] Attempting to resolve [{assemblyName}]");
#endif

            var runtimeLibrary = _dependencyContext.RuntimeLibraries.FirstOrDefault(x => NamesMatch(x));
            if (runtimeLibrary != null)
            {
#if DEBUG
                _trace.WriteLine($"[NetCoreAssemblyRuntimeLoader] Found [{runtimeLibrary}]");
#endif
                var wrapper = new CompilationLibrary(runtimeLibrary.Type, runtimeLibrary.Name,
                                                     runtimeLibrary.Version, runtimeLibrary.Hash,
                                                     runtimeLibrary.RuntimeAssemblyGroups.SelectMany(a => a.AssetPaths),
                                                     runtimeLibrary.Dependencies, runtimeLibrary.Serviceable);

                var loadedAssemblies = new List <string>();
                if (_resolver.TryResolveAssemblyPaths(wrapper, loadedAssemblies) && loadedAssemblies.Any())
                {
                    return(_loadContext.LoadFromAssemblyPath(loadedAssemblies[0]));
                }
            }

            return(null);
        }
Exemplo n.º 7
0
        private Assembly Resolve(AssemblyLoadContext context, AssemblyName assemblyName)
        {
            var dependency = _dependencyContext.RuntimeLibraries.SingleOrDefault(d => d.Name.Equals(assemblyName.Name, StringComparison.OrdinalIgnoreCase));

            if (dependency == null)
            {
                return(null);
            }

            var library = new CompilationLibrary(
                dependency.Type,
                dependency.Name,
                dependency.Version,
                dependency.Hash,
                dependency.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
                dependency.Dependencies,
                dependency.Serviceable);

            var assemblies = new List <string>();

            if (_resolver.TryResolveAssemblyPaths(library, assemblies) && assemblies.Count > 0)
            {
                try
                {
                    return(_assemblyLoadContext.LoadFromAssemblyPath(assemblies[0]));
                }
                catch (Exception e)
                {
                    Debug.Write(e);
                }
            }

            return(null);
        }
Exemplo n.º 8
0
 private IEnumerable <string> ResolveReferencePaths(ICompilationAssemblyResolver resolver, List <string> assemblies)
 {
     if (!resolver.TryResolveAssemblyPaths(this, assemblies))
     {
         throw new InvalidOperationException(SR.Format(SR.LibraryLocationNotFound, Name));
     }
     return(assemblies);
 }
Exemplo n.º 9
0
 private IEnumerable <string> ResolveReferencePaths(ICompilationAssemblyResolver resolver, List <string> assemblies)
 {
     if (!resolver.TryResolveAssemblyPaths(this, assemblies))
     {
         throw new InvalidOperationException($"Cannot find compilation library location for package '{Name}'");
     }
     return(assemblies);
 }
Exemplo n.º 10
0
        private Assembly?OnResolving(AssemblyLoadContext context, AssemblyName name)
        {
            //hack for loaded assemblies
            if (name.Name == "Mapster")
            {
                return(typeof(TypeAdapterConfig).Assembly);
            }
            if (name.Name == "Mapster.Core")
            {
                return(typeof(MapperAttribute).Assembly);
            }
            if (name.Name == "System.Text.Json")
            {
                return(typeof(JsonIgnoreAttribute).Assembly);
            }

            var(library, assetPath) = (from lib in _dependencyContext.RuntimeLibraries
                                       from grp in lib.RuntimeAssemblyGroups
                                       where grp.Runtime == string.Empty
                                       from path in grp.AssetPaths
                                       where string.Equals(GetAssemblyName(path), name.Name, StringComparison.OrdinalIgnoreCase)
                                       select(lib, path)).FirstOrDefault();

            if (library == null)
            {
                Console.WriteLine("Cannot find library: " + name.Name);
                return(null);
            }

            try
            {
                var wrapped = new CompilationLibrary(
                    library.Type,
                    library.Name,
                    library.Version,
                    library.Hash,
                    new[] { assetPath },
                    library.Dependencies,
                    library.Serviceable);

                var assemblies = new List <string>();
                _assemblyResolver.TryResolveAssemblyPaths(wrapped, assemblies);

                if (assemblies.Count == 0)
                {
                    Console.WriteLine($"Cannot find assembly path: {name.Name} (type={library.Type}, version={library.Version})");
                    return(null);
                }

                return(_loadContext.LoadFromAssemblyPath(assemblies[0]));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Cannot find assembly path: {name.Name} (type={library.Type}, version={library.Version})");
                Console.WriteLine("exception: " + ex.Message);
                return(null);
            }
        }
Exemplo n.º 11
0
        private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
        {
            bool NamesMatch(RuntimeLibrary runtime)
            {
                return(string.Equals(runtime.Name, name.Name, StringComparison.OrdinalIgnoreCase));
            }

            bool NamesContain(RuntimeLibrary runtime)
            {
                return(runtime.Name.IndexOf(name.Name, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            Log($"OnResolving: {name}");

            try
            {
                RuntimeLibrary library =
                    _dependencyContext.RuntimeLibraries.FirstOrDefault(NamesMatch);

                if (library == null)
                {
                    library =
                        _dependencyContext.RuntimeLibraries.FirstOrDefault(NamesContain);
                }
                if (library != null)
                {
                    var wrapper = new CompilationLibrary(
                        library.Type,
                        library.Name,
                        library.Version,
                        library.Hash,
                        library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
                        library.Dependencies,
                        library.Serviceable);

                    var assemblies = new List <string>();
                    _assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies);

                    if (assemblies.Count > 0)
                    {
                        Log($"Resolved {assemblies[0]}");
                        return(_loadContext.LoadFromAssemblyPath(assemblies[0]));
                    }

                    Log("Failed to resolve assembly");
                }
            }
            catch (Exception ex)
            {
                Log($"OnResolving error: {ex}");
            }
            return(null);
        }
Exemplo n.º 12
0
        private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
        {
            bool NamesMatchOrContain(RuntimeLibrary runtime)
            {
                bool matched = string.Equals(runtime.Name, name.Name, StringComparison.OrdinalIgnoreCase);

                // if not matched by exact name or not a default corehook module (which should be matched exactly)
                if (!matched && !runtime.Name.Contains(CoreHookModuleName))
                {
                    return(runtime.Name.IndexOf(name.Name, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                ;
                return(matched);
            }

            Log($"OnResolving: {name}");

            try
            {
                RuntimeLibrary library =
                    dependencyContext.RuntimeLibraries.FirstOrDefault(NamesMatchOrContain);

                if (library != null)
                {
                    var wrapper = new CompilationLibrary(
                        library.Type,
                        library.Name,
                        library.Version,
                        library.Hash,
                        library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
                        library.Dependencies,
                        library.Serviceable);

                    var assemblies = new List <string>();
                    assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies);

                    if (assemblies.Count > 0)
                    {
                        Log($"Resolved {assemblies[0]}");
                        return(loadContext.LoadFromAssemblyPath(assemblies[0]));
                    }
                    else
                    {
                        Log("Failed to resolve assembly");
                    }
                }
            }
            catch (Exception ex)
            {
                Log($"OnResolving error: {ex}");
            }
            return(null);
        }
Exemplo n.º 13
0
        private Assembly?OnResolving(AssemblyLoadContext context, AssemblyName name)
        {
            //hack for loaded assemblies
            if (name.Name == "Mapster")
            {
                return(typeof(MapperAttribute).Assembly);
            }
            if (name.Name == "System.Text.Json")
            {
                return(typeof(JsonIgnoreAttribute).Assembly);
            }

            var library = _dependencyContext.RuntimeLibraries
                          .FirstOrDefault(it => string.Equals(it.Name, name.Name, StringComparison.OrdinalIgnoreCase));

            if (library == null)
            {
                Console.WriteLine("Cannot find library: " + name.Name);
                return(null);
            }

            try
            {
                var wrapped = new CompilationLibrary(
                    library.Type,
                    library.Name,
                    library.Version,
                    library.Hash,
                    library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
                    library.Dependencies,
                    library.Serviceable);

                var assemblies = new List <string>();
                _assemblyResolver.TryResolveAssemblyPaths(wrapped, assemblies);

                if (assemblies.Count == 0)
                {
                    Console.WriteLine($"Cannot find assembly path: {name.Name} (type={library.Type}, version={library.Version})");
                    return(null);
                }

                return(_loadContext.LoadFromAssemblyPath(assemblies[0]));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Cannot find assembly path: {name.Name} (type={library.Type}, version={library.Version})");
                Console.WriteLine("exception: " + ex.Message);
                return(null);
            }
        }
Exemplo n.º 14
0
 private static Assembly Default_Resolving(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)
 {
     if (DependencyDLL.ContainsKey(assemblyName.Name))
     {
         var compilationLibrary = DependencyDLL[assemblyName.Name];
         var assemblies         = new List <string>();
         if (AssemblyResolver.TryResolveAssemblyPaths(compilationLibrary, assemblies) && assemblies.Count > 0)
         {
             var assembly = assemblyLoadContext.LoadFromAssemblyPath(assemblies[0]);
             FindDependency(assembly);
             return(assembly);
         }
     }
     return(null);
 }
Exemplo n.º 15
0
        private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
        {
            bool NamesMatch(Library library)
            {
                return(string.Equals(library.Name, name.Name, StringComparison.OrdinalIgnoreCase));
            }

            CompilationLibrary compilationLibrary = _dependencyContext
                                                    .CompileLibraries.FirstOrDefault(NamesMatch);

            if (compilationLibrary == null)
            {
                RuntimeLibrary runtimeLibrary = _dependencyContext
                                                .RuntimeLibraries.FirstOrDefault(NamesMatch);

                if (runtimeLibrary != null)
                {
                    compilationLibrary = new CompilationLibrary(
                        runtimeLibrary.Type,
                        runtimeLibrary.Name,
                        runtimeLibrary.Version,
                        runtimeLibrary.Hash,
                        runtimeLibrary.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
                        runtimeLibrary.Dependencies,
                        runtimeLibrary.Serviceable
                        );
                }
            }

            if (compilationLibrary != null)
            {
                var assemblies = new List <string>();

                _assemblyResolver.TryResolveAssemblyPaths(compilationLibrary, assemblies);

                if (assemblies.Count > 0)
                {
                    return(_loadContext.LoadFromAssemblyPath(assemblies[0]));
                }
            }

            return(null);
        }
Exemplo n.º 16
0
        protected override Assembly Load(AssemblyName assemblyName)
        {
            Assembly assembly = _onResolve(assemblyName);

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

            RuntimeLibrary runtimeLibrary = _dependencyContext.RuntimeLibraries.FirstOrDefault(
                lib => string.Equals(lib.Name, assemblyName.Name, StringComparison.OrdinalIgnoreCase));

            CompilationLibrary library;

            if (runtimeLibrary != null)
            {
                library = new CompilationLibrary(
                    runtimeLibrary.Type,
                    runtimeLibrary.Name,
                    runtimeLibrary.Version,
                    runtimeLibrary.Hash,
                    runtimeLibrary.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
                    runtimeLibrary.Dependencies,
                    runtimeLibrary.Serviceable);
            }
            else
            {
                library = _dependencyContext.CompileLibraries.FirstOrDefault(lib => string.Equals(lib.Name, assemblyName.Name, StringComparison.OrdinalIgnoreCase));
            }

            if (library == null)
            {
                return(null);
            }

            var assemblies = new List <string>();

            _assemblyResolver.TryResolveAssemblyPaths(library, assemblies);

            return(assemblies.Count > 0 ? LoadFromAssemblyPath(assemblies[0]) : null);
        }
        Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
        {
            // Try to find dependency from .deps.json
            if (assemblyFileNameToLibraryMap.TryGetValue(name.Name, out var library))
            {
                var wrapper = new CompilationLibrary(library.Type, library.Name, library.Version, library.Hash,
                                                     library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
                                                     library.Dependencies, library.Serviceable);

                var assemblies = new List <string>();
                if (assemblyResolver.TryResolveAssemblyPaths(wrapper, assemblies))
                {
                    var assembly = assemblies.FirstOrDefault(a => string.Equals(name.Name, Path.GetFileNameWithoutExtension(a), StringComparison.OrdinalIgnoreCase));
                    if (assembly != null)
                    {
                        return(loadContext.LoadFromAssemblyPath(assembly));
                    }
                }
            }

            // Try to find dependency in the local folder
            var assemblyPath = Path.Combine(assemblyFolder, name.Name);

            foreach (var extension in new[] { ".dll", ".exe" })
            {
                try
                {
                    var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath + extension);
                    if (assembly != null)
                    {
                        return(assembly);
                    }
                }
                catch { }
            }

            return(null);
        }
Exemplo n.º 18
0
        private Assembly OnResolving(AssemblyLoadContext context, AssemblyName name)
        {
            var library = dependencyContext.RuntimeLibraries
                          .Where(runtime => string.Equals(runtime.Name, name.Name, StringComparison.OrdinalIgnoreCase))
                          .FirstOrDefault();

            if (library == null)
            {
                library = dependencyContext.RuntimeLibraries.Where(runtime =>
                {
                    var path = runtime.RuntimeAssemblyGroups.SelectMany(group => group.AssetPaths).FirstOrDefault();

                    return(path?.EndsWith($"{name.Name}.dll") == true);
                })
                          .FirstOrDefault();
            }

            if (library != null)
            {
                var wrapper = new CompilationLibrary(
                    library.Type,
                    library.Name,
                    library.Version,
                    library.Hash,
                    library.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths),
                    library.Dependencies,
                    library.Serviceable);

                var assemblies = new List <string>();
                resolver.TryResolveAssemblyPaths(wrapper, assemblies);

                if (assemblies.Count > 0)
                {
                    var dependency = loadContext.LoadFromAssemblyPath(assemblies[0]);

                    Console.WriteLine($"Loaded dependency {assemblies[0]}.");

                    return(dependency);
                }
            }

            // load 'manually'
            var runtimePath = Path.Combine(packagesPath, $"{library.Name.ToLower()}/{library.Version}/");
            var path        = string.Concat(runtimePath, library.RuntimeAssemblyGroups.First().AssetPaths.First());

            try
            {
                var dependency = loadContext.LoadFromAssemblyPath(path);

                Console.WriteLine($"Loaded dependency {Path.GetFileName( path )}.");

                return(dependency);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());

                var loadedAssembly = context.Assemblies.SingleOrDefault(x => x.GetName().Name.Equals(library.Name));

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

            return(null);
        }