1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-28 16:10:28 -05:00

implement service resolver for script plugins

This commit is contained in:
RaidMax
2020-09-26 18:13:56 -05:00
parent bacafebb99
commit a11596a8fd
6 changed files with 56 additions and 4 deletions

View File

@ -61,7 +61,7 @@ namespace IW4MAdmin.Application.Misc
_onProcessing.Dispose();
}
public async Task Initialize(IManager manager, IScriptCommandFactory scriptCommandFactory)
public async Task Initialize(IManager manager, IScriptCommandFactory scriptCommandFactory, IScriptPluginServiceResolver serviceResolver)
{
await _onProcessing.WaitAsync();
@ -114,6 +114,7 @@ namespace IW4MAdmin.Application.Misc
_scriptEngine.Execute(script);
_scriptEngine.SetValue("_localization", Utilities.CurrentLocalization);
_scriptEngine.SetValue("_serviceResolver", serviceResolver);
dynamic pluginObject = _scriptEngine.GetValue("plugin").ToObject();
Author = pluginObject.author;
@ -164,6 +165,11 @@ namespace IW4MAdmin.Application.Misc
successfullyLoaded = true;
}
catch (JavaScriptException ex)
{
throw new PluginException($"An error occured while initializing script plugin: {ex.Error} (Line: {ex.Location.Start.Line}, Character: {ex.Location.Start.Column})") { PluginFile = _fileName };
}
catch
{
throw;

View File

@ -0,0 +1,31 @@
using SharedLibraryCore.Interfaces;
using System;
using System.Linq;
namespace IW4MAdmin.Application.Misc
{
/// <summary>
/// implementation of IScriptPluginServiceResolver
/// </summary>
public class ScriptPluginServiceResolver : IScriptPluginServiceResolver
{
private readonly IServiceProvider _serviceProvider;
public ScriptPluginServiceResolver(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public object ResolveService(string serviceName)
{
var serviceType = typeof(IScriptPluginServiceResolver).Assembly.GetTypes().FirstOrDefault(_type => _type.Name == serviceName);
if (serviceType == null)
{
throw new InvalidOperationException($"No service type '{serviceName}' defined in IW4MAdmin assembly");
}
return _serviceProvider.GetService(serviceType);
}
}
}