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/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 01b33810..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,23 +22,31 @@ 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, + IInternalEventFactory eventFactory, + IEventBus eventBus, + IPluginFactory pluginFactory, + IPluginManager pluginManager, + IEventHandlerController eventHandlerController, + ILuaService luaService + ) { - EventFactory = eventFactory.Get(); + EventFactory = eventFactory; EventBus = eventBus; PluginFactory = pluginFactory; PluginManager = pluginManager; Logger = logger; - EventHandlerController = eventHandlerControllerBuilder.CreateEventHandlerController(); + EventHandlerController = eventHandlerController; Subscription = EventBus.RegisterListener(); 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.. { @@ -53,8 +60,9 @@ public Engine(ILogger logger, IEventFactory eventFactory, IEventBus eventBus, IP 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/IEngine.cs b/Backend/IEngine.cs index 1d61aebf..0de11e7d 100644 --- a/Backend/IEngine.cs +++ b/Backend/IEngine.cs @@ -1,13 +1,12 @@ using Slipstream.Shared; +using System; namespace Slipstream.Backend { - public interface IEngine + public interface IEngine : IDisposable { void UnregisterSubscription(IEventBusSubscription subscription); void Start(); - - void Dispose(); } } \ No newline at end of file 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 e01a171a..2645f105 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; @@ -7,7 +8,6 @@ using Slipstream.Shared.Helpers.StrongParameters; using System; using System.Collections.Generic; -using System.Linq; using static Slipstream.Components.Internal.IInternalEventFactory; namespace Slipstream.Backend @@ -17,29 +17,27 @@ public class PluginManager : IPluginManager, IPluginFactory private readonly IInternalEventFactory InternalEventFactory; private readonly IEventBus EventBus; private readonly IDictionary PluginWorkers = new Dictionary(); + private readonly IDictionary PluginFactories = new Dictionary(); private readonly ILogger Logger; - private readonly ComponentRegistrator Registrator; - private readonly List LuaGluesFactories = new List(); - private readonly Dictionary> ComponentPlugins = new Dictionary>(); + private readonly ILifetimeScope LifetimeScope; public PluginManager( - IEventFactory eventFactory, + IInternalEventFactory internalEventFactory, IEventBus eventBus, - IServiceLocator serviceLocator, ILogger logger, - EventHandlerControllerBuilder eventHandlerControllerBuilder) + ILifetimeScope lifetimeScope + ) { - Registrator = new ComponentRegistrator(ComponentPlugins, LuaGluesFactories, eventFactory, logger, eventBus, eventHandlerControllerBuilder, serviceLocator); - - 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 = eventFactory.Get(); + InternalEventFactory = internalEventFactory; EventBus = eventBus; Logger = logger; + LifetimeScope = lifetimeScope; } public void UnregisterPlugin(IPlugin p) @@ -131,10 +129,15 @@ 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); - if (!ComponentPlugins.ContainsKey(pluginName)) + if (!PluginFactories.ContainsKey(pluginName)) throw new KeyNotFoundException($"Plugin name '{pluginName}' not found"); - return ComponentPlugins[pluginName].Invoke(reg); + + return (IPlugin)LifetimeScope.Resolve( + PluginFactories[pluginName], + new NamedParameter("id", pluginId), + new NamedParameter("configuration", configuration), + new NamedParameter("eventBus", eventBus) + ); } public void Dispose() diff --git a/Backend/PluginWorker.cs b/Backend/PluginWorker.cs index 116bc63a..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(IPlugin plugin, IPlugin.EventHandlerArgs e) + 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 aabc40e4..00000000 --- a/Components/AppilcationUpdate/ApplicationUpdate.cs +++ /dev/null @@ -1,29 +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) - { - var eventFactory = new EventFactory.ApplicationUpdateEventFactory(); - - ctx.RegisterEventFactory(typeof(IApplicationUpdateEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.ApplicationUpdateEventHandler)); - ctx.RegisterPlugin(NAME, CreatePlugin); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new ApplicationUpdatePlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.EventFactory.Get(), - ctx.Logger.ForContext(typeof(ApplicationUpdate)), - ctx.EventBus, - ctx.PluginParameters - ); - } - } -} diff --git a/Components/AppilcationUpdate/EventHandler/ApplicationUpdateEventHandler.cs b/Components/AppilcationUpdate/EventHandler/ApplicationUpdateEventHandler.cs index 8a5aae74..2a7d0651 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,14 +8,9 @@ namespace Slipstream.Components.AppilcationUpdate.EventHandler { internal class ApplicationUpdateEventHandler : IEventHandler { - public event EventHandler OnApplicationUpdateLatestVersionChanged; - public event EventHandler OnApplicationUpdateCommandCheckLatestVersion; - private readonly IEventHandlerController Parent; + public event EventHandler? OnApplicationUpdateLatestVersionChanged; - public ApplicationUpdateEventHandler(IEventHandlerController eventHandler) - { - Parent = eventHandler; - } + public event EventHandler? OnApplicationUpdateCommandCheckLatestVersion; public IEventHandler.HandledStatus HandleEvent(IEvent @event) { @@ -25,14 +22,14 @@ 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); + onEvent.Invoke(this, args); return IEventHandler.HandledStatus.Handled; } 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..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; @@ -30,11 +31,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 +68,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 +120,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 +137,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())); @@ -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 eec0b247..00000000 --- a/Components/Audio/Audio.cs +++ /dev/null @@ -1,32 +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); - ctx.RegisterEventFactory(typeof(IAudioEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.AudioEventHandler)); - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); - } - - private IPlugin CreateAudioPlugin(IComponentPluginCreationContext ctx) - { - return new AudioPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger, - ctx.EventBus, - ctx.EventFactory.Get(), - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/Audio/EventHandler/AudioEventHandler.cs b/Components/Audio/EventHandler/AudioEventHandler.cs index 3dff091b..a16d61fe 100644 --- a/Components/Audio/EventHandler/AudioEventHandler.cs +++ b/Components/Audio/EventHandler/AudioEventHandler.cs @@ -2,95 +2,43 @@ using Slipstream.Components.Audio.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Audio.EventHandler { internal class AudioEventHandler : IEventHandler { - private readonly EventHandlerController Parent; + public event EventHandler? OnAudioCommandPlay; - public AudioEventHandler(EventHandlerController eventHandler) - { - Parent = eventHandler; - } - - public delegate void OnAudioCommandPlayHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnAudioCommandSayHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnAudioCommandSendDevicesHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnAudioCommandSetOutputDeviceHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnAudioOutputDeviceHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnAudioCommandSay; - public event OnAudioCommandPlayHandler? OnAudioCommandPlay; + public event EventHandler? OnAudioCommandSendDevices; - public event OnAudioCommandSayHandler? OnAudioCommandSay; + public event EventHandler? OnAudioCommandSetOutputDevice; - 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(this, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file 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/Audio/Plugins/AudioPlugin.cs b/Components/Audio/Plugins/AudioPlugin.cs index c215bac7..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; } @@ -55,10 +56,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) @@ -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 f1092b7d..292dfa54 100644 --- a/Components/BasePlugin.cs +++ b/Components/BasePlugin.cs @@ -1,10 +1,11 @@ #nullable enable using Slipstream.Shared; +using System; namespace Slipstream.Components { - public class BasePlugin : IPlugin + public abstract class BasePlugin { public string Id { get; } = "INVALID-PLUGIN-ID"; private string name = "INVALID-PLUGIN-NAME"; @@ -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/ComponentPluginCreationContext.cs b/Components/ComponentPluginCreationContext.cs deleted file mode 100644 index 59206f22..00000000 --- a/Components/ComponentPluginCreationContext.cs +++ /dev/null @@ -1,72 +0,0 @@ -using Serilog; -using Slipstream.Backend; -using Slipstream.Components.Internal; -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 { return ComponentRegistration.EventHandlerControllerBuilder.CreateEventHandlerController(); } - } - - public ILogger Logger - { - get { return ComponentRegistration.Logger; } - } - - public IEventBus EventBus - { - get { return ComponentRegistration.EventBus; } - } - - public IEventFactory EventFactory - { - get { return ComponentRegistration.EventFactory; } - } - - public IServiceLocator ServiceLocator - { - get { return ComponentRegistration.ServiceLocator; } - } - - public string PluginId { get; } - - public string PluginName { get; } - - public Parameters PluginParameters { get; } - - public NLua.Lua Lua { get; } - - public List LuaGlueFactories { get; } - - public IPluginManager PluginManager { get; } - - public IPluginFactory PluginFactory { get; } - - public ComponentPluginCreationContext( - ComponentRegistrator componentRegistration, - IPluginManager pluginManager, - IPluginFactory pluginFactory, - List luaGlueFactories, - string pluginId, - string pluginName, - Parameters pluginParameters) - { - ComponentRegistration = componentRegistration; - PluginManager = pluginManager; - PluginFactory = pluginFactory; - LuaGlueFactories = luaGlueFactories; - PluginId = pluginId; - PluginName = pluginName; - PluginParameters = pluginParameters; - 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 bad22161..00000000 --- a/Components/ComponentRegistrator.cs +++ /dev/null @@ -1,64 +0,0 @@ -using Serilog; -using Slipstream.Backend; -using Slipstream.Components.Internal; -using Slipstream.Shared; -using System; -using System.Collections.Generic; - -namespace Slipstream.Components -{ - internal class ComponentRegistrator : IComponentRegistrationContext - { - private readonly List LuaGlueFactories; - private readonly Dictionary> Plugins; - - public ILogger Logger { get; internal set; } - - public IEventBus EventBus { get; internal set; } - - public IEventFactory EventFactory { get; internal set; } - - public IServiceLocator ServiceLocator { get; internal set; } - - public EventHandlerControllerBuilder EventHandlerControllerBuilder { get; } - - public ComponentRegistrator( - Dictionary> plugins, - List luaGlueFactories, - IEventFactory eventFactory, - ILogger logger, - IEventBus eventBus, - EventHandlerControllerBuilder eventHandlerControllerBuilder, - IServiceLocator serviceLocator - ) - { - Plugins = plugins; - LuaGlueFactories = luaGlueFactories; - EventHandlerControllerBuilder = eventHandlerControllerBuilder; - EventFactory = eventFactory; - Logger = logger; - EventBus = eventBus; - ServiceLocator = serviceLocator; - } - - 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); - } - - public void RegisterLuaGlue(ILuaGlueFactory luaGlueFactory) - { - LuaGlueFactories.Add(luaGlueFactory); - } - } -} \ No newline at end of file diff --git a/Components/Discord/Discord.cs b/Components/Discord/Discord.cs deleted file mode 100644 index 63c3da34..00000000 --- a/Components/Discord/Discord.cs +++ /dev/null @@ -1,28 +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); - ctx.RegisterEventHandler(typeof(EventHandler.DiscordEventHandler)); - ctx.RegisterEventFactory(typeof(IDiscordEventFactory), eventFactory); - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new Plugins.DiscordPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.EventBus, - ctx.EventFactory.Get(), - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/Discord/EventHandler/DiscordEventHandler.cs b/Components/Discord/EventHandler/DiscordEventHandler.cs index ed8aa4cc..db1cd35e 100644 --- a/Components/Discord/EventHandler/DiscordEventHandler.cs +++ b/Components/Discord/EventHandler/DiscordEventHandler.cs @@ -1,80 +1,41 @@ -using Slipstream.Components.Discord.Events; +#nullable enable + +using Slipstream.Components.Discord.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Discord.EventHandler { 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 EventHandler? OnDiscordConnected; - public event OnDiscordConnectedHandler? OnDiscordConnected; + public event EventHandler? OnDiscordDisconnected; - public event OnDiscordDisconnectedHandler? OnDiscordDisconnected; + public event EventHandler? OnDiscordMessageReceived; - public event OnDiscordMessageReceivedHandler? OnDiscordMessageReceived; + public event EventHandler? OnDiscordCommandSendMessage; - public event OnDiscordCommandSendMessageHandler? OnDiscordCommandSendMessage; - - public DiscordEventHandler(EventHandlerController eventHandler) + public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - Parent = eventHandler; + return @event switch + { + DiscordConnected tev => OnEvent(OnDiscordConnected, tev), + DiscordDisconnected tev => OnEvent(OnDiscordDisconnected, tev), + DiscordMessageReceived tev => OnEvent(OnDiscordMessageReceived, tev), + DiscordCommandSendMessage tev => OnEvent(OnDiscordCommandSendMessage, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; } - public IEventHandler.HandledStatus HandleEvent(IEvent @event) + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) { - switch (@event) + if (onEvent != null) { - 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; - } + onEvent.Invoke(this, args); + return IEventHandler.HandledStatus.Handled; } - - return IEventHandler.HandledStatus.NotMine; + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/Discord/LuaGlueFactory.cs b/Components/Discord/LuaGlueFactory.cs deleted file mode 100644 index d4b5093f..00000000 --- a/Components/Discord/LuaGlueFactory.cs +++ /dev/null @@ -1,21 +0,0 @@ -using Slipstream.Shared; - -namespace Slipstream.Components.Discord -{ - internal class LuaGlueFactory : ILuaGlueFactory - { - private readonly IEventBus EventBus; - private readonly IDiscordEventFactory EventFactory; - - public LuaGlueFactory(IEventBus eventBus, IDiscordEventFactory 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/Discord/Plugins/DiscordPlugin.cs b/Components/Discord/Plugins/DiscordPlugin.cs index 7fcb486d..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; @@ -41,19 +40,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() @@ -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/EventHandler/FileMonitor.cs b/Components/FileMonitor/EventHandler/FileMonitor.cs index f9cb8a88..2a5fdf00 100644 --- a/Components/FileMonitor/EventHandler/FileMonitor.cs +++ b/Components/FileMonitor/EventHandler/FileMonitor.cs @@ -2,109 +2,46 @@ using Slipstream.Components.FileMonitor.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.FileMonitor.EventHandler { internal class FileMonitor : IEventHandler { - private readonly IEventHandlerController Parent; + public event EventHandler? OnFileMonitorCommandScan; - public FileMonitor(IEventHandlerController eventHandler) - { - Parent = eventHandler; - } - - public delegate void OnFileMonitorCommandScanHandler(IEventHandlerController source, EventHandlerArgs e); - - public delegate void OnFileMonitorFileChangedHandler(IEventHandlerController source, EventHandlerArgs e); - - public delegate void OnFileMonitorFileCreatedHandler(IEventHandlerController source, EventHandlerArgs e); - - public delegate void OnFileMonitorFileDeletedHandler(IEventHandlerController source, EventHandlerArgs e); - - public delegate void OnFileMonitorFileRenamedHandler(IEventHandlerController source, EventHandlerArgs e); - - public delegate void OnFileMonitorScanCompletedHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnFileMonitorFileChanged; - public event OnFileMonitorCommandScanHandler? OnFileMonitorCommandScan; + public event EventHandler? OnFileMonitorFileCreated; - public event OnFileMonitorFileChangedHandler? OnFileMonitorFileChanged; + public event EventHandler? OnFileMonitorFileDeleted; - public event OnFileMonitorFileCreatedHandler? OnFileMonitorFileCreated; + public event EventHandler? OnFileMonitorFileRenamed; - 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(this, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/FileMonitor/FileMonitor.cs b/Components/FileMonitor/FileMonitor.cs deleted file mode 100644 index 58e13d05..00000000 --- a/Components/FileMonitor/FileMonitor.cs +++ /dev/null @@ -1,30 +0,0 @@ -using Slipstream.Backend; -using Slipstream.Components.FileMonitor.Plugins; - -namespace Slipstream.Components.FileMonitor -{ - internal class FileMonitor : IComponent - { - private const string NAME = "FileMonitorPlugin"; - - public void Register(IComponentRegistrationContext ctx) - { - var eventFactory = new EventFactory.FileMonitorEventFactory(); - - ctx.RegisterPlugin(NAME, CreatePlugin); - ctx.RegisterEventFactory(typeof(IFileMonitorEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.FileMonitor)); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new FileMonitorPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.EventFactory.Get(), - 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 a20eabed..00000000 --- a/Components/IComponentPluginCreationContext.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Slipstream.Backend; -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; } - List LuaGlueFactories { get; } - IPluginManager PluginManager { get; } - IPluginFactory PluginFactory { get; } - } -} \ No newline at end of file diff --git a/Components/IComponentPluginDependencies.cs b/Components/IComponentPluginDependencies.cs deleted file mode 100644 index 807862f1..00000000 --- a/Components/IComponentPluginDependencies.cs +++ /dev/null @@ -1,15 +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; } - IEventFactory EventFactory { get; } - IServiceLocator ServiceLocator { get; } - } -} \ No newline at end of file diff --git a/Components/IComponentRegistrationContext.cs b/Components/IComponentRegistrationContext.cs deleted file mode 100644 index 4e141c25..00000000 --- a/Components/IComponentRegistrationContext.cs +++ /dev/null @@ -1,23 +0,0 @@ -using Serilog; -using Slipstream.Backend; -using Slipstream.Shared; -using System; - -namespace Slipstream.Components -{ - internal interface IComponentRegistrationContext - { - public ILogger Logger { get; } - public IEventBus EventBus { get; } - - public EventHandlerControllerBuilder EventHandlerControllerBuilder { 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/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/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/IPlugin.cs b/Components/IPlugin.cs index 4847f85d..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,19 +8,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; } @@ -29,5 +18,7 @@ public EventHandlerArgs(T e) public bool FullThreadControl { get; } public void Run(); + + public IEnumerable CreateLuaGlues(); } } \ No newline at end of file diff --git a/Components/IRacing/EventHandler/IRacing.cs b/Components/IRacing/EventHandler/IRacing.cs index 384c2d22..e6782974 100644 --- a/Components/IRacing/EventHandler/IRacing.cs +++ b/Components/IRacing/EventHandler/IRacing.cs @@ -2,376 +2,103 @@ using Slipstream.Components.IRacing.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.IRacing.EventHandler { internal class IRacing : IEventHandler { - private readonly EventHandlerController Parent; + public event EventHandler? OnIRacingCarCompletedLap; - public IRacing(EventHandlerController eventHandler) - { - Parent = eventHandler; - } - - public delegate void OnIRacingCarCompletedLapHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingCarInfoHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingCommandSendCarInfoHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingCommandSendRaceFlagsHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingCommandSendSessionStateHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingCommandSendTrackInfoHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingCommandSendWeatherInfoHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingConnectedHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingDisconnectedHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingDriverIncidentHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingPitEnterHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingPitExitHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingPitstopReportHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingRaceFlagsHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnIRacingTrackInfoHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCarInfo; - public delegate void OnIRacingWeatherInfoHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCommandSendCarInfo; - public delegate void OnIRacingPracticeHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCommandSendRaceFlags; - public delegate void OnIRacingQualifyHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCommandSendSessionState; - public delegate void OnIRacingRaceHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCommandSendTrackInfo; - public delegate void OnIRacingTestingHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingCommandSendWeatherInfo; - public delegate void OnIRacingWarmupHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingConnected; - public delegate void OnIRacingCarPositionHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingDisconnected; - public delegate void OnIRacingRawHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingDriverIncident; - public delegate void OnIRacingTowedHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingPitEnter; - public delegate void OnIRacingTrackPositionHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnIRacingPitExit; - public event OnIRacingCarCompletedLapHandler? OnIRacingCarCompletedLap; + public event EventHandler? OnIRacingPitstopReport; - public event OnIRacingCarInfoHandler? OnIRacingCarInfo; + public event EventHandler? OnIRacingRaceFlags; - public event OnIRacingCommandSendCarInfoHandler? OnIRacingCommandSendCarInfo; + public event EventHandler? OnIRacingTrackInfo; - public event OnIRacingCommandSendRaceFlagsHandler? OnIRacingCommandSendRaceFlags; + public event EventHandler? OnIRacingWeatherInfo; - public event OnIRacingCommandSendSessionStateHandler? OnIRacingCommandSendSessionState; + public event EventHandler? OnIRacingPractice; - public event OnIRacingCommandSendTrackInfoHandler? OnIRacingCommandSendTrackInfo; + public event EventHandler? OnIRacingQualify; - public event OnIRacingCommandSendWeatherInfoHandler? OnIRacingCommandSendWeatherInfo; + public event EventHandler? OnIRacingRace; - public event OnIRacingConnectedHandler? OnIRacingConnected; + public event EventHandler? OnIRacingTesting; - public event OnIRacingDisconnectedHandler? OnIRacingDisconnected; + public event EventHandler? OnIRacingWarmup; - public event OnIRacingDriverIncidentHandler? OnIRacingDriverIncident; + public event EventHandler? OnIRacingCarPosition; - public event OnIRacingPitEnterHandler? OnIRacingPitEnter; + public event EventHandler? OnIRacingRaw; - public event OnIRacingPitExitHandler? OnIRacingPitExit; + public event EventHandler? OnIRacingTowed; - 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(this, args); + return IEventHandler.HandledStatus.Handled; } - - return IEventHandler.HandledStatus.NotMine; + return IEventHandler.HandledStatus.UseDefault; } } } \ 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 2fd80453..00000000 --- a/Components/IRacing/IRacing.cs +++ /dev/null @@ -1,31 +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); - ctx.RegisterEventFactory(typeof(IIRacingEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.IRacing)); - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new IRacingPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.EventFactory.Get(), - ctx.EventBus, - ctx.PluginParameters - ); - } - } -} \ No newline at end of file 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/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/EventHandler/Internal.cs b/Components/Internal/EventHandler/Internal.cs index 53bd8a00..78ff7cbc 100644 --- a/Components/Internal/EventHandler/Internal.cs +++ b/Components/Internal/EventHandler/Internal.cs @@ -2,111 +2,46 @@ using Slipstream.Components.Internal.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Internal.EventHandler { internal class Internal : IEventHandler { - private readonly IEventHandlerController Parent; + public event EventHandler? OnInternalCommandPluginRegister; - public Internal(IEventHandlerController parent) - { - Parent = parent; - } - - public delegate void OnInternalCommandPluginRegisterHandler(IEventHandlerController source, EventHandlerArgs e); - - public delegate void OnInternalCommandPluginStatesHandler(IEventHandlerController source, EventHandlerArgs e); - - public delegate void OnInternalCommandPluginUnregisterHandler(IEventHandlerController source, EventHandlerArgs e); - - public delegate void OnInternalPluginStateHandler(IEventHandlerController source, EventHandlerArgs e); - - public delegate void OnInternalShutdownHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnInternalCommandPluginStates; - public delegate void OnInternalCommandShutdownHandler(IEventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnInternalCommandPluginUnregister; - public event OnInternalCommandPluginRegisterHandler? OnInternalCommandPluginRegister; + public event EventHandler? OnInternalPluginState; - public event OnInternalCommandPluginStatesHandler? OnInternalCommandPluginStates; + public event EventHandler? OnInternalShutdown; - public event OnInternalCommandPluginUnregisterHandler? OnInternalCommandPluginUnregister; + public event EventHandler? OnInternalCommandShutdown; - 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(this, args); + return IEventHandler.HandledStatus.Handled; } - - return IEventHandler.HandledStatus.NotMine; + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file 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 afc15c25..00000000 --- a/Components/Internal/Internal.cs +++ /dev/null @@ -1,19 +0,0 @@ -using Slipstream.Components.Internal.Services; - -namespace Slipstream.Components.Internal -{ - internal class Internal : IComponent - { - 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)); - 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/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/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/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/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..885766cf 100644 --- a/Components/Internal/Services/LuaService.cs +++ b/Components/Internal/Services/LuaService.cs @@ -1,32 +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 List LuaGlues; - private readonly NLua.Lua Lua; + private readonly IPluginManager PluginManager; + private readonly List LuaGlues = new List(); - public LuaService( - List? luaGlues = null) + public LuaService(IPluginManager pluginManager) { - Lua = new NLua.Lua(); + PluginManager = pluginManager; + } + + public ILuaContext Parse(string filename) + { + if (LuaGlues.Count > 0) + throw new System.Exception("This have already been populated"); + + var Lua = new NLua.Lua(); Lua.State.Encoding = Encoding.UTF8; - luaGlues ??= new List(); - LuaGlues = luaGlues; + PluginManager.ForAllPluginsExecute(plugin => + { + 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/EventHandler/Lua.cs b/Components/Lua/EventHandler/Lua.cs index d99928dc..42f379fe 100644 --- a/Components/Lua/EventHandler/Lua.cs +++ b/Components/Lua/EventHandler/Lua.cs @@ -2,39 +2,31 @@ using Slipstream.Components.Lua.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Lua.EventHandler { internal class Lua : IEventHandler { - private readonly EventHandlerController Parent; + public event EventHandler? OnLuaCommandDeduplicateEvents; - public Lua(EventHandlerController parent) + public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - Parent = parent; + return @event switch + { + LuaCommandDeduplicateEvents tev => OnEvent(OnLuaCommandDeduplicateEvents, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; } - public delegate void OnLuaDeduplicateEventsHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnLuaDeduplicateEventsHandler? OnLuaCommandDeduplicateEvents; - - public IEventHandler.HandledStatus HandleEvent(IEvent @event) + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) { - switch (@event) + if (onEvent != null) { - case LuaCommandDeduplicateEvents tev: - if (OnLuaCommandDeduplicateEvents != null) - { - OnLuaCommandDeduplicateEvents?.Invoke(Parent, new EventHandlerArgs(tev)); - return IEventHandler.HandledStatus.Handled; - } - else - { - return IEventHandler.HandledStatus.UseDefault; - } + onEvent.Invoke(this, args); + return IEventHandler.HandledStatus.Handled; } - - return IEventHandler.HandledStatus.NotMine; + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/Lua/Lua.cs b/Components/Lua/Lua.cs deleted file mode 100644 index 0695a173..00000000 --- a/Components/Lua/Lua.cs +++ /dev/null @@ -1,58 +0,0 @@ -using Slipstream.Backend; -using Slipstream.Components.FileMonitor; -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) - { - var eventFactory = new EventFactory.LuaEventFactory(new EventSerdeService()); - - ctx.RegisterPlugin("LuaPlugin", CreateLuaPlugin); - ctx.RegisterPlugin("LuaManagerPlugin", CreateLuaManagerPlugin); - ctx.RegisterEventFactory(typeof(ILuaEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.Lua)); - } - - private IPlugin CreateLuaManagerPlugin(IComponentPluginCreationContext ctx) - { - return new LuaManagerPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger.ForContext(typeof(LuaManagerPlugin)), - ctx.EventFactory.Get(), - ctx.EventBus, - ctx.PluginManager, - ctx.PluginFactory, - ctx.ServiceLocator); - } - - 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 - ); - - return new LuaPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger.ForContext(typeof(LuaPlugin)), - ctx.EventFactory, - 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 053916e5..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; @@ -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(); @@ -64,7 +64,7 @@ IServiceLocator serviceLocator 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; } @@ -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]); @@ -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) @@ -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 f3383a9d..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; @@ -24,22 +25,23 @@ public LuaPlugin( IEventHandlerController eventHandlerController, string id, ILogger logger, - IEventFactory eventFactory, + ILuaEventFactory luaEventFactory, + IInternalEventFactory internalEventFactory, IEventBus eventBus, - ILuaSevice luaService, + ILuaService 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); // 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"); @@ -82,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/EventHandler/Playback.cs b/Components/Playback/EventHandler/Playback.cs index 79083433..09b339a5 100644 --- a/Components/Playback/EventHandler/Playback.cs +++ b/Components/Playback/EventHandler/Playback.cs @@ -2,53 +2,34 @@ using Slipstream.Components.Playback.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Playback.EventHandler { internal class Playback : IEventHandler { - private readonly EventHandlerController Parent; + public event EventHandler? OnPlaybackCommandInjectEvents; - public Playback(EventHandlerController parent) + public event EventHandler? OnPlaybackCommandSaveEvents; + + public IEventHandler.HandledStatus HandleEvent(IEvent @event) { - Parent = parent; + return @event switch + { + PlaybackCommandInjectEvents tev => OnEvent(OnPlaybackCommandInjectEvents, tev), + PlaybackCommandSaveEvents tev => OnEvent(OnPlaybackCommandSaveEvents, tev), + _ => IEventHandler.HandledStatus.NotMine, + }; } - public delegate void OnPlaybackCommandInjectEventsHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnPlaybackCommandInjectEventsHandler? OnPlaybackCommandInjectEvents; - - public delegate void OnPlaybackCommandSaveEventsHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnPlaybackCommandSaveEventsHandler? OnPlaybackCommandSaveEvents; - - public IEventHandler.HandledStatus HandleEvent(IEvent @event) + private IEventHandler.HandledStatus OnEvent(EventHandler? onEvent, TEvent args) { - switch (@event) + if (onEvent != null) { - 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; - } + onEvent.Invoke(this, args); + return IEventHandler.HandledStatus.Handled; } - - return IEventHandler.HandledStatus.NotMine; + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file 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 deleted file mode 100644 index 26759e85..00000000 --- a/Components/Playback/Playback.cs +++ /dev/null @@ -1,30 +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); - ctx.RegisterEventFactory(typeof(IPlaybackEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.Playback)); - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new PlaybackPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger, - ctx.EventBus, - ctx.ServiceLocator.Get() - ); - } - } -} \ No newline at end of file diff --git a/Components/Playback/Plugins/PlaybackPlugin.cs b/Components/Playback/Plugins/PlaybackPlugin.cs index cfddc4ce..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,20 +10,23 @@ 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.Event); - playback.OnPlaybackCommandSaveEvents += (s, e) => OnPlaybackCommandSaveEvents(e.Event); + playback.OnPlaybackCommandInjectEvents += (s, e) => OnPlaybackCommandInjectEvents(e); + playback.OnPlaybackCommandSaveEvents += (s, e) => OnPlaybackCommandSaveEvents(e); Logger = logger; } @@ -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/EventHandler/Twitch.cs b/Components/Twitch/EventHandler/Twitch.cs index df23a295..bbb6af03 100644 --- a/Components/Twitch/EventHandler/Twitch.cs +++ b/Components/Twitch/EventHandler/Twitch.cs @@ -2,151 +2,55 @@ using Slipstream.Components.Twitch.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.Twitch.EventHandler { internal class Twitch : IEventHandler { - private readonly EventHandlerController Parent; + public event EventHandler? OnTwitchCommandSendMessage; - public Twitch(EventHandlerController eventHandler) - { - Parent = eventHandler; - } - - public delegate void OnTwitchCommandSendMessageHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnTwitchCommandSendWhisperHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnTwitchConnectedHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnTwitchDisconnectedHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnTwitchReceivedMessageHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnTwitchReceivedWhisperHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnTwitchUserSubscribedHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchCommandSendWhisper; - public delegate void OnTwitchGiftedSubscriptionHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchConnected; - public delegate void OnTwitchRaidedHandler(EventHandlerController source, EventHandlerArgs e); + public event EventHandler? OnTwitchDisconnected; - public event OnTwitchCommandSendMessageHandler? OnTwitchCommandSendMessage; + public event EventHandler? OnTwitchReceivedMessage; - public event OnTwitchCommandSendWhisperHandler? OnTwitchCommandSendWhisper; + public event EventHandler? OnTwitchReceivedWhisper; - public event OnTwitchConnectedHandler? OnTwitchConnected; + public event EventHandler? OnTwitchUserSubscribed; - public event OnTwitchDisconnectedHandler? OnTwitchDisconnected; + public event EventHandler? OnTwitchGiftedSubscription; - 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(this, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file 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/Plugins/TwitchPlugin.cs b/Components/Twitch/Plugins/TwitchPlugin.cs index 2cbaaca1..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; @@ -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"); @@ -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 8fd5cc40..00000000 --- a/Components/Twitch/Twitch.cs +++ /dev/null @@ -1,30 +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); - ctx.RegisterEventFactory(typeof(ITwitchEventFactory), eventFactory); - ctx.RegisterEventHandler(typeof(EventHandler.Twitch)); - ctx.RegisterLuaGlue(new LuaGlueFactory(ctx.EventBus, eventFactory)); - } - - private IPlugin CreatePlugin(IComponentPluginCreationContext ctx) - { - return new TwitchPlugin( - ctx.EventHandlerController, - ctx.PluginId, - ctx.Logger.ForContext(typeof(Twitch)), - ctx.EventFactory.Get(), - 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 69e3d787..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; } @@ -44,7 +45,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")); @@ -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 51777338..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.EventFactory.Get(), - 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.EventFactory.Get(), - ctx.EventBus, - txrxService, - ctx.PluginParameters - ); - } - } -} \ No newline at end of file diff --git a/Components/UI/EventHandler/UIEventHandler.cs b/Components/UI/EventHandler/UIEventHandler.cs index c893747f..e34b05b8 100644 --- a/Components/UI/EventHandler/UIEventHandler.cs +++ b/Components/UI/EventHandler/UIEventHandler.cs @@ -2,81 +2,40 @@ using Slipstream.Components.UI.Events; using Slipstream.Shared; +using System; namespace Slipstream.Components.UI.EventHandler { internal class UIEventHandler : IEventHandler { - private readonly EventHandlerController Parent; + public event EventHandler? OnUIButtonTriggered; - public UIEventHandler(EventHandlerController eventHandler) - { - Parent = eventHandler; - } - - public delegate void OnUIButtonTriggeredHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnUICommandCreateButtonHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnUICommandDeleteButtonHandler(EventHandlerController source, EventHandlerArgs e); - - public delegate void OnUICommandWriteToConsoleHandler(EventHandlerController source, EventHandlerArgs e); - - public event OnUIButtonTriggeredHandler? OnUIButtonTriggered; + public event EventHandler? OnUICommandCreateButton; - public event OnUICommandCreateButtonHandler? OnUICommandCreateButton; + public event EventHandler? OnUICommandDeleteButton; - 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(this, args); + return IEventHandler.HandledStatus.Handled; + } + return IEventHandler.HandledStatus.UseDefault; } } } \ No newline at end of file diff --git a/Components/UI/LuaGlue.cs b/Components/UI/LuaGlue.cs index 1b1a798c..5e879ff6 100644 --- a/Components/UI/LuaGlue.cs +++ b/Components/UI/LuaGlue.cs @@ -12,12 +12,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, string prefix) { Logger = logger; EventBus = eventBus; EventFactory = eventFactory; - Prefix = logPrefix; + Prefix = prefix; } 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/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 93fe2e5c..00000000 --- a/Components/UI/UI.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace Slipstream.Components.UI -{ - internal class UI : IComponent - { - 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)); - } - } -} \ No newline at end of file diff --git a/Components/WinFormUI/Forms/MainWindow.cs b/Components/WinFormUI/Forms/MainWindow.cs index b356aaeb..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; @@ -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/Components/WinFormUI/Plugins/WinFormUIPlugin.cs b/Components/WinFormUI/Plugins/WinFormUIPlugin.cs index 22f9e885..26cdf18d 100644 --- a/Components/WinFormUI/Plugins/WinFormUIPlugin.cs +++ b/Components/WinFormUI/Plugins/WinFormUIPlugin.cs @@ -1,27 +1,40 @@ +using Slipstream.Components.Internal; +using Slipstream.Components.Playback; +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 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 IEnumerable CreateLuaGlues() + { + return new ILuaGlue[] { }; + } + 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 deleted file mode 100644 index a50f7d82..00000000 --- a/Components/WinFormUI/WinFormUI.cs +++ /dev/null @@ -1,26 +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.EventFactory, - ctx.EventBus, - new ApplicationVersionService() - ); - } - } -} \ No newline at end of file diff --git a/Program.cs b/Program.cs index 39888bd8..c4c9b487 100644 --- a/Program.cs +++ b/Program.cs @@ -1,8 +1,7 @@ -using Microsoft.Extensions.DependencyInjection; +using Autofac; using Serilog; using Slipstream.Components.Internal; using Slipstream.Components.Internal.Services; -using Slipstream.Components.UI.EventFactory; using Slipstream.Shared; using System; using System.Windows.Forms; @@ -19,44 +18,39 @@ 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().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(); var logger = new LoggerConfiguration() .MinimumLevel.Verbose() @@ -64,17 +58,37 @@ 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(); + + // EventHandlers + builder.RegisterAssemblyTypes(typeof(Program).Assembly) + .Where(t => t.IsAssignableTo()) + .Where(t => !t.IsAbstract) + .As() + .AsSelf() + .InstancePerDependency(); + builder.RegisterAssemblyTypes(typeof(Program).Assembly) + .Where(t => t.Name.EndsWith("EventFactory")) + .AsImplementedInterfaces() + .SingleInstance(); + builder.RegisterType().As().InstancePerDependency(); + + // Plugins + builder.RegisterAssemblyTypes(typeof(Program).Assembly) + .Where(t => t.IsAssignableTo()) + .Where(t => !t.IsAbstract) + .As() + .AsSelf() + .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/EventHandlerController.cs b/Shared/EventHandlerController.cs index e8336e33..33ff25ff 100644 --- a/Shared/EventHandlerController.cs +++ b/Shared/EventHandlerController.cs @@ -1,6 +1,7 @@ -using System; +using Autofac; +using Slipstream.Backend; +using System; using System.Collections.Generic; -using static Slipstream.Shared.IEventHandlerController; #nullable enable @@ -8,16 +9,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 OnDefaultHandler? OnDefault; + 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 +32,7 @@ public T Get() return (T)Handlers[typeof(T)]; } - internal void Add(IEventHandler eventHandler) + private void Add(IEventHandler eventHandler) { Handlers.Add(eventHandler.GetType(), eventHandler); } @@ -49,7 +53,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/EventHandlerControllerBuilder.cs b/Shared/EventHandlerControllerBuilder.cs deleted file mode 100644 index adaa9421..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, new object[] { controller }); - - controller.Add(instance); - } - - return controller; - } - } -} \ No newline at end of file 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/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/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(); 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 cd68d284..86bba9f3 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 @@ -201,16 +201,14 @@ + - - - @@ -219,20 +217,13 @@ - - - - - - - + - @@ -261,41 +252,24 @@ - - - - - - + - - - - - + - - - - - - - - @@ -328,8 +302,6 @@ - - @@ -406,7 +378,6 @@ - @@ -424,8 +395,6 @@ - - MainWindow.cs 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 @@ +