From 9583c7ad461dfb455f054a02f9807f4b0bccb87a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Thu, 1 Apr 2021 13:56:10 +0200 Subject: [PATCH 1/9] Switch to AutoFac --- App.config | 8 +++++++ Backend/IEngine.cs | 3 ++- Program.cs | 53 ++++++++++++++++++++++------------------------ Slipstream.csproj | 12 +++++------ packages.config | 4 ++-- 5 files changed, 43 insertions(+), 37 deletions(-) diff --git a/App.config b/App.config index 6ecd65db..6a2f02f9 100644 --- a/App.config +++ b/App.config @@ -42,6 +42,14 @@ + + + + + + + + diff --git a/Backend/IEngine.cs b/Backend/IEngine.cs index 1d61aebf..c59e9945 100644 --- a/Backend/IEngine.cs +++ b/Backend/IEngine.cs @@ -1,8 +1,9 @@ using Slipstream.Shared; +using System; namespace Slipstream.Backend { - public interface IEngine + public interface IEngine : IDisposable { void UnregisterSubscription(IEventBusSubscription subscription); diff --git a/Program.cs b/Program.cs index 39888bd8..9cc72b3f 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,4 @@ -using Microsoft.Extensions.DependencyInjection; +using Autofac; using Serilog; using Slipstream.Components.Internal; using Slipstream.Components.Internal.Services; @@ -19,44 +19,42 @@ private static void Main() Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - var services = new ServiceCollection(); + var builder = new ContainerBuilder(); - ConfigureServices(services); - - using ServiceProvider serviceProvider = services.BuildServiceProvider(); - - var _ = serviceProvider.GetService(); // HACK: Will inject EventBus/EventFactory into sink + ConfigureServices(builder); string cwd = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Slipstream\"; System.IO.Directory.CreateDirectory(cwd); System.IO.Directory.SetCurrentDirectory(cwd); - Start(serviceProvider); + using var container = builder.Build(); + + Start(container); } - private static void Start(ServiceProvider serviceProvider) + private static void Start(IContainer container) { - var engine = serviceProvider.GetRequiredService(); + using var scope = container.BeginLifetimeScope(); + + var _ = scope.Resolve(); // HACK: Will inject EventBus/EventFactory into sink + using var engine = scope.Resolve(); engine.Start(); - engine.Dispose(); } - private static void ConfigureServices(ServiceCollection services) + private static void ConfigureServices(ContainerBuilder builder) { - services.AddScoped(); - services.AddScoped(x => x.GetService()); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(x => new StateService(x.GetService(), Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Slipstream\state.txt")); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(x => x.GetService()); - services.AddScoped(x => x.GetService()); - services.AddScoped(); - services.AddScoped(); - services.AddScoped(); - services.AddSingleton(); + builder.RegisterType().As().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().As().SingleInstance(); + builder.RegisterType().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().SingleInstance(); + builder.RegisterType().SingleInstance(); + builder.Register(c => new StateService(c.Resolve(), Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Slipstream\state.txt")).As().SingleInstance(); var logger = new LoggerConfiguration() .MinimumLevel.Verbose() @@ -64,9 +62,8 @@ private static void ConfigureServices(ServiceCollection services) .WriteTo.SlipstreamConsoleSink(out SlipstreamConsoleSink sink) .CreateLogger(); - services.AddScoped(_ => logger); - services.AddScoped(_ => sink); - services.AddScoped(); + builder.RegisterInstance(logger).As().SingleInstance(); + builder.RegisterInstance(sink).As().SingleInstance(); } private class PopulateSink diff --git a/Slipstream.csproj b/Slipstream.csproj index cd68d284..6425aaec 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -56,6 +56,9 @@ 8.0 + + packages\Autofac.6.1.0\lib\netstandard2.0\Autofac.dll + packages\DeltaCompressionDotNet.1.1.0\lib\net20\DeltaCompressionDotNet.dll @@ -74,12 +77,6 @@ packages\Microsoft.Bcl.AsyncInterfaces.5.0.0\lib\net461\Microsoft.Bcl.AsyncInterfaces.dll - - packages\Microsoft.Extensions.DependencyInjection.5.0.1\lib\net461\Microsoft.Extensions.DependencyInjection.dll - - - packages\Microsoft.Extensions.DependencyInjection.Abstractions.5.0.0\lib\net461\Microsoft.Extensions.DependencyInjection.Abstractions.dll - packages\Microsoft.Extensions.Logging.Abstractions.5.0.0\lib\net461\Microsoft.Extensions.Logging.Abstractions.dll @@ -146,6 +143,9 @@ + + packages\System.Diagnostics.DiagnosticSource.4.7.1\lib\net46\System.Diagnostics.DiagnosticSource.dll + packages\System.Memory.4.5.4\lib\net461\System.Memory.dll diff --git a/packages.config b/packages.config index 82b1288e..8dc185a9 100644 --- a/packages.config +++ b/packages.config @@ -1,11 +1,10 @@  + - - @@ -23,6 +22,7 @@ + From 4e5308fcf2a7aaa052d1fe83dc8fb0bf2054cd04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Thu, 1 Apr 2021 14:03:01 +0200 Subject: [PATCH 2/9] Remove ServiceLocator - replaced by DI --- Backend/PluginManager.cs | 26 ++++++++++++++++--- Components/ComponentPluginCreationContext.cs | 11 ++++---- Components/ComponentRegistrator.cs | 6 +---- Components/IComponentPluginCreationContext.cs | 2 ++ Components/IComponentPluginDependencies.cs | 1 - Components/Lua/Lua.cs | 2 +- Components/Lua/Plugins/LuaManagerPlugin.cs | 6 ++--- Components/Playback/Playback.cs | 2 +- Program.cs | 1 - Shared/IServiceLocator.cs | 9 ------- Shared/ServiceLocator.cs | 25 ------------------ Slipstream.csproj | 2 -- 12 files changed, 35 insertions(+), 58 deletions(-) delete mode 100644 Shared/IServiceLocator.cs delete mode 100644 Shared/ServiceLocator.cs diff --git a/Backend/PluginManager.cs b/Backend/PluginManager.cs index e01a171a..42cf4e05 100644 --- a/Backend/PluginManager.cs +++ b/Backend/PluginManager.cs @@ -15,6 +15,7 @@ namespace Slipstream.Backend public class PluginManager : IPluginManager, IPluginFactory { private readonly IInternalEventFactory InternalEventFactory; + private readonly IEventSerdeService EventSerdeService; private readonly IEventBus EventBus; private readonly IDictionary PluginWorkers = new Dictionary(); private readonly ILogger Logger; @@ -25,11 +26,18 @@ public class PluginManager : IPluginManager, IPluginFactory public PluginManager( IEventFactory eventFactory, IEventBus eventBus, - IServiceLocator serviceLocator, ILogger logger, - EventHandlerControllerBuilder eventHandlerControllerBuilder) + EventHandlerControllerBuilder eventHandlerControllerBuilder, + IEventSerdeService eventSerdeService) { - Registrator = new ComponentRegistrator(ComponentPlugins, LuaGluesFactories, eventFactory, logger, eventBus, eventHandlerControllerBuilder, serviceLocator); + Registrator = new ComponentRegistrator( + ComponentPlugins, + LuaGluesFactories, + eventFactory, + logger, + eventBus, + eventHandlerControllerBuilder + ); foreach (var type in typeof(PluginManager).Assembly.GetTypes().Where(t => typeof(IComponent).IsAssignableFrom(t) && !t.IsAbstract && t.IsClass)) { @@ -38,6 +46,7 @@ public PluginManager( } InternalEventFactory = eventFactory.Get(); + EventSerdeService = eventSerdeService; EventBus = eventBus; Logger = logger; } @@ -131,7 +140,16 @@ public IPlugin CreatePlugin(string pluginId, string name, Parameters configurati public IPlugin CreatePlugin(string pluginId, string pluginName, IEventBus eventBus, Parameters configuration) { - ComponentPluginCreationContext reg = new ComponentPluginCreationContext(Registrator, this, this, LuaGluesFactories, pluginId, pluginName, configuration); + ComponentPluginCreationContext reg = new ComponentPluginCreationContext( + Registrator, + this, + this, + LuaGluesFactories, + pluginId, + pluginName, + configuration, + EventSerdeService + ); if (!ComponentPlugins.ContainsKey(pluginName)) throw new KeyNotFoundException($"Plugin name '{pluginName}' not found"); return ComponentPlugins[pluginName].Invoke(reg); diff --git a/Components/ComponentPluginCreationContext.cs b/Components/ComponentPluginCreationContext.cs index 59206f22..ac1ab0b5 100644 --- a/Components/ComponentPluginCreationContext.cs +++ b/Components/ComponentPluginCreationContext.cs @@ -31,11 +31,6 @@ public IEventFactory EventFactory get { return ComponentRegistration.EventFactory; } } - public IServiceLocator ServiceLocator - { - get { return ComponentRegistration.ServiceLocator; } - } - public string PluginId { get; } public string PluginName { get; } @@ -50,6 +45,8 @@ public IServiceLocator ServiceLocator public IPluginFactory PluginFactory { get; } + public IEventSerdeService EventSerdeService { get; } + public ComponentPluginCreationContext( ComponentRegistrator componentRegistration, IPluginManager pluginManager, @@ -57,7 +54,8 @@ public ComponentPluginCreationContext( List luaGlueFactories, string pluginId, string pluginName, - Parameters pluginParameters) + Parameters pluginParameters, + IEventSerdeService eventSerdeService) { ComponentRegistration = componentRegistration; PluginManager = pluginManager; @@ -66,6 +64,7 @@ public ComponentPluginCreationContext( PluginId = pluginId; PluginName = pluginName; PluginParameters = pluginParameters; + EventSerdeService = eventSerdeService; Lua = new NLua.Lua(); } } diff --git a/Components/ComponentRegistrator.cs b/Components/ComponentRegistrator.cs index bad22161..09994507 100644 --- a/Components/ComponentRegistrator.cs +++ b/Components/ComponentRegistrator.cs @@ -18,8 +18,6 @@ internal class ComponentRegistrator : IComponentRegistrationContext public IEventFactory EventFactory { get; internal set; } - public IServiceLocator ServiceLocator { get; internal set; } - public EventHandlerControllerBuilder EventHandlerControllerBuilder { get; } public ComponentRegistrator( @@ -28,8 +26,7 @@ public ComponentRegistrator( IEventFactory eventFactory, ILogger logger, IEventBus eventBus, - EventHandlerControllerBuilder eventHandlerControllerBuilder, - IServiceLocator serviceLocator + EventHandlerControllerBuilder eventHandlerControllerBuilder ) { Plugins = plugins; @@ -38,7 +35,6 @@ IServiceLocator serviceLocator EventFactory = eventFactory; Logger = logger; EventBus = eventBus; - ServiceLocator = serviceLocator; } public void RegisterEventFactory(Type type, T factory) diff --git a/Components/IComponentPluginCreationContext.cs b/Components/IComponentPluginCreationContext.cs index a20eabed..2eb09180 100644 --- a/Components/IComponentPluginCreationContext.cs +++ b/Components/IComponentPluginCreationContext.cs @@ -1,4 +1,5 @@ using Slipstream.Backend; +using Slipstream.Components.Internal; using Slipstream.Shared.Helpers.StrongParameters; using System.Collections.Generic; @@ -12,5 +13,6 @@ internal interface IComponentPluginCreationContext : IComponentPluginDependencie List LuaGlueFactories { get; } IPluginManager PluginManager { get; } IPluginFactory PluginFactory { get; } + IEventSerdeService EventSerdeService { get; } } } \ No newline at end of file diff --git a/Components/IComponentPluginDependencies.cs b/Components/IComponentPluginDependencies.cs index 807862f1..c25bb364 100644 --- a/Components/IComponentPluginDependencies.cs +++ b/Components/IComponentPluginDependencies.cs @@ -10,6 +10,5 @@ internal interface IComponentPluginDependencies ILogger Logger { get; } IEventBus EventBus { get; } IEventFactory EventFactory { get; } - IServiceLocator ServiceLocator { get; } } } \ No newline at end of file diff --git a/Components/Lua/Lua.cs b/Components/Lua/Lua.cs index 0695a173..bb47b7a2 100644 --- a/Components/Lua/Lua.cs +++ b/Components/Lua/Lua.cs @@ -28,7 +28,7 @@ private IPlugin CreateLuaManagerPlugin(IComponentPluginCreationContext ctx) ctx.EventBus, ctx.PluginManager, ctx.PluginFactory, - ctx.ServiceLocator); + ctx.EventSerdeService); } private IPlugin CreateLuaPlugin(IComponentPluginCreationContext ctx) diff --git a/Components/Lua/Plugins/LuaManagerPlugin.cs b/Components/Lua/Plugins/LuaManagerPlugin.cs index 053916e5..e4abd142 100644 --- a/Components/Lua/Plugins/LuaManagerPlugin.cs +++ b/Components/Lua/Plugins/LuaManagerPlugin.cs @@ -44,7 +44,7 @@ public LuaManagerPlugin( IEventBus eventBus, IPluginManager pluginManager, IPluginFactory pluginFactory, - IServiceLocator serviceLocator + IEventSerdeService eventSerdeService ) : base(eventHandlerController, id, "LuaManagerPlugin", id) { Logger = logger; @@ -52,7 +52,7 @@ IServiceLocator serviceLocator EventBus = eventBus; PluginManager = pluginManager; PluginFactory = pluginFactory; - EventSerdeService = serviceLocator.Get(); + EventSerdeService = eventSerdeService; var fileMonitor = EventHandlerController.Get(); var @internal = EventHandlerController.Get(); @@ -132,7 +132,7 @@ private void NewFile(string filePath) private void DeletedFile(string filePath) { - if(Scripts.ContainsKey(filePath)) + if (Scripts.ContainsKey(filePath)) { PluginManager.UnregisterPlugin(Scripts[filePath]); diff --git a/Components/Playback/Playback.cs b/Components/Playback/Playback.cs index 26759e85..e879ac02 100644 --- a/Components/Playback/Playback.cs +++ b/Components/Playback/Playback.cs @@ -23,7 +23,7 @@ private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) ctx.PluginId, ctx.Logger, ctx.EventBus, - ctx.ServiceLocator.Get() + ctx.EventSerdeService ); } } diff --git a/Program.cs b/Program.cs index 9cc72b3f..045b3207 100644 --- a/Program.cs +++ b/Program.cs @@ -45,7 +45,6 @@ private static void Start(IContainer container) private static void ConfigureServices(ContainerBuilder builder) { builder.RegisterType().As().As().SingleInstance(); - builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().As().SingleInstance(); diff --git a/Shared/IServiceLocator.cs b/Shared/IServiceLocator.cs deleted file mode 100644 index 808f2451..00000000 --- a/Shared/IServiceLocator.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Slipstream.Components.Internal -{ - public interface IServiceLocator - { - T Get(); - - void Add(T service); - } -} \ No newline at end of file diff --git a/Shared/ServiceLocator.cs b/Shared/ServiceLocator.cs deleted file mode 100644 index a4f81374..00000000 --- a/Shared/ServiceLocator.cs +++ /dev/null @@ -1,25 +0,0 @@ -using System.Collections.Generic; - -namespace Slipstream.Components.Internal.Services -{ - public class ServiceLocator : IServiceLocator - { - internal readonly IDictionary Factories = new Dictionary(); - - public ServiceLocator(IEventSerdeService eventSerdeService, IStateService stateService) - { - Add(eventSerdeService); - Add(stateService); - } - - public T Get() - { - return (T)Factories[typeof(T)]; - } - - public void Add(T service) - { - Factories.Add(typeof(T), service); - } - } -} \ No newline at end of file diff --git a/Slipstream.csproj b/Slipstream.csproj index 6425aaec..e4abd0ef 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -424,8 +424,6 @@ - - MainWindow.cs From e81e7320da9a8bcb49db59c1c8b8a97a707a9f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Thu, 1 Apr 2021 14:53:02 +0200 Subject: [PATCH 3/9] Refactor EventHandlers: DRYing up --- Backend/Engine.cs | 8 +- Backend/PluginWorker.cs | 2 +- .../ApplicationUpdateEventHandler.cs | 16 +- .../Plugins/ApplicationUpdatePlugin.cs | 24 +- .../Audio/EventHandler/AudioEventHandler.cs | 91 ++-- Components/Audio/Plugins/AudioPlugin.cs | 8 +- Components/BasePlugin.cs | 7 +- .../EventHandler/DiscordEventHandler.cs | 80 ++-- Components/Discord/Plugins/DiscordPlugin.cs | 10 +- .../FileMonitor/EventHandler/FileMonitor.cs | 106 ++--- Components/IPlugin.cs | 14 +- Components/IRacing/EventHandler/IRacing.cs | 390 +++--------------- Components/Internal/EventHandler/Internal.cs | 108 ++--- Components/Lua/EventHandler/Lua.cs | 31 +- Components/Lua/Plugins/LuaManagerPlugin.cs | 42 +- Components/Lua/Plugins/LuaPlugin.cs | 2 +- Components/Playback/EventHandler/Playback.cs | 46 +-- Components/Playback/Plugins/PlaybackPlugin.cs | 4 +- Components/Twitch/EventHandler/Twitch.cs | 151 ++----- Components/Twitch/Plugins/TwitchPlugin.cs | 4 +- Components/Txrx/Plugins/TransmitterPlugin.cs | 2 +- Components/UI/EventHandler/UIEventHandler.cs | 76 +--- Components/WinFormUI/Forms/MainWindow.cs | 10 +- Shared/EventHandlerController.cs | 4 +- Shared/IEventHandlerController.cs | 6 +- 25 files changed, 321 insertions(+), 921 deletions(-) diff --git a/Backend/Engine.cs b/Backend/Engine.cs index 01b33810..93dc72d7 100644 --- a/Backend/Engine.cs +++ b/Backend/Engine.cs @@ -36,10 +36,10 @@ public Engine(ILogger logger, IEventFactory eventFactory, IEventBus eventBus, IP var internalEventHandler = EventHandlerController.Get(); - internalEventHandler.OnInternalCommandPluginRegister += (_, e) => OnCommandPluginRegister(e.Event); - internalEventHandler.OnInternalCommandPluginUnregister += (_, e) => OnCommandPluginUnregister(e.Event); - internalEventHandler.OnInternalCommandPluginStates += (_, e) => OnCommandPluginStates(e.Event); - internalEventHandler.OnInternalCommandShutdown += (_, e) => OnInternalCommandShutdown(e.Event); + internalEventHandler.OnInternalCommandPluginRegister += (_, e) => OnCommandPluginRegister(e); + internalEventHandler.OnInternalCommandPluginUnregister += (_, e) => OnCommandPluginUnregister(e); + internalEventHandler.OnInternalCommandPluginStates += (_, e) => OnCommandPluginStates(e); + internalEventHandler.OnInternalCommandShutdown += (_, e) => OnInternalCommandShutdown(e); // Plugins.. { diff --git a/Backend/PluginWorker.cs b/Backend/PluginWorker.cs index 116bc63a..15de2527 100644 --- a/Backend/PluginWorker.cs +++ b/Backend/PluginWorker.cs @@ -24,7 +24,7 @@ public PluginWorker(IPlugin plugin, IEventBusSubscription subscription, IInterna EventFactory = eventFactory; } - private void Plugin_OnStateChanged(IPlugin plugin, IPlugin.EventHandlerArgs e) + private void Plugin_OnStateChanged(object source, IPlugin plugin) { EventBus.PublishEvent(EventFactory.CreateInternalPluginState(plugin.Id, plugin.Name, plugin.DisplayName, PluginStatusEnum.Registered)); } diff --git a/Components/AppilcationUpdate/EventHandler/ApplicationUpdateEventHandler.cs b/Components/AppilcationUpdate/EventHandler/ApplicationUpdateEventHandler.cs index 8a5aae74..99d88ee3 100644 --- a/Components/AppilcationUpdate/EventHandler/ApplicationUpdateEventHandler.cs +++ b/Components/AppilcationUpdate/EventHandler/ApplicationUpdateEventHandler.cs @@ -1,4 +1,6 @@ -using Slipstream.Components.AppilcationUpdate.Events; +#nullable enable + +using Slipstream.Components.AppilcationUpdate.Events; using Slipstream.Shared; using System; @@ -6,8 +8,10 @@ namespace Slipstream.Components.AppilcationUpdate.EventHandler { internal class ApplicationUpdateEventHandler : IEventHandler { - public event EventHandler OnApplicationUpdateLatestVersionChanged; - public event EventHandler OnApplicationUpdateCommandCheckLatestVersion; + public event EventHandler? OnApplicationUpdateLatestVersionChanged; + + public event EventHandler? OnApplicationUpdateCommandCheckLatestVersion; + private readonly IEventHandlerController Parent; public ApplicationUpdateEventHandler(IEventHandlerController eventHandler) @@ -25,9 +29,9 @@ public IEventHandler.HandledStatus HandleEvent(IEvent @event) }; } - private IEventHandler.HandledStatus OnEvent(EventHandler onEvent, TEvent args) + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) { - if(onEvent != null) + if (onEvent != null) { onEvent.Invoke(Parent, args); return IEventHandler.HandledStatus.Handled; @@ -35,4 +39,4 @@ private IEventHandler.HandledStatus OnEvent(EventHandler onEvent return IEventHandler.HandledStatus.UseDefault; } } -} +} \ No newline at end of file diff --git a/Components/AppilcationUpdate/Plugins/ApplicationUpdatePlugin.cs b/Components/AppilcationUpdate/Plugins/ApplicationUpdatePlugin.cs index a1193969..0088ae9c 100644 --- a/Components/AppilcationUpdate/Plugins/ApplicationUpdatePlugin.cs +++ b/Components/AppilcationUpdate/Plugins/ApplicationUpdatePlugin.cs @@ -30,11 +30,11 @@ static ApplicationUpdatePlugin() } public ApplicationUpdatePlugin( - IEventHandlerController eventHandlerController, - string id, - IApplicationUpdateEventFactory applicationUpdateEventFactory, - ILogger logger, - IEventBus eventBus, + IEventHandlerController eventHandlerController, + string id, + IApplicationUpdateEventFactory applicationUpdateEventFactory, + ILogger logger, + IEventBus eventBus, Parameters configuration) : base(eventHandlerController, id, nameof(ApplicationUpdatePlugin), id, true) { @@ -67,18 +67,18 @@ private void SubscribeToInternalEvents() { // Registering for internal plugin state event var internalEvents = EventHandlerController.Get(); - internalEvents.OnInternalPluginState += (s, e) => OnInternalPluginState(s, e); + internalEvents.OnInternalPluginState += (s, e) => OnInternalPluginState(e); } - private void OnInternalPluginState(IEventHandlerController s, EventHandlerArgs e) + private void OnInternalPluginState(InternalPluginState e) { // to prevent multiple updates - if(updateNotified) + if (updateNotified) { return; } - if (e.Event.PluginName == this.Name && e.Event.PluginStatus == "Registered") + if (e.PluginName == this.Name && e.PluginStatus == "Registered") { // Send update event to check for update at startup this.eventBus.PublishEvent(this.applicationUpdateEventFactory.CreateApplicationUpdateCommandCheckLatestVersion()); @@ -119,7 +119,7 @@ private static UpdateManager CreateUpdateManager(string updateLocation, bool pre var isGitHub = updateLocation.StartsWith("https://github.com"); - if(isGitHub) + if (isGitHub) { var asyncUpdateManager = UpdateManager.GitHubUpdateManager(updateLocation, prerelease: prerelease); asyncUpdateManager.Wait(); @@ -136,7 +136,7 @@ private async Task CheckForAppUpdates() var canUpdate = await updateManager.CheckForUpdate(); - if(canUpdate.ReleasesToApply.Any()) + if (canUpdate.ReleasesToApply.Any()) { Logger.Information("Auto update, new version available, raising the event"); this.eventBus.PublishEvent(this.applicationUpdateEventFactory.CreateApplicationUpdateLatestVersionChanged(canUpdate.FutureReleaseEntry.Version.ToString())); @@ -160,4 +160,4 @@ private async Task DoAppUpdates(UpdateManager updateManager) Logger.Information($"Auto update, update completed for {releaseInfo.Version}"); } } -} +} \ No newline at end of file diff --git a/Components/Audio/EventHandler/AudioEventHandler.cs b/Components/Audio/EventHandler/AudioEventHandler.cs index 3dff091b..379e3616 100644 --- a/Components/Audio/EventHandler/AudioEventHandler.cs +++ b/Components/Audio/EventHandler/AudioEventHandler.cs @@ -2,6 +2,7 @@ using Slipstream.Components.Audio.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Audio.EventHandler { @@ -14,83 +15,37 @@ public AudioEventHandler(EventHandlerController eventHandler) Parent = eventHandler; } - public delegate void OnAudioCommandPlayHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnAudioCommandPlay; - public delegate void OnAudioCommandSayHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnAudioCommandSay; - public delegate void OnAudioCommandSendDevicesHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnAudioCommandSendDevices; - public delegate void OnAudioCommandSetOutputDeviceHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnAudioCommandSetOutputDevice; - public delegate void OnAudioOutputDeviceHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnAudioCommandPlayHandler? OnAudioCommandPlay; - - public event OnAudioCommandSayHandler? OnAudioCommandSay; - - public event OnAudioCommandSendDevicesHandler? OnAudioCommandSendDevices; - - public event OnAudioCommandSetOutputDeviceHandler? OnAudioCommandSetOutputDevice; - - public event OnAudioOutputDeviceHandler? OnAudioOutputDevice; + public event EventHandler? OnAudioOutputDevice; public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - switch (@event) + return @event switch { - case AudioCommandSay tev: - if (OnAudioCommandSay != null) - { - OnAudioCommandSay.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case AudioCommandPlay tev: - if (OnAudioCommandPlay != null) - { - OnAudioCommandPlay.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case AudioCommandSendDevices tev: - if (OnAudioCommandSendDevices != null) - { - OnAudioCommandSendDevices.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case AudioOutputDevice tev: - if (OnAudioOutputDevice != null) - { - OnAudioOutputDevice.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case AudioCommandSetOutputDevice tev: - if (OnAudioCommandSetOutputDevice != null) - { - OnAudioCommandSetOutputDevice.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - } + AudioCommandSay tev => OnEvent(OnAudioCommandSay, tev), + AudioCommandPlay tev => OnEvent(OnAudioCommandPlay, tev), + AudioCommandSendDevices tev => OnEvent(OnAudioCommandSendDevices, tev), + AudioOutputDevice tev => OnEvent(OnAudioOutputDevice, tev), + AudioCommandSetOutputDevice tev => OnEvent(OnAudioCommandSetOutputDevice, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; + } - return IEventHandler.HandledStatus.NotMine; + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) + { + if (onEvent != null) + { + onEvent.Invoke(Parent, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/Audio/Plugins/AudioPlugin.cs b/Components/Audio/Plugins/AudioPlugin.cs index c215bac7..5e8e0c0a 100644 --- a/Components/Audio/Plugins/AudioPlugin.cs +++ b/Components/Audio/Plugins/AudioPlugin.cs @@ -55,10 +55,10 @@ public AudioPlugin(IEventHandlerController eventHandlerController, string id, IL var Audio = EventHandlerController.Get(); - Audio.OnAudioCommandSay += (_, e) => OnAudioCommandSay(e.Event); - Audio.OnAudioCommandPlay += (_, e) => OnAudioCommandPlay(e.Event); - Audio.OnAudioCommandSendDevices += (_, e) => OnAudioCommandSendDevices(e.Event); - Audio.OnAudioCommandSetOutputDevice += (_, e) => OnAudioCommandSetOutputDevice(e.Event); + Audio.OnAudioCommandSay += (_, e) => OnAudioCommandSay(e); + Audio.OnAudioCommandPlay += (_, e) => OnAudioCommandPlay(e); + Audio.OnAudioCommandSendDevices += (_, e) => OnAudioCommandSendDevices(e); + Audio.OnAudioCommandSetOutputDevice += (_, e) => OnAudioCommandSetOutputDevice(e); } private void OnAudioCommandSetOutputDevice(AudioCommandSetOutputDevice @event) diff --git a/Components/BasePlugin.cs b/Components/BasePlugin.cs index f1092b7d..33ad30aa 100644 --- a/Components/BasePlugin.cs +++ b/Components/BasePlugin.cs @@ -1,6 +1,7 @@ #nullable enable using Slipstream.Shared; +using System; namespace Slipstream.Components { @@ -12,7 +13,7 @@ public class BasePlugin : IPlugin public string Name { get { return name; } - set { name = value; OnStateChanged?.Invoke(this, new IPlugin.EventHandlerArgs(this)); } + set { name = value; OnStateChanged?.Invoke(this, this); } } private string displayName = "INVALID-DISPLAY-NAME"; @@ -20,10 +21,10 @@ public string Name public string DisplayName { get { return displayName; } - set { displayName = value; OnStateChanged?.Invoke(this, new IPlugin.EventHandlerArgs(this)); } + set { displayName = value; OnStateChanged?.Invoke(this, this); } } - public event IPlugin.OnStateChangedHandler? OnStateChanged; + public event EventHandler? OnStateChanged; public bool Reconfigurable { get; } public bool FullThreadControl { get; } diff --git a/Components/Discord/EventHandler/DiscordEventHandler.cs b/Components/Discord/EventHandler/DiscordEventHandler.cs index ed8aa4cc..c1958fb7 100644 --- a/Components/Discord/EventHandler/DiscordEventHandler.cs +++ b/Components/Discord/EventHandler/DiscordEventHandler.cs @@ -1,5 +1,8 @@ -using Slipstream.Components.Discord.Events; +#nullable enable + +using Slipstream.Components.Discord.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Discord.EventHandler { @@ -7,21 +10,13 @@ internal class DiscordEventHandler : IEventHandler { private readonly EventHandlerController Parent; - public delegate void OnDiscordConnectedHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnDiscordDisconnectedHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnDiscordMessageReceivedHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnDiscordCommandSendMessageHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnDiscordConnectedHandler? OnDiscordConnected; + public event EventHandler? OnDiscordConnected; - public event OnDiscordDisconnectedHandler? OnDiscordDisconnected; + public event EventHandler? OnDiscordDisconnected; - public event OnDiscordMessageReceivedHandler? OnDiscordMessageReceived; + public event EventHandler? OnDiscordMessageReceived; - public event OnDiscordCommandSendMessageHandler? OnDiscordCommandSendMessage; + public event EventHandler? OnDiscordCommandSendMessage; public DiscordEventHandler(EventHandlerController eventHandler) { @@ -30,51 +25,24 @@ public DiscordEventHandler(EventHandlerController eventHandler) public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - switch (@event) + return @event switch { - case DiscordConnected tev: - if (OnDiscordConnected != null) - { - OnDiscordConnected.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case DiscordDisconnected tev: - if (OnDiscordDisconnected != null) - { - OnDiscordDisconnected.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case DiscordMessageReceived tev: - if (OnDiscordMessageReceived != null) - { - OnDiscordMessageReceived.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case DiscordCommandSendMessage tev: - if (OnDiscordCommandSendMessage != null) - { - OnDiscordCommandSendMessage.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - } + DiscordConnected tev => OnEvent(OnDiscordConnected, tev), + DiscordDisconnected tev => OnEvent(OnDiscordDisconnected, tev), + DiscordMessageReceived tev => OnEvent(OnDiscordMessageReceived, tev), + DiscordCommandSendMessage tev => OnEvent(OnDiscordCommandSendMessage, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; + } - return IEventHandler.HandledStatus.NotMine; + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) + { + if (onEvent != null) + { + onEvent.Invoke(Parent, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/Discord/Plugins/DiscordPlugin.cs b/Components/Discord/Plugins/DiscordPlugin.cs index 7fcb486d..6df31361 100644 --- a/Components/Discord/Plugins/DiscordPlugin.cs +++ b/Components/Discord/Plugins/DiscordPlugin.cs @@ -41,19 +41,19 @@ public DiscordPlugin(IEventHandlerController eventHandlerController, string plug eventHandler.OnDiscordCommandSendMessage += EventHandler_OnDiscordCommandSendMessage; } - private void EventHandler_OnDiscordCommandSendMessage(EventHandlerController source, EventHandlerArgs e) + private void EventHandler_OnDiscordCommandSendMessage(object source, Events.DiscordCommandSendMessage e) { if (Client == null) return; - if (!DiscordChannelIdMap.ContainsKey(e.Event.ChannelId)) + if (!DiscordChannelIdMap.ContainsKey(e.ChannelId)) { - var channel = Client.GetChannelAsync(e.Event.ChannelId).GetAwaiter().GetResult(); + var channel = Client.GetChannelAsync(e.ChannelId).GetAwaiter().GetResult(); - DiscordChannelIdMap.Add(e.Event.ChannelId, channel); + DiscordChannelIdMap.Add(e.ChannelId, channel); } - DiscordChannelIdMap[e.Event.ChannelId].SendMessageAsync(e.Event.Message, e.Event.TextToSpeech); + DiscordChannelIdMap[e.ChannelId].SendMessageAsync(e.Message, e.TextToSpeech); } public override void Run() diff --git a/Components/FileMonitor/EventHandler/FileMonitor.cs b/Components/FileMonitor/EventHandler/FileMonitor.cs index f9cb8a88..ea4f2034 100644 --- a/Components/FileMonitor/EventHandler/FileMonitor.cs +++ b/Components/FileMonitor/EventHandler/FileMonitor.cs @@ -2,6 +2,7 @@ using Slipstream.Components.FileMonitor.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.FileMonitor.EventHandler { @@ -14,97 +15,40 @@ public FileMonitor(IEventHandlerController eventHandler) Parent = eventHandler; } - public delegate void OnFileMonitorCommandScanHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnFileMonitorCommandScan; - public delegate void OnFileMonitorFileChangedHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnFileMonitorFileChanged; - public delegate void OnFileMonitorFileCreatedHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnFileMonitorFileCreated; - public delegate void OnFileMonitorFileDeletedHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnFileMonitorFileDeleted; - public delegate void OnFileMonitorFileRenamedHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnFileMonitorFileRenamed; - public delegate void OnFileMonitorScanCompletedHandler(IEventHandlerController source, EventHandlerArgs e); - - public event OnFileMonitorCommandScanHandler? OnFileMonitorCommandScan; - - public event OnFileMonitorFileChangedHandler? OnFileMonitorFileChanged; - - public event OnFileMonitorFileCreatedHandler? OnFileMonitorFileCreated; - - public event OnFileMonitorFileDeletedHandler? OnFileMonitorFileDeleted; - - public event OnFileMonitorFileRenamedHandler? OnFileMonitorFileRenamed; - - public event OnFileMonitorScanCompletedHandler? OnFileMonitorScanCompleted; + public event EventHandler? OnFileMonitorScanCompleted; public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - switch (@event) + return @event switch { - case FileMonitorCommandScan tev: - if (OnFileMonitorCommandScan != null) - { - OnFileMonitorCommandScan.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case FileMonitorFileCreated tev: - if (OnFileMonitorFileCreated != null) - { - OnFileMonitorFileCreated.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case FileMonitorFileChanged tev: - if (OnFileMonitorFileChanged != null) - { - OnFileMonitorFileChanged.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case FileMonitorFileDeleted tev: - if (OnFileMonitorFileDeleted != null) - { - OnFileMonitorFileDeleted.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case FileMonitorFileRenamed tev: - if (OnFileMonitorFileRenamed != null) - { - OnFileMonitorFileRenamed.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case FileMonitorScanCompleted tev: - if (OnFileMonitorScanCompleted != null) - { - OnFileMonitorScanCompleted.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - } + FileMonitorCommandScan tev => OnEvent(OnFileMonitorCommandScan, tev), + FileMonitorFileCreated tev => OnEvent(OnFileMonitorFileCreated, tev), + FileMonitorFileChanged tev => OnEvent(OnFileMonitorFileChanged, tev), + FileMonitorFileDeleted tev => OnEvent(OnFileMonitorFileDeleted, tev), + FileMonitorFileRenamed tev => OnEvent(OnFileMonitorFileRenamed, tev), + FileMonitorScanCompleted tev => OnEvent(OnFileMonitorScanCompleted, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; + } - return IEventHandler.HandledStatus.NotMine; + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) + { + if (onEvent != null) + { + onEvent.Invoke(Parent, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/IPlugin.cs b/Components/IPlugin.cs index 4847f85d..499ce834 100644 --- a/Components/IPlugin.cs +++ b/Components/IPlugin.cs @@ -7,19 +7,7 @@ namespace Slipstream.Components { public interface IPlugin : IDisposable { - public class EventHandlerArgs : EventArgs - { - public T Event { get; } - - public EventHandlerArgs(T e) - { - Event = e; - } - } - - public delegate void OnStateChangedHandler(IPlugin source, EventHandlerArgs e); - - public event OnStateChangedHandler? OnStateChanged; + public event EventHandler? OnStateChanged; public string Id { get; } public string Name { get; } diff --git a/Components/IRacing/EventHandler/IRacing.cs b/Components/IRacing/EventHandler/IRacing.cs index 384c2d22..0f03757b 100644 --- a/Components/IRacing/EventHandler/IRacing.cs +++ b/Components/IRacing/EventHandler/IRacing.cs @@ -2,6 +2,7 @@ using Slipstream.Components.IRacing.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.IRacing.EventHandler { @@ -14,364 +15,97 @@ public IRacing(EventHandlerController eventHandler) Parent = eventHandler; } - public delegate void OnIRacingCarCompletedLapHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCarCompletedLap; - public delegate void OnIRacingCarInfoHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCarInfo; - public delegate void OnIRacingCommandSendCarInfoHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCommandSendCarInfo; - public delegate void OnIRacingCommandSendRaceFlagsHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCommandSendRaceFlags; - public delegate void OnIRacingCommandSendSessionStateHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCommandSendSessionState; - public delegate void OnIRacingCommandSendTrackInfoHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCommandSendTrackInfo; - public delegate void OnIRacingCommandSendWeatherInfoHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCommandSendWeatherInfo; - public delegate void OnIRacingConnectedHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingConnected; - public delegate void OnIRacingDisconnectedHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingDisconnected; - public delegate void OnIRacingDriverIncidentHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingDriverIncident; - public delegate void OnIRacingPitEnterHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingPitEnter; - public delegate void OnIRacingPitExitHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingPitExit; - public delegate void OnIRacingPitstopReportHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingPitstopReport; - public delegate void OnIRacingRaceFlagsHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingRaceFlags; - public delegate void OnIRacingTrackInfoHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingTrackInfo; - public delegate void OnIRacingWeatherInfoHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingWeatherInfo; - public delegate void OnIRacingPracticeHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingPractice; - public delegate void OnIRacingQualifyHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingQualify; - public delegate void OnIRacingRaceHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingRace; - public delegate void OnIRacingTestingHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingTesting; - public delegate void OnIRacingWarmupHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingWarmup; - public delegate void OnIRacingCarPositionHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCarPosition; - public delegate void OnIRacingRawHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingRaw; - public delegate void OnIRacingTowedHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingTowed; - public delegate void OnIRacingTrackPositionHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnIRacingCarCompletedLapHandler? OnIRacingCarCompletedLap; - - public event OnIRacingCarInfoHandler? OnIRacingCarInfo; - - public event OnIRacingCommandSendCarInfoHandler? OnIRacingCommandSendCarInfo; - - public event OnIRacingCommandSendRaceFlagsHandler? OnIRacingCommandSendRaceFlags; - - public event OnIRacingCommandSendSessionStateHandler? OnIRacingCommandSendSessionState; - - public event OnIRacingCommandSendTrackInfoHandler? OnIRacingCommandSendTrackInfo; - - public event OnIRacingCommandSendWeatherInfoHandler? OnIRacingCommandSendWeatherInfo; - - public event OnIRacingConnectedHandler? OnIRacingConnected; - - public event OnIRacingDisconnectedHandler? OnIRacingDisconnected; - - public event OnIRacingDriverIncidentHandler? OnIRacingDriverIncident; - - public event OnIRacingPitEnterHandler? OnIRacingPitEnter; - - public event OnIRacingPitExitHandler? OnIRacingPitExit; - - public event OnIRacingPitstopReportHandler? OnIRacingPitstopReport; - - public event OnIRacingRaceFlagsHandler? OnIRacingRaceFlags; - - public event OnIRacingTrackInfoHandler? OnIRacingTrackInfo; - - public event OnIRacingWeatherInfoHandler? OnIRacingWeatherInfo; - - public event OnIRacingPracticeHandler? OnIRacingPractice; - - public event OnIRacingQualifyHandler? OnIRacingQualify; - - public event OnIRacingRaceHandler? OnIRacingRace; - - public event OnIRacingTestingHandler? OnIRacingTesting; - - public event OnIRacingWarmupHandler? OnIRacingWarmup; - - public event OnIRacingCarPositionHandler? OnIRacingCarPosition; - - public event OnIRacingRawHandler? OnIRacingRaw; - - public event OnIRacingTowedHandler? OnIRacingTowed; - - public event OnIRacingTrackPositionHandler? OnIRacingTrackPosition; + public event EventHandler? OnIRacingTrackPosition; public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - switch (@event) + return @event switch { - case IRacingConnected tev: - if (OnIRacingConnected != null) - { - OnIRacingConnected.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingDisconnected tev: - if (OnIRacingDisconnected != null) - { - OnIRacingDisconnected.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingTrackInfo tev: - if (OnIRacingTrackInfo != null) - { - OnIRacingTrackInfo.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingWeatherInfo tev: - if (OnIRacingWeatherInfo != null) - { - OnIRacingWeatherInfo.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingCarInfo tev: - if (OnIRacingCarInfo != null) - { - OnIRacingCarInfo.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingRaceFlags tev: - if (OnIRacingRaceFlags != null) - { - OnIRacingRaceFlags.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingCompletedLap tev: - if (OnIRacingCarCompletedLap != null) - { - OnIRacingCarCompletedLap.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingPitEnter tev: - if (OnIRacingPitEnter != null) - { - OnIRacingPitEnter.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingPitExit tev: - if (OnIRacingPitExit != null) - { - OnIRacingPitExit.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingPitstopReport tev: - if (OnIRacingPitstopReport != null) - { - OnIRacingPitstopReport.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingCommandSendCarInfo tev: - if (OnIRacingCommandSendCarInfo != null) - { - OnIRacingCommandSendCarInfo.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingCommandSendTrackInfo tev: - if (OnIRacingCommandSendTrackInfo != null) - { - OnIRacingCommandSendTrackInfo.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingCommandSendWeatherInfo tev: - if (OnIRacingCommandSendWeatherInfo != null) - { - OnIRacingCommandSendWeatherInfo.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingCommandSendSessionState tev: - if (OnIRacingCommandSendSessionState != null) - { - OnIRacingCommandSendSessionState.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingCommandSendRaceFlags tev: - if (OnIRacingCommandSendRaceFlags != null) - { - OnIRacingCommandSendRaceFlags.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingDriverIncident tev: - if (OnIRacingDriverIncident != null) - { - OnIRacingDriverIncident(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingPractice tev: - if (OnIRacingPractice != null) - { - OnIRacingPractice(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingQualify tev: - if (OnIRacingQualify != null) - { - OnIRacingQualify(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingRace tev: - if (OnIRacingRace != null) - { - OnIRacingRace(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingTesting tev: - if (OnIRacingTesting != null) - { - OnIRacingTesting(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingWarmup tev: - if (OnIRacingWarmup != null) - { - OnIRacingWarmup(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingCarPosition tev: - if (OnIRacingCarPosition != null) - { - OnIRacingCarPosition(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingRaw tev: - if (OnIRacingRaw != null) - { - OnIRacingRaw(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case IRacingTowed tev: - if (OnIRacingTowed != null) - { - OnIRacingTowed(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } + IRacingConnected tev => OnEvent(OnIRacingConnected, tev), + IRacingDisconnected tev => OnEvent(OnIRacingDisconnected, tev), + IRacingTrackInfo tev => OnEvent(OnIRacingTrackInfo, tev), + IRacingWeatherInfo tev => OnEvent(OnIRacingWeatherInfo, tev), + IRacingCarInfo tev => OnEvent(OnIRacingCarInfo, tev), + IRacingRaceFlags tev => OnEvent(OnIRacingRaceFlags, tev), + IRacingCompletedLap tev => OnEvent(OnIRacingCarCompletedLap, tev), + IRacingPitEnter tev => OnEvent(OnIRacingPitEnter, tev), + IRacingPitExit tev => OnEvent(OnIRacingPitExit, tev), + IRacingPitstopReport tev => OnEvent(OnIRacingPitstopReport, tev), + IRacingCommandSendCarInfo tev => OnEvent(OnIRacingCommandSendCarInfo, tev), + IRacingCommandSendTrackInfo tev => OnEvent(OnIRacingCommandSendTrackInfo, tev), + IRacingCommandSendWeatherInfo tev => OnEvent(OnIRacingCommandSendWeatherInfo, tev), + IRacingCommandSendSessionState tev => OnEvent(OnIRacingCommandSendSessionState, tev), + IRacingCommandSendRaceFlags tev => OnEvent(OnIRacingCommandSendRaceFlags, tev), + IRacingDriverIncident tev => OnEvent(OnIRacingDriverIncident, tev), + IRacingPractice tev => OnEvent(OnIRacingPractice, tev), + IRacingQualify tev => OnEvent(OnIRacingQualify, tev), + IRacingRace tev => OnEvent(OnIRacingRace, tev), + IRacingTesting tev => OnEvent(OnIRacingTesting, tev), + IRacingWarmup tev => OnEvent(OnIRacingWarmup, tev), + IRacingCarPosition tev => OnEvent(OnIRacingCarPosition, tev), + IRacingRaw tev => OnEvent(OnIRacingRaw, tev), + IRacingTowed tev => OnEvent(OnIRacingTowed, tev), + IRacingTrackPosition tev => OnEvent(OnIRacingTrackPosition, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; + } - case IRacingTrackPosition tev: - if (OnIRacingTrackPosition != null) - { - OnIRacingTrackPosition(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) + { + if (onEvent != null) + { + onEvent.Invoke(Parent, args); + return IEventHandler.HandledStatus.Handled; } - - return IEventHandler.HandledStatus.NotMine; + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/Internal/EventHandler/Internal.cs b/Components/Internal/EventHandler/Internal.cs index 53bd8a00..6d941379 100644 --- a/Components/Internal/EventHandler/Internal.cs +++ b/Components/Internal/EventHandler/Internal.cs @@ -2,6 +2,7 @@ using Slipstream.Components.Internal.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Internal.EventHandler { @@ -14,99 +15,40 @@ public Internal(IEventHandlerController parent) Parent = parent; } - public delegate void OnInternalCommandPluginRegisterHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnInternalCommandPluginRegister; - public delegate void OnInternalCommandPluginStatesHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnInternalCommandPluginStates; - public delegate void OnInternalCommandPluginUnregisterHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnInternalCommandPluginUnregister; - public delegate void OnInternalPluginStateHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnInternalPluginState; - public delegate void OnInternalShutdownHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnInternalShutdown; - public delegate void OnInternalCommandShutdownHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnInternalCommandShutdown; - public event OnInternalCommandPluginRegisterHandler? OnInternalCommandPluginRegister; - - public event OnInternalCommandPluginStatesHandler? OnInternalCommandPluginStates; - - public event OnInternalCommandPluginUnregisterHandler? OnInternalCommandPluginUnregister; - - public event OnInternalPluginStateHandler? OnInternalPluginState; - - public event OnInternalShutdownHandler? OnInternalShutdown; - - public event OnInternalCommandShutdownHandler? OnInternalCommandShutdown; - - public IEventHandler.HandledStatus HandleEvent(IEvent ev) + public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - switch (ev) + return @event switch { - // Internal + InternalCommandPluginRegister tev => OnEvent(OnInternalCommandPluginRegister, tev), + InternalCommandPluginUnregister tev => OnEvent(OnInternalCommandPluginUnregister, tev), + InternalPluginState tev => OnEvent(OnInternalPluginState, tev), + InternalCommandPluginStates tev => OnEvent(OnInternalCommandPluginStates, tev), + InternalShutdown tev => OnEvent(OnInternalShutdown, tev), + InternalCommandShutdown tev => OnEvent(OnInternalCommandShutdown, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; + } - case InternalCommandPluginRegister tev: - if (OnInternalCommandPluginRegister != null) - { - OnInternalCommandPluginRegister?.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case InternalCommandPluginUnregister tev: - if (OnInternalCommandPluginUnregister != null) - { - OnInternalCommandPluginUnregister.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case InternalPluginState tev: - if (OnInternalPluginState != null) - { - OnInternalPluginState.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case InternalCommandPluginStates tev: - if (OnInternalCommandPluginStates != null) - { - OnInternalCommandPluginStates.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case InternalShutdown tev: - if (OnInternalShutdown != null) - { - OnInternalShutdown.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case InternalCommandShutdown tev: - if (OnInternalCommandShutdown != null) - { - OnInternalCommandShutdown.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) + { + if (onEvent != null) + { + onEvent.Invoke(Parent, args); + return IEventHandler.HandledStatus.Handled; } - - return IEventHandler.HandledStatus.NotMine; + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/Lua/EventHandler/Lua.cs b/Components/Lua/EventHandler/Lua.cs index d99928dc..5a9a7718 100644 --- a/Components/Lua/EventHandler/Lua.cs +++ b/Components/Lua/EventHandler/Lua.cs @@ -2,6 +2,7 @@ using Slipstream.Components.Lua.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Lua.EventHandler { @@ -14,27 +15,25 @@ public Lua(EventHandlerController parent) Parent = parent; } - public delegate void OnLuaDeduplicateEventsHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnLuaDeduplicateEventsHandler? OnLuaCommandDeduplicateEvents; + public event EventHandler? OnLuaCommandDeduplicateEvents; public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - switch (@event) + return @event switch { - case LuaCommandDeduplicateEvents tev: - if (OnLuaCommandDeduplicateEvents != null) - { - OnLuaCommandDeduplicateEvents?.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - } + LuaCommandDeduplicateEvents tev => OnEvent(OnLuaCommandDeduplicateEvents, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; + } - return IEventHandler.HandledStatus.NotMine; + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) + { + if (onEvent != null) + { + onEvent.Invoke(Parent, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/Lua/Plugins/LuaManagerPlugin.cs b/Components/Lua/Plugins/LuaManagerPlugin.cs index e4abd142..5e693e8c 100644 --- a/Components/Lua/Plugins/LuaManagerPlugin.cs +++ b/Components/Lua/Plugins/LuaManagerPlugin.cs @@ -64,7 +64,7 @@ IEventSerdeService eventSerdeService fileMonitor.OnFileMonitorFileRenamed += EventHandler_OnFileMonitorFileRenamed; fileMonitor.OnFileMonitorScanCompleted += EventHandler_OnFileMonitorScanCompleted; @internal.OnInternalPluginState += EventHandler_OnInternalPluginState; - lua.OnLuaCommandDeduplicateEvents += (s, e) => EventHandler_OnLuaCommandDeduplicateEvents(e.Event); + lua.OnLuaCommandDeduplicateEvents += (s, e) => EventHandler_OnLuaCommandDeduplicateEvents(e); BootupEventsDeadline = DateTime.Now.AddMilliseconds(500); @@ -82,11 +82,11 @@ private void EventHandler_OnLuaCommandDeduplicateEvents(LuaCommandDeduplicateEve BootupEventsDeadline = DateTime.Now.AddMilliseconds(500); } - private void EventHandler_OnInternalPluginState(IEventHandlerController source, EventHandlerArgs e) + private void EventHandler_OnInternalPluginState(object source, InternalPluginState e) { - if (e.Event.PluginStatus == "Registered" && e.Event.PluginName == "LuaPlugin") + if (e.PluginStatus == "Registered" && e.PluginName == "LuaPlugin") { - WaitingForLuaScripts.Remove(e.Event.Id); + WaitingForLuaScripts.Remove(e.Id); if (WaitingForLuaScripts.Count == 0) { @@ -96,7 +96,7 @@ private void EventHandler_OnInternalPluginState(IEventHandlerController source, } } - private void EventHandler_OnFileMonitorScanCompleted(IEventHandlerController source, EventHandlerArgs e) + private void EventHandler_OnFileMonitorScanCompleted(object source, FileMonitorScanCompleted e) { BootUp = false; } @@ -140,42 +140,42 @@ private void DeletedFile(string filePath) } } - private void EventHandler_OnFileMonitorFileRenamed(IEventHandlerController source, EventHandlerArgs e) + private void EventHandler_OnFileMonitorFileRenamed(object source, FileMonitorFileRenamed e) { // If we're been unregistered (while app is running) and re-registered,we need to ignore these events // until we're ready (received the FileMonitorScanCompleted). We need to revisit this later. - if (e.Event.FilePath == null || e.Event.OldFilePath == null || BootUp) + if (e.FilePath == null || e.OldFilePath == null || BootUp) return; - if (IsApplicable(e.Event.OldFilePath)) - DeletedFile(e.Event.OldFilePath); - if (IsApplicable(e.Event.FilePath)) - NewFile(e.Event.FilePath); + if (IsApplicable(e.OldFilePath)) + DeletedFile(e.OldFilePath); + if (IsApplicable(e.FilePath)) + NewFile(e.FilePath); } - private void EventHandler_OnFileMonitorFileChanged(IEventHandlerController source, EventHandlerArgs e) + private void EventHandler_OnFileMonitorFileChanged(object source, FileMonitorFileChanged e) { - if (e.Event.FilePath == null || !IsApplicable(e.Event.FilePath) || BootUp) + if (e.FilePath == null || !IsApplicable(e.FilePath) || BootUp) return; - DeletedFile(e.Event.FilePath); - NewFile(e.Event.FilePath); + DeletedFile(e.FilePath); + NewFile(e.FilePath); } - private void EventHandler_OnFileMonitorFileDeleted(IEventHandlerController source, EventHandlerArgs e) + private void EventHandler_OnFileMonitorFileDeleted(object source, FileMonitorFileDeleted e) { - if (e.Event.FilePath == null || !IsApplicable(e.Event.FilePath) || BootUp) + if (e.FilePath == null || !IsApplicable(e.FilePath) || BootUp) return; - DeletedFile(e.Event.FilePath); + DeletedFile(e.FilePath); } - private void EventHandler_OnFileMonitorFileCreated(IEventHandlerController source, EventHandlerArgs e) + private void EventHandler_OnFileMonitorFileCreated(object source, FileMonitorFileCreated e) { - if (e.Event.FilePath == null || !IsApplicable(e.Event.FilePath)) + if (e.FilePath == null || !IsApplicable(e.FilePath)) return; - NewFile(e.Event.FilePath); + NewFile(e.FilePath); } private bool IsApplicable(string FilePath) diff --git a/Components/Lua/Plugins/LuaPlugin.cs b/Components/Lua/Plugins/LuaPlugin.cs index f3383a9d..1e779caf 100644 --- a/Components/Lua/Plugins/LuaPlugin.cs +++ b/Components/Lua/Plugins/LuaPlugin.cs @@ -39,7 +39,7 @@ Parameters configuration // Avoid that WriteToConsole is evaluated by Lua, that in turn will // add more WriteToConsole events, making a endless loop EventHandlerController.Get().OnUICommandWriteToConsole += (s, e) => { }; - EventHandlerController.OnDefault += (s, e) => LuaContext?.HandleEvent(e.Event); + EventHandlerController.OnDefault += (s, e) => LuaContext?.HandleEvent(e); FilePath = configuration.Extract("filepath"); diff --git a/Components/Playback/EventHandler/Playback.cs b/Components/Playback/EventHandler/Playback.cs index 79083433..87b22a88 100644 --- a/Components/Playback/EventHandler/Playback.cs +++ b/Components/Playback/EventHandler/Playback.cs @@ -2,6 +2,7 @@ using Slipstream.Components.Playback.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Playback.EventHandler { @@ -14,41 +15,28 @@ public Playback(EventHandlerController parent) Parent = parent; } - public delegate void OnPlaybackCommandInjectEventsHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnPlaybackCommandInjectEvents; - public event OnPlaybackCommandInjectEventsHandler? OnPlaybackCommandInjectEvents; - - public delegate void OnPlaybackCommandSaveEventsHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnPlaybackCommandSaveEventsHandler? OnPlaybackCommandSaveEvents; + public event EventHandler? OnPlaybackCommandSaveEvents; public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - switch (@event) + return @event switch { - case PlaybackCommandInjectEvents tev: - if (OnPlaybackCommandInjectEvents != null) - { - OnPlaybackCommandInjectEvents.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case PlaybackCommandSaveEvents tev: - if (OnPlaybackCommandSaveEvents != null) - { - OnPlaybackCommandSaveEvents.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - } + PlaybackCommandInjectEvents tev => OnEvent(OnPlaybackCommandInjectEvents, tev), + PlaybackCommandSaveEvents tev => OnEvent(OnPlaybackCommandSaveEvents, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; + } - return IEventHandler.HandledStatus.NotMine; + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) + { + if (onEvent != null) + { + onEvent.Invoke(Parent, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/Playback/Plugins/PlaybackPlugin.cs b/Components/Playback/Plugins/PlaybackPlugin.cs index cfddc4ce..c1047003 100644 --- a/Components/Playback/Plugins/PlaybackPlugin.cs +++ b/Components/Playback/Plugins/PlaybackPlugin.cs @@ -21,8 +21,8 @@ public PlaybackPlugin(IEventHandlerController eventHandlerController, string id, EventSerdeService = eventSerdeService; var playback = EventHandlerController.Get(); - playback.OnPlaybackCommandInjectEvents += (s, e) => OnPlaybackCommandInjectEvents(e.Event); - playback.OnPlaybackCommandSaveEvents += (s, e) => OnPlaybackCommandSaveEvents(e.Event); + playback.OnPlaybackCommandInjectEvents += (s, e) => OnPlaybackCommandInjectEvents(e); + playback.OnPlaybackCommandSaveEvents += (s, e) => OnPlaybackCommandSaveEvents(e); Logger = logger; } diff --git a/Components/Twitch/EventHandler/Twitch.cs b/Components/Twitch/EventHandler/Twitch.cs index df23a295..776c90ab 100644 --- a/Components/Twitch/EventHandler/Twitch.cs +++ b/Components/Twitch/EventHandler/Twitch.cs @@ -2,6 +2,7 @@ using Slipstream.Components.Twitch.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Twitch.EventHandler { @@ -14,139 +15,49 @@ public Twitch(EventHandlerController eventHandler) Parent = eventHandler; } - public delegate void OnTwitchCommandSendMessageHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchCommandSendMessage; - public delegate void OnTwitchCommandSendWhisperHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchCommandSendWhisper; - public delegate void OnTwitchConnectedHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchConnected; - public delegate void OnTwitchDisconnectedHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchDisconnected; - public delegate void OnTwitchReceivedMessageHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchReceivedMessage; - public delegate void OnTwitchReceivedWhisperHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchReceivedWhisper; - public delegate void OnTwitchUserSubscribedHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchUserSubscribed; - public delegate void OnTwitchGiftedSubscriptionHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchGiftedSubscription; - public delegate void OnTwitchRaidedHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnTwitchCommandSendMessageHandler? OnTwitchCommandSendMessage; - - public event OnTwitchCommandSendWhisperHandler? OnTwitchCommandSendWhisper; - - public event OnTwitchConnectedHandler? OnTwitchConnected; - - public event OnTwitchDisconnectedHandler? OnTwitchDisconnected; - - public event OnTwitchReceivedMessageHandler? OnTwitchReceivedMessage; - - public event OnTwitchReceivedWhisperHandler? OnTwitchReceivedWhisper; - - public event OnTwitchUserSubscribedHandler? OnTwitchUserSubscribed; - - public event OnTwitchGiftedSubscriptionHandler? OnTwitchGiftedSubscription; - - public event OnTwitchRaidedHandler? OnTwitchRaided; + public event EventHandler? OnTwitchRaided; public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - switch (@event) + return @event switch { - case TwitchConnected tev: - if (OnTwitchConnected != null) - { - OnTwitchConnected.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case TwitchDisconnected tev: - if (OnTwitchDisconnected != null) - { - OnTwitchDisconnected.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case TwitchCommandSendMessage tev: - if (OnTwitchCommandSendMessage != null) - { - OnTwitchCommandSendMessage.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case TwitchCommandSendWhisper tev: - if (OnTwitchCommandSendWhisper != null) - { - OnTwitchCommandSendWhisper.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case TwitchReceivedMessage tev: - if (OnTwitchReceivedMessage != null) - { - OnTwitchReceivedMessage.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case TwitchReceivedWhisper tev: - if (OnTwitchReceivedWhisper != null) - { - OnTwitchReceivedWhisper.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case TwitchUserSubscribed tev: - if (OnTwitchUserSubscribed != null) - { - OnTwitchUserSubscribed.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case TwitchGiftedSubscription tev: - if (OnTwitchGiftedSubscription != null) - { - OnTwitchGiftedSubscription.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case TwitchRaided tev: - if (OnTwitchRaided != null) - { - OnTwitchRaided.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - } + TwitchConnected tev => OnEvent(OnTwitchConnected, tev), + TwitchDisconnected tev => OnEvent(OnTwitchDisconnected, tev), + TwitchCommandSendMessage tev => OnEvent(OnTwitchCommandSendMessage, tev), + TwitchCommandSendWhisper tev => OnEvent(OnTwitchCommandSendWhisper, tev), + TwitchReceivedMessage tev => OnEvent(OnTwitchReceivedMessage, tev), + TwitchReceivedWhisper tev => OnEvent(OnTwitchReceivedWhisper, tev), + TwitchUserSubscribed tev => OnEvent(OnTwitchUserSubscribed, tev), + TwitchGiftedSubscription tev => OnEvent(OnTwitchGiftedSubscription, tev), + TwitchRaided tev => OnEvent(OnTwitchRaided, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; + } - return IEventHandler.HandledStatus.NotMine; + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) + { + if (onEvent != null) + { + onEvent.Invoke(Parent, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/Twitch/Plugins/TwitchPlugin.cs b/Components/Twitch/Plugins/TwitchPlugin.cs index 2cbaaca1..8ec96109 100644 --- a/Components/Twitch/Plugins/TwitchPlugin.cs +++ b/Components/Twitch/Plugins/TwitchPlugin.cs @@ -61,8 +61,8 @@ public TwitchPlugin(IEventHandlerController eventHandlerController, string id, I var twitchEventHandler = EventHandlerController.Get(); - twitchEventHandler.OnTwitchCommandSendMessage += (_, e) => SendMessage(e.Event.Message); - twitchEventHandler.OnTwitchCommandSendWhisper += (_, e) => SendWhisper(e.Event.To, e.Event.Message); + twitchEventHandler.OnTwitchCommandSendMessage += (_, e) => SendMessage(e.Message); + twitchEventHandler.OnTwitchCommandSendWhisper += (_, e) => SendWhisper(e.To, e.Message); TwitchUsername = configuration.Extract("twitch_username"); TwitchChannel = configuration.Extract("twitch_channel"); diff --git a/Components/Txrx/Plugins/TransmitterPlugin.cs b/Components/Txrx/Plugins/TransmitterPlugin.cs index 69e3d787..2e80b44b 100644 --- a/Components/Txrx/Plugins/TransmitterPlugin.cs +++ b/Components/Txrx/Plugins/TransmitterPlugin.cs @@ -44,7 +44,7 @@ public TransmitterPlugin(IEventHandlerController eventHandlerController, string Ip = configuration.Extract("ip"); Port = (int)configuration.Extract("port"); - EventHandlerController.OnDefault += (_, e) => OnEvent(e.Event); + EventHandlerController.OnDefault += (_, e) => OnEvent(e); // To avoid that we get an endless loop, we will Unregister the "other" end in this instance EventBus.PublishEvent(EventFactory.CreateInternalCommandPluginUnregister("ReceiverPlugin")); diff --git a/Components/UI/EventHandler/UIEventHandler.cs b/Components/UI/EventHandler/UIEventHandler.cs index c893747f..ba8e1a54 100644 --- a/Components/UI/EventHandler/UIEventHandler.cs +++ b/Components/UI/EventHandler/UIEventHandler.cs @@ -2,6 +2,7 @@ using Slipstream.Components.UI.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.UI.EventHandler { @@ -14,69 +15,34 @@ public UIEventHandler(EventHandlerController eventHandler) Parent = eventHandler; } - public delegate void OnUIButtonTriggeredHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnUIButtonTriggered; - public delegate void OnUICommandCreateButtonHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnUICommandCreateButton; - public delegate void OnUICommandDeleteButtonHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnUICommandDeleteButton; - public delegate void OnUICommandWriteToConsoleHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnUIButtonTriggeredHandler? OnUIButtonTriggered; - - public event OnUICommandCreateButtonHandler? OnUICommandCreateButton; - - public event OnUICommandDeleteButtonHandler? OnUICommandDeleteButton; - - public event OnUICommandWriteToConsoleHandler? OnUICommandWriteToConsole; + public event EventHandler? OnUICommandWriteToConsole; public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - switch (@event) + return @event switch { - case UICommandWriteToConsole tev: - if (OnUICommandWriteToConsole != null) - { - OnUICommandWriteToConsole.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case UICommandCreateButton tev: - if (OnUICommandCreateButton != null) - { - OnUICommandCreateButton.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case UICommandDeleteButton tev: - if (OnUICommandDeleteButton != null) - { - OnUICommandDeleteButton.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - case UIButtonTriggered tev: - if (OnUIButtonTriggered != null) - { - OnUIButtonTriggered.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } - } + UICommandWriteToConsole tev => OnEvent(OnUICommandWriteToConsole, tev), + UICommandCreateButton tev => OnEvent(OnUICommandCreateButton, tev), + UICommandDeleteButton tev => OnEvent(OnUICommandDeleteButton, tev), + UIButtonTriggered tev => OnEvent(OnUIButtonTriggered, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; + } - return IEventHandler.HandledStatus.NotMine; + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) + { + if (onEvent != null) + { + onEvent.Invoke(Parent, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/WinFormUI/Forms/MainWindow.cs b/Components/WinFormUI/Forms/MainWindow.cs index b356aaeb..be1dd632 100644 --- a/Components/WinFormUI/Forms/MainWindow.cs +++ b/Components/WinFormUI/Forms/MainWindow.cs @@ -128,11 +128,11 @@ private void EventListenerMain() var internalEventHandler = EventHandler.Get(); var uiEventHandler = EventHandler.Get(); - internalEventHandler.OnInternalPluginState += (_, e) => EventHandler_OnInternalPluginState(e.Event); - internalEventHandler.OnInternalShutdown += (_, e) => EventHandler_OnInteralShutdown(e.Event); - uiEventHandler.OnUICommandWriteToConsole += (_, e) => PendingMessages.Add($"{DateTime.Now:s} {e.Event.Message}"); - uiEventHandler.OnUICommandCreateButton += (_, e) => EventHandler_OnUICommandCreateButton(e.Event); - uiEventHandler.OnUICommandDeleteButton += (_, e) => EventHandler_OnUICommandDeleteButton(e.Event); + internalEventHandler.OnInternalPluginState += (_, e) => EventHandler_OnInternalPluginState(e); + internalEventHandler.OnInternalShutdown += (_, e) => EventHandler_OnInteralShutdown(e); + uiEventHandler.OnUICommandWriteToConsole += (_, e) => PendingMessages.Add($"{DateTime.Now:s} {e.Message}"); + uiEventHandler.OnUICommandCreateButton += (_, e) => EventHandler_OnUICommandCreateButton(e); + uiEventHandler.OnUICommandDeleteButton += (_, e) => EventHandler_OnUICommandDeleteButton(e); // Request full state of all known plugins, so we get any that might be started before "us" EventBus.PublishEvent(InternalEventFactory.CreateInternalCommandPluginStates()); diff --git a/Shared/EventHandlerController.cs b/Shared/EventHandlerController.cs index e8336e33..73b5643f 100644 --- a/Shared/EventHandlerController.cs +++ b/Shared/EventHandlerController.cs @@ -13,7 +13,7 @@ public class EventHandlerController : IEventHandlerController private volatile bool enabled = true; public bool Enabled { get { return enabled; } set { enabled = value; } } - public event OnDefaultHandler? OnDefault; + public event EventHandler? OnDefault; internal void Add(Type handlerInterface, IEventHandler implementation) { @@ -49,7 +49,7 @@ public void HandleEvent(IEvent? ev) break; case IEventHandler.HandledStatus.UseDefault: - OnDefault?.Invoke(this, new EventHandlerArgs(ev)); + OnDefault?.Invoke(this, ev); handled = true; break; diff --git a/Shared/IEventHandlerController.cs b/Shared/IEventHandlerController.cs index fc531a99..2bd6a7dd 100644 --- a/Shared/IEventHandlerController.cs +++ b/Shared/IEventHandlerController.cs @@ -1,14 +1,14 @@ #nullable enable +using System; + namespace Slipstream.Shared { public interface IEventHandlerController { public bool Enabled { get; set; } - public delegate void OnDefaultHandler(IEventHandlerController source, EventHandlerArgs e); - - public event OnDefaultHandler? OnDefault; + public event EventHandler? OnDefault; public T Get(); From 84a905141435e85a7e54680b45cff35a23690dfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Thu, 1 Apr 2021 14:53:23 +0200 Subject: [PATCH 4/9] Do no redeclare Dispose() --- Backend/IEngine.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Backend/IEngine.cs b/Backend/IEngine.cs index c59e9945..0de11e7d 100644 --- a/Backend/IEngine.cs +++ b/Backend/IEngine.cs @@ -8,7 +8,5 @@ public interface IEngine : IDisposable void UnregisterSubscription(IEventBusSubscription subscription); void Start(); - - void Dispose(); } } \ No newline at end of file From ac17e4e21144a8b7007c3af5d38757693ea6a5b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Thu, 1 Apr 2021 14:57:31 +0200 Subject: [PATCH 5/9] Break circular dependency in EventHandlers EventHandlerController got IEventHandler childrens, which in turns needs a reference to EventHandlerController --- .../EventHandler/ApplicationUpdateEventHandler.cs | 9 +-------- Components/Audio/EventHandler/AudioEventHandler.cs | 9 +-------- Components/Discord/EventHandler/DiscordEventHandler.cs | 9 +-------- Components/FileMonitor/EventHandler/FileMonitor.cs | 9 +-------- Components/IRacing/EventHandler/IRacing.cs | 9 +-------- Components/Internal/EventHandler/Internal.cs | 9 +-------- Components/Lua/EventHandler/Lua.cs | 9 +-------- Components/Playback/EventHandler/Playback.cs | 9 +-------- Components/Twitch/EventHandler/Twitch.cs | 9 +-------- Components/UI/EventHandler/UIEventHandler.cs | 9 +-------- Shared/EventHandlerControllerBuilder.cs | 2 +- 11 files changed, 11 insertions(+), 81 deletions(-) diff --git a/Components/AppilcationUpdate/EventHandler/ApplicationUpdateEventHandler.cs b/Components/AppilcationUpdate/EventHandler/ApplicationUpdateEventHandler.cs index 99d88ee3..2a7d0651 100644 --- a/Components/AppilcationUpdate/EventHandler/ApplicationUpdateEventHandler.cs +++ b/Components/AppilcationUpdate/EventHandler/ApplicationUpdateEventHandler.cs @@ -12,13 +12,6 @@ internal class ApplicationUpdateEventHandler : IEventHandler public event EventHandler? OnApplicationUpdateCommandCheckLatestVersion; - private readonly IEventHandlerController Parent; - - public ApplicationUpdateEventHandler(IEventHandlerController eventHandler) - { - Parent = eventHandler; - } - public IEventHandler.HandledStatus HandleEvent(IEvent @event) { return @event switch @@ -33,7 +26,7 @@ private IEventHandler.HandledStatus OnEvent(EventHandler? onEven { if (onEvent != null) { - onEvent.Invoke(Parent, args); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } return IEventHandler.HandledStatus.UseDefault; diff --git a/Components/Audio/EventHandler/AudioEventHandler.cs b/Components/Audio/EventHandler/AudioEventHandler.cs index 379e3616..a16d61fe 100644 --- a/Components/Audio/EventHandler/AudioEventHandler.cs +++ b/Components/Audio/EventHandler/AudioEventHandler.cs @@ -8,13 +8,6 @@ namespace Slipstream.Components.Audio.EventHandler { internal class AudioEventHandler : IEventHandler { - private readonly EventHandlerController Parent; - - public AudioEventHandler(EventHandlerController eventHandler) - { - Parent = eventHandler; - } - public event EventHandler? OnAudioCommandPlay; public event EventHandler? OnAudioCommandSay; @@ -42,7 +35,7 @@ private IEventHandler.HandledStatus OnEvent(EventHandler? onEven { if (onEvent != null) { - onEvent.Invoke(Parent, args); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } return IEventHandler.HandledStatus.UseDefault; diff --git a/Components/Discord/EventHandler/DiscordEventHandler.cs b/Components/Discord/EventHandler/DiscordEventHandler.cs index c1958fb7..db1cd35e 100644 --- a/Components/Discord/EventHandler/DiscordEventHandler.cs +++ b/Components/Discord/EventHandler/DiscordEventHandler.cs @@ -8,8 +8,6 @@ namespace Slipstream.Components.Discord.EventHandler { internal class DiscordEventHandler : IEventHandler { - private readonly EventHandlerController Parent; - public event EventHandler? OnDiscordConnected; public event EventHandler? OnDiscordDisconnected; @@ -18,11 +16,6 @@ internal class DiscordEventHandler : IEventHandler public event EventHandler? OnDiscordCommandSendMessage; - public DiscordEventHandler(EventHandlerController eventHandler) - { - Parent = eventHandler; - } - public IEventHandler.HandledStatus HandleEvent(IEvent @event) { return @event switch @@ -39,7 +32,7 @@ private IEventHandler.HandledStatus OnEvent(EventHandler? onEven { if (onEvent != null) { - onEvent.Invoke(Parent, args); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } return IEventHandler.HandledStatus.UseDefault; diff --git a/Components/FileMonitor/EventHandler/FileMonitor.cs b/Components/FileMonitor/EventHandler/FileMonitor.cs index ea4f2034..2a5fdf00 100644 --- a/Components/FileMonitor/EventHandler/FileMonitor.cs +++ b/Components/FileMonitor/EventHandler/FileMonitor.cs @@ -8,13 +8,6 @@ namespace Slipstream.Components.FileMonitor.EventHandler { internal class FileMonitor : IEventHandler { - private readonly IEventHandlerController Parent; - - public FileMonitor(IEventHandlerController eventHandler) - { - Parent = eventHandler; - } - public event EventHandler? OnFileMonitorCommandScan; public event EventHandler? OnFileMonitorFileChanged; @@ -45,7 +38,7 @@ private IEventHandler.HandledStatus OnEvent(EventHandler? onEven { if (onEvent != null) { - onEvent.Invoke(Parent, args); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } return IEventHandler.HandledStatus.UseDefault; diff --git a/Components/IRacing/EventHandler/IRacing.cs b/Components/IRacing/EventHandler/IRacing.cs index 0f03757b..e6782974 100644 --- a/Components/IRacing/EventHandler/IRacing.cs +++ b/Components/IRacing/EventHandler/IRacing.cs @@ -8,13 +8,6 @@ namespace Slipstream.Components.IRacing.EventHandler { internal class IRacing : IEventHandler { - private readonly EventHandlerController Parent; - - public IRacing(EventHandlerController eventHandler) - { - Parent = eventHandler; - } - public event EventHandler? OnIRacingCarCompletedLap; public event EventHandler? OnIRacingCarInfo; @@ -102,7 +95,7 @@ private IEventHandler.HandledStatus OnEvent(EventHandler? onEven { if (onEvent != null) { - onEvent.Invoke(Parent, args); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } return IEventHandler.HandledStatus.UseDefault; diff --git a/Components/Internal/EventHandler/Internal.cs b/Components/Internal/EventHandler/Internal.cs index 6d941379..78ff7cbc 100644 --- a/Components/Internal/EventHandler/Internal.cs +++ b/Components/Internal/EventHandler/Internal.cs @@ -8,13 +8,6 @@ namespace Slipstream.Components.Internal.EventHandler { internal class Internal : IEventHandler { - private readonly IEventHandlerController Parent; - - public Internal(IEventHandlerController parent) - { - Parent = parent; - } - public event EventHandler? OnInternalCommandPluginRegister; public event EventHandler? OnInternalCommandPluginStates; @@ -45,7 +38,7 @@ private IEventHandler.HandledStatus OnEvent(EventHandler? onEven { if (onEvent != null) { - onEvent.Invoke(Parent, args); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } return IEventHandler.HandledStatus.UseDefault; diff --git a/Components/Lua/EventHandler/Lua.cs b/Components/Lua/EventHandler/Lua.cs index 5a9a7718..42f379fe 100644 --- a/Components/Lua/EventHandler/Lua.cs +++ b/Components/Lua/EventHandler/Lua.cs @@ -8,13 +8,6 @@ namespace Slipstream.Components.Lua.EventHandler { internal class Lua : IEventHandler { - private readonly EventHandlerController Parent; - - public Lua(EventHandlerController parent) - { - Parent = parent; - } - public event EventHandler? OnLuaCommandDeduplicateEvents; public IEventHandler.HandledStatus HandleEvent(IEvent @event) @@ -30,7 +23,7 @@ private IEventHandler.HandledStatus OnEvent(EventHandler? onEven { if (onEvent != null) { - onEvent.Invoke(Parent, args); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } return IEventHandler.HandledStatus.UseDefault; diff --git a/Components/Playback/EventHandler/Playback.cs b/Components/Playback/EventHandler/Playback.cs index 87b22a88..09b339a5 100644 --- a/Components/Playback/EventHandler/Playback.cs +++ b/Components/Playback/EventHandler/Playback.cs @@ -8,13 +8,6 @@ namespace Slipstream.Components.Playback.EventHandler { internal class Playback : IEventHandler { - private readonly EventHandlerController Parent; - - public Playback(EventHandlerController parent) - { - Parent = parent; - } - public event EventHandler? OnPlaybackCommandInjectEvents; public event EventHandler? OnPlaybackCommandSaveEvents; @@ -33,7 +26,7 @@ private IEventHandler.HandledStatus OnEvent(EventHandler? onEven { if (onEvent != null) { - onEvent.Invoke(Parent, args); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } return IEventHandler.HandledStatus.UseDefault; diff --git a/Components/Twitch/EventHandler/Twitch.cs b/Components/Twitch/EventHandler/Twitch.cs index 776c90ab..bbb6af03 100644 --- a/Components/Twitch/EventHandler/Twitch.cs +++ b/Components/Twitch/EventHandler/Twitch.cs @@ -8,13 +8,6 @@ namespace Slipstream.Components.Twitch.EventHandler { internal class Twitch : IEventHandler { - private readonly EventHandlerController Parent; - - public Twitch(EventHandlerController eventHandler) - { - Parent = eventHandler; - } - public event EventHandler? OnTwitchCommandSendMessage; public event EventHandler? OnTwitchCommandSendWhisper; @@ -54,7 +47,7 @@ private IEventHandler.HandledStatus OnEvent(EventHandler? onEven { if (onEvent != null) { - onEvent.Invoke(Parent, args); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } return IEventHandler.HandledStatus.UseDefault; diff --git a/Components/UI/EventHandler/UIEventHandler.cs b/Components/UI/EventHandler/UIEventHandler.cs index ba8e1a54..e34b05b8 100644 --- a/Components/UI/EventHandler/UIEventHandler.cs +++ b/Components/UI/EventHandler/UIEventHandler.cs @@ -8,13 +8,6 @@ namespace Slipstream.Components.UI.EventHandler { internal class UIEventHandler : IEventHandler { - private readonly EventHandlerController Parent; - - public UIEventHandler(EventHandlerController eventHandler) - { - Parent = eventHandler; - } - public event EventHandler? OnUIButtonTriggered; public event EventHandler? OnUICommandCreateButton; @@ -39,7 +32,7 @@ private IEventHandler.HandledStatus OnEvent(EventHandler? onEven { if (onEvent != null) { - onEvent.Invoke(Parent, args); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } return IEventHandler.HandledStatus.UseDefault; diff --git a/Shared/EventHandlerControllerBuilder.cs b/Shared/EventHandlerControllerBuilder.cs index adaa9421..c53d33f4 100644 --- a/Shared/EventHandlerControllerBuilder.cs +++ b/Shared/EventHandlerControllerBuilder.cs @@ -20,7 +20,7 @@ public IEventHandlerController CreateEventHandlerController() foreach (var t in EventHandlers) { - var instance = (IEventHandler)Activator.CreateInstance(t, new object[] { controller }); + var instance = (IEventHandler)Activator.CreateInstance(t); controller.Add(instance); } From d0f0a64aca88dd22fa0cff82c99f10632fa2328b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Thu, 1 Apr 2021 15:42:01 +0200 Subject: [PATCH 6/9] Removes EventHandlerControllerBuilder - uses Autofac --- Backend/Engine.cs | 4 +-- Backend/LifetimeScopeExtensions.cs | 21 +++++++++++++ Backend/PluginManager.cs | 14 +++++---- .../AppilcationUpdate/ApplicationUpdate.cs | 4 +-- Components/Audio/Audio.cs | 1 - Components/ComponentPluginCreationContext.cs | 9 +++--- Components/ComponentRegistrator.cs | 12 +------ Components/Discord/Discord.cs | 1 - Components/FileMonitor/FileMonitor.cs | 1 - Components/IComponentPluginDependencies.cs | 1 + Components/IComponentRegistrationContext.cs | 6 +--- Components/IRacing/IRacing.cs | 1 - Components/Internal/Internal.cs | 1 - Components/Lua/Lua.cs | 1 - Components/Playback/Playback.cs | 1 - Components/Twitch/Twitch.cs | 1 - Components/UI/UI.cs | 1 - Program.cs | 10 +++++- Shared/EventHandlerController.cs | 15 ++++++--- Shared/EventHandlerControllerBuilder.cs | 31 ------------------- Slipstream.csproj | 2 +- 21 files changed, 60 insertions(+), 78 deletions(-) create mode 100644 Backend/LifetimeScopeExtensions.cs delete mode 100644 Shared/EventHandlerControllerBuilder.cs diff --git a/Backend/Engine.cs b/Backend/Engine.cs index 93dc72d7..1f1a3ec5 100644 --- a/Backend/Engine.cs +++ b/Backend/Engine.cs @@ -23,14 +23,14 @@ internal class Engine : IEngine, IDisposable private readonly IEventHandlerController EventHandlerController; private bool Stopped = false; - public Engine(ILogger logger, IEventFactory eventFactory, IEventBus eventBus, IPluginFactory pluginFactory, IPluginManager pluginManager, EventHandlerControllerBuilder eventHandlerControllerBuilder) + public Engine(ILogger logger, IEventFactory eventFactory, IEventBus eventBus, IPluginFactory pluginFactory, IPluginManager pluginManager, IEventHandlerController eventHandlerController) { EventFactory = eventFactory.Get(); EventBus = eventBus; PluginFactory = pluginFactory; PluginManager = pluginManager; Logger = logger; - EventHandlerController = eventHandlerControllerBuilder.CreateEventHandlerController(); + EventHandlerController = eventHandlerController; Subscription = EventBus.RegisterListener(); diff --git a/Backend/LifetimeScopeExtensions.cs b/Backend/LifetimeScopeExtensions.cs new file mode 100644 index 00000000..0b80b750 --- /dev/null +++ b/Backend/LifetimeScopeExtensions.cs @@ -0,0 +1,21 @@ +using Autofac; +using Autofac.Core; +using Autofac.Core.Activators.Reflection; +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Slipstream.Backend +{ + public static class LifetimeScopeExtensions + { + public static IEnumerable GetImplementingTypes(this ILifetimeScope scope) + { + return scope.ComponentRegistry + .RegistrationsFor(new TypedService(typeof(T))) + .Select(x => x.Activator) + .OfType() + .Select(x => x.LimitType); + } + } +} \ No newline at end of file diff --git a/Backend/PluginManager.cs b/Backend/PluginManager.cs index 42cf4e05..91aa1d8a 100644 --- a/Backend/PluginManager.cs +++ b/Backend/PluginManager.cs @@ -1,5 +1,6 @@ #nullable enable +using Autofac; using Serilog; using Slipstream.Components; using Slipstream.Components.Internal; @@ -19,6 +20,7 @@ public class PluginManager : IPluginManager, IPluginFactory private readonly IEventBus EventBus; private readonly IDictionary PluginWorkers = new Dictionary(); private readonly ILogger Logger; + private readonly ILifetimeScope LifetimeScope; private readonly ComponentRegistrator Registrator; private readonly List LuaGluesFactories = new List(); private readonly Dictionary> ComponentPlugins = new Dictionary>(); @@ -27,17 +29,15 @@ public PluginManager( IEventFactory eventFactory, IEventBus eventBus, ILogger logger, - EventHandlerControllerBuilder eventHandlerControllerBuilder, - IEventSerdeService eventSerdeService) + IEventSerdeService eventSerdeService, + ILifetimeScope lifetimeScope) { Registrator = new ComponentRegistrator( ComponentPlugins, LuaGluesFactories, eventFactory, logger, - eventBus, - eventHandlerControllerBuilder - ); + eventBus); foreach (var type in typeof(PluginManager).Assembly.GetTypes().Where(t => typeof(IComponent).IsAssignableFrom(t) && !t.IsAbstract && t.IsClass)) { @@ -49,6 +49,7 @@ public PluginManager( EventSerdeService = eventSerdeService; EventBus = eventBus; Logger = logger; + LifetimeScope = lifetimeScope; } public void UnregisterPlugin(IPlugin p) @@ -148,7 +149,8 @@ public IPlugin CreatePlugin(string pluginId, string pluginName, IEventBus eventB pluginId, pluginName, configuration, - EventSerdeService + EventSerdeService, + LifetimeScope.Resolve() ); if (!ComponentPlugins.ContainsKey(pluginName)) throw new KeyNotFoundException($"Plugin name '{pluginName}' not found"); diff --git a/Components/AppilcationUpdate/ApplicationUpdate.cs b/Components/AppilcationUpdate/ApplicationUpdate.cs index aabc40e4..2e7939c6 100644 --- a/Components/AppilcationUpdate/ApplicationUpdate.cs +++ b/Components/AppilcationUpdate/ApplicationUpdate.cs @@ -5,12 +5,12 @@ namespace Slipstream.Components.AppilcationUpdate public class ApplicationUpdate : IComponent { private const string NAME = nameof(ApplicationUpdatePlugin); + void IComponent.Register(IComponentRegistrationContext ctx) { var eventFactory = new EventFactory.ApplicationUpdateEventFactory(); ctx.RegisterEventFactory(typeof(IApplicationUpdateEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.ApplicationUpdateEventHandler)); ctx.RegisterPlugin(NAME, CreatePlugin); } @@ -26,4 +26,4 @@ private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) ); } } -} +} \ No newline at end of file diff --git a/Components/Audio/Audio.cs b/Components/Audio/Audio.cs index eec0b247..784b4402 100644 --- a/Components/Audio/Audio.cs +++ b/Components/Audio/Audio.cs @@ -13,7 +13,6 @@ public void Register(IComponentRegistrationContext ctx) ctx.RegisterPlugin(NAME, CreateAudioPlugin); ctx.RegisterEventFactory(typeof(IAudioEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.AudioEventHandler)); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } diff --git a/Components/ComponentPluginCreationContext.cs b/Components/ComponentPluginCreationContext.cs index ac1ab0b5..0e0a928c 100644 --- a/Components/ComponentPluginCreationContext.cs +++ b/Components/ComponentPluginCreationContext.cs @@ -11,10 +11,7 @@ internal class ComponentPluginCreationContext : IComponentPluginCreationContext { private readonly ComponentRegistrator ComponentRegistration; - public IEventHandlerController EventHandlerController - { - get { return ComponentRegistration.EventHandlerControllerBuilder.CreateEventHandlerController(); } - } + public IEventHandlerController EventHandlerController { get; } public ILogger Logger { @@ -55,7 +52,8 @@ public ComponentPluginCreationContext( string pluginId, string pluginName, Parameters pluginParameters, - IEventSerdeService eventSerdeService) + IEventSerdeService eventSerdeService, + IEventHandlerController eventHandlerController) { ComponentRegistration = componentRegistration; PluginManager = pluginManager; @@ -65,6 +63,7 @@ public ComponentPluginCreationContext( PluginName = pluginName; PluginParameters = pluginParameters; EventSerdeService = eventSerdeService; + EventHandlerController = eventHandlerController; Lua = new NLua.Lua(); } } diff --git a/Components/ComponentRegistrator.cs b/Components/ComponentRegistrator.cs index 09994507..a5a657a9 100644 --- a/Components/ComponentRegistrator.cs +++ b/Components/ComponentRegistrator.cs @@ -18,20 +18,15 @@ internal class ComponentRegistrator : IComponentRegistrationContext public IEventFactory EventFactory { get; internal set; } - public EventHandlerControllerBuilder EventHandlerControllerBuilder { get; } - public ComponentRegistrator( Dictionary> plugins, List luaGlueFactories, IEventFactory eventFactory, ILogger logger, - IEventBus eventBus, - EventHandlerControllerBuilder eventHandlerControllerBuilder - ) + IEventBus eventBus) { Plugins = plugins; LuaGlueFactories = luaGlueFactories; - EventHandlerControllerBuilder = eventHandlerControllerBuilder; EventFactory = eventFactory; Logger = logger; EventBus = eventBus; @@ -42,11 +37,6 @@ public void RegisterEventFactory(Type type, T factory) EventFactory.Add(type, factory); } - public void RegisterEventHandler(Type type) - { - EventHandlerControllerBuilder.Add(type); - } - public void RegisterPlugin(string name, Func plugin) { Plugins.Add(name, plugin); diff --git a/Components/Discord/Discord.cs b/Components/Discord/Discord.cs index 63c3da34..3b07cd82 100644 --- a/Components/Discord/Discord.cs +++ b/Components/Discord/Discord.cs @@ -9,7 +9,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.DiscordEventFactory(); ctx.RegisterPlugin("DiscordPlugin", CreatePlugin); - ctx.RegisterEventHandler(typeof(EventHandler.DiscordEventHandler)); ctx.RegisterEventFactory(typeof(IDiscordEventFactory), eventFactory); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } diff --git a/Components/FileMonitor/FileMonitor.cs b/Components/FileMonitor/FileMonitor.cs index 58e13d05..26efbd3b 100644 --- a/Components/FileMonitor/FileMonitor.cs +++ b/Components/FileMonitor/FileMonitor.cs @@ -13,7 +13,6 @@ public void Register(IComponentRegistrationContext ctx) ctx.RegisterPlugin(NAME, CreatePlugin); ctx.RegisterEventFactory(typeof(IFileMonitorEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.FileMonitor)); } private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) diff --git a/Components/IComponentPluginDependencies.cs b/Components/IComponentPluginDependencies.cs index c25bb364..77909048 100644 --- a/Components/IComponentPluginDependencies.cs +++ b/Components/IComponentPluginDependencies.cs @@ -7,6 +7,7 @@ namespace Slipstream.Components internal interface IComponentPluginDependencies { IEventHandlerController EventHandlerController { get; } + ILogger Logger { get; } IEventBus EventBus { get; } IEventFactory EventFactory { get; } diff --git a/Components/IComponentRegistrationContext.cs b/Components/IComponentRegistrationContext.cs index 4e141c25..030228ed 100644 --- a/Components/IComponentRegistrationContext.cs +++ b/Components/IComponentRegistrationContext.cs @@ -1,5 +1,4 @@ using Serilog; -using Slipstream.Backend; using Slipstream.Shared; using System; @@ -8,16 +7,13 @@ namespace Slipstream.Components internal interface IComponentRegistrationContext { public ILogger Logger { get; } - public IEventBus EventBus { get; } - public EventHandlerControllerBuilder EventHandlerControllerBuilder { get; } + public IEventBus EventBus { get; } public void RegisterPlugin(string name, Func plugin); public void RegisterEventFactory(Type type, T factory); - public void RegisterEventHandler(Type type); - void RegisterLuaGlue(ILuaGlueFactory luaGlueFactory); } } \ No newline at end of file diff --git a/Components/IRacing/IRacing.cs b/Components/IRacing/IRacing.cs index 2fd80453..8c253dbf 100644 --- a/Components/IRacing/IRacing.cs +++ b/Components/IRacing/IRacing.cs @@ -13,7 +13,6 @@ public void Register(IComponentRegistrationContext ctx) ctx.RegisterPlugin(NAME, CreatePlugin); ctx.RegisterEventFactory(typeof(IIRacingEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.IRacing)); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } diff --git a/Components/Internal/Internal.cs b/Components/Internal/Internal.cs index afc15c25..07e530ac 100644 --- a/Components/Internal/Internal.cs +++ b/Components/Internal/Internal.cs @@ -9,7 +9,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.InternalEventFactory(); ctx.RegisterEventFactory(typeof(IInternalEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.Internal)); ctx.RegisterLuaGlue(new LuaGlues.CoreLuaGlueFactory(new EventSerdeService())); ctx.RegisterLuaGlue(new LuaGlues.HttpLuaGlueFactory(ctx.Logger)); ctx.RegisterLuaGlue(new LuaGlues.InternalLuaGlueFactory(ctx.EventBus, eventFactory)); diff --git a/Components/Lua/Lua.cs b/Components/Lua/Lua.cs index bb47b7a2..87db2b72 100644 --- a/Components/Lua/Lua.cs +++ b/Components/Lua/Lua.cs @@ -15,7 +15,6 @@ public void Register(IComponentRegistrationContext ctx) ctx.RegisterPlugin("LuaPlugin", CreateLuaPlugin); ctx.RegisterPlugin("LuaManagerPlugin", CreateLuaManagerPlugin); ctx.RegisterEventFactory(typeof(ILuaEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.Lua)); } private IPlugin CreateLuaManagerPlugin(IComponentPluginCreationContext ctx) diff --git a/Components/Playback/Playback.cs b/Components/Playback/Playback.cs index e879ac02..5fe31454 100644 --- a/Components/Playback/Playback.cs +++ b/Components/Playback/Playback.cs @@ -12,7 +12,6 @@ public void Register(IComponentRegistrationContext ctx) ctx.RegisterPlugin("PlaybackPlugin", CreatePlugin); ctx.RegisterEventFactory(typeof(IPlaybackEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.Playback)); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } diff --git a/Components/Twitch/Twitch.cs b/Components/Twitch/Twitch.cs index 8fd5cc40..aa580854 100644 --- a/Components/Twitch/Twitch.cs +++ b/Components/Twitch/Twitch.cs @@ -11,7 +11,6 @@ public void Register(IComponentRegistrationContext ctx) ctx.RegisterPlugin("TwitchPlugin", CreatePlugin); ctx.RegisterEventFactory(typeof(ITwitchEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.Twitch)); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } diff --git a/Components/UI/UI.cs b/Components/UI/UI.cs index 93fe2e5c..2ebc1ceb 100644 --- a/Components/UI/UI.cs +++ b/Components/UI/UI.cs @@ -7,7 +7,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.UIEventFactory(); ctx.RegisterEventFactory(typeof(IUIEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.UIEventHandler)); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.Logger, ctx.EventBus, eventFactory)); } diff --git a/Program.cs b/Program.cs index 045b3207..f650743c 100644 --- a/Program.cs +++ b/Program.cs @@ -48,7 +48,6 @@ private static void ConfigureServices(ContainerBuilder builder) builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().As().SingleInstance(); - builder.RegisterType().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); @@ -63,6 +62,15 @@ private static void ConfigureServices(ContainerBuilder builder) builder.RegisterInstance(logger).As().SingleInstance(); builder.RegisterInstance(sink).As().SingleInstance(); + + // EventHandlers + builder.RegisterAssemblyTypes(typeof(Program).Assembly) + .Where(t => t.IsAssignableTo()) + .Where(t => !t.IsAbstract) + .As() + .AsSelf() + .InstancePerDependency(); + builder.RegisterType().As().InstancePerDependency(); } private class PopulateSink diff --git a/Shared/EventHandlerController.cs b/Shared/EventHandlerController.cs index 73b5643f..f873dbd0 100644 --- a/Shared/EventHandlerController.cs +++ b/Shared/EventHandlerController.cs @@ -1,4 +1,6 @@ -using System; +using Autofac; +using Slipstream.Backend; +using System; using System.Collections.Generic; using static Slipstream.Shared.IEventHandlerController; @@ -8,16 +10,19 @@ namespace Slipstream.Shared { public class EventHandlerController : IEventHandlerController { - internal readonly IDictionary Handlers = new Dictionary(); + private readonly IDictionary Handlers = new Dictionary(); private volatile bool enabled = true; public bool Enabled { get { return enabled; } set { enabled = value; } } public event EventHandler? OnDefault; - internal void Add(Type handlerInterface, IEventHandler implementation) + public EventHandlerController(ILifetimeScope scope) { - Handlers.Add(handlerInterface, implementation); + foreach (var h in scope.GetImplementingTypes()) + { + Add((IEventHandler)scope.Resolve(h)); + } } public T Get() @@ -28,7 +33,7 @@ public T Get() return (T)Handlers[typeof(T)]; } - internal void Add(IEventHandler eventHandler) + private void Add(IEventHandler eventHandler) { Handlers.Add(eventHandler.GetType(), eventHandler); } diff --git a/Shared/EventHandlerControllerBuilder.cs b/Shared/EventHandlerControllerBuilder.cs deleted file mode 100644 index c53d33f4..00000000 --- a/Shared/EventHandlerControllerBuilder.cs +++ /dev/null @@ -1,31 +0,0 @@ -#nullable enable - -using System; -using System.Collections.Generic; - -namespace Slipstream.Shared -{ - public class EventHandlerControllerBuilder - { - private readonly List EventHandlers = new List(); - - public void Add(Type t) - { - EventHandlers.Add(t); - } - - public IEventHandlerController CreateEventHandlerController() - { - var controller = new EventHandlerController(); - - foreach (var t in EventHandlers) - { - var instance = (IEventHandler)Activator.CreateInstance(t); - - controller.Add(instance); - } - - return controller; - } - } -} \ No newline at end of file diff --git a/Slipstream.csproj b/Slipstream.csproj index e4abd0ef..88ecb1c9 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -201,6 +201,7 @@ + @@ -290,7 +291,6 @@ - From 41b47cd3dff5bd4a5e38145cc2be0667ff0b5e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Thu, 1 Apr 2021 16:31:09 +0200 Subject: [PATCH 7/9] Replace EventFactory with AutoFac --- Backend/Engine.cs | 4 +- Backend/PluginManager.cs | 29 ++++++++-- .../AppilcationUpdate/ApplicationUpdate.cs | 5 +- Components/Audio/Audio.cs | 3 +- Components/ComponentPluginCreationContext.cs | 56 +++++++++++++++++-- Components/ComponentRegistrator.cs | 9 --- Components/Discord/Discord.cs | 3 +- Components/FileMonitor/FileMonitor.cs | 8 +-- Components/IComponentPluginCreationContext.cs | 19 +++++++ Components/IComponentPluginDependencies.cs | 1 - Components/IComponentRegistrationContext.cs | 2 - Components/IRacing/IRacing.cs | 3 +- Components/Internal/Internal.cs | 5 +- Components/Lua/Lua.cs | 12 ++-- Components/Lua/Plugins/LuaPlugin.cs | 7 ++- Components/Playback/Playback.cs | 1 - Components/Twitch/Twitch.cs | 3 +- Components/Txrx/Txrx.cs | 4 +- Components/UI/UI.cs | 2 - Components/WinFormUI/Forms/MainWindow.cs | 8 +-- .../WinFormUI/Plugins/WinFormUIPlugin.cs | 15 +++-- Components/WinFormUI/WinFormUI.cs | 4 +- Program.cs | 9 ++- Shared/EventFactory.cs | 22 -------- Shared/IEventFactory.cs | 11 ---- Slipstream.csproj | 2 - 26 files changed, 137 insertions(+), 110 deletions(-) delete mode 100644 Shared/EventFactory.cs delete mode 100644 Shared/IEventFactory.cs diff --git a/Backend/Engine.cs b/Backend/Engine.cs index 1f1a3ec5..d67256d4 100644 --- a/Backend/Engine.cs +++ b/Backend/Engine.cs @@ -23,9 +23,9 @@ internal class Engine : IEngine, IDisposable private readonly IEventHandlerController EventHandlerController; private bool Stopped = false; - public Engine(ILogger logger, IEventFactory eventFactory, IEventBus eventBus, IPluginFactory pluginFactory, IPluginManager pluginManager, IEventHandlerController eventHandlerController) + public Engine(ILogger logger, IInternalEventFactory eventFactory, IEventBus eventBus, IPluginFactory pluginFactory, IPluginManager pluginManager, IEventHandlerController eventHandlerController) { - EventFactory = eventFactory.Get(); + EventFactory = eventFactory; EventBus = eventBus; PluginFactory = pluginFactory; PluginManager = pluginManager; diff --git a/Backend/PluginManager.cs b/Backend/PluginManager.cs index 91aa1d8a..db508b94 100644 --- a/Backend/PluginManager.cs +++ b/Backend/PluginManager.cs @@ -3,7 +3,16 @@ using Autofac; using Serilog; using Slipstream.Components; +using Slipstream.Components.AppilcationUpdate; +using Slipstream.Components.Audio; +using Slipstream.Components.Discord; +using Slipstream.Components.FileMonitor; using Slipstream.Components.Internal; +using Slipstream.Components.IRacing; +using Slipstream.Components.Lua; +using Slipstream.Components.Playback; +using Slipstream.Components.Twitch; +using Slipstream.Components.UI; using Slipstream.Shared; using Slipstream.Shared.Helpers.StrongParameters; using System; @@ -26,16 +35,16 @@ public class PluginManager : IPluginManager, IPluginFactory private readonly Dictionary> ComponentPlugins = new Dictionary>(); public PluginManager( - IEventFactory eventFactory, + IInternalEventFactory internalEventFactory, IEventBus eventBus, ILogger logger, IEventSerdeService eventSerdeService, - ILifetimeScope lifetimeScope) + ILifetimeScope lifetimeScope + ) { Registrator = new ComponentRegistrator( ComponentPlugins, LuaGluesFactories, - eventFactory, logger, eventBus); @@ -45,7 +54,7 @@ public PluginManager( ((IComponent)Activator.CreateInstance(type)).Register(Registrator); } - InternalEventFactory = eventFactory.Get(); + InternalEventFactory = internalEventFactory; EventSerdeService = eventSerdeService; EventBus = eventBus; Logger = logger; @@ -150,7 +159,17 @@ public IPlugin CreatePlugin(string pluginId, string pluginName, IEventBus eventB pluginName, configuration, EventSerdeService, - LifetimeScope.Resolve() + LifetimeScope.Resolve(), + LifetimeScope.Resolve(), + LifetimeScope.Resolve(), + LifetimeScope.Resolve(), + LifetimeScope.Resolve(), + LifetimeScope.Resolve(), + LifetimeScope.Resolve(), + LifetimeScope.Resolve(), + LifetimeScope.Resolve(), + LifetimeScope.Resolve(), + LifetimeScope.Resolve() ); if (!ComponentPlugins.ContainsKey(pluginName)) throw new KeyNotFoundException($"Plugin name '{pluginName}' not found"); diff --git a/Components/AppilcationUpdate/ApplicationUpdate.cs b/Components/AppilcationUpdate/ApplicationUpdate.cs index 2e7939c6..767c9fa1 100644 --- a/Components/AppilcationUpdate/ApplicationUpdate.cs +++ b/Components/AppilcationUpdate/ApplicationUpdate.cs @@ -8,9 +8,6 @@ public class ApplicationUpdate : IComponent void IComponent.Register(IComponentRegistrationContext ctx) { - var eventFactory = new EventFactory.ApplicationUpdateEventFactory(); - - ctx.RegisterEventFactory(typeof(IApplicationUpdateEventFactory), eventFactory); ctx.RegisterPlugin(NAME, CreatePlugin); } @@ -19,7 +16,7 @@ private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) return new ApplicationUpdatePlugin( ctx.EventHandlerController, ctx.PluginId, - ctx.EventFactory.Get(), + ctx.ApplicationUpdateEventFactory, ctx.Logger.ForContext(typeof(ApplicationUpdate)), ctx.EventBus, ctx.PluginParameters diff --git a/Components/Audio/Audio.cs b/Components/Audio/Audio.cs index 784b4402..8c4b3719 100644 --- a/Components/Audio/Audio.cs +++ b/Components/Audio/Audio.cs @@ -12,7 +12,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.AudioEventFactory(); ctx.RegisterPlugin(NAME, CreateAudioPlugin); - ctx.RegisterEventFactory(typeof(IAudioEventFactory), eventFactory); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } @@ -23,7 +22,7 @@ private IPlugin CreateAudioPlugin(IComponentPluginCreationContext ctx) ctx.PluginId, ctx.Logger, ctx.EventBus, - ctx.EventFactory.Get(), + ctx.AudioEventFactory, ctx.PluginParameters ); } diff --git a/Components/ComponentPluginCreationContext.cs b/Components/ComponentPluginCreationContext.cs index 0e0a928c..cd2f5b76 100644 --- a/Components/ComponentPluginCreationContext.cs +++ b/Components/ComponentPluginCreationContext.cs @@ -1,6 +1,15 @@ using Serilog; using Slipstream.Backend; +using Slipstream.Components.AppilcationUpdate; +using Slipstream.Components.Audio; +using Slipstream.Components.Discord; +using Slipstream.Components.FileMonitor; using Slipstream.Components.Internal; +using Slipstream.Components.IRacing; +using Slipstream.Components.Lua; +using Slipstream.Components.Playback; +using Slipstream.Components.Twitch; +using Slipstream.Components.UI; using Slipstream.Shared; using Slipstream.Shared.Helpers.StrongParameters; using System.Collections.Generic; @@ -23,11 +32,6 @@ public IEventBus EventBus get { return ComponentRegistration.EventBus; } } - public IEventFactory EventFactory - { - get { return ComponentRegistration.EventFactory; } - } - public string PluginId { get; } public string PluginName { get; } @@ -44,6 +48,26 @@ public IEventFactory EventFactory public IEventSerdeService EventSerdeService { get; } + public IInternalEventFactory InternalEventFactory { get; } + + public IUIEventFactory UIEventFactory { get; } + + public IPlaybackEventFactory PlaybackEventFactory { get; } + + public ILuaEventFactory LuaEventFactory { get; } + + public IApplicationUpdateEventFactory ApplicationUpdateEventFactory { get; } + + public IFileMonitorEventFactory FileMonitorEventFactory { get; } + + public IAudioEventFactory AudioEventFactory { get; } + + public IDiscordEventFactory DiscordEventFactory { get; } + + public IIRacingEventFactory IRacingEventFactory { get; } + + public ITwitchEventFactory TwitchEventFactory { get; } + public ComponentPluginCreationContext( ComponentRegistrator componentRegistration, IPluginManager pluginManager, @@ -53,7 +77,17 @@ public ComponentPluginCreationContext( string pluginName, Parameters pluginParameters, IEventSerdeService eventSerdeService, - IEventHandlerController eventHandlerController) + IEventHandlerController eventHandlerController, + IInternalEventFactory internalEventFactory, + IUIEventFactory uiEventFactory, + IPlaybackEventFactory playbackEventFactory, + ILuaEventFactory luaEventFactory, + IApplicationUpdateEventFactory applicationUpdateEventFactory, + IFileMonitorEventFactory fileMonitorEventFactory, + IAudioEventFactory audioEventFactory, + IDiscordEventFactory discordEventFactory, + IIRacingEventFactory iRacingEventFactory, + ITwitchEventFactory twitchEventFactory) { ComponentRegistration = componentRegistration; PluginManager = pluginManager; @@ -64,6 +98,16 @@ public ComponentPluginCreationContext( PluginParameters = pluginParameters; EventSerdeService = eventSerdeService; EventHandlerController = eventHandlerController; + InternalEventFactory = internalEventFactory; + UIEventFactory = uiEventFactory; + PlaybackEventFactory = playbackEventFactory; + LuaEventFactory = luaEventFactory; + ApplicationUpdateEventFactory = applicationUpdateEventFactory; + FileMonitorEventFactory = fileMonitorEventFactory; + AudioEventFactory = audioEventFactory; + DiscordEventFactory = discordEventFactory; + IRacingEventFactory = iRacingEventFactory; + TwitchEventFactory = twitchEventFactory; Lua = new NLua.Lua(); } } diff --git a/Components/ComponentRegistrator.cs b/Components/ComponentRegistrator.cs index a5a657a9..a439020d 100644 --- a/Components/ComponentRegistrator.cs +++ b/Components/ComponentRegistrator.cs @@ -16,27 +16,18 @@ internal class ComponentRegistrator : IComponentRegistrationContext public IEventBus EventBus { get; internal set; } - public IEventFactory EventFactory { get; internal set; } - public ComponentRegistrator( Dictionary> plugins, List luaGlueFactories, - IEventFactory eventFactory, ILogger logger, IEventBus eventBus) { Plugins = plugins; LuaGlueFactories = luaGlueFactories; - EventFactory = eventFactory; Logger = logger; EventBus = eventBus; } - public void RegisterEventFactory(Type type, T factory) - { - EventFactory.Add(type, factory); - } - public void RegisterPlugin(string name, Func plugin) { Plugins.Add(name, plugin); diff --git a/Components/Discord/Discord.cs b/Components/Discord/Discord.cs index 3b07cd82..bdb4ef57 100644 --- a/Components/Discord/Discord.cs +++ b/Components/Discord/Discord.cs @@ -9,7 +9,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.DiscordEventFactory(); ctx.RegisterPlugin("DiscordPlugin", CreatePlugin); - ctx.RegisterEventFactory(typeof(IDiscordEventFactory), eventFactory); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } @@ -19,7 +18,7 @@ private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) ctx.EventHandlerController, ctx.PluginId, ctx.EventBus, - ctx.EventFactory.Get(), + ctx.DiscordEventFactory, ctx.PluginParameters ); } diff --git a/Components/FileMonitor/FileMonitor.cs b/Components/FileMonitor/FileMonitor.cs index 26efbd3b..d9d66049 100644 --- a/Components/FileMonitor/FileMonitor.cs +++ b/Components/FileMonitor/FileMonitor.cs @@ -1,5 +1,4 @@ -using Slipstream.Backend; -using Slipstream.Components.FileMonitor.Plugins; +using Slipstream.Components.FileMonitor.Plugins; namespace Slipstream.Components.FileMonitor { @@ -9,10 +8,7 @@ internal class FileMonitor : IComponent public void Register(IComponentRegistrationContext ctx) { - var eventFactory = new EventFactory.FileMonitorEventFactory(); - ctx.RegisterPlugin(NAME, CreatePlugin); - ctx.RegisterEventFactory(typeof(IFileMonitorEventFactory), eventFactory); } private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) @@ -20,7 +16,7 @@ private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) return new FileMonitorPlugin( ctx.EventHandlerController, ctx.PluginId, - ctx.EventFactory.Get(), + ctx.FileMonitorEventFactory, ctx.EventBus, ctx.PluginParameters ); diff --git a/Components/IComponentPluginCreationContext.cs b/Components/IComponentPluginCreationContext.cs index 2eb09180..d41ca190 100644 --- a/Components/IComponentPluginCreationContext.cs +++ b/Components/IComponentPluginCreationContext.cs @@ -1,5 +1,14 @@ using Slipstream.Backend; +using Slipstream.Components.AppilcationUpdate; +using Slipstream.Components.Audio; +using Slipstream.Components.Discord; +using Slipstream.Components.FileMonitor; using Slipstream.Components.Internal; +using Slipstream.Components.IRacing; +using Slipstream.Components.Lua; +using Slipstream.Components.Playback; +using Slipstream.Components.Twitch; +using Slipstream.Components.UI; using Slipstream.Shared.Helpers.StrongParameters; using System.Collections.Generic; @@ -14,5 +23,15 @@ internal interface IComponentPluginCreationContext : IComponentPluginDependencie IPluginManager PluginManager { get; } IPluginFactory PluginFactory { get; } IEventSerdeService EventSerdeService { get; } + IInternalEventFactory InternalEventFactory { get; } + IUIEventFactory UIEventFactory { get; } + IPlaybackEventFactory PlaybackEventFactory { get; } + ILuaEventFactory LuaEventFactory { get; } + IApplicationUpdateEventFactory ApplicationUpdateEventFactory { get; } + IFileMonitorEventFactory FileMonitorEventFactory { get; } + IAudioEventFactory AudioEventFactory { get; } + IDiscordEventFactory DiscordEventFactory { get; } + IIRacingEventFactory IRacingEventFactory { get; } + ITwitchEventFactory TwitchEventFactory { get; } } } \ No newline at end of file diff --git a/Components/IComponentPluginDependencies.cs b/Components/IComponentPluginDependencies.cs index 77909048..a13490ae 100644 --- a/Components/IComponentPluginDependencies.cs +++ b/Components/IComponentPluginDependencies.cs @@ -10,6 +10,5 @@ internal interface IComponentPluginDependencies ILogger Logger { get; } IEventBus EventBus { get; } - IEventFactory EventFactory { get; } } } \ No newline at end of file diff --git a/Components/IComponentRegistrationContext.cs b/Components/IComponentRegistrationContext.cs index 030228ed..7eca11e0 100644 --- a/Components/IComponentRegistrationContext.cs +++ b/Components/IComponentRegistrationContext.cs @@ -12,8 +12,6 @@ internal interface IComponentRegistrationContext public void RegisterPlugin(string name, Func plugin); - public void RegisterEventFactory(Type type, T factory); - void RegisterLuaGlue(ILuaGlueFactory luaGlueFactory); } } \ No newline at end of file diff --git a/Components/IRacing/IRacing.cs b/Components/IRacing/IRacing.cs index 8c253dbf..ef2a7300 100644 --- a/Components/IRacing/IRacing.cs +++ b/Components/IRacing/IRacing.cs @@ -12,7 +12,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.IRacingEventFactory(); ctx.RegisterPlugin(NAME, CreatePlugin); - ctx.RegisterEventFactory(typeof(IIRacingEventFactory), eventFactory); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } @@ -21,7 +20,7 @@ private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) return new IRacingPlugin( ctx.EventHandlerController, ctx.PluginId, - ctx.EventFactory.Get(), + ctx.IRacingEventFactory, ctx.EventBus, ctx.PluginParameters ); diff --git a/Components/Internal/Internal.cs b/Components/Internal/Internal.cs index 07e530ac..42ded6f4 100644 --- a/Components/Internal/Internal.cs +++ b/Components/Internal/Internal.cs @@ -6,12 +6,9 @@ internal class Internal : IComponent { public void Register(IComponentRegistrationContext ctx) { - var eventFactory = new EventFactory.InternalEventFactory(); - - ctx.RegisterEventFactory(typeof(IInternalEventFactory), eventFactory); ctx.RegisterLuaGlue(new LuaGlues.CoreLuaGlueFactory(new EventSerdeService())); ctx.RegisterLuaGlue(new LuaGlues.HttpLuaGlueFactory(ctx.Logger)); - ctx.RegisterLuaGlue(new LuaGlues.InternalLuaGlueFactory(ctx.EventBus, eventFactory)); + ctx.RegisterLuaGlue(new LuaGlues.InternalLuaGlueFactory(ctx.EventBus, new EventFactory.InternalEventFactory())); ctx.RegisterLuaGlue(new LuaGlues.StateLuaGlueFactory(new StateService(ctx.Logger, "state.txt"))); } } diff --git a/Components/Lua/Lua.cs b/Components/Lua/Lua.cs index 87db2b72..a1a864bd 100644 --- a/Components/Lua/Lua.cs +++ b/Components/Lua/Lua.cs @@ -1,6 +1,4 @@ -using Slipstream.Backend; -using Slipstream.Components.FileMonitor; -using Slipstream.Components.Internal.Services; +using Slipstream.Components.Internal.Services; using Slipstream.Components.Lua.Plugins; using System.Collections.Generic; @@ -10,11 +8,8 @@ internal class Lua : IComponent { public void Register(IComponentRegistrationContext ctx) { - var eventFactory = new EventFactory.LuaEventFactory(new EventSerdeService()); - ctx.RegisterPlugin("LuaPlugin", CreateLuaPlugin); ctx.RegisterPlugin("LuaManagerPlugin", CreateLuaManagerPlugin); - ctx.RegisterEventFactory(typeof(ILuaEventFactory), eventFactory); } private IPlugin CreateLuaManagerPlugin(IComponentPluginCreationContext ctx) @@ -23,7 +18,7 @@ private IPlugin CreateLuaManagerPlugin(IComponentPluginCreationContext ctx) ctx.EventHandlerController, ctx.PluginId, ctx.Logger.ForContext(typeof(LuaManagerPlugin)), - ctx.EventFactory.Get(), + ctx.FileMonitorEventFactory, ctx.EventBus, ctx.PluginManager, ctx.PluginFactory, @@ -47,7 +42,8 @@ private IPlugin CreateLuaPlugin(IComponentPluginCreationContext ctx) ctx.EventHandlerController, ctx.PluginId, ctx.Logger.ForContext(typeof(LuaPlugin)), - ctx.EventFactory, + ctx.LuaEventFactory, + ctx.InternalEventFactory, ctx.EventBus, luaService, ctx.PluginParameters diff --git a/Components/Lua/Plugins/LuaPlugin.cs b/Components/Lua/Plugins/LuaPlugin.cs index 1e779caf..c68705d0 100644 --- a/Components/Lua/Plugins/LuaPlugin.cs +++ b/Components/Lua/Plugins/LuaPlugin.cs @@ -24,15 +24,16 @@ public LuaPlugin( IEventHandlerController eventHandlerController, string id, ILogger logger, - IEventFactory eventFactory, + ILuaEventFactory luaEventFactory, + IInternalEventFactory internalEventFactory, IEventBus eventBus, ILuaSevice luaService, Parameters configuration ) : base(eventHandlerController, id, "LuaPlugin", id) { Logger = logger; - LuaEventFactory = eventFactory.Get(); - InternalEventFactory = eventFactory.Get(); + LuaEventFactory = luaEventFactory; + InternalEventFactory = internalEventFactory; LuaService = luaService; EventBus = new CapturingEventBus(eventBus); diff --git a/Components/Playback/Playback.cs b/Components/Playback/Playback.cs index 5fe31454..d80928d6 100644 --- a/Components/Playback/Playback.cs +++ b/Components/Playback/Playback.cs @@ -11,7 +11,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.PlaybackEventFactory(); ctx.RegisterPlugin("PlaybackPlugin", CreatePlugin); - ctx.RegisterEventFactory(typeof(IPlaybackEventFactory), eventFactory); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } diff --git a/Components/Twitch/Twitch.cs b/Components/Twitch/Twitch.cs index aa580854..5ed63ad6 100644 --- a/Components/Twitch/Twitch.cs +++ b/Components/Twitch/Twitch.cs @@ -10,7 +10,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.TwitchEventFactory(); ctx.RegisterPlugin("TwitchPlugin", CreatePlugin); - ctx.RegisterEventFactory(typeof(ITwitchEventFactory), eventFactory); ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } @@ -20,7 +19,7 @@ private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) ctx.EventHandlerController, ctx.PluginId, ctx.Logger.ForContext(typeof(Twitch)), - ctx.EventFactory.Get(), + ctx.TwitchEventFactory, ctx.EventBus, ctx.PluginParameters ); diff --git a/Components/Txrx/Txrx.cs b/Components/Txrx/Txrx.cs index 51777338..ddc0fe97 100644 --- a/Components/Txrx/Txrx.cs +++ b/Components/Txrx/Txrx.cs @@ -22,7 +22,7 @@ private IPlugin CreateReceiverPlugin(IComponentPluginCreationContext ctx) ctx.EventHandlerController, ctx.PluginId, ctx.Logger, - ctx.EventFactory.Get(), + ctx.InternalEventFactory, ctx.EventBus, txrxService, ctx.PluginParameters @@ -37,7 +37,7 @@ private IPlugin CreateTransmitterPlugin(IComponentPluginCreationContext ctx) ctx.EventHandlerController, ctx.PluginId, ctx.Logger, - ctx.EventFactory.Get(), + ctx.InternalEventFactory, ctx.EventBus, txrxService, ctx.PluginParameters diff --git a/Components/UI/UI.cs b/Components/UI/UI.cs index 2ebc1ceb..cb71b071 100644 --- a/Components/UI/UI.cs +++ b/Components/UI/UI.cs @@ -6,8 +6,6 @@ public void Register(IComponentRegistrationContext ctx) { var eventFactory = new EventFactory.UIEventFactory(); - ctx.RegisterEventFactory(typeof(IUIEventFactory), eventFactory); - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.Logger, ctx.EventBus, eventFactory)); } } diff --git a/Components/WinFormUI/Forms/MainWindow.cs b/Components/WinFormUI/Forms/MainWindow.cs index be1dd632..04ed385a 100644 --- a/Components/WinFormUI/Forms/MainWindow.cs +++ b/Components/WinFormUI/Forms/MainWindow.cs @@ -33,11 +33,11 @@ public partial class MainWindow : Form private readonly IEventHandlerController EventHandler; private bool ShuttingDown = false; - public MainWindow(IEventFactory eventFactory, IEventBus eventBus, IApplicationVersionService applicationVersionService, IEventHandlerController eventHandlerController) + public MainWindow(IInternalEventFactory internalEventFactory, IUIEventFactory uiEventFactory, IPlaybackEventFactory playbackEventFactory, IEventBus eventBus, IApplicationVersionService applicationVersionService, IEventHandlerController eventHandlerController) { - InternalEventFactory = eventFactory.Get(); - UIEventFactory = eventFactory.Get(); - PlaybackEventFactory = eventFactory.Get(); + InternalEventFactory = internalEventFactory; + UIEventFactory = uiEventFactory; + PlaybackEventFactory = playbackEventFactory; EventHandler = eventHandlerController; diff --git a/Components/WinFormUI/Plugins/WinFormUIPlugin.cs b/Components/WinFormUI/Plugins/WinFormUIPlugin.cs index 22f9e885..a8235550 100644 --- a/Components/WinFormUI/Plugins/WinFormUIPlugin.cs +++ b/Components/WinFormUI/Plugins/WinFormUIPlugin.cs @@ -1,3 +1,6 @@ +using Slipstream.Components.Internal; +using Slipstream.Components.Playback; +using Slipstream.Components.UI; using Slipstream.Components.WinFormUI.Forms; using Slipstream.Shared; using System.Windows.Forms; @@ -8,20 +11,24 @@ namespace Slipstream.Components.WinFormUI.Plugins { internal class WinFormUIPlugin : BasePlugin { - private readonly IEventFactory EventFactory; private readonly IEventBus EventBus; private readonly IApplicationVersionService ApplicationVersionService; + private readonly IInternalEventFactory InternalEventFactory; + private readonly IUIEventFactory UIEventFactory; + private readonly IPlaybackEventFactory PlaybackEventFactory; - public WinFormUIPlugin(IEventHandlerController eventHandlerController, string id, IEventFactory eventFactory, IEventBus eventBus, IApplicationVersionService applicationVersionService) : base(eventHandlerController, id, "WinFormUIPlugin", id, true, true) + public WinFormUIPlugin(IEventHandlerController eventHandlerController, string id, IInternalEventFactory internalEventFactory, IUIEventFactory uiEventFactory, IPlaybackEventFactory playbackEventFactory, IEventBus eventBus, IApplicationVersionService applicationVersionService) : base(eventHandlerController, id, "WinFormUIPlugin", id, true, true) { - EventFactory = eventFactory; + InternalEventFactory = internalEventFactory; + UIEventFactory = uiEventFactory; + PlaybackEventFactory = playbackEventFactory; EventBus = eventBus; ApplicationVersionService = applicationVersionService; } public override void Run() { - Application.Run(new MainWindow(EventFactory, EventBus, ApplicationVersionService, EventHandlerController)); + Application.Run(new MainWindow(InternalEventFactory, UIEventFactory, PlaybackEventFactory, EventBus, ApplicationVersionService, EventHandlerController)); } } } \ No newline at end of file diff --git a/Components/WinFormUI/WinFormUI.cs b/Components/WinFormUI/WinFormUI.cs index a50f7d82..5e27bd9c 100644 --- a/Components/WinFormUI/WinFormUI.cs +++ b/Components/WinFormUI/WinFormUI.cs @@ -17,7 +17,9 @@ private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) return new WinFormUIPlugin( ctx.EventHandlerController, ctx.PluginId, - ctx.EventFactory, + ctx.InternalEventFactory, + ctx.UIEventFactory, + ctx.PlaybackEventFactory, ctx.EventBus, new ApplicationVersionService() ); diff --git a/Program.cs b/Program.cs index f650743c..c986d86d 100644 --- a/Program.cs +++ b/Program.cs @@ -48,7 +48,6 @@ private static void ConfigureServices(ContainerBuilder builder) builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().As().SingleInstance(); - builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().SingleInstance(); @@ -70,15 +69,19 @@ private static void ConfigureServices(ContainerBuilder builder) .As() .AsSelf() .InstancePerDependency(); + builder.RegisterAssemblyTypes(typeof(Program).Assembly) + .Where(t => t.Name.EndsWith("EventFactory")) + .AsImplementedInterfaces() + .SingleInstance(); builder.RegisterType().As().InstancePerDependency(); } private class PopulateSink { - public PopulateSink(SlipstreamConsoleSink sink, IEventBus eventBus, IEventFactory eventFactory) + public PopulateSink(SlipstreamConsoleSink sink, IEventBus eventBus, Components.UI.IUIEventFactory uiEventFactory) { sink.EventBus = eventBus; - sink.EventFactory = new UIEventFactory(); + sink.EventFactory = uiEventFactory; } } } diff --git a/Shared/EventFactory.cs b/Shared/EventFactory.cs deleted file mode 100644 index c12aa044..00000000 --- a/Shared/EventFactory.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace Slipstream.Shared -{ - public class EventFactory : IEventFactory - { - private readonly IDictionary Factories = new Dictionary(); - - public void Add(Type factoryInterface, T factoryImplementation) - { - Factories.Add(factoryInterface, factoryImplementation); - } - - public T Get() - { - if (!Factories.ContainsKey(typeof(T))) - throw new KeyNotFoundException($"No EventFactory '{typeof(T)} found"); - return (T)Factories[typeof(T)]; - } - } -} \ No newline at end of file diff --git a/Shared/IEventFactory.cs b/Shared/IEventFactory.cs deleted file mode 100644 index cf26f5f7..00000000 --- a/Shared/IEventFactory.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System; - -namespace Slipstream.Shared -{ - public interface IEventFactory - { - T Get(); - - void Add(Type factoryInterface, T factoryImplementation); - } -} \ No newline at end of file diff --git a/Slipstream.csproj b/Slipstream.csproj index 88ecb1c9..7a3dd05a 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -329,7 +329,6 @@ - @@ -406,7 +405,6 @@ - From f22650463f0cf9e80560663991e4a2f17262713a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Fri, 2 Apr 2021 01:06:05 +0200 Subject: [PATCH 8/9] Removing LuaGlueFactories - handled by AutoFac --- Backend/PluginManager.cs | 16 +++++++--- Components/Audio/Audio.cs | 1 - Components/Audio/LuaGlueFactory.cs | 21 ------------- Components/ComponentPluginCreationContext.cs | 10 +++--- Components/ComponentRegistrator.cs | 10 ------ Components/Discord/Discord.cs | 1 - Components/Discord/LuaGlueFactory.cs | 21 ------------- Components/IComponentPluginCreationContext.cs | 2 +- Components/IComponentRegistrationContext.cs | 2 -- Components/ILuaGlueFactory.cs | 7 ----- Components/IRacing/IRacing.cs | 1 - Components/IRacing/LuaGlueFactory.cs | 21 ------------- Components/Internal/Internal.cs | 4 --- .../Internal/LuaGLues/CoreLuaGlueFactory.cs | 17 ---------- .../Internal/LuaGLues/HttpLuaGlueFactory.cs | 19 ------------ .../LuaGLues/InternalLuaGlueFactory.cs | 21 ------------- .../Internal/LuaGLues/StateLuaGlueFactory.cs | 19 ------------ Components/Internal/Services/LuaContext.cs | 7 ++--- Components/Internal/Services/LuaService.cs | 10 ++++-- Components/Lua/Lua.cs | 9 +----- Components/Playback/LuaGlueFactory.cs | 24 -------------- Components/Playback/Playback.cs | 1 - Components/Twitch/LuaGlueFactory.cs | 21 ------------- Components/Twitch/Twitch.cs | 1 - Components/UI/LuaGlue.cs | 6 ++-- Components/UI/LuaGlueFactory.cs | 31 ------------------- Components/UI/UI.cs | 3 -- Program.cs | 10 +++++- Slipstream.csproj | 11 ------- 29 files changed, 41 insertions(+), 286 deletions(-) delete mode 100644 Components/Audio/LuaGlueFactory.cs delete mode 100644 Components/Discord/LuaGlueFactory.cs delete mode 100644 Components/ILuaGlueFactory.cs delete mode 100644 Components/IRacing/LuaGlueFactory.cs delete mode 100644 Components/Internal/LuaGLues/CoreLuaGlueFactory.cs delete mode 100644 Components/Internal/LuaGLues/HttpLuaGlueFactory.cs delete mode 100644 Components/Internal/LuaGLues/InternalLuaGlueFactory.cs delete mode 100644 Components/Internal/LuaGLues/StateLuaGlueFactory.cs delete mode 100644 Components/Playback/LuaGlueFactory.cs delete mode 100644 Components/Twitch/LuaGlueFactory.cs delete mode 100644 Components/UI/LuaGlueFactory.cs diff --git a/Backend/PluginManager.cs b/Backend/PluginManager.cs index db508b94..76d23469 100644 --- a/Backend/PluginManager.cs +++ b/Backend/PluginManager.cs @@ -17,6 +17,7 @@ using Slipstream.Shared.Helpers.StrongParameters; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using static Slipstream.Components.Internal.IInternalEventFactory; @@ -31,8 +32,8 @@ public class PluginManager : IPluginManager, IPluginFactory private readonly ILogger Logger; private readonly ILifetimeScope LifetimeScope; private readonly ComponentRegistrator Registrator; - private readonly List LuaGluesFactories = new List(); private readonly Dictionary> ComponentPlugins = new Dictionary>(); + private readonly IEnumerable LuaGlueTypes; public PluginManager( IInternalEventFactory internalEventFactory, @@ -44,7 +45,6 @@ ILifetimeScope lifetimeScope { Registrator = new ComponentRegistrator( ComponentPlugins, - LuaGluesFactories, logger, eventBus); @@ -59,6 +59,7 @@ ILifetimeScope lifetimeScope EventBus = eventBus; Logger = logger; LifetimeScope = lifetimeScope; + LuaGlueTypes = lifetimeScope.GetImplementingTypes(); } public void UnregisterPlugin(IPlugin p) @@ -150,11 +151,17 @@ public IPlugin CreatePlugin(string pluginId, string name, Parameters configurati public IPlugin CreatePlugin(string pluginId, string pluginName, IEventBus eventBus, Parameters configuration) { + List luaGlues = new List(); + + foreach (var luaGlueType in LuaGlueTypes) + { + luaGlues.Add((ILuaGlue)LifetimeScope.Resolve(luaGlueType, new NamedParameter("configuration", configuration))); + } + ComponentPluginCreationContext reg = new ComponentPluginCreationContext( Registrator, this, this, - LuaGluesFactories, pluginId, pluginName, configuration, @@ -169,7 +176,8 @@ public IPlugin CreatePlugin(string pluginId, string pluginName, IEventBus eventB LifetimeScope.Resolve(), LifetimeScope.Resolve(), LifetimeScope.Resolve(), - LifetimeScope.Resolve() + LifetimeScope.Resolve(), + luaGlues ); if (!ComponentPlugins.ContainsKey(pluginName)) throw new KeyNotFoundException($"Plugin name '{pluginName}' not found"); diff --git a/Components/Audio/Audio.cs b/Components/Audio/Audio.cs index 8c4b3719..4c04cf9a 100644 --- a/Components/Audio/Audio.cs +++ b/Components/Audio/Audio.cs @@ -12,7 +12,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.AudioEventFactory(); ctx.RegisterPlugin(NAME, CreateAudioPlugin); - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } private IPlugin CreateAudioPlugin(IComponentPluginCreationContext ctx) diff --git a/Components/Audio/LuaGlueFactory.cs b/Components/Audio/LuaGlueFactory.cs deleted file mode 100644 index 9924cd96..00000000 --- a/Components/Audio/LuaGlueFactory.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Slipstream.Shared; - -namespace Slipstream.Components.Audio -{ - internal class LuaGlueFactory : ILuaGlueFactory - { - private readonly IEventBus EventBus; - private readonly IAudioEventFactory EventFactory; - - public LuaGlueFactory(IEventBus eventBus, IAudioEventFactory eventFactory) - { - EventBus = eventBus; - EventFactory = eventFactory; - } - - public ILuaGlue CreateLuaGlue(IComponentPluginCreationContext ctx) - { - return new LuaGlue(EventBus, EventFactory); - } - } -} \ No newline at end of file diff --git a/Components/ComponentPluginCreationContext.cs b/Components/ComponentPluginCreationContext.cs index cd2f5b76..ae3098a6 100644 --- a/Components/ComponentPluginCreationContext.cs +++ b/Components/ComponentPluginCreationContext.cs @@ -40,8 +40,6 @@ public IEventBus EventBus public NLua.Lua Lua { get; } - public List LuaGlueFactories { get; } - public IPluginManager PluginManager { get; } public IPluginFactory PluginFactory { get; } @@ -68,11 +66,12 @@ public IEventBus EventBus public ITwitchEventFactory TwitchEventFactory { get; } + public IEnumerable LuaGlues { get; } + public ComponentPluginCreationContext( ComponentRegistrator componentRegistration, IPluginManager pluginManager, IPluginFactory pluginFactory, - List luaGlueFactories, string pluginId, string pluginName, Parameters pluginParameters, @@ -87,12 +86,12 @@ public ComponentPluginCreationContext( IAudioEventFactory audioEventFactory, IDiscordEventFactory discordEventFactory, IIRacingEventFactory iRacingEventFactory, - ITwitchEventFactory twitchEventFactory) + ITwitchEventFactory twitchEventFactory, + IEnumerable luaGlues) { ComponentRegistration = componentRegistration; PluginManager = pluginManager; PluginFactory = pluginFactory; - LuaGlueFactories = luaGlueFactories; PluginId = pluginId; PluginName = pluginName; PluginParameters = pluginParameters; @@ -108,6 +107,7 @@ public ComponentPluginCreationContext( DiscordEventFactory = discordEventFactory; IRacingEventFactory = iRacingEventFactory; TwitchEventFactory = twitchEventFactory; + LuaGlues = luaGlues; Lua = new NLua.Lua(); } } diff --git a/Components/ComponentRegistrator.cs b/Components/ComponentRegistrator.cs index a439020d..8c48703f 100644 --- a/Components/ComponentRegistrator.cs +++ b/Components/ComponentRegistrator.cs @@ -1,6 +1,4 @@ using Serilog; -using Slipstream.Backend; -using Slipstream.Components.Internal; using Slipstream.Shared; using System; using System.Collections.Generic; @@ -9,7 +7,6 @@ namespace Slipstream.Components { internal class ComponentRegistrator : IComponentRegistrationContext { - private readonly List LuaGlueFactories; private readonly Dictionary> Plugins; public ILogger Logger { get; internal set; } @@ -18,12 +15,10 @@ internal class ComponentRegistrator : IComponentRegistrationContext public ComponentRegistrator( Dictionary> plugins, - List luaGlueFactories, ILogger logger, IEventBus eventBus) { Plugins = plugins; - LuaGlueFactories = luaGlueFactories; Logger = logger; EventBus = eventBus; } @@ -32,10 +27,5 @@ public void RegisterPlugin(string name, Func LuaGlueFactories { get; } IPluginManager PluginManager { get; } IPluginFactory PluginFactory { get; } IEventSerdeService EventSerdeService { get; } @@ -33,5 +32,6 @@ internal interface IComponentPluginCreationContext : IComponentPluginDependencie IDiscordEventFactory DiscordEventFactory { get; } IIRacingEventFactory IRacingEventFactory { get; } ITwitchEventFactory TwitchEventFactory { get; } + IEnumerable LuaGlues { get; } } } \ No newline at end of file diff --git a/Components/IComponentRegistrationContext.cs b/Components/IComponentRegistrationContext.cs index 7eca11e0..8b7d70fb 100644 --- a/Components/IComponentRegistrationContext.cs +++ b/Components/IComponentRegistrationContext.cs @@ -11,7 +11,5 @@ internal interface IComponentRegistrationContext public IEventBus EventBus { get; } public void RegisterPlugin(string name, Func plugin); - - void RegisterLuaGlue(ILuaGlueFactory luaGlueFactory); } } \ No newline at end of file diff --git a/Components/ILuaGlueFactory.cs b/Components/ILuaGlueFactory.cs deleted file mode 100644 index 57371a49..00000000 --- a/Components/ILuaGlueFactory.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Slipstream.Components -{ - internal interface ILuaGlueFactory - { - ILuaGlue CreateLuaGlue(IComponentPluginCreationContext ctx); - } -} \ No newline at end of file diff --git a/Components/IRacing/IRacing.cs b/Components/IRacing/IRacing.cs index ef2a7300..889709ab 100644 --- a/Components/IRacing/IRacing.cs +++ b/Components/IRacing/IRacing.cs @@ -12,7 +12,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.IRacingEventFactory(); ctx.RegisterPlugin(NAME, CreatePlugin); - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) diff --git a/Components/IRacing/LuaGlueFactory.cs b/Components/IRacing/LuaGlueFactory.cs deleted file mode 100644 index d9db1d3d..00000000 --- a/Components/IRacing/LuaGlueFactory.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Slipstream.Shared; - -namespace Slipstream.Components.IRacing -{ - internal class LuaGlueFactory : ILuaGlueFactory - { - private readonly IEventBus EventBus; - private readonly IIRacingEventFactory EventFactory; - - public LuaGlueFactory(IEventBus eventBus, IIRacingEventFactory eventFactory) - { - EventBus = eventBus; - EventFactory = eventFactory; - } - - public ILuaGlue CreateLuaGlue(IComponentPluginCreationContext ctx) - { - return new LuaGlue(EventBus, EventFactory); - } - } -} \ No newline at end of file diff --git a/Components/Internal/Internal.cs b/Components/Internal/Internal.cs index 42ded6f4..ed5af0c8 100644 --- a/Components/Internal/Internal.cs +++ b/Components/Internal/Internal.cs @@ -6,10 +6,6 @@ internal class Internal : IComponent { public void Register(IComponentRegistrationContext ctx) { - ctx.RegisterLuaGlue(new LuaGlues.CoreLuaGlueFactory(new EventSerdeService())); - ctx.RegisterLuaGlue(new LuaGlues.HttpLuaGlueFactory(ctx.Logger)); - ctx.RegisterLuaGlue(new LuaGlues.InternalLuaGlueFactory(ctx.EventBus, new EventFactory.InternalEventFactory())); - ctx.RegisterLuaGlue(new LuaGlues.StateLuaGlueFactory(new StateService(ctx.Logger, "state.txt"))); } } } \ No newline at end of file diff --git a/Components/Internal/LuaGLues/CoreLuaGlueFactory.cs b/Components/Internal/LuaGLues/CoreLuaGlueFactory.cs deleted file mode 100644 index ec64141d..00000000 --- a/Components/Internal/LuaGLues/CoreLuaGlueFactory.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Slipstream.Components.Internal.LuaGlues -{ - internal class CoreLuaGlueFactory : ILuaGlueFactory - { - private readonly IEventSerdeService EventSerdeService; - - public CoreLuaGlueFactory(IEventSerdeService eventSerdeService) - { - EventSerdeService = eventSerdeService; - } - - public ILuaGlue CreateLuaGlue(IComponentPluginCreationContext ctx) - { - return new CoreLuaGlue(EventSerdeService); - } - } -} \ No newline at end of file diff --git a/Components/Internal/LuaGLues/HttpLuaGlueFactory.cs b/Components/Internal/LuaGLues/HttpLuaGlueFactory.cs deleted file mode 100644 index aba1ae68..00000000 --- a/Components/Internal/LuaGLues/HttpLuaGlueFactory.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Serilog; - -namespace Slipstream.Components.Internal.LuaGlues -{ - internal class HttpLuaGlueFactory : ILuaGlueFactory - { - private readonly ILogger Logger; - - public HttpLuaGlueFactory(ILogger logger) - { - Logger = logger; - } - - public ILuaGlue CreateLuaGlue(IComponentPluginCreationContext ctx) - { - return new HttpLuaGlue(Logger); - } - } -} \ No newline at end of file diff --git a/Components/Internal/LuaGLues/InternalLuaGlueFactory.cs b/Components/Internal/LuaGLues/InternalLuaGlueFactory.cs deleted file mode 100644 index bade43d8..00000000 --- a/Components/Internal/LuaGLues/InternalLuaGlueFactory.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Slipstream.Shared; - -namespace Slipstream.Components.Internal.LuaGlues -{ - internal class InternalLuaGlueFactory : ILuaGlueFactory - { - private readonly IEventBus EventBus; - private readonly IInternalEventFactory EventFactory; - - public InternalLuaGlueFactory(IEventBus eventBus, IInternalEventFactory eventFactory) - { - EventBus = eventBus; - EventFactory = eventFactory; - } - - public ILuaGlue CreateLuaGlue(IComponentPluginCreationContext ctx) - { - return new InternalLuaGlue(EventBus, EventFactory); - } - } -} \ No newline at end of file diff --git a/Components/Internal/LuaGLues/StateLuaGlueFactory.cs b/Components/Internal/LuaGLues/StateLuaGlueFactory.cs deleted file mode 100644 index a76471ab..00000000 --- a/Components/Internal/LuaGLues/StateLuaGlueFactory.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Slipstream.Components.Internal.Services; - -namespace Slipstream.Components.Internal.LuaGlues -{ - internal class StateLuaGlueFactory : ILuaGlueFactory - { - private readonly IStateService StateService; - - public StateLuaGlueFactory(IStateService stateService) - { - StateService = stateService; - } - - public ILuaGlue CreateLuaGlue(IComponentPluginCreationContext ctx) - { - return new StateLuaGlue(StateService); - } - } -} \ No newline at end of file diff --git a/Components/Internal/Services/LuaContext.cs b/Components/Internal/Services/LuaContext.cs index 85f2828f..03dced70 100644 --- a/Components/Internal/Services/LuaContext.cs +++ b/Components/Internal/Services/LuaContext.cs @@ -13,10 +13,7 @@ public class LuaContext : ILuaContext private NLua.Lua? Lua; - public LuaContext( - string filePath, - NLua.Lua lua - ) + public LuaContext(string filePath, NLua.Lua lua) { try { @@ -52,7 +49,7 @@ public void HandleEvent(IEvent @event) if (@event.Uptime - LastLuaGC > 1000) { LastLuaGC = @event.Uptime; - Lua.DoString("collectgarbage()"); + Lua?.DoString("collectgarbage()"); } HandleFunc?.Call(@event); diff --git a/Components/Internal/Services/LuaService.cs b/Components/Internal/Services/LuaService.cs index f176251b..14eef659 100644 --- a/Components/Internal/Services/LuaService.cs +++ b/Components/Internal/Services/LuaService.cs @@ -7,17 +7,21 @@ namespace Slipstream.Components.Internal.Services { internal class LuaService : ILuaSevice { - private readonly List LuaGlues; + private readonly List LuaGlues = new List(); private readonly NLua.Lua Lua; public LuaService( - List? luaGlues = null) + IEnumerable? luaGlues = null) { Lua = new NLua.Lua(); Lua.State.Encoding = Encoding.UTF8; luaGlues ??= new List(); - LuaGlues = luaGlues; + + foreach (var lg in luaGlues) + { + LuaGlues.Add(lg); + } foreach (var glue in luaGlues) { diff --git a/Components/Lua/Lua.cs b/Components/Lua/Lua.cs index a1a864bd..7057706a 100644 --- a/Components/Lua/Lua.cs +++ b/Components/Lua/Lua.cs @@ -27,15 +27,8 @@ private IPlugin CreateLuaManagerPlugin(IComponentPluginCreationContext ctx) private IPlugin CreateLuaPlugin(IComponentPluginCreationContext ctx) { - List states = new List(); - - foreach (var factories in ctx.LuaGlueFactories) - { - states.Add(factories.CreateLuaGlue(ctx)); - } - var luaService = new LuaService( - states + ctx.LuaGlues ); return new LuaPlugin( diff --git a/Components/Playback/LuaGlueFactory.cs b/Components/Playback/LuaGlueFactory.cs deleted file mode 100644 index dfb34bc3..00000000 --- a/Components/Playback/LuaGlueFactory.cs +++ /dev/null @@ -1,24 +0,0 @@ -using Slipstream.Shared; - -namespace Slipstream.Components.Playback -{ - internal partial class Playback - { - internal class LuaGlueFactory : ILuaGlueFactory - { - private readonly IEventBus EventBus; - private readonly IPlaybackEventFactory EventFactory; - - public LuaGlueFactory(IEventBus eventBus, IPlaybackEventFactory eventFactory) - { - EventBus = eventBus; - EventFactory = eventFactory; - } - - public ILuaGlue CreateLuaGlue(IComponentPluginCreationContext ctx) - { - return new LuaGlue(EventBus, EventFactory); - } - } - } -} \ No newline at end of file diff --git a/Components/Playback/Playback.cs b/Components/Playback/Playback.cs index d80928d6..cfc46d2e 100644 --- a/Components/Playback/Playback.cs +++ b/Components/Playback/Playback.cs @@ -11,7 +11,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.PlaybackEventFactory(); ctx.RegisterPlugin("PlaybackPlugin", CreatePlugin); - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) diff --git a/Components/Twitch/LuaGlueFactory.cs b/Components/Twitch/LuaGlueFactory.cs deleted file mode 100644 index deaaa858..00000000 --- a/Components/Twitch/LuaGlueFactory.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Slipstream.Shared; - -namespace Slipstream.Components.Twitch -{ - internal class LuaGlueFactory : ILuaGlueFactory - { - private readonly IEventBus EventBus; - private readonly ITwitchEventFactory EventFactory; - - public LuaGlueFactory(IEventBus eventBus, ITwitchEventFactory eventFactory) - { - EventBus = eventBus; - EventFactory = eventFactory; - } - - public ILuaGlue CreateLuaGlue(IComponentPluginCreationContext ctx) - { - return new LuaGlue(EventBus, EventFactory); - } - } -} \ No newline at end of file diff --git a/Components/Twitch/Twitch.cs b/Components/Twitch/Twitch.cs index 5ed63ad6..e0c01c44 100644 --- a/Components/Twitch/Twitch.cs +++ b/Components/Twitch/Twitch.cs @@ -10,7 +10,6 @@ public void Register(IComponentRegistrationContext ctx) var eventFactory = new EventFactory.TwitchEventFactory(); ctx.RegisterPlugin("TwitchPlugin", CreatePlugin); - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); } private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) diff --git a/Components/UI/LuaGlue.cs b/Components/UI/LuaGlue.cs index 1b1a798c..7f119260 100644 --- a/Components/UI/LuaGlue.cs +++ b/Components/UI/LuaGlue.cs @@ -1,5 +1,7 @@ using Serilog; using Slipstream.Shared; +using Slipstream.Shared.Helpers.StrongParameters; +using System.IO; #nullable enable @@ -12,12 +14,12 @@ public class LuaGlue : ILuaGlue private readonly IUIEventFactory EventFactory; private readonly string Prefix; - public LuaGlue(ILogger logger, IEventBus eventBus, IUIEventFactory eventFactory, string logPrefix) + public LuaGlue(ILogger logger, IEventBus eventBus, IUIEventFactory eventFactory, Parameters configuration) { Logger = logger; EventBus = eventBus; EventFactory = eventFactory; - Prefix = logPrefix; + Prefix = Path.GetFileName(configuration.GetOrDefault("filepath", "")); } public void SetupLua(NLua.Lua lua) diff --git a/Components/UI/LuaGlueFactory.cs b/Components/UI/LuaGlueFactory.cs deleted file mode 100644 index 6781d8a4..00000000 --- a/Components/UI/LuaGlueFactory.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Serilog; -using Slipstream.Shared; -using System.IO; - -namespace Slipstream.Components.UI -{ - internal class LuaGlueFactory : ILuaGlueFactory - { - private readonly ILogger Logger; - private readonly IEventBus EventBus; - private readonly IUIEventFactory EventFactory; - - public LuaGlueFactory(ILogger logger, IEventBus eventBus, IUIEventFactory eventFactory) - { - Logger = logger; - EventBus = eventBus; - EventFactory = eventFactory; - } - - public ILuaGlue CreateLuaGlue(IComponentPluginCreationContext ctx) - { - string prefix = ""; - - if (ctx.PluginName == "LuaPlugin") - { - prefix = Path.GetFileName(ctx.PluginParameters.Get("filepath")); - } - return new LuaGlue(Logger, EventBus, EventFactory, prefix); - } - } -} \ No newline at end of file diff --git a/Components/UI/UI.cs b/Components/UI/UI.cs index cb71b071..3f7de6e4 100644 --- a/Components/UI/UI.cs +++ b/Components/UI/UI.cs @@ -4,9 +4,6 @@ internal class UI : IComponent { public void Register(IComponentRegistrationContext ctx) { - var eventFactory = new EventFactory.UIEventFactory(); - - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.Logger, ctx.EventBus, eventFactory)); } } } \ No newline at end of file diff --git a/Program.cs b/Program.cs index c986d86d..97291371 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,8 @@ using Autofac; using Serilog; +using Slipstream.Components; using Slipstream.Components.Internal; using Slipstream.Components.Internal.Services; -using Slipstream.Components.UI.EventFactory; using Slipstream.Shared; using System; using System.Windows.Forms; @@ -74,6 +74,14 @@ private static void ConfigureServices(ContainerBuilder builder) .AsImplementedInterfaces() .SingleInstance(); builder.RegisterType().As().InstancePerDependency(); + + // LuaGlues + builder.RegisterAssemblyTypes(typeof(Program).Assembly) + .Where(t => t.IsAssignableTo()) + .Where(t => !t.IsAbstract) + .As() + .AsSelf() + .InstancePerDependency(); } private class PopulateSink diff --git a/Slipstream.csproj b/Slipstream.csproj index 7a3dd05a..bd2beff7 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -210,7 +210,6 @@ - @@ -220,20 +219,13 @@ - - - - - - - @@ -262,9 +254,7 @@ - - @@ -286,7 +276,6 @@ - From 6f28a543a4a14d06e65e248b1374a6cd42001b3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Fri, 2 Apr 2021 09:40:41 +0200 Subject: [PATCH 9/9] Use Autofac to create plugins LuaGlueFactories are not removed and a part of a Plugin. Upon starting loading a lua script, it will ask all existing plugins for their glues (if any) and apply them. A side-effect of this meant, that Components without Plugins but with LuaGlue wouldn't work. To fix this, two new plugins were defined: - InternalPlugin - UIPlugin Could finally remove all the component registration code, so now component is merely a directory containing plugins, factories, events and handlers. --- Backend/Bootstrap/init.lua | 1 + Backend/Engine.cs | 16 ++- Backend/PluginManager.cs | 66 ++-------- Backend/PluginWorker.cs | 2 +- CHANGELOG.md | 2 + .../AppilcationUpdate/ApplicationUpdate.cs | 26 ---- .../Plugins/ApplicationUpdatePlugin.cs | 10 +- Components/Audio/Audio.cs | 29 ----- Components/Audio/Plugins/AudioPlugin.cs | 8 +- Components/BasePlugin.cs | 4 +- Components/ComponentPluginCreationContext.cs | 114 ------------------ Components/ComponentRegistrator.cs | 31 ----- Components/Discord/Discord.cs | 25 ---- Components/Discord/Plugins/DiscordPlugin.cs | 12 +- Components/FileMonitor/FileMonitor.cs | 25 ---- .../FileMonitor/Plugins/FileMonitorPlugin.cs | 7 +- Components/IComponent.cs | 7 -- Components/IComponentPluginCreationContext.cs | 37 ------ Components/IComponentPluginDependencies.cs | 14 --- Components/IComponentRegistrationContext.cs | 15 --- Components/ILuaGlue.cs | 2 +- Components/IPlugin.cs | 5 +- Components/IRacing/IIRacingEventFactory.cs | 1 - Components/IRacing/IRacing.cs | 28 ----- .../IRacing/Plugins/GameState/StateFactory.cs | 2 +- Components/IRacing/Plugins/IRacingPlugin.cs | 8 +- Components/IRacing/Plugins/Models/LapState.cs | 4 +- .../Plugins/Trackers/IRacingSessionTracker.cs | 1 - .../{ILuaSevice.cs => ILuaService.cs} | 2 +- Components/Internal/Internal.cs | 11 -- Components/Internal/LuaGLues/StateLuaGlue.cs | 2 +- Components/Internal/Plugins/InternalPlugin.cs | 44 +++++++ Components/Internal/Services/LuaService.cs | 38 +++--- Components/Lua/Lua.cs | 46 ------- Components/Lua/Plugins/LuaManagerPlugin.cs | 7 +- Components/Lua/Plugins/LuaPlugin.cs | 12 +- Components/Playback/Playback.cs | 27 ----- Components/Playback/Plugins/PlaybackPlugin.cs | 13 +- Components/Twitch/Plugins/TwitchPlugin.cs | 7 +- Components/Twitch/Twitch.cs | 27 ----- Components/Txrx/Plugins/ReceiverPlugin.cs | 8 +- Components/Txrx/Plugins/TransmitterPlugin.cs | 8 +- Components/Txrx/Txrx.cs | 47 -------- Components/UI/LuaGlue.cs | 6 +- Components/UI/Plugins/InternalPlugin.cs | 39 ++++++ Components/UI/UI.cs | 9 -- .../WinFormUI/Plugins/WinFormUIPlugin.cs | 8 +- Components/WinFormUI/WinFormUI.cs | 28 ----- Program.cs | 9 +- Shared/EventHandlerController.cs | 1 - .../Validators/ArrayValidator.cs | 4 +- .../Validators/DictionaryValidator.cs | 2 +- Slipstream.csproj | 22 +--- 53 files changed, 245 insertions(+), 684 deletions(-) delete mode 100644 Components/AppilcationUpdate/ApplicationUpdate.cs delete mode 100644 Components/Audio/Audio.cs delete mode 100644 Components/ComponentPluginCreationContext.cs delete mode 100644 Components/ComponentRegistrator.cs delete mode 100644 Components/Discord/Discord.cs delete mode 100644 Components/FileMonitor/FileMonitor.cs delete mode 100644 Components/IComponent.cs delete mode 100644 Components/IComponentPluginCreationContext.cs delete mode 100644 Components/IComponentPluginDependencies.cs delete mode 100644 Components/IComponentRegistrationContext.cs delete mode 100644 Components/IRacing/IRacing.cs rename Components/Internal/{ILuaSevice.cs => ILuaService.cs} (81%) delete mode 100644 Components/Internal/Internal.cs create mode 100644 Components/Internal/Plugins/InternalPlugin.cs delete mode 100644 Components/Lua/Lua.cs delete mode 100644 Components/Playback/Playback.cs delete mode 100644 Components/Twitch/Twitch.cs delete mode 100644 Components/Txrx/Txrx.cs create mode 100644 Components/UI/Plugins/InternalPlugin.cs delete mode 100644 Components/UI/UI.cs delete mode 100644 Components/WinFormUI/WinFormUI.cs diff --git a/Backend/Bootstrap/init.lua b/Backend/Bootstrap/init.lua index 459e06f2..1e56d5d3 100644 --- a/Backend/Bootstrap/init.lua +++ b/Backend/Bootstrap/init.lua @@ -47,4 +47,5 @@ register_plugin({ plugin_name = "FileMonitorPlugin"}) register_plugin({ plugin_name = "PlaybackPlugin"}) -- UI to show console output +register_plugin({ plugin_name = "UIPlugin"}) register_plugin({ plugin_name = "WinFormUIPlugin"}) \ No newline at end of file diff --git a/Backend/Engine.cs b/Backend/Engine.cs index d67256d4..51427316 100644 --- a/Backend/Engine.cs +++ b/Backend/Engine.cs @@ -2,7 +2,6 @@ using Serilog; using Slipstream.Components.Internal; using Slipstream.Components.Internal.Events; -using Slipstream.Components.Internal.Services; using Slipstream.Shared; using Slipstream.Shared.Helpers.StrongParameters; using System; @@ -23,7 +22,15 @@ internal class Engine : IEngine, IDisposable private readonly IEventHandlerController EventHandlerController; private bool Stopped = false; - public Engine(ILogger logger, IInternalEventFactory eventFactory, IEventBus eventBus, IPluginFactory pluginFactory, IPluginManager pluginManager, IEventHandlerController eventHandlerController) + public Engine( + ILogger logger, + IInternalEventFactory eventFactory, + IEventBus eventBus, + IPluginFactory pluginFactory, + IPluginManager pluginManager, + IEventHandlerController eventHandlerController, + ILuaService luaService + ) { EventFactory = eventFactory; EventBus = eventBus; @@ -53,8 +60,9 @@ public Engine(ILogger logger, IInternalEventFactory eventFactory, IEventBus even Logger.Information("Loading {initcfg}", initFilename); - // FIXME: This needs to be reimplemented - var luaService = new LuaService(new System.Collections.Generic.List { new Slipstream.Components.Internal.LuaGlues.InternalLuaGlue(eventBus, EventFactory) }); + // We need to bootstrap this. InternalPlugin isn't loaded yet, as we're about to parse init.lua. However + // InternalPlugin provides the register_plugin() method, so we need to add it here, to make init.lua work + PluginManager.RegisterPlugin(PluginFactory.CreatePlugin("InternalPlugin", "InternalPlugin", new Parameters())); luaService.Parse(initFilename); } diff --git a/Backend/PluginManager.cs b/Backend/PluginManager.cs index 76d23469..2645f105 100644 --- a/Backend/PluginManager.cs +++ b/Backend/PluginManager.cs @@ -3,22 +3,11 @@ using Autofac; using Serilog; using Slipstream.Components; -using Slipstream.Components.AppilcationUpdate; -using Slipstream.Components.Audio; -using Slipstream.Components.Discord; -using Slipstream.Components.FileMonitor; using Slipstream.Components.Internal; -using Slipstream.Components.IRacing; -using Slipstream.Components.Lua; -using Slipstream.Components.Playback; -using Slipstream.Components.Twitch; -using Slipstream.Components.UI; using Slipstream.Shared; using Slipstream.Shared.Helpers.StrongParameters; using System; using System.Collections.Generic; -using System.IO; -using System.Linq; using static Slipstream.Components.Internal.IInternalEventFactory; namespace Slipstream.Backend @@ -26,40 +15,29 @@ namespace Slipstream.Backend public class PluginManager : IPluginManager, IPluginFactory { private readonly IInternalEventFactory InternalEventFactory; - private readonly IEventSerdeService EventSerdeService; private readonly IEventBus EventBus; private readonly IDictionary PluginWorkers = new Dictionary(); + private readonly IDictionary PluginFactories = new Dictionary(); private readonly ILogger Logger; private readonly ILifetimeScope LifetimeScope; - private readonly ComponentRegistrator Registrator; - private readonly Dictionary> ComponentPlugins = new Dictionary>(); - private readonly IEnumerable LuaGlueTypes; public PluginManager( IInternalEventFactory internalEventFactory, IEventBus eventBus, ILogger logger, - IEventSerdeService eventSerdeService, ILifetimeScope lifetimeScope ) { - Registrator = new ComponentRegistrator( - ComponentPlugins, - logger, - eventBus); - - foreach (var type in typeof(PluginManager).Assembly.GetTypes().Where(t => typeof(IComponent).IsAssignableFrom(t) && !t.IsAbstract && t.IsClass)) + foreach (var type in lifetimeScope.GetImplementingTypes()) { - logger.Information("Initializing component {pluginName}", type.Name); - ((IComponent)Activator.CreateInstance(type)).Register(Registrator); + logger.Information("Found plugin {pluginName}", type.Name); + PluginFactories.Add(type.Name, type); } InternalEventFactory = internalEventFactory; - EventSerdeService = eventSerdeService; EventBus = eventBus; Logger = logger; LifetimeScope = lifetimeScope; - LuaGlueTypes = lifetimeScope.GetImplementingTypes(); } public void UnregisterPlugin(IPlugin p) @@ -151,37 +129,15 @@ public IPlugin CreatePlugin(string pluginId, string name, Parameters configurati public IPlugin CreatePlugin(string pluginId, string pluginName, IEventBus eventBus, Parameters configuration) { - List luaGlues = new List(); - - foreach (var luaGlueType in LuaGlueTypes) - { - luaGlues.Add((ILuaGlue)LifetimeScope.Resolve(luaGlueType, new NamedParameter("configuration", configuration))); - } + if (!PluginFactories.ContainsKey(pluginName)) + throw new KeyNotFoundException($"Plugin name '{pluginName}' not found"); - ComponentPluginCreationContext reg = new ComponentPluginCreationContext( - Registrator, - this, - this, - pluginId, - pluginName, - configuration, - EventSerdeService, - LifetimeScope.Resolve(), - LifetimeScope.Resolve(), - LifetimeScope.Resolve(), - LifetimeScope.Resolve(), - LifetimeScope.Resolve(), - LifetimeScope.Resolve(), - LifetimeScope.Resolve(), - LifetimeScope.Resolve(), - LifetimeScope.Resolve(), - LifetimeScope.Resolve(), - LifetimeScope.Resolve(), - luaGlues + return (IPlugin)LifetimeScope.Resolve( + PluginFactories[pluginName], + new NamedParameter("id", pluginId), + new NamedParameter("configuration", configuration), + new NamedParameter("eventBus", eventBus) ); - if (!ComponentPlugins.ContainsKey(pluginName)) - throw new KeyNotFoundException($"Plugin name '{pluginName}' not found"); - return ComponentPlugins[pluginName].Invoke(reg); } public void Dispose() diff --git a/Backend/PluginWorker.cs b/Backend/PluginWorker.cs index 15de2527..5ee61f81 100644 --- a/Backend/PluginWorker.cs +++ b/Backend/PluginWorker.cs @@ -24,7 +24,7 @@ public PluginWorker(IPlugin plugin, IEventBusSubscription subscription, IInterna EventFactory = eventFactory; } - private void Plugin_OnStateChanged(object source, IPlugin plugin) + private void Plugin_OnStateChanged(object source, BasePlugin plugin) { EventBus.PublishEvent(EventFactory.CreateInternalPluginState(plugin.Id, plugin.Name, plugin.DisplayName, PluginStatusEnum.Registered)); } diff --git a/CHANGELOG.md b/CHANGELOG.md index b47a5d63..f6e842de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ - adds WinFormUI component - containing the Slipstream UI. You need to add `register_plugin({ plugin_name = "WinFormUIPlugin"}) ` to you `init.lua` to get it. - add `internal:shutdown()` lua function that quits the application + - Adds `InternalPlugin` (automatically loaded) + - Adds `UIPlugin` that handles generic UI functionality. Needs to be added to your init.lua! ## [0.5.0](https://github.com/dennis/slipstream/releases/tag/v0.5.0) (2021-03-25) [Full Changelog](https://github.com/dennis/slipstream/compare/v0.4.1...v0.5.0) diff --git a/Components/AppilcationUpdate/ApplicationUpdate.cs b/Components/AppilcationUpdate/ApplicationUpdate.cs deleted file mode 100644 index 767c9fa1..00000000 --- a/Components/AppilcationUpdate/ApplicationUpdate.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Slipstream.Components.AppilcationUpdate.Plugins; - -namespace Slipstream.Components.AppilcationUpdate -{ - public class ApplicationUpdate : IComponent - { - private const string NAME = nameof(ApplicationUpdatePlugin); - - void IComponent.Register(IComponentRegistrationContext ctx) - { - ctx.RegisterPlugin(NAME, CreatePlugin); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new ApplicationUpdatePlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.ApplicationUpdateEventFactory, - ctx.Logger.ForContext(typeof(ApplicationUpdate)), - ctx.EventBus, - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/AppilcationUpdate/Plugins/ApplicationUpdatePlugin.cs b/Components/AppilcationUpdate/Plugins/ApplicationUpdatePlugin.cs index 0088ae9c..b1a4cdbd 100644 --- a/Components/AppilcationUpdate/Plugins/ApplicationUpdatePlugin.cs +++ b/Components/AppilcationUpdate/Plugins/ApplicationUpdatePlugin.cs @@ -1,16 +1,17 @@ using Serilog; +using Slipstream.Components.Internal.Events; using Slipstream.Shared; using Slipstream.Shared.Helpers.StrongParameters; using Slipstream.Shared.Helpers.StrongParameters.Validators; using Squirrel; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; -using Slipstream.Components.Internal.Events; namespace Slipstream.Components.AppilcationUpdate.Plugins { - public class ApplicationUpdatePlugin : BasePlugin + public class ApplicationUpdatePlugin : BasePlugin, IPlugin { private readonly EventHandler.ApplicationUpdateEventHandler applicationUpdate; private readonly IApplicationUpdateEventFactory applicationUpdateEventFactory; @@ -159,5 +160,10 @@ private async Task DoAppUpdates(UpdateManager updateManager) var releaseInfo = await updateManager.UpdateApp(); Logger.Information($"Auto update, update completed for {releaseInfo.Version}"); } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { }; + } } } \ No newline at end of file diff --git a/Components/Audio/Audio.cs b/Components/Audio/Audio.cs deleted file mode 100644 index 4c04cf9a..00000000 --- a/Components/Audio/Audio.cs +++ /dev/null @@ -1,29 +0,0 @@ -using Slipstream.Backend; -using Slipstream.Components.Audio.Plugins; - -namespace Slipstream.Components.Audio -{ - internal class Audio : IComponent - { - private const string NAME = "AudioPlugin"; - - public void Register(IComponentRegistrationContext ctx) - { - var eventFactory = new EventFactory.AudioEventFactory(); - - ctx.RegisterPlugin(NAME, CreateAudioPlugin); - } - - private IPlugin CreateAudioPlugin(IComponentPluginCreationContext ctx) - { - return new AudioPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger, - ctx.EventBus, - ctx.AudioEventFactory, - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/Audio/Plugins/AudioPlugin.cs b/Components/Audio/Plugins/AudioPlugin.cs index 5e8e0c0a..45769d86 100644 --- a/Components/Audio/Plugins/AudioPlugin.cs +++ b/Components/Audio/Plugins/AudioPlugin.cs @@ -5,6 +5,7 @@ using Slipstream.Shared.Helpers.StrongParameters; using Slipstream.Shared.Helpers.StrongParameters.Validators; using System; +using System.Collections.Generic; using System.IO; using System.Speech.Synthesis; using System.Threading; @@ -13,7 +14,7 @@ namespace Slipstream.Components.Audio.Plugins { - internal class AudioPlugin : BasePlugin + public class AudioPlugin : BasePlugin, IPlugin { public static DictionaryValidator ConfigurationValidator { get; } @@ -129,5 +130,10 @@ private void Play(WaveStream stream, float volume) Thread.Sleep(100); } } + + public IEnumerable CreateLuaGlues() + { + return new LuaGlue[] { new LuaGlue(EventBus, EventFactory) }; + } } } \ No newline at end of file diff --git a/Components/BasePlugin.cs b/Components/BasePlugin.cs index 33ad30aa..292dfa54 100644 --- a/Components/BasePlugin.cs +++ b/Components/BasePlugin.cs @@ -5,7 +5,7 @@ namespace Slipstream.Components { - public class BasePlugin : IPlugin + public abstract class BasePlugin { public string Id { get; } = "INVALID-PLUGIN-ID"; private string name = "INVALID-PLUGIN-NAME"; @@ -24,7 +24,7 @@ public string DisplayName set { displayName = value; OnStateChanged?.Invoke(this, this); } } - public event EventHandler? OnStateChanged; + public event EventHandler? OnStateChanged; public bool Reconfigurable { get; } public bool FullThreadControl { get; } diff --git a/Components/ComponentPluginCreationContext.cs b/Components/ComponentPluginCreationContext.cs deleted file mode 100644 index ae3098a6..00000000 --- a/Components/ComponentPluginCreationContext.cs +++ /dev/null @@ -1,114 +0,0 @@ -using Serilog; -using Slipstream.Backend; -using Slipstream.Components.AppilcationUpdate; -using Slipstream.Components.Audio; -using Slipstream.Components.Discord; -using Slipstream.Components.FileMonitor; -using Slipstream.Components.Internal; -using Slipstream.Components.IRacing; -using Slipstream.Components.Lua; -using Slipstream.Components.Playback; -using Slipstream.Components.Twitch; -using Slipstream.Components.UI; -using Slipstream.Shared; -using Slipstream.Shared.Helpers.StrongParameters; -using System.Collections.Generic; - -namespace Slipstream.Components -{ - internal class ComponentPluginCreationContext : IComponentPluginCreationContext - { - private readonly ComponentRegistrator ComponentRegistration; - - public IEventHandlerController EventHandlerController { get; } - - public ILogger Logger - { - get { return ComponentRegistration.Logger; } - } - - public IEventBus EventBus - { - get { return ComponentRegistration.EventBus; } - } - - public string PluginId { get; } - - public string PluginName { get; } - - public Parameters PluginParameters { get; } - - public NLua.Lua Lua { get; } - - public IPluginManager PluginManager { get; } - - public IPluginFactory PluginFactory { get; } - - public IEventSerdeService EventSerdeService { get; } - - public IInternalEventFactory InternalEventFactory { get; } - - public IUIEventFactory UIEventFactory { get; } - - public IPlaybackEventFactory PlaybackEventFactory { get; } - - public ILuaEventFactory LuaEventFactory { get; } - - public IApplicationUpdateEventFactory ApplicationUpdateEventFactory { get; } - - public IFileMonitorEventFactory FileMonitorEventFactory { get; } - - public IAudioEventFactory AudioEventFactory { get; } - - public IDiscordEventFactory DiscordEventFactory { get; } - - public IIRacingEventFactory IRacingEventFactory { get; } - - public ITwitchEventFactory TwitchEventFactory { get; } - - public IEnumerable LuaGlues { get; } - - public ComponentPluginCreationContext( - ComponentRegistrator componentRegistration, - IPluginManager pluginManager, - IPluginFactory pluginFactory, - string pluginId, - string pluginName, - Parameters pluginParameters, - IEventSerdeService eventSerdeService, - IEventHandlerController eventHandlerController, - IInternalEventFactory internalEventFactory, - IUIEventFactory uiEventFactory, - IPlaybackEventFactory playbackEventFactory, - ILuaEventFactory luaEventFactory, - IApplicationUpdateEventFactory applicationUpdateEventFactory, - IFileMonitorEventFactory fileMonitorEventFactory, - IAudioEventFactory audioEventFactory, - IDiscordEventFactory discordEventFactory, - IIRacingEventFactory iRacingEventFactory, - ITwitchEventFactory twitchEventFactory, - IEnumerable luaGlues) - { - ComponentRegistration = componentRegistration; - PluginManager = pluginManager; - PluginFactory = pluginFactory; - PluginId = pluginId; - PluginName = pluginName; - PluginParameters = pluginParameters; - EventSerdeService = eventSerdeService; - EventHandlerController = eventHandlerController; - InternalEventFactory = internalEventFactory; - UIEventFactory = uiEventFactory; - PlaybackEventFactory = playbackEventFactory; - LuaEventFactory = luaEventFactory; - ApplicationUpdateEventFactory = applicationUpdateEventFactory; - FileMonitorEventFactory = fileMonitorEventFactory; - AudioEventFactory = audioEventFactory; - DiscordEventFactory = discordEventFactory; - IRacingEventFactory = iRacingEventFactory; - TwitchEventFactory = twitchEventFactory; - LuaGlues = luaGlues; - Lua = new NLua.Lua(); - } - } -} \ No newline at end of file diff --git a/Components/ComponentRegistrator.cs b/Components/ComponentRegistrator.cs deleted file mode 100644 index 8c48703f..00000000 --- a/Components/ComponentRegistrator.cs +++ /dev/null @@ -1,31 +0,0 @@ -using Serilog; -using Slipstream.Shared; -using System; -using System.Collections.Generic; - -namespace Slipstream.Components -{ - internal class ComponentRegistrator : IComponentRegistrationContext - { - private readonly Dictionary> Plugins; - - public ILogger Logger { get; internal set; } - - public IEventBus EventBus { get; internal set; } - - public ComponentRegistrator( - Dictionary> plugins, - ILogger logger, - IEventBus eventBus) - { - Plugins = plugins; - Logger = logger; - EventBus = eventBus; - } - - public void RegisterPlugin(string name, Func plugin) - { - Plugins.Add(name, plugin); - } - } -} \ No newline at end of file diff --git a/Components/Discord/Discord.cs b/Components/Discord/Discord.cs deleted file mode 100644 index b52aba3f..00000000 --- a/Components/Discord/Discord.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Slipstream.Backend; - -namespace Slipstream.Components.Discord -{ - internal class Discord : IComponent - { - public void Register(IComponentRegistrationContext ctx) - { - var eventFactory = new EventFactory.DiscordEventFactory(); - - ctx.RegisterPlugin("DiscordPlugin", CreatePlugin); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new Plugins.DiscordPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.EventBus, - ctx.DiscordEventFactory, - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/Discord/Plugins/DiscordPlugin.cs b/Components/Discord/Plugins/DiscordPlugin.cs index 6df31361..0b05f59a 100644 --- a/Components/Discord/Plugins/DiscordPlugin.cs +++ b/Components/Discord/Plugins/DiscordPlugin.cs @@ -2,7 +2,6 @@ using DSharpPlus; using DSharpPlus.Entities; -using Serilog; using Slipstream.Components.Discord.EventHandler; using Slipstream.Shared; using Slipstream.Shared.Helpers.StrongParameters; @@ -12,7 +11,7 @@ namespace Slipstream.Components.Discord.Plugins { - internal class DiscordPlugin : BasePlugin + internal class DiscordPlugin : BasePlugin, IPlugin { private static readonly DictionaryValidator ConfigurationValidator; @@ -103,14 +102,19 @@ private Task Client_MessageCreated(DSharpPlus.EventArgs.MessageCreateEventArgs e return Task.CompletedTask; EventBus.PublishEvent(EventFactory.CreateDiscordMessageReceived( + fromId: e.Author.Id, + from: e.Author.Username + "#" + e.Author.Discriminator, channelId: e.Channel.Id, channel: e.Channel.Name, - from: e.Author.Username + "#" + e.Author.Discriminator, - fromId: e.Author.Id, message: e.Message.Content )); return Task.CompletedTask; } + + public IEnumerable CreateLuaGlues() + { + return new LuaGlue[] { new LuaGlue(EventBus, EventFactory) }; + } } } \ No newline at end of file diff --git a/Components/FileMonitor/FileMonitor.cs b/Components/FileMonitor/FileMonitor.cs deleted file mode 100644 index d9d66049..00000000 --- a/Components/FileMonitor/FileMonitor.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Slipstream.Components.FileMonitor.Plugins; - -namespace Slipstream.Components.FileMonitor -{ - internal class FileMonitor : IComponent - { - private const string NAME = "FileMonitorPlugin"; - - public void Register(IComponentRegistrationContext ctx) - { - ctx.RegisterPlugin(NAME, CreatePlugin); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new FileMonitorPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.FileMonitorEventFactory, - ctx.EventBus, - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/FileMonitor/Plugins/FileMonitorPlugin.cs b/Components/FileMonitor/Plugins/FileMonitorPlugin.cs index 83dac342..ced5406d 100644 --- a/Components/FileMonitor/Plugins/FileMonitorPlugin.cs +++ b/Components/FileMonitor/Plugins/FileMonitorPlugin.cs @@ -10,7 +10,7 @@ namespace Slipstream.Components.FileMonitor.Plugins { - internal class FileMonitorPlugin : BasePlugin + internal class FileMonitorPlugin : BasePlugin, IPlugin { private static readonly DictionaryValidator ConfigurationValidator; private readonly IFileMonitorEventFactory EventFactory; @@ -97,5 +97,10 @@ private void WatcherOnCreated(object sender, FileSystemEventArgs e) { EventBus.PublishEvent(EventFactory.CreateFileMonitorFileCreated(e.FullPath)); } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { }; + } } } \ No newline at end of file diff --git a/Components/IComponent.cs b/Components/IComponent.cs deleted file mode 100644 index 4b50c261..00000000 --- a/Components/IComponent.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Slipstream.Components -{ - internal interface IComponent - { - public void Register(IComponentRegistrationContext reg); - } -} \ No newline at end of file diff --git a/Components/IComponentPluginCreationContext.cs b/Components/IComponentPluginCreationContext.cs deleted file mode 100644 index 08c2d930..00000000 --- a/Components/IComponentPluginCreationContext.cs +++ /dev/null @@ -1,37 +0,0 @@ -using Slipstream.Backend; -using Slipstream.Components.AppilcationUpdate; -using Slipstream.Components.Audio; -using Slipstream.Components.Discord; -using Slipstream.Components.FileMonitor; -using Slipstream.Components.Internal; -using Slipstream.Components.IRacing; -using Slipstream.Components.Lua; -using Slipstream.Components.Playback; -using Slipstream.Components.Twitch; -using Slipstream.Components.UI; -using Slipstream.Shared.Helpers.StrongParameters; -using System.Collections.Generic; - -namespace Slipstream.Components -{ - internal interface IComponentPluginCreationContext : IComponentPluginDependencies - { - string PluginId { get; } - string PluginName { get; } - Parameters PluginParameters { get; } - IPluginManager PluginManager { get; } - IPluginFactory PluginFactory { get; } - IEventSerdeService EventSerdeService { get; } - IInternalEventFactory InternalEventFactory { get; } - IUIEventFactory UIEventFactory { get; } - IPlaybackEventFactory PlaybackEventFactory { get; } - ILuaEventFactory LuaEventFactory { get; } - IApplicationUpdateEventFactory ApplicationUpdateEventFactory { get; } - IFileMonitorEventFactory FileMonitorEventFactory { get; } - IAudioEventFactory AudioEventFactory { get; } - IDiscordEventFactory DiscordEventFactory { get; } - IIRacingEventFactory IRacingEventFactory { get; } - ITwitchEventFactory TwitchEventFactory { get; } - IEnumerable LuaGlues { get; } - } -} \ No newline at end of file diff --git a/Components/IComponentPluginDependencies.cs b/Components/IComponentPluginDependencies.cs deleted file mode 100644 index a13490ae..00000000 --- a/Components/IComponentPluginDependencies.cs +++ /dev/null @@ -1,14 +0,0 @@ -using Serilog; -using Slipstream.Components.Internal; -using Slipstream.Shared; - -namespace Slipstream.Components -{ - internal interface IComponentPluginDependencies - { - IEventHandlerController EventHandlerController { get; } - - ILogger Logger { get; } - IEventBus EventBus { get; } - } -} \ No newline at end of file diff --git a/Components/IComponentRegistrationContext.cs b/Components/IComponentRegistrationContext.cs deleted file mode 100644 index 8b7d70fb..00000000 --- a/Components/IComponentRegistrationContext.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Serilog; -using Slipstream.Shared; -using System; - -namespace Slipstream.Components -{ - internal interface IComponentRegistrationContext - { - public ILogger Logger { get; } - - public IEventBus EventBus { get; } - - public void RegisterPlugin(string name, Func plugin); - } -} \ No newline at end of file diff --git a/Components/ILuaGlue.cs b/Components/ILuaGlue.cs index 40181f4a..638423c5 100644 --- a/Components/ILuaGlue.cs +++ b/Components/ILuaGlue.cs @@ -1,6 +1,6 @@ namespace Slipstream.Components { - internal interface ILuaGlue + public interface ILuaGlue { void SetupLua(NLua.Lua lua); diff --git a/Components/IPlugin.cs b/Components/IPlugin.cs index 499ce834..65e54018 100644 --- a/Components/IPlugin.cs +++ b/Components/IPlugin.cs @@ -1,5 +1,6 @@ using Slipstream.Shared; using System; +using System.Collections.Generic; #nullable enable @@ -7,7 +8,7 @@ namespace Slipstream.Components { public interface IPlugin : IDisposable { - public event EventHandler? OnStateChanged; + public event EventHandler? OnStateChanged; public string Id { get; } public string Name { get; } @@ -17,5 +18,7 @@ public interface IPlugin : IDisposable public bool FullThreadControl { get; } public void Run(); + + public IEnumerable CreateLuaGlues(); } } \ No newline at end of file diff --git a/Components/IRacing/IIRacingEventFactory.cs b/Components/IRacing/IIRacingEventFactory.cs index ff5822b4..3a1b1d04 100644 --- a/Components/IRacing/IIRacingEventFactory.cs +++ b/Components/IRacing/IIRacingEventFactory.cs @@ -1,6 +1,5 @@ using Slipstream.Components.IRacing.Events; using Slipstream.Components.IRacing.Plugins.GameState; -using Slipstream.Shared; #nullable enable diff --git a/Components/IRacing/IRacing.cs b/Components/IRacing/IRacing.cs deleted file mode 100644 index 889709ab..00000000 --- a/Components/IRacing/IRacing.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Slipstream.Backend; -using Slipstream.Components.IRacing.Plugins; - -namespace Slipstream.Components.IRacing -{ - internal class IRacing : IComponent - { - private const string NAME = "IRacingPlugin"; - - public void Register(IComponentRegistrationContext ctx) - { - var eventFactory = new EventFactory.IRacingEventFactory(); - - ctx.RegisterPlugin(NAME, CreatePlugin); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new IRacingPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.IRacingEventFactory, - ctx.EventBus, - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/IRacing/Plugins/GameState/StateFactory.cs b/Components/IRacing/Plugins/GameState/StateFactory.cs index 522de3e0..e5e145f5 100644 --- a/Components/IRacing/Plugins/GameState/StateFactory.cs +++ b/Components/IRacing/Plugins/GameState/StateFactory.cs @@ -2,8 +2,8 @@ using iRacingSDK; using System; -using System.Linq; using System.Collections.Generic; +using System.Linq; using static Slipstream.Components.IRacing.IIRacingEventFactory; namespace Slipstream.Components.IRacing.Plugins.GameState diff --git a/Components/IRacing/Plugins/IRacingPlugin.cs b/Components/IRacing/Plugins/IRacingPlugin.cs index 134ff920..4544a736 100644 --- a/Components/IRacing/Plugins/IRacingPlugin.cs +++ b/Components/IRacing/Plugins/IRacingPlugin.cs @@ -2,13 +2,14 @@ using Slipstream.Shared; using Slipstream.Shared.Helpers.StrongParameters; using Slipstream.Shared.Helpers.StrongParameters.Validators; +using System.Collections.Generic; using System.Threading; #nullable enable namespace Slipstream.Components.IRacing.Plugins { - internal class IRacingPlugin : BasePlugin + internal class IRacingPlugin : BasePlugin, IPlugin { public static DictionaryValidator ConfigurationValidator { get; } @@ -70,5 +71,10 @@ public override void Run() Thread.Sleep(5000); } } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { new LuaGlue(EventBus, EventFactory) }; + } } } \ No newline at end of file diff --git a/Components/IRacing/Plugins/Models/LapState.cs b/Components/IRacing/Plugins/Models/LapState.cs index dd2a354f..258fb3bd 100644 --- a/Components/IRacing/Plugins/Models/LapState.cs +++ b/Components/IRacing/Plugins/Models/LapState.cs @@ -1,6 +1,4 @@ -using System; - -namespace Slipstream.Components.IRacing.Plugins.Models +namespace Slipstream.Components.IRacing.Plugins.Models { internal class LapState { diff --git a/Components/IRacing/Plugins/Trackers/IRacingSessionTracker.cs b/Components/IRacing/Plugins/Trackers/IRacingSessionTracker.cs index bec791db..4045ad56 100644 --- a/Components/IRacing/Plugins/Trackers/IRacingSessionTracker.cs +++ b/Components/IRacing/Plugins/Trackers/IRacingSessionTracker.cs @@ -2,7 +2,6 @@ using Slipstream.Components.IRacing.Plugins.Models; using Slipstream.Shared; using System; -using System.Collections.Generic; using static Slipstream.Components.IRacing.IIRacingEventFactory; #nullable enable diff --git a/Components/Internal/ILuaSevice.cs b/Components/Internal/ILuaService.cs similarity index 81% rename from Components/Internal/ILuaSevice.cs rename to Components/Internal/ILuaService.cs index 5d35324d..1ec539a4 100644 --- a/Components/Internal/ILuaSevice.cs +++ b/Components/Internal/ILuaService.cs @@ -2,7 +2,7 @@ namespace Slipstream.Components.Internal { - public interface ILuaSevice + public interface ILuaService { ILuaContext Parse(string filename); diff --git a/Components/Internal/Internal.cs b/Components/Internal/Internal.cs deleted file mode 100644 index ed5af0c8..00000000 --- a/Components/Internal/Internal.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Slipstream.Components.Internal.Services; - -namespace Slipstream.Components.Internal -{ - internal class Internal : IComponent - { - public void Register(IComponentRegistrationContext ctx) - { - } - } -} \ No newline at end of file diff --git a/Components/Internal/LuaGLues/StateLuaGlue.cs b/Components/Internal/LuaGLues/StateLuaGlue.cs index f7b5ded3..46291f90 100644 --- a/Components/Internal/LuaGLues/StateLuaGlue.cs +++ b/Components/Internal/LuaGLues/StateLuaGlue.cs @@ -1,6 +1,6 @@ #nullable enable -namespace Slipstream.Components.Internal.Services +namespace Slipstream.Components.Internal.LuaGlues { public class StateLuaGlue : ILuaGlue { diff --git a/Components/Internal/Plugins/InternalPlugin.cs b/Components/Internal/Plugins/InternalPlugin.cs new file mode 100644 index 00000000..7faf9da2 --- /dev/null +++ b/Components/Internal/Plugins/InternalPlugin.cs @@ -0,0 +1,44 @@ +using Serilog; +using Slipstream.Components.Internal.LuaGlues; +using Slipstream.Shared; +using System.Collections.Generic; + +namespace Slipstream.Components.Internal.Plugins +{ + public class InternalPlugin : BasePlugin, IPlugin + { + private readonly IInternalEventFactory EventFactory; + private readonly IEventBus EventBus; + private readonly ILogger Logger; + private readonly IEventSerdeService EventSerdeService; + private readonly IStateService StateService; + + public InternalPlugin( + IEventHandlerController eventHandlerController, + string id, + ILogger logger, + IEventBus eventBus, + IInternalEventFactory eventFactory, + IEventSerdeService eventSerdeService, + IStateService stateService) + : base(eventHandlerController, id, nameof(InternalPlugin), id, true) + { + EventFactory = eventFactory; + Logger = logger; + EventBus = eventBus; + EventSerdeService = eventSerdeService; + StateService = stateService; + } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] + { + new CoreLuaGlue(EventSerdeService), + new HttpLuaGlue(Logger), + new InternalLuaGlue(EventBus, EventFactory), + new StateLuaGlue(StateService) + }; + } + } +} \ No newline at end of file diff --git a/Components/Internal/Services/LuaService.cs b/Components/Internal/Services/LuaService.cs index 14eef659..885766cf 100644 --- a/Components/Internal/Services/LuaService.cs +++ b/Components/Internal/Services/LuaService.cs @@ -1,36 +1,44 @@ -using System.Collections.Generic; +using Slipstream.Backend; +using System.Collections.Generic; +using System.Diagnostics; using System.Text; #nullable enable namespace Slipstream.Components.Internal.Services { - internal class LuaService : ILuaSevice + internal class LuaService : ILuaService { + private readonly IPluginManager PluginManager; private readonly List LuaGlues = new List(); - private readonly NLua.Lua Lua; - public LuaService( - IEnumerable? luaGlues = null) + public LuaService(IPluginManager pluginManager) { - Lua = new NLua.Lua(); - Lua.State.Encoding = Encoding.UTF8; + PluginManager = pluginManager; + } + + public ILuaContext Parse(string filename) + { + if (LuaGlues.Count > 0) + throw new System.Exception("This have already been populated"); - luaGlues ??= new List(); + var Lua = new NLua.Lua(); + Lua.State.Encoding = Encoding.UTF8; - foreach (var lg in luaGlues) + PluginManager.ForAllPluginsExecute(plugin => { - LuaGlues.Add(lg); - } + foreach (var glue in plugin.CreateLuaGlues()) + { + LuaGlues.Add(glue); + } + }); - foreach (var glue in luaGlues) + foreach (var glue in LuaGlues) { + Debug.WriteLine($"Initializing NLua for {filename} with {glue}"); glue.SetupLua(Lua); } - } - public ILuaContext Parse(string filename) - { return new LuaContext(filename, Lua); } diff --git a/Components/Lua/Lua.cs b/Components/Lua/Lua.cs deleted file mode 100644 index 7057706a..00000000 --- a/Components/Lua/Lua.cs +++ /dev/null @@ -1,46 +0,0 @@ -using Slipstream.Components.Internal.Services; -using Slipstream.Components.Lua.Plugins; -using System.Collections.Generic; - -namespace Slipstream.Components.Lua -{ - internal class Lua : IComponent - { - public void Register(IComponentRegistrationContext ctx) - { - ctx.RegisterPlugin("LuaPlugin", CreateLuaPlugin); - ctx.RegisterPlugin("LuaManagerPlugin", CreateLuaManagerPlugin); - } - - private IPlugin CreateLuaManagerPlugin(IComponentPluginCreationContext ctx) - { - return new LuaManagerPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger.ForContext(typeof(LuaManagerPlugin)), - ctx.FileMonitorEventFactory, - ctx.EventBus, - ctx.PluginManager, - ctx.PluginFactory, - ctx.EventSerdeService); - } - - private IPlugin CreateLuaPlugin(IComponentPluginCreationContext ctx) - { - var luaService = new LuaService( - ctx.LuaGlues - ); - - return new LuaPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger.ForContext(typeof(LuaPlugin)), - ctx.LuaEventFactory, - ctx.InternalEventFactory, - ctx.EventBus, - luaService, - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/Lua/Plugins/LuaManagerPlugin.cs b/Components/Lua/Plugins/LuaManagerPlugin.cs index 5e693e8c..12db8147 100644 --- a/Components/Lua/Plugins/LuaManagerPlugin.cs +++ b/Components/Lua/Plugins/LuaManagerPlugin.cs @@ -16,7 +16,7 @@ namespace Slipstream.Components.Lua.Plugins { - internal class LuaManagerPlugin : BasePlugin + internal class LuaManagerPlugin : BasePlugin, IPlugin { private readonly ILogger Logger; private readonly IFileMonitorEventFactory EventFactory; @@ -209,5 +209,10 @@ public override void Run() BootupEventsDeadline = null; } } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { }; + } } } \ No newline at end of file diff --git a/Components/Lua/Plugins/LuaPlugin.cs b/Components/Lua/Plugins/LuaPlugin.cs index c68705d0..b7853604 100644 --- a/Components/Lua/Plugins/LuaPlugin.cs +++ b/Components/Lua/Plugins/LuaPlugin.cs @@ -4,19 +4,20 @@ using Slipstream.Components.UI.EventHandler; using Slipstream.Shared; using Slipstream.Shared.Helpers.StrongParameters; +using System.Collections.Generic; using System.IO; #nullable enable namespace Slipstream.Components.Lua.Plugins { - public class LuaPlugin : BasePlugin + public class LuaPlugin : BasePlugin, IPlugin { private readonly ILogger Logger; private readonly ILuaEventFactory LuaEventFactory; private readonly IInternalEventFactory InternalEventFactory; private readonly CapturingEventBus EventBus; - private readonly ILuaSevice LuaService; + private readonly ILuaService LuaService; private ILuaContext? LuaContext; private readonly string FilePath; @@ -27,7 +28,7 @@ public LuaPlugin( ILuaEventFactory luaEventFactory, IInternalEventFactory internalEventFactory, IEventBus eventBus, - ILuaSevice luaService, + ILuaService luaService, Parameters configuration ) : base(eventHandlerController, id, "LuaPlugin", id) { @@ -83,5 +84,10 @@ public override void Run() HandleLuaException(e); } } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { }; + } } } \ No newline at end of file diff --git a/Components/Playback/Playback.cs b/Components/Playback/Playback.cs deleted file mode 100644 index cfc46d2e..00000000 --- a/Components/Playback/Playback.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Slipstream.Backend; -using Slipstream.Components.Internal; -using Slipstream.Components.Playback.Plugins; - -namespace Slipstream.Components.Playback -{ - internal partial class Playback : IComponent - { - public void Register(IComponentRegistrationContext ctx) - { - var eventFactory = new EventFactory.PlaybackEventFactory(); - - ctx.RegisterPlugin("PlaybackPlugin", CreatePlugin); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new PlaybackPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger, - ctx.EventBus, - ctx.EventSerdeService - ); - } - } -} \ No newline at end of file diff --git a/Components/Playback/Plugins/PlaybackPlugin.cs b/Components/Playback/Plugins/PlaybackPlugin.cs index c1047003..473bd4aa 100644 --- a/Components/Playback/Plugins/PlaybackPlugin.cs +++ b/Components/Playback/Plugins/PlaybackPlugin.cs @@ -2,6 +2,7 @@ using Slipstream.Components.Internal; using Slipstream.Components.Playback.Events; using Slipstream.Shared; +using System.Collections.Generic; using System.IO; using System.Threading; @@ -9,16 +10,19 @@ namespace Slipstream.Components.Playback.Plugins { - internal class PlaybackPlugin : BasePlugin + internal class PlaybackPlugin : BasePlugin, IPlugin { private readonly ILogger Logger; private readonly IEventBus EventBus; private readonly IEventSerdeService EventSerdeService; + private readonly IPlaybackEventFactory EventFactory; - public PlaybackPlugin(IEventHandlerController eventHandlerController, string id, ILogger logger, IEventBus eventBus, IEventSerdeService eventSerdeService) : base(eventHandlerController, id, "PlaybackPlugin", id, true) + public PlaybackPlugin(IEventHandlerController eventHandlerController, string id, ILogger logger, IEventBus eventBus, IPlaybackEventFactory eventFactory, IEventSerdeService eventSerdeService) : base(eventHandlerController, id, "PlaybackPlugin", id, true) { EventBus = eventBus; EventSerdeService = eventSerdeService; + EventFactory = eventFactory; + var playback = EventHandlerController.Get(); playback.OnPlaybackCommandInjectEvents += (s, e) => OnPlaybackCommandInjectEvents(e); @@ -79,5 +83,10 @@ private void OnPlaybackCommandInjectEvents(PlaybackCommandInjectEvents @event) Logger.Warning($"Error reading {@event.Filename}: {e.Message}"); } } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { new LuaGlue(EventBus, EventFactory) }; + } } } \ No newline at end of file diff --git a/Components/Twitch/Plugins/TwitchPlugin.cs b/Components/Twitch/Plugins/TwitchPlugin.cs index 8ec96109..c4338a9d 100644 --- a/Components/Twitch/Plugins/TwitchPlugin.cs +++ b/Components/Twitch/Plugins/TwitchPlugin.cs @@ -17,7 +17,7 @@ namespace Slipstream.Components.Twitch.Plugins { - internal class TwitchPlugin : BasePlugin + internal class TwitchPlugin : BasePlugin, IPlugin { private readonly IEventBus EventBus; private readonly ITwitchEventFactory EventFactory; @@ -331,5 +331,10 @@ private void OnWhisperReceived(object sender, OnWhisperReceivedArgs e) EventFactory.CreateTwitchReceivedWhisper(message.DisplayName, message.Message) ); } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { new LuaGlue(EventBus, EventFactory) }; + } } } \ No newline at end of file diff --git a/Components/Twitch/Twitch.cs b/Components/Twitch/Twitch.cs deleted file mode 100644 index e0c01c44..00000000 --- a/Components/Twitch/Twitch.cs +++ /dev/null @@ -1,27 +0,0 @@ -using Slipstream.Backend; -using Slipstream.Components.Twitch.Plugins; - -namespace Slipstream.Components.Twitch -{ - internal class Twitch : IComponent - { - public void Register(IComponentRegistrationContext ctx) - { - var eventFactory = new EventFactory.TwitchEventFactory(); - - ctx.RegisterPlugin("TwitchPlugin", CreatePlugin); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new TwitchPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger.ForContext(typeof(Twitch)), - ctx.TwitchEventFactory, - ctx.EventBus, - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/Txrx/Plugins/ReceiverPlugin.cs b/Components/Txrx/Plugins/ReceiverPlugin.cs index 6a2eb44d..ffebc9e7 100644 --- a/Components/Txrx/Plugins/ReceiverPlugin.cs +++ b/Components/Txrx/Plugins/ReceiverPlugin.cs @@ -5,6 +5,7 @@ using Slipstream.Shared.Helpers.StrongParameters; using Slipstream.Shared.Helpers.StrongParameters.Validators; using System; +using System.Collections.Generic; using System.Diagnostics; using System.Net; using System.Net.Sockets; @@ -13,7 +14,7 @@ namespace Slipstream.Components.Txrx.Plugins { - public class ReceiverPlugin : BasePlugin + public class ReceiverPlugin : BasePlugin, IPlugin { internal static DictionaryValidator ConfigurationValidator { get; } @@ -130,5 +131,10 @@ public override void Run() ReadData(); } } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { }; + } } } \ No newline at end of file diff --git a/Components/Txrx/Plugins/TransmitterPlugin.cs b/Components/Txrx/Plugins/TransmitterPlugin.cs index 2e80b44b..1d50aa18 100644 --- a/Components/Txrx/Plugins/TransmitterPlugin.cs +++ b/Components/Txrx/Plugins/TransmitterPlugin.cs @@ -4,6 +4,7 @@ using Slipstream.Shared.Helpers.StrongParameters; using Slipstream.Shared.Helpers.StrongParameters.Validators; using System; +using System.Collections.Generic; using System.Diagnostics; using System.Net.Sockets; using System.Threading; @@ -12,7 +13,7 @@ namespace Slipstream.Components.Txrx.Plugins { - public class TransmitterPlugin : BasePlugin + public class TransmitterPlugin : BasePlugin, IPlugin { private static DictionaryValidator ConfigurationValidator { get; } @@ -132,5 +133,10 @@ public override void Run() Connect(); } } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { }; + } } } \ No newline at end of file diff --git a/Components/Txrx/Txrx.cs b/Components/Txrx/Txrx.cs deleted file mode 100644 index ddc0fe97..00000000 --- a/Components/Txrx/Txrx.cs +++ /dev/null @@ -1,47 +0,0 @@ -using Slipstream.Backend; -using Slipstream.Components.Internal; -using Slipstream.Components.Internal.Services; -using Slipstream.Components.Txrx.Plugins; -using Slipstream.Components.Txrx.Services; - -namespace Slipstream.Components.Audio -{ - internal class Txrx : IComponent - { - public void Register(IComponentRegistrationContext ctx) - { - ctx.RegisterPlugin("TransmitterPlugin", CreateTransmitterPlugin); - ctx.RegisterPlugin("ReceiverPlugin", CreateReceiverPlugin); - } - - private IPlugin CreateReceiverPlugin(IComponentPluginCreationContext ctx) - { - var txrxService = new TxrxService(new EventSerdeService()); - - return new ReceiverPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger, - ctx.InternalEventFactory, - ctx.EventBus, - txrxService, - ctx.PluginParameters - ); - } - - private IPlugin CreateTransmitterPlugin(IComponentPluginCreationContext ctx) - { - var txrxService = new TxrxService(new EventSerdeService()); - - return new TransmitterPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger, - ctx.InternalEventFactory, - ctx.EventBus, - txrxService, - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/UI/LuaGlue.cs b/Components/UI/LuaGlue.cs index 7f119260..5e879ff6 100644 --- a/Components/UI/LuaGlue.cs +++ b/Components/UI/LuaGlue.cs @@ -1,7 +1,5 @@ using Serilog; using Slipstream.Shared; -using Slipstream.Shared.Helpers.StrongParameters; -using System.IO; #nullable enable @@ -14,12 +12,12 @@ public class LuaGlue : ILuaGlue private readonly IUIEventFactory EventFactory; private readonly string Prefix; - public LuaGlue(ILogger logger, IEventBus eventBus, IUIEventFactory eventFactory, Parameters configuration) + public LuaGlue(ILogger logger, IEventBus eventBus, IUIEventFactory eventFactory, string prefix) { Logger = logger; EventBus = eventBus; EventFactory = eventFactory; - Prefix = Path.GetFileName(configuration.GetOrDefault("filepath", "")); + Prefix = prefix; } public void SetupLua(NLua.Lua lua) diff --git a/Components/UI/Plugins/InternalPlugin.cs b/Components/UI/Plugins/InternalPlugin.cs new file mode 100644 index 00000000..ad33f055 --- /dev/null +++ b/Components/UI/Plugins/InternalPlugin.cs @@ -0,0 +1,39 @@ +using Serilog; +using Slipstream.Shared; +using Slipstream.Shared.Helpers.StrongParameters; +using System.Collections.Generic; +using System.IO; + +namespace Slipstream.Components.UI.Plugins +{ + public class UIPlugin : BasePlugin, IPlugin + { + private readonly IUIEventFactory EventFactory; + private readonly IEventBus EventBus; + private readonly ILogger Logger; + private readonly string Prefix; + + public UIPlugin( + IEventHandlerController eventHandlerController, + string id, + ILogger logger, + IEventBus eventBus, + IUIEventFactory eventFactory, + Parameters configuration + ) : base(eventHandlerController, id, nameof(UIPlugin), id, true) + { + EventFactory = eventFactory; + Logger = logger; + EventBus = eventBus; + Prefix = Path.GetFileName(configuration.GetOrDefault("filepath", "")); + } + + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] + { + new LuaGlue(Logger, EventBus, EventFactory, Prefix) + }; + } + } +} \ No newline at end of file diff --git a/Components/UI/UI.cs b/Components/UI/UI.cs deleted file mode 100644 index 3f7de6e4..00000000 --- a/Components/UI/UI.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace Slipstream.Components.UI -{ - internal class UI : IComponent - { - public void Register(IComponentRegistrationContext ctx) - { - } - } -} \ No newline at end of file diff --git a/Components/WinFormUI/Plugins/WinFormUIPlugin.cs b/Components/WinFormUI/Plugins/WinFormUIPlugin.cs index a8235550..26cdf18d 100644 --- a/Components/WinFormUI/Plugins/WinFormUIPlugin.cs +++ b/Components/WinFormUI/Plugins/WinFormUIPlugin.cs @@ -3,13 +3,14 @@ using Slipstream.Components.UI; using Slipstream.Components.WinFormUI.Forms; using Slipstream.Shared; +using System.Collections.Generic; using System.Windows.Forms; #nullable enable namespace Slipstream.Components.WinFormUI.Plugins { - internal class WinFormUIPlugin : BasePlugin + internal class WinFormUIPlugin : BasePlugin, IPlugin { private readonly IEventBus EventBus; private readonly IApplicationVersionService ApplicationVersionService; @@ -26,6 +27,11 @@ public WinFormUIPlugin(IEventHandlerController eventHandlerController, string id ApplicationVersionService = applicationVersionService; } + public IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { }; + } + public override void Run() { Application.Run(new MainWindow(InternalEventFactory, UIEventFactory, PlaybackEventFactory, EventBus, ApplicationVersionService, EventHandlerController)); diff --git a/Components/WinFormUI/WinFormUI.cs b/Components/WinFormUI/WinFormUI.cs deleted file mode 100644 index 5e27bd9c..00000000 --- a/Components/WinFormUI/WinFormUI.cs +++ /dev/null @@ -1,28 +0,0 @@ -using Slipstream.Components.WinFormUI.Plugins; -using Slipstream.Shared; - -namespace Slipstream.Components.WinFormUI -{ - internal class WinFormUI : IComponent - { - private const string NAME = "WinFormUIPlugin"; - - public void Register(IComponentRegistrationContext ctx) - { - ctx.RegisterPlugin(NAME, CreatePlugin); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new WinFormUIPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.InternalEventFactory, - ctx.UIEventFactory, - ctx.PlaybackEventFactory, - ctx.EventBus, - new ApplicationVersionService() - ); - } - } -} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 97291371..c4c9b487 100644 --- a/Program.cs +++ b/Program.cs @@ -1,6 +1,5 @@ using Autofac; using Serilog; -using Slipstream.Components; using Slipstream.Components.Internal; using Slipstream.Components.Internal.Services; using Slipstream.Shared; @@ -48,7 +47,7 @@ private static void ConfigureServices(ContainerBuilder builder) builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().As().As().SingleInstance(); - builder.RegisterType().As().SingleInstance(); + builder.RegisterType().As().InstancePerDependency(); builder.RegisterType().As().SingleInstance(); builder.RegisterType().SingleInstance(); builder.Register(c => new StateService(c.Resolve(), Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\Slipstream\state.txt")).As().SingleInstance(); @@ -75,11 +74,11 @@ private static void ConfigureServices(ContainerBuilder builder) .SingleInstance(); builder.RegisterType().As().InstancePerDependency(); - // LuaGlues + // Plugins builder.RegisterAssemblyTypes(typeof(Program).Assembly) - .Where(t => t.IsAssignableTo()) + .Where(t => t.IsAssignableTo()) .Where(t => !t.IsAbstract) - .As() + .As() .AsSelf() .InstancePerDependency(); } diff --git a/Shared/EventHandlerController.cs b/Shared/EventHandlerController.cs index f873dbd0..33ff25ff 100644 --- a/Shared/EventHandlerController.cs +++ b/Shared/EventHandlerController.cs @@ -2,7 +2,6 @@ using Slipstream.Backend; using System; using System.Collections.Generic; -using static Slipstream.Shared.IEventHandlerController; #nullable enable diff --git a/Shared/Helpers/StrongParameters/Validators/ArrayValidator.cs b/Shared/Helpers/StrongParameters/Validators/ArrayValidator.cs index c3dd1c61..2854db37 100644 --- a/Shared/Helpers/StrongParameters/Validators/ArrayValidator.cs +++ b/Shared/Helpers/StrongParameters/Validators/ArrayValidator.cs @@ -4,7 +4,7 @@ namespace Slipstream.Shared.Helpers.StrongParameters.Validators { - internal class ArrayFailValidator : IValidator + public class ArrayFailValidator : IValidator { public bool Required => false; @@ -14,7 +14,7 @@ public void Validate(object v) } } - internal class ArrayValidator : IValidator + public class ArrayValidator : IValidator { private IValidator Schema = new ArrayFailValidator(); diff --git a/Shared/Helpers/StrongParameters/Validators/DictionaryValidator.cs b/Shared/Helpers/StrongParameters/Validators/DictionaryValidator.cs index 5d9fb5cd..0f4c347a 100644 --- a/Shared/Helpers/StrongParameters/Validators/DictionaryValidator.cs +++ b/Shared/Helpers/StrongParameters/Validators/DictionaryValidator.cs @@ -5,7 +5,7 @@ namespace Slipstream.Shared.Helpers.StrongParameters.Validators { - internal class DictionaryValidator : IValidator + public class DictionaryValidator : IValidator { private readonly Dictionary Schema = new Dictionary(); private bool AllowExtras = false; diff --git a/Slipstream.csproj b/Slipstream.csproj index bd2beff7..86bba9f3 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -203,14 +203,12 @@ - - @@ -222,7 +220,7 @@ - + @@ -255,36 +253,23 @@ - - - - + - - - - + - - - - - - - @@ -317,7 +302,6 @@ -