2017-05-22 23:59:31 +00:00
|
|
|
|
using System;
|
2017-05-25 02:24:43 +00:00
|
|
|
|
using System.Collections;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Collections.Immutable;
|
2017-07-15 03:04:16 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using NLog;
|
2017-08-04 17:43:25 +00:00
|
|
|
|
#if GLOBAL_NADEKO
|
|
|
|
|
using NadekoBot.Common;
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
|
|
2017-10-13 04:14:54 +00:00
|
|
|
|
namespace NadekoBot.Core.Services
|
2017-05-22 23:59:31 +00:00
|
|
|
|
{
|
2017-05-25 02:24:43 +00:00
|
|
|
|
public interface INServiceProvider : IServiceProvider, IEnumerable<object>
|
2017-05-22 23:59:31 +00:00
|
|
|
|
{
|
|
|
|
|
T GetService<T>();
|
2017-10-04 22:51:12 +00:00
|
|
|
|
IEnumerable<Type> LoadFrom(Assembly assembly);
|
|
|
|
|
INServiceProvider AddManual<T>(T obj);
|
|
|
|
|
object Unload(Type t);
|
2017-05-22 23:59:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class NServiceProvider : INServiceProvider
|
|
|
|
|
{
|
2017-10-04 22:51:12 +00:00
|
|
|
|
private readonly object _locker = new object();
|
|
|
|
|
private readonly Logger _log;
|
|
|
|
|
|
|
|
|
|
public readonly Dictionary<Type, object> _services = new Dictionary<Type, object>();
|
|
|
|
|
public IReadOnlyDictionary<Type, object> Services => _services;
|
|
|
|
|
|
|
|
|
|
public NServiceProvider()
|
2017-05-22 23:59:31 +00:00
|
|
|
|
{
|
2017-10-04 22:51:12 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
|
|
|
|
}
|
2017-05-22 23:59:31 +00:00
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
public T GetService<T>()
|
|
|
|
|
{
|
|
|
|
|
return (T)((IServiceProvider)(this)).GetService(typeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
object IServiceProvider.GetService(Type serviceType)
|
|
|
|
|
{
|
|
|
|
|
_services.TryGetValue(serviceType, out var toReturn);
|
|
|
|
|
return toReturn;
|
|
|
|
|
}
|
2017-07-15 03:04:16 +00:00
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
public INServiceProvider AddManual<T>(T obj)
|
|
|
|
|
{
|
|
|
|
|
lock (_locker)
|
2017-05-22 23:59:31 +00:00
|
|
|
|
{
|
2017-10-04 22:51:12 +00:00
|
|
|
|
_services.TryAdd(typeof(T), obj);
|
2017-05-22 23:59:31 +00:00
|
|
|
|
}
|
2017-10-04 22:51:12 +00:00
|
|
|
|
return this;
|
|
|
|
|
}
|
2017-05-22 23:59:31 +00:00
|
|
|
|
|
2017-10-13 00:21:39 +00:00
|
|
|
|
public INServiceProvider UpdateManual<T>(T obj)
|
|
|
|
|
{
|
|
|
|
|
lock (_locker)
|
|
|
|
|
{
|
|
|
|
|
_services.Remove(typeof(T));
|
|
|
|
|
_services.TryAdd(typeof(T), obj);
|
|
|
|
|
}
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
public IEnumerable<Type> LoadFrom(Assembly assembly)
|
|
|
|
|
{
|
|
|
|
|
List<Type> addedTypes = new List<Type>();
|
|
|
|
|
|
|
|
|
|
Type[] allTypes;
|
|
|
|
|
try
|
2017-05-22 23:59:31 +00:00
|
|
|
|
{
|
2017-10-04 22:51:12 +00:00
|
|
|
|
allTypes = assembly.GetTypes();
|
2017-05-22 23:59:31 +00:00
|
|
|
|
}
|
2017-10-04 22:51:12 +00:00
|
|
|
|
catch (ReflectionTypeLoadException ex)
|
2017-07-15 03:04:16 +00:00
|
|
|
|
{
|
2017-10-04 22:51:12 +00:00
|
|
|
|
Console.WriteLine(ex.LoaderExceptions[0]);
|
|
|
|
|
return Enumerable.Empty<Type>();
|
|
|
|
|
}
|
|
|
|
|
var services = new Queue<Type>(allTypes
|
|
|
|
|
.Where(x => x.GetInterfaces().Contains(typeof(INService))
|
|
|
|
|
&& !x.GetTypeInfo().IsInterface && !x.GetTypeInfo().IsAbstract
|
2017-08-04 17:43:25 +00:00
|
|
|
|
#if GLOBAL_NADEKO
|
2017-10-04 22:51:12 +00:00
|
|
|
|
&& x.GetTypeInfo().GetCustomAttribute<NoPublicBot>() == null
|
2017-08-04 17:43:25 +00:00
|
|
|
|
#endif
|
|
|
|
|
)
|
2017-10-04 22:51:12 +00:00
|
|
|
|
.ToArray());
|
2017-07-15 03:04:16 +00:00
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
addedTypes.AddRange(services);
|
2017-07-15 03:04:16 +00:00
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
var interfaces = new HashSet<Type>(allTypes
|
|
|
|
|
.Where(x => x.GetInterfaces().Contains(typeof(INService))
|
|
|
|
|
&& x.GetTypeInfo().IsInterface));
|
2017-07-20 03:10:39 +00:00
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
var alreadyFailed = new Dictionary<Type, int>();
|
|
|
|
|
lock (_locker)
|
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
var sw = Stopwatch.StartNew();
|
|
|
|
|
var swInstance = new Stopwatch();
|
|
|
|
|
while (services.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var type = services.Dequeue(); //get a type i need to make an instance of
|
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
if (_services.TryGetValue(type, out _)) // if that type is already instantiated, skip
|
2017-07-15 03:04:16 +00:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var ctor = type.GetConstructors()[0];
|
|
|
|
|
var argTypes = ctor
|
|
|
|
|
.GetParameters()
|
|
|
|
|
.Select(x => x.ParameterType)
|
|
|
|
|
.ToArray(); // get constructor argument types i need to pass in
|
|
|
|
|
|
|
|
|
|
var args = new List<object>(argTypes.Length);
|
|
|
|
|
foreach (var arg in argTypes) //get constructor arguments from the dictionary of already instantiated types
|
|
|
|
|
{
|
2017-10-04 22:51:12 +00:00
|
|
|
|
if (_services.TryGetValue(arg, out var argObj)) //if i got current one, add it to the list of instances and move on
|
2017-07-15 03:04:16 +00:00
|
|
|
|
args.Add(argObj);
|
|
|
|
|
else //if i failed getting it, add it to the end, and break
|
|
|
|
|
{
|
|
|
|
|
services.Enqueue(type);
|
2017-07-20 03:10:39 +00:00
|
|
|
|
if (alreadyFailed.ContainsKey(type))
|
|
|
|
|
{
|
|
|
|
|
alreadyFailed[type]++;
|
|
|
|
|
if (alreadyFailed[type] > 3)
|
|
|
|
|
_log.Warn(type.Name + " wasn't instantiated in the first 3 attempts. Missing " + arg.Name + " type");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
alreadyFailed.Add(type, 1);
|
2017-07-15 03:04:16 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (args.Count != argTypes.Length)
|
|
|
|
|
continue;
|
2017-10-04 22:51:12 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
swInstance.Restart();
|
|
|
|
|
var instance = ctor.Invoke(args.ToArray());
|
|
|
|
|
swInstance.Stop();
|
|
|
|
|
if (swInstance.Elapsed.TotalSeconds > 5)
|
|
|
|
|
_log.Info($"{type.Name} took {swInstance.Elapsed.TotalSeconds:F2}s to load.");
|
|
|
|
|
var interfaceType = interfaces.FirstOrDefault(x => instance.GetType().GetInterfaces().Contains(x));
|
|
|
|
|
if (interfaceType != null)
|
2017-10-04 22:51:12 +00:00
|
|
|
|
{
|
|
|
|
|
addedTypes.Add(interfaceType);
|
|
|
|
|
_services.TryAdd(interfaceType, instance);
|
|
|
|
|
}
|
2017-07-15 03:04:16 +00:00
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
_services.TryAdd(type, instance);
|
2017-07-15 03:04:16 +00:00
|
|
|
|
}
|
|
|
|
|
sw.Stop();
|
|
|
|
|
_log.Info($"All services loaded in {sw.Elapsed.TotalSeconds:F2}s");
|
|
|
|
|
}
|
2017-10-04 22:51:12 +00:00
|
|
|
|
return addedTypes;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-04 22:51:12 +00:00
|
|
|
|
public object Unload(Type t)
|
2017-05-22 23:59:31 +00:00
|
|
|
|
{
|
2017-10-04 22:51:12 +00:00
|
|
|
|
lock (_locker)
|
|
|
|
|
{
|
|
|
|
|
if (_services.TryGetValue(t, out var obj))
|
|
|
|
|
{
|
|
|
|
|
_services.Remove(t);
|
|
|
|
|
return obj;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
}
|
2017-05-25 02:24:43 +00:00
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator() => _services.Values.GetEnumerator();
|
|
|
|
|
|
|
|
|
|
public IEnumerator<object> GetEnumerator() => _services.Values.GetEnumerator();
|
2017-05-22 23:59:31 +00:00
|
|
|
|
}
|
2017-10-04 22:51:12 +00:00
|
|
|
|
}
|