2023-04-04 19:24:13 -04:00
|
|
|
|
using System;
|
2020-09-26 19:13:56 -04:00
|
|
|
|
using System.Linq;
|
2023-04-04 19:24:13 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2020-09-26 19:13:56 -04:00
|
|
|
|
|
2023-04-04 19:24:13 -04:00
|
|
|
|
namespace IW4MAdmin.Application.Plugin.Script
|
2020-09-26 19:13:56 -04:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// implementation of IScriptPluginServiceResolver
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class ScriptPluginServiceResolver : IScriptPluginServiceResolver
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
|
|
|
|
|
public ScriptPluginServiceResolver(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ResolveService(string serviceName)
|
|
|
|
|
{
|
2020-09-28 21:32:53 -04:00
|
|
|
|
var serviceType = DetermineRootType(serviceName);
|
|
|
|
|
return _serviceProvider.GetService(serviceType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ResolveService(string serviceName, string[] genericParameters)
|
|
|
|
|
{
|
|
|
|
|
var serviceType = DetermineRootType(serviceName, genericParameters.Length);
|
2023-04-04 19:24:13 -04:00
|
|
|
|
var genericTypes = genericParameters.Select(genericTypeParam => DetermineRootType(genericTypeParam));
|
2020-09-28 21:32:53 -04:00
|
|
|
|
var resolvedServiceType = serviceType.MakeGenericType(genericTypes.ToArray());
|
|
|
|
|
return _serviceProvider.GetService(resolvedServiceType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Type DetermineRootType(string serviceName, int genericParamCount = 0)
|
|
|
|
|
{
|
|
|
|
|
var typeCollection = AppDomain.CurrentDomain.GetAssemblies()
|
|
|
|
|
.SelectMany(t => t.GetTypes());
|
2023-04-04 19:24:13 -04:00
|
|
|
|
var generatedName = $"{serviceName}{(genericParamCount == 0 ? "" : $"`{genericParamCount}")}".ToLower();
|
|
|
|
|
var serviceType = typeCollection.FirstOrDefault(type => type.Name.ToLower() == generatedName);
|
2020-09-26 19:13:56 -04:00
|
|
|
|
|
|
|
|
|
if (serviceType == null)
|
|
|
|
|
{
|
2020-09-28 21:32:53 -04:00
|
|
|
|
throw new InvalidOperationException($"No object type '{serviceName}' defined in loaded assemblies");
|
2020-09-26 19:13:56 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-28 21:32:53 -04:00
|
|
|
|
return serviceType;
|
2020-09-26 19:13:56 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|