Radarr/src/NzbDrone.Common/Composition/ContainerBuilderBase.cs

137 lines
4.9 KiB
C#
Raw Normal View History

2018-04-21 04:59:31 +00:00
using System;
2013-04-20 00:05:48 +00:00
using System.Collections.Generic;
using System.IO;
2013-04-20 00:05:48 +00:00
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Loader;
2013-08-16 03:32:23 +00:00
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Messaging;
2013-05-10 23:53:50 +00:00
using TinyIoC;
2013-04-20 00:05:48 +00:00
2013-05-10 23:53:50 +00:00
namespace NzbDrone.Common.Composition
2013-04-20 00:05:48 +00:00
{
public abstract class ContainerBuilderBase
{
private readonly List<Type> _loadedTypes;
2018-04-21 04:59:31 +00:00
protected IContainer Container { get; }
2013-05-10 23:53:50 +00:00
2018-04-21 04:59:31 +00:00
protected ContainerBuilderBase(IStartupContext args, List<string> assemblies)
2013-04-20 00:05:48 +00:00
{
_loadedTypes = new List<Type>();
2018-11-23 07:03:32 +00:00
assemblies.Add(OsInfo.IsWindows ? "Radarr.Windows" : "Radarr.Mono");
assemblies.Add("Radarr.Common");
2018-04-21 04:59:31 +00:00
2019-12-22 22:08:53 +00:00
var startupPath = AppDomain.CurrentDomain.BaseDirectory;
foreach (var assemblyName in assemblies)
{
_loadedTypes.AddRange(AssemblyLoadContext.Default.LoadFromAssemblyPath(Path.Combine(startupPath, $"{assemblyName}.dll")).GetExportedTypes());
}
var toRegisterResolver = new List<string> { "System.Data.SQLite" };
2019-12-22 22:08:53 +00:00
toRegisterResolver.AddRange(assemblies.Intersect(new[] { "Radarr.Core" }));
RegisterNativeResolver(toRegisterResolver);
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ContainerResolveEventHandler);
2013-04-20 00:05:48 +00:00
Container = new Container(new TinyIoCContainer(), _loadedTypes);
2013-04-20 00:05:48 +00:00
AutoRegisterInterfaces();
2013-08-16 03:32:23 +00:00
Container.Register(args);
}
private static Assembly ContainerResolveEventHandler(object sender, ResolveEventArgs args)
{
2019-12-22 22:08:53 +00:00
var resolver = new AssemblyDependencyResolver(args.RequestingAssembly.Location);
var assemblyPath = resolver.ResolveAssemblyToPath(new AssemblyName(args.Name));
if (assemblyPath == null)
{
return null;
}
return AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
}
public static void RegisterNativeResolver(IEnumerable<string> assemblyNames)
{
// This ensures we look for sqlite3 using libsqlite3.so.0 on Linux and not libsqlite3.so which
// is less likely to exist.
foreach (var name in assemblyNames)
{
var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(
2019-12-22 22:08:53 +00:00
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"{name}.dll"));
try
{
NativeLibrary.SetDllImportResolver(assembly, LoadNativeLib);
}
catch (InvalidOperationException)
{
// This can only be set once per assembly
// Catch required for NzbDrone.Host tests
}
}
}
private static IntPtr LoadNativeLib(string libraryName, Assembly assembly, DllImportSearchPath? dllImportSearchPath)
{
var mappedName = libraryName;
if (OsInfo.IsLinux)
{
if (libraryName == "sqlite3")
{
mappedName = "libsqlite3.so.0";
}
else if (libraryName == "mediainfo")
{
mappedName = "libmediainfo.so.0";
}
}
return NativeLibrary.Load(mappedName, assembly, dllImportSearchPath);
}
2013-04-20 00:05:48 +00:00
private void AutoRegisterInterfaces()
{
2013-05-10 23:53:50 +00:00
var loadedInterfaces = _loadedTypes.Where(t => t.IsInterface).ToList();
2013-05-11 20:06:57 +00:00
var implementedInterfaces = _loadedTypes.SelectMany(t => t.GetInterfaces());
2013-04-20 00:05:48 +00:00
2013-05-10 23:53:50 +00:00
var contracts = loadedInterfaces.Union(implementedInterfaces).Where(c => !c.IsGenericTypeDefinition && !string.IsNullOrWhiteSpace(c.FullName))
2013-05-11 20:06:57 +00:00
.Where(c => !c.FullName.StartsWith("System"))
2013-05-11 06:16:10 +00:00
.Except(new List<Type> { typeof(IMessage), typeof(IEvent), typeof(IContainer) }).Distinct().OrderBy(c => c.FullName);
2013-05-10 23:53:50 +00:00
foreach (var contract in contracts)
2013-04-20 00:05:48 +00:00
{
AutoRegisterImplementations(contract);
}
}
protected void AutoRegisterImplementations<TContract>()
{
AutoRegisterImplementations(typeof(TContract));
}
private void AutoRegisterImplementations(Type contractType)
{
var implementations = Container.GetImplementations(contractType).Where(c => !c.IsGenericTypeDefinition).ToList();
2013-05-10 23:53:50 +00:00
2013-04-20 00:05:48 +00:00
if (implementations.Count == 0)
{
return;
}
2019-12-22 22:08:53 +00:00
2013-04-20 00:05:48 +00:00
if (implementations.Count == 1)
{
2013-05-10 23:53:50 +00:00
var impl = implementations.Single();
2013-05-30 01:33:20 +00:00
Container.RegisterSingleton(contractType, impl);
2013-04-20 00:05:48 +00:00
}
else
{
2013-05-30 01:33:20 +00:00
Container.RegisterAllAsSingleton(contractType, implementations);
2013-04-20 00:05:48 +00:00
}
}
}
}