Skip to content

Commit

Permalink
implement service resolver for script plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
RaidMax committed Sep 26, 2020
1 parent bacafeb commit a11596a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 4 deletions.
8 changes: 5 additions & 3 deletions Application/ApplicationManager.cs
Expand Up @@ -67,13 +67,14 @@ public class ApplicationManager : IManager
private readonly IEventHandler _eventHandler;
private readonly IScriptCommandFactory _scriptCommandFactory;
private readonly IMetaRegistration _metaRegistration;
private readonly IScriptPluginServiceResolver _scriptPluginServiceResolver;

public ApplicationManager(ILogger logger, IMiddlewareActionHandler actionHandler, IEnumerable<IManagerCommand> commands,
ITranslationLookup translationLookup, IConfigurationHandler<CommandConfiguration> commandConfiguration,
IConfigurationHandler<ApplicationConfiguration> appConfigHandler, IGameServerInstanceFactory serverInstanceFactory,
IEnumerable<IPlugin> plugins, IParserRegexFactory parserRegexFactory, IEnumerable<IRegisterEvent> customParserEvents,
IEventHandler eventHandler, IScriptCommandFactory scriptCommandFactory, IDatabaseContextFactory contextFactory, IMetaService metaService,
IMetaRegistration metaRegistration)
IMetaRegistration metaRegistration, IScriptPluginServiceResolver scriptPluginServiceResolver)
{
MiddlewareActionHandler = actionHandler;
_servers = new ConcurrentBag<Server>();
Expand All @@ -100,6 +101,7 @@ public class ApplicationManager : IManager
_eventHandler = eventHandler;
_scriptCommandFactory = scriptCommandFactory;
_metaRegistration = metaRegistration;
_scriptPluginServiceResolver = scriptPluginServiceResolver;
Plugins = plugins;
}

Expand Down Expand Up @@ -277,12 +279,12 @@ public async Task Init()
{
if (plugin is ScriptPlugin scriptPlugin)
{
await scriptPlugin.Initialize(this, _scriptCommandFactory);
await scriptPlugin.Initialize(this, _scriptCommandFactory, _scriptPluginServiceResolver);
scriptPlugin.Watcher.Changed += async (sender, e) =>
{
try
{
await scriptPlugin.Initialize(this, _scriptCommandFactory);
await scriptPlugin.Initialize(this, _scriptCommandFactory, _scriptPluginServiceResolver);
}
catch (Exception ex)
Expand Down
1 change: 1 addition & 0 deletions Application/Main.cs
Expand Up @@ -249,6 +249,7 @@ private static IServiceCollection ConfigureServices(string[] args)
.AddSingleton<IEntityService<EFClient>, ClientService>()
.AddSingleton<IMetaService, MetaService>()
.AddSingleton<IMetaRegistration, MetaRegistration>()
.AddSingleton<IScriptPluginServiceResolver, ScriptPluginServiceResolver>()
.AddSingleton<IResourceQueryHelper<ClientPaginationRequest, ReceivedPenaltyResponse>, ReceivedPenaltyResourceQueryHelper>()
.AddSingleton<IResourceQueryHelper<ClientPaginationRequest, AdministeredPenaltyResponse>, AdministeredPenaltyResourceQueryHelper>()
.AddSingleton<IResourceQueryHelper<ClientPaginationRequest, UpdatedAliasResponse>, UpdatedAliasResourceQueryHelper>()
Expand Down
8 changes: 7 additions & 1 deletion Application/Misc/ScriptPlugin.cs
Expand Up @@ -61,7 +61,7 @@ public ScriptPlugin(string filename, string workingDirectory = null)
_onProcessing.Dispose();
}

public async Task Initialize(IManager manager, IScriptCommandFactory scriptCommandFactory)
public async Task Initialize(IManager manager, IScriptCommandFactory scriptCommandFactory, IScriptPluginServiceResolver serviceResolver)
{
await _onProcessing.WaitAsync();

Expand Down Expand Up @@ -114,6 +114,7 @@ public async Task Initialize(IManager manager, IScriptCommandFactory scriptComma

_scriptEngine.Execute(script);
_scriptEngine.SetValue("_localization", Utilities.CurrentLocalization);
_scriptEngine.SetValue("_serviceResolver", serviceResolver);
dynamic pluginObject = _scriptEngine.GetValue("plugin").ToObject();

Author = pluginObject.author;
Expand Down Expand Up @@ -164,6 +165,11 @@ public async Task Initialize(IManager manager, IScriptCommandFactory scriptComma
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;
Expand Down
31 changes: 31 additions & 0 deletions Application/Misc/ScriptPluginServiceResolver.cs
@@ -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);
}
}
}
2 changes: 2 additions & 0 deletions Plugins/ScriptPlugins/SampleScriptPluginCommand.js
Expand Up @@ -44,6 +44,8 @@ let plugin = {
},

onLoadAsync: function (manager) {
this.logger = _serviceResolver.ResolveService("ILogger");
this.logger.WriteDebug("sample plugin loaded");
},

onUnloadAsync: function () {
Expand Down
10 changes: 10 additions & 0 deletions SharedLibraryCore/Interfaces/IScriptPluginServiceResolver.cs
@@ -0,0 +1,10 @@
namespace SharedLibraryCore.Interfaces
{
/// <summary>
/// interface used to dynamically resolve services by string name
/// </summary>
public interface IScriptPluginServiceResolver
{
object ResolveService(string serviceName);
}
}

0 comments on commit a11596a

Please sign in to comment.