From 01e4bc79e5d8bbdf6065891c20478fe773d38dd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Sat, 9 Jan 2021 23:35:04 +0100 Subject: [PATCH 1/6] InternalReconfigured rename to InternalCommandReconfigure --- Backend/Engine.cs | 2 +- Frontend/MainWindow.cs | 2 +- Shared/EventFactory.cs | 4 ++-- Shared/EventHandler.cs | 10 +++++----- ...alReconfigured.cs => InternalCommandReconfigure.cs} | 6 +++--- Shared/IEventFactory.cs | 2 +- Slipstream.csproj | 2 +- 7 files changed, 14 insertions(+), 14 deletions(-) rename Shared/Events/Internal/{InternalReconfigured.cs => InternalCommandReconfigure.cs} (78%) diff --git a/Backend/Engine.cs b/Backend/Engine.cs index a9c11fa2..e7db4d56 100644 --- a/Backend/Engine.cs +++ b/Backend/Engine.cs @@ -37,7 +37,7 @@ public Engine(IEventFactory eventFactory, IEventBus eventBus, IPluginFactory plu EventHandler.OnInternalCommandPluginRegister += (s, e) => OnCommandPluginRegister(e.Event); EventHandler.OnInternalCommandPluginUnregister += (s, e) => OnCommandPluginUnregister(e.Event); EventHandler.OnInternalCommandPluginStates += (s, e) => OnCommandPluginStates(e.Event); - EventHandler.OnInternalReconfigured += (s, e) => OnInternalReconfigured(); + EventHandler.OnInternalCommandReconfigure += (s, e) => OnInternalReconfigured(); EventHandler.OnInternalBootupEvents += (s, e) => OnInternalBootupEvents(e.Event); BootupEventsDeadline = DateTime.Now.AddMilliseconds(500); diff --git a/Frontend/MainWindow.cs b/Frontend/MainWindow.cs index 344d17d2..9fbfe4e1 100644 --- a/Frontend/MainWindow.cs +++ b/Frontend/MainWindow.cs @@ -228,7 +228,7 @@ private void SettingsToolStripMenuItem_Click(object sender, EventArgs e) { if(new SettingsForm().ShowDialog(this) == DialogResult.OK) { - EventBus.PublishEvent(EventFactory.CreateInternalReconfigured()); + EventBus.PublishEvent(EventFactory.CreateInternalCommandReconfigure()); } } } diff --git a/Shared/EventFactory.cs b/Shared/EventFactory.cs index 5bf6f6d7..01f8b36f 100644 --- a/Shared/EventFactory.cs +++ b/Shared/EventFactory.cs @@ -70,9 +70,9 @@ public InternalPluginState CreateInternalPluginState(string pluginId, string plu return new InternalPluginState { Id = pluginId, PluginName = pluginName, DisplayName = displayName, PluginStatus = pluginStatus.ToString() }; } - public InternalReconfigured CreateInternalReconfigured() + public InternalCommandReconfigure CreateInternalCommandReconfigure() { - return new InternalReconfigured(); + return new InternalCommandReconfigure(); } public InternalBootupEvents CreateInternalBootupEvents(IEvent[] events) diff --git a/Shared/EventHandler.cs b/Shared/EventHandler.cs index 0cad3e88..cfa47366 100644 --- a/Shared/EventHandler.cs +++ b/Shared/EventHandler.cs @@ -35,8 +35,8 @@ public EventHandlerArgs(T e) public delegate void OnInternalCommandPluginStatesHandler(EventHandler source, EventHandlerArgs e); public event OnInternalCommandPluginStatesHandler? OnInternalCommandPluginStates; - public delegate void OnInternalReconfiguredHandler(EventHandler source, EventHandlerArgs e); - public event OnInternalReconfiguredHandler? OnInternalReconfigured; + public delegate void OnInternalCommandReconfigureHandler(EventHandler source, EventHandlerArgs e); + public event OnInternalCommandReconfigureHandler? OnInternalCommandReconfigure; public delegate void OnInternalBootupEventsHandler(EventHandler source, EventHandlerArgs e); public event OnInternalBootupEventsHandler? OnInternalBootupEvents; @@ -201,11 +201,11 @@ public void HandleEvent(IEvent? ev) else OnInternalCommandPluginStates.Invoke(this, new EventHandlerArgs(tev)); break; - case Shared.Events.Internal.InternalReconfigured tev: - if (OnInternalReconfigured == null) + case Shared.Events.Internal.InternalCommandReconfigure tev: + if (OnInternalCommandReconfigure == null) OnDefault?.Invoke(this, new EventHandlerArgs(tev)); else - OnInternalReconfigured.Invoke(this, new EventHandlerArgs(tev)); + OnInternalCommandReconfigure.Invoke(this, new EventHandlerArgs(tev)); break; case Shared.Events.Internal.InternalBootupEvents tev: if (OnInternalBootupEvents == null) diff --git a/Shared/Events/Internal/InternalReconfigured.cs b/Shared/Events/Internal/InternalCommandReconfigure.cs similarity index 78% rename from Shared/Events/Internal/InternalReconfigured.cs rename to Shared/Events/Internal/InternalCommandReconfigure.cs index c2a486e5..213b65bb 100644 --- a/Shared/Events/Internal/InternalReconfigured.cs +++ b/Shared/Events/Internal/InternalCommandReconfigure.cs @@ -4,14 +4,14 @@ namespace Slipstream.Shared.Events.Internal { - public class InternalReconfigured : IEvent + public class InternalCommandReconfigure : IEvent { - public string EventType => "InternalReconfigured"; + public string EventType => "InternalCommandReconfigure"; public bool ExcludeFromTxrx => true; public override bool Equals(object? obj) { - return obj is InternalReconfigured reconfigured && + return obj is InternalCommandReconfigure reconfigured && EventType == reconfigured.EventType && ExcludeFromTxrx == reconfigured.ExcludeFromTxrx; } diff --git a/Shared/IEventFactory.cs b/Shared/IEventFactory.cs index bb7d32f7..6136d467 100644 --- a/Shared/IEventFactory.cs +++ b/Shared/IEventFactory.cs @@ -38,7 +38,7 @@ public enum IRacingSessionStateEnum InternalCommandPluginStates CreateInternalCommandPluginStates(); InternalCommandPluginUnregister CreateInternalCommandPluginUnregister(string pluginId); InternalPluginState CreateInternalPluginState(string pluginId, string pluginName, string displayName, PluginStatusEnum pluginStatus); - InternalReconfigured CreateInternalReconfigured(); + InternalCommandReconfigure CreateInternalCommandReconfigure(); InternalBootupEvents CreateInternalBootupEvents(IEvent[] events); IRacingCarCompletedLap CreateIRacingCarCompletedLap(double sessionTime, long carIdx, double time, int lapsCompleted, float? fuelDiff, bool localUser); diff --git a/Slipstream.csproj b/Slipstream.csproj index e1856337..15c406a0 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -172,7 +172,7 @@ - + From 4e4f18a93d1a5a971cc2d564f79bdbfc86a006ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Sat, 9 Jan 2021 23:49:52 +0100 Subject: [PATCH 2/6] Move lua function print to UIMethodCollection .. as it is related to UI and implemented by MainWindow --- .../LuaServiceLib/CoreMethodCollection.cs | 23 +++---------------- Backend/Services/LuaServiceLib/LuaContext.cs | 4 ++-- .../LuaServiceLib/UIMethodCollection.cs | 15 +++++++++--- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/Backend/Services/LuaServiceLib/CoreMethodCollection.cs b/Backend/Services/LuaServiceLib/CoreMethodCollection.cs index a0bf2011..35ef4fe5 100644 --- a/Backend/Services/LuaServiceLib/CoreMethodCollection.cs +++ b/Backend/Services/LuaServiceLib/CoreMethodCollection.cs @@ -23,7 +23,6 @@ public DelayedExecution(LuaFunction luaFunction, DateTime triggersAt) } } - private readonly string Prefix; private readonly IEventFactory EventFactory; private readonly IEventBus EventBus; private readonly IEventSerdeService EventSerdeService; @@ -31,18 +30,17 @@ public DelayedExecution(LuaFunction luaFunction, DateTime triggersAt) private readonly IDictionary DebounceDelayedFunctions = new Dictionary(); private readonly IDictionary WaitDelayedFunctions = new Dictionary(); - public static CoreMethodCollection Register(IEventFactory eventFactory, IEventBus eventBus, string logPrefix, IEventSerdeService eventSerdeService, Lua lua) + public static CoreMethodCollection Register(IEventFactory eventFactory, IEventBus eventBus, IEventSerdeService eventSerdeService, Lua lua) { - var m = new CoreMethodCollection(eventFactory, eventBus, logPrefix, eventSerdeService); + var m = new CoreMethodCollection(eventFactory, eventBus, eventSerdeService); m.Register(lua); return m; } - public CoreMethodCollection(IEventFactory eventFactory, IEventBus eventBus, string logPrefix, IEventSerdeService eventSerdeService) + public CoreMethodCollection(IEventFactory eventFactory, IEventBus eventBus, IEventSerdeService eventSerdeService) { - Prefix = logPrefix; EventFactory = eventFactory; EventBus = eventBus; EventSerdeService = eventSerdeService; @@ -54,19 +52,12 @@ public void Register(Lua lua) // Make old Lua script work as before lua.DoString(@" -function print(s); core:print(s); end function debounce(a, b, c); core:debounce(a, b, c); end function wait(a, b, c); core:wait(a, b, c); end function event_to_json(a); return core:event_to_json(a); end "); } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "This is expose in Lua, so we want to keep that naming style")] - public void print(string s) - { - EventBus.PublishEvent(EventFactory.CreateUICommandWriteToConsole($"{Prefix}: {s}")); - } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "This is expose in Lua, so we want to keep that naming style")] public void debounce(string name, LuaFunction func, float debounceLength) { @@ -74,10 +65,6 @@ public void debounce(string name, LuaFunction func, float debounceLength) { DebounceDelayedFunctions[name] = new DelayedExecution(func, DateTime.Now.AddSeconds(debounceLength)); } - else - { - print("Can't debounce without a function"); - } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "This is expose in Lua, so we want to keep that naming style")] @@ -88,10 +75,6 @@ public void wait(string name, LuaFunction func, float duration) if (!WaitDelayedFunctions.ContainsKey(name)) WaitDelayedFunctions[name] = new DelayedExecution(func, DateTime.Now.AddSeconds(duration)); } - else - { - print("Can't wait without a function"); - } } [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "This is expose in Lua, so we want to keep that naming style")] diff --git a/Backend/Services/LuaServiceLib/LuaContext.cs b/Backend/Services/LuaServiceLib/LuaContext.cs index 6bc51d8d..6c7f85b1 100644 --- a/Backend/Services/LuaServiceLib/LuaContext.cs +++ b/Backend/Services/LuaServiceLib/LuaContext.cs @@ -19,11 +19,11 @@ public LuaContext(IEventFactory eventFactory, IEventBus eventBus, IStateService { Lua = new Lua(); - CoreMethodCollection_ = CoreMethodCollection.Register(eventFactory, eventBus, logPrefix, new EventSerdeService(), Lua); + CoreMethodCollection_ = CoreMethodCollection.Register(eventFactory, eventBus, new EventSerdeService(), Lua); AudioMethodCollection.Register(eventBus, eventFactory, Lua); TwitchMethodCollection.Register(eventBus, eventFactory, Lua); StateMethodCollection.Register(stateService, Lua); - UIMethodCollection.Register(eventBus, eventFactory, Lua); + UIMethodCollection.Register(eventBus, eventFactory, logPrefix, Lua); InternalMethodCollection.Register(eventBus, eventFactory, Lua); HttpMethodCollection.Register(eventBus, eventFactory, Lua); IRacingMethodCollection.Register(eventBus, eventFactory, Lua); diff --git a/Backend/Services/LuaServiceLib/UIMethodCollection.cs b/Backend/Services/LuaServiceLib/UIMethodCollection.cs index 6526d9ca..0e01bc0a 100644 --- a/Backend/Services/LuaServiceLib/UIMethodCollection.cs +++ b/Backend/Services/LuaServiceLib/UIMethodCollection.cs @@ -11,31 +11,40 @@ public class UIMethodCollection { private readonly IEventBus EventBus; private readonly IEventFactory EventFactory; + private readonly string Prefix; - public static UIMethodCollection Register(IEventBus eventBus, IEventFactory eventFactory, Lua lua) + public static UIMethodCollection Register(IEventBus eventBus, IEventFactory eventFactory, string logPrefix, Lua lua) { - var m = new UIMethodCollection(eventBus, eventFactory); + var m = new UIMethodCollection(eventBus, eventFactory, logPrefix); m.Register(lua); return m; } - public UIMethodCollection(IEventBus eventBus, IEventFactory eventFactory) + public UIMethodCollection(IEventBus eventBus, IEventFactory eventFactory, string logPrefix) { EventBus = eventBus; EventFactory = eventFactory; + Prefix = logPrefix; } public void Register(Lua lua) { lua["ui"] = this; lua.DoString(@" +function print(s); ui:print(s); end function create_button(a); ui:create_button(a); end function delete_button(a); ui:delete_button(a); end "); } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "This is expose in Lua, so we want to keep that naming style")] + public void print(string s) + { + EventBus.PublishEvent(EventFactory.CreateUICommandWriteToConsole($"{Prefix}: {s}")); + } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "This is expose in Lua, so we want to keep that naming style")] public void create_button(string text) { From 6df3a2b0e1dd306fc339826d7fed6acfe121c8e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Sun, 10 Jan 2021 00:02:23 +0100 Subject: [PATCH 3/6] InternalBootupEvents renamed to InternalCommandDeduplicateEvents --- Backend/Engine.cs | 4 ++-- Backend/Plugins/LuaPlugin.cs | 2 +- Shared/EventFactory.cs | 4 ++-- Shared/EventHandler.cs | 10 +++++----- ...upEvents.cs => InternalCommandDeduplicateEvents.cs} | 6 +++--- Shared/IEventFactory.cs | 2 +- Slipstream.csproj | 2 +- 7 files changed, 15 insertions(+), 15 deletions(-) rename Shared/Events/Internal/{InternalBootupEvents.cs => InternalCommandDeduplicateEvents.cs} (80%) diff --git a/Backend/Engine.cs b/Backend/Engine.cs index e7db4d56..f6bc4474 100644 --- a/Backend/Engine.cs +++ b/Backend/Engine.cs @@ -38,7 +38,7 @@ public Engine(IEventFactory eventFactory, IEventBus eventBus, IPluginFactory plu EventHandler.OnInternalCommandPluginUnregister += (s, e) => OnCommandPluginUnregister(e.Event); EventHandler.OnInternalCommandPluginStates += (s, e) => OnCommandPluginStates(e.Event); EventHandler.OnInternalCommandReconfigure += (s, e) => OnInternalReconfigured(); - EventHandler.OnInternalBootupEvents += (s, e) => OnInternalBootupEvents(e.Event); + EventHandler.OnInternalCommandDeduplicateEvents += (s, e) => OnInternalCommandDeduplicateEvents(e.Event); BootupEventsDeadline = DateTime.Now.AddMilliseconds(500); @@ -91,7 +91,7 @@ public Engine(IEventFactory eventFactory, IEventBus eventBus, IPluginFactory plu EventBus.Enabled = true; } - private void OnInternalBootupEvents(InternalBootupEvents @event) + private void OnInternalCommandDeduplicateEvents(InternalCommandDeduplicateEvents @event) { foreach(var e in EventSerdeService.DeserializeMultiple(@event.Events)) { diff --git a/Backend/Plugins/LuaPlugin.cs b/Backend/Plugins/LuaPlugin.cs index ddd39a7a..2352aa4c 100644 --- a/Backend/Plugins/LuaPlugin.cs +++ b/Backend/Plugins/LuaPlugin.cs @@ -51,7 +51,7 @@ private void StartLua() var eventsCaptured = EventBus.CapturedEvents; EventBus.StopCapturing(); - EventBus.PublishEvent(EventFactory.CreateInternalBootupEvents(eventsCaptured)); + EventBus.PublishEvent(EventFactory.CreateInternalCommandDeduplicateEvents(eventsCaptured)); } private void HandleLuaException(LuaException e) diff --git a/Shared/EventFactory.cs b/Shared/EventFactory.cs index 01f8b36f..28c806c7 100644 --- a/Shared/EventFactory.cs +++ b/Shared/EventFactory.cs @@ -75,7 +75,7 @@ public InternalCommandReconfigure CreateInternalCommandReconfigure() return new InternalCommandReconfigure(); } - public InternalBootupEvents CreateInternalBootupEvents(IEvent[] events) + public InternalCommandDeduplicateEvents CreateInternalCommandDeduplicateEvents(IEvent[] events) { string json = ""; @@ -84,7 +84,7 @@ public InternalBootupEvents CreateInternalBootupEvents(IEvent[] events) json += EventSerdeService.Serialize(e) + "\n"; } - return new InternalBootupEvents + return new InternalCommandDeduplicateEvents { Events = json }; diff --git a/Shared/EventHandler.cs b/Shared/EventHandler.cs index cfa47366..bab00774 100644 --- a/Shared/EventHandler.cs +++ b/Shared/EventHandler.cs @@ -38,8 +38,8 @@ public EventHandlerArgs(T e) public delegate void OnInternalCommandReconfigureHandler(EventHandler source, EventHandlerArgs e); public event OnInternalCommandReconfigureHandler? OnInternalCommandReconfigure; - public delegate void OnInternalBootupEventsHandler(EventHandler source, EventHandlerArgs e); - public event OnInternalBootupEventsHandler? OnInternalBootupEvents; + public delegate void OnInternalCommandDeduplicateEventsHandler(EventHandler source, EventHandlerArgs e); + public event OnInternalCommandDeduplicateEventsHandler? OnInternalCommandDeduplicateEvents; #endregion @@ -207,11 +207,11 @@ public void HandleEvent(IEvent? ev) else OnInternalCommandReconfigure.Invoke(this, new EventHandlerArgs(tev)); break; - case Shared.Events.Internal.InternalBootupEvents tev: - if (OnInternalBootupEvents == null) + case Shared.Events.Internal.InternalCommandDeduplicateEvents tev: + if (OnInternalCommandDeduplicateEvents == null) OnDefault?.Invoke(this, new EventHandlerArgs(tev)); else - OnInternalBootupEvents.Invoke(this, new EventHandlerArgs(tev)); + OnInternalCommandDeduplicateEvents.Invoke(this, new EventHandlerArgs(tev)); break; // File Monitor diff --git a/Shared/Events/Internal/InternalBootupEvents.cs b/Shared/Events/Internal/InternalCommandDeduplicateEvents.cs similarity index 80% rename from Shared/Events/Internal/InternalBootupEvents.cs rename to Shared/Events/Internal/InternalCommandDeduplicateEvents.cs index e4cb91ac..3e59586a 100644 --- a/Shared/Events/Internal/InternalBootupEvents.cs +++ b/Shared/Events/Internal/InternalCommandDeduplicateEvents.cs @@ -2,9 +2,9 @@ namespace Slipstream.Shared.Events.Internal { - public class InternalBootupEvents : IEvent + public class InternalCommandDeduplicateEvents : IEvent { - public string EventType => "InternalBootupEvents"; + public string EventType => "InternalCommandDeduplicateEvents"; public bool ExcludeFromTxrx => true; @@ -12,7 +12,7 @@ public class InternalBootupEvents : IEvent public override bool Equals(object obj) { - return obj is InternalBootupEvents events && + return obj is InternalCommandDeduplicateEvents events && EventType == events.EventType && ExcludeFromTxrx == events.ExcludeFromTxrx && Events == events.Events; diff --git a/Shared/IEventFactory.cs b/Shared/IEventFactory.cs index 6136d467..f8ee9aa0 100644 --- a/Shared/IEventFactory.cs +++ b/Shared/IEventFactory.cs @@ -39,7 +39,7 @@ public enum IRacingSessionStateEnum InternalCommandPluginUnregister CreateInternalCommandPluginUnregister(string pluginId); InternalPluginState CreateInternalPluginState(string pluginId, string pluginName, string displayName, PluginStatusEnum pluginStatus); InternalCommandReconfigure CreateInternalCommandReconfigure(); - InternalBootupEvents CreateInternalBootupEvents(IEvent[] events); + InternalCommandDeduplicateEvents CreateInternalCommandDeduplicateEvents(IEvent[] events); IRacingCarCompletedLap CreateIRacingCarCompletedLap(double sessionTime, long carIdx, double time, int lapsCompleted, float? fuelDiff, bool localUser); IRacingCarInfo CreateIRacingCarInfo( diff --git a/Slipstream.csproj b/Slipstream.csproj index 15c406a0..f0b761e7 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -171,7 +171,7 @@ - + From 15ebed005a48696946b237fd0b8c88c5e8093843 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Sun, 10 Jan 2021 08:03:41 +0100 Subject: [PATCH 4/6] Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f860bd5e..0f4ef1db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ - Lua: Add iracing_send_race_flags() that will make IRacingPlugin send race flags - InternalInitialized event removed. Refactored to avoid having it - Lua: Removed plugin_enable(), plugin_disable(). Register/Unregister them instead + - Renamed Events: + - UICommandWriteToConsole is now UIMethodCollection + - InternalReconfigured is now InternalCommandReconfigure + - InternalBootupEvents is now InternalCommandDeduplicateEvents - UI: Stores/restores Window position and size - Event: Adds TwitchReceivedMessage event (captures all messages, not only commands) - Event: Removes TwitchReceivedCommand event as this is already sent as a TwitchReceivedMessage From b629ae2f6f208d2807ed71412884a5b09dfbc8fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Sun, 10 Jan 2021 09:49:29 +0100 Subject: [PATCH 5/6] Rename FileTriggerPlugin to LuaManagerPlugin Also moved some management code from Engine to LuaManagerPlugin --- Backend/Engine.cs | 47 +++---------------- Backend/PluginManager.cs | 7 +-- ...leTriggerPlugin.cs => LuaManagerPlugin.cs} | 46 ++++++++++++++++-- Slipstream.csproj | 2 +- 4 files changed, 54 insertions(+), 48 deletions(-) rename Backend/Plugins/{FileTriggerPlugin.cs => LuaManagerPlugin.cs} (73%) diff --git a/Backend/Engine.cs b/Backend/Engine.cs index f6bc4474..cf390284 100644 --- a/Backend/Engine.cs +++ b/Backend/Engine.cs @@ -2,35 +2,28 @@ using Slipstream.Shared; using Slipstream.Shared.Events.Internal; using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using static Slipstream.Shared.IEventFactory; #nullable enable namespace Slipstream.Backend { - partial class Engine : Worker, IEngine, IDisposable + class Engine : Worker, IEngine, IDisposable { private readonly IEventFactory EventFactory; private readonly IEventBus EventBus; private readonly IPluginManager PluginManager; private readonly IEventBusSubscription Subscription; private readonly IPluginFactory PluginFactory; - private readonly IEventSerdeService EventSerdeService; private readonly Shared.EventHandler EventHandler = new Shared.EventHandler(); - private DateTime? BootupEventsDeadline; - private readonly List CapturedBootupEvents = new List(); - - public Engine(IEventFactory eventFactory, IEventBus eventBus, IPluginFactory pluginFactory, IPluginManager pluginManager, ILuaSevice luaService, IApplicationVersionService applicationVersionService, IEventSerdeService eventSerdeService) : base("engine") + public Engine(IEventFactory eventFactory, IEventBus eventBus, IPluginFactory pluginFactory, IPluginManager pluginManager, ILuaSevice luaService, IApplicationVersionService applicationVersionService) : base("engine") { EventFactory = eventFactory; EventBus = eventBus; PluginFactory = pluginFactory; PluginManager = pluginManager; - EventSerdeService = eventSerdeService; Subscription = EventBus.RegisterListener(); @@ -38,9 +31,6 @@ public Engine(IEventFactory eventFactory, IEventBus eventBus, IPluginFactory plu EventHandler.OnInternalCommandPluginUnregister += (s, e) => OnCommandPluginUnregister(e.Event); EventHandler.OnInternalCommandPluginStates += (s, e) => OnCommandPluginStates(e.Event); EventHandler.OnInternalCommandReconfigure += (s, e) => OnInternalReconfigured(); - EventHandler.OnInternalCommandDeduplicateEvents += (s, e) => OnInternalCommandDeduplicateEvents(e.Event); - - BootupEventsDeadline = DateTime.Now.AddMilliseconds(500); // Plugins.. { @@ -71,12 +61,12 @@ public Engine(IEventFactory eventFactory, IEventBus eventBus, IPluginFactory plu -- register_plugin(""TransmitterPlugin"", ""TransmitterPlugin"") -- register_plugin(""ReceiverPlugin"", ""ReceiverPlugin"") --- FileTriggerPlugin listens for FileMonitorPlugin events and acts on them. --- Currently it will only act on files ending with .lua, which it launches --- a plugin for. If the file is modified, it will take down the plugin and +-- LuaManagerPlugin listens for FileMonitorPlugin events and acts on them. +-- It will only act on files ending with .lua, which it launches +-- a LuaPlugin for. If the file is modified, it will take down the plugin and -- launch a new one with the same file. If files are moved out of the directory -- it is consider as if it were deleted. Deleted files are taken down. -register_plugin(""FileTriggerPlugin"", ""FileTriggerPlugin"") +register_plugin(""LuaManagerPlugin"", ""LuaManagerPlugin"") -- FileMonitorPlugin monitors the script directory and sends out events -- every time a file is created, renamed, modified or deleted @@ -91,17 +81,6 @@ public Engine(IEventFactory eventFactory, IEventBus eventBus, IPluginFactory plu EventBus.Enabled = true; } - private void OnInternalCommandDeduplicateEvents(InternalCommandDeduplicateEvents @event) - { - foreach(var e in EventSerdeService.DeserializeMultiple(@event.Events)) - { - CapturedBootupEvents.Add(e); - } - - // Postponing deadline - BootupEventsDeadline = DateTime.Now.AddMilliseconds(500); - } - private void OnInternalReconfigured() { PluginManager.RestartReconfigurablePlugins(); @@ -134,20 +113,6 @@ protected override void Main() { while (!Stopped) { - if(BootupEventsDeadline != null && BootupEventsDeadline <= DateTime.Now) - { - // We have collected the events published when LuaScripts were booting. To avoid - // publishing the same events multiple times, we remove duplicates and then publish it - foreach (var e in CapturedBootupEvents.Distinct()) - { - EventBus.PublishEvent(e); - } - - CapturedBootupEvents.Clear(); - - BootupEventsDeadline = null; - } - EventHandler.HandleEvent(Subscription.NextEvent(10)); } } diff --git a/Backend/PluginManager.cs b/Backend/PluginManager.cs index a30c1abe..3bb6cfaa 100644 --- a/Backend/PluginManager.cs +++ b/Backend/PluginManager.cs @@ -18,14 +18,15 @@ public class PluginManager : IPluginManager, IPluginFactory private readonly IApplicationConfiguration ApplicationConfiguration; private readonly IStateService StateService; private readonly ITxrxService TxrxService; - - public PluginManager(IEventFactory eventFactory, IEventBus eventBus, IApplicationConfiguration applicationConfiguration, IStateService stateService, ITxrxService txrxService) + private readonly IEventSerdeService EventSerdeService; + public PluginManager(IEventFactory eventFactory, IEventBus eventBus, IApplicationConfiguration applicationConfiguration, IStateService stateService, ITxrxService txrxService, IEventSerdeService eventSerdeService) { EventFactory = eventFactory; EventBus = eventBus; ApplicationConfiguration = applicationConfiguration; StateService = stateService; TxrxService = txrxService; + EventSerdeService = eventSerdeService; } private void UnregisterPluginsWithoutLock() @@ -173,7 +174,7 @@ public IPlugin CreatePlugin(string id, string name, IEventBus eventBus) return name switch { "FileMonitorPlugin" => new FileMonitorPlugin(id, EventFactory, eventBus, ApplicationConfiguration), - "FileTriggerPlugin" => new FileTriggerPlugin(id, EventFactory, eventBus, this, this), + "LuaManagerPlugin" => new LuaManagerPlugin(id, EventFactory, eventBus, this, this, EventSerdeService), "AudioPlugin" => new AudioPlugin(id, EventFactory, eventBus, ApplicationConfiguration), "IRacingPlugin" => new IRacingPlugin(id, EventFactory, eventBus), "TwitchPlugin" => new TwitchPlugin(id, EventFactory, eventBus, ApplicationConfiguration), diff --git a/Backend/Plugins/FileTriggerPlugin.cs b/Backend/Plugins/LuaManagerPlugin.cs similarity index 73% rename from Backend/Plugins/FileTriggerPlugin.cs rename to Backend/Plugins/LuaManagerPlugin.cs index ad88958b..d2bf70e8 100644 --- a/Backend/Plugins/FileTriggerPlugin.cs +++ b/Backend/Plugins/LuaManagerPlugin.cs @@ -1,18 +1,23 @@ -using Slipstream.Shared; +using Slipstream.Backend.Services; +using Slipstream.Shared; +using Slipstream.Shared.Events.Internal; +using System; using System.Collections.Generic; using System.IO; +using System.Linq; using EventHandler = Slipstream.Shared.EventHandler; #nullable enable namespace Slipstream.Backend.Plugins { - internal class FileTriggerPlugin : BasePlugin + internal class LuaManagerPlugin : BasePlugin { private readonly IEventFactory EventFactory; private readonly IEventBus EventBus; private readonly IPluginManager PluginManager; private readonly IPluginFactory PluginFactory; + private readonly IEventSerdeService EventSerdeService; private readonly IDictionary Scripts = new Dictionary(); // At bootup we will receive zero or more FileCreated events ending with a ScanCompleted. @@ -22,12 +27,16 @@ internal class FileTriggerPlugin : BasePlugin private readonly List WaitingForLuaScripts = new List(); - public FileTriggerPlugin(string id, IEventFactory eventFactory, IEventBus eventBus, IPluginManager pluginManager, IPluginFactory pluginFactory) : base(id, "FileTriggerPlugin", "FileTriggerPlugin", "Core") + private DateTime? BootupEventsDeadline; + private readonly List CapturedBootupEvents = new List(); + + public LuaManagerPlugin(string id, IEventFactory eventFactory, IEventBus eventBus, IPluginManager pluginManager, IPluginFactory pluginFactory, IEventSerdeService eventSerdeService) : base(id, "LuaManagerPlugin", "LuaManagerPlugin", "Core") { EventFactory = eventFactory; EventBus = eventBus; PluginManager = pluginManager; PluginFactory = pluginFactory; + EventSerdeService = eventSerdeService; EventHandler.OnFileMonitorFileCreated += EventHandler_OnFileMonitorFileCreated; EventHandler.OnFileMonitorFileDeleted += EventHandler_OnFileMonitorFileDeleted; @@ -35,10 +44,24 @@ public FileTriggerPlugin(string id, IEventFactory eventFactory, IEventBus eventB EventHandler.OnFileMonitorFileRenamed += EventHandler_OnFileMonitorFileRenamed; EventHandler.OnFileMonitorScanCompleted += EventHandler_OnFileMonitorScanCompleted; EventHandler.OnInternalPluginState += EventHandler_OnInternalPluginState; + EventHandler.OnInternalCommandDeduplicateEvents += (s, e) => EventHandler_OnInternalCommandDeduplicateEvents(e.Event); + + BootupEventsDeadline = DateTime.Now.AddMilliseconds(500); EventBus.PublishEvent(EventFactory.CreateFileMonitorCommandScan()); } + private void EventHandler_OnInternalCommandDeduplicateEvents(InternalCommandDeduplicateEvents @event) + { + foreach (var e in EventSerdeService.DeserializeMultiple(@event.Events)) + { + CapturedBootupEvents.Add(e); + } + + // Postponing deadline + BootupEventsDeadline = DateTime.Now.AddMilliseconds(500); + } + private void EventHandler_OnInternalPluginState(EventHandler source, EventHandler.EventHandlerArgs e) { if (e.Event.PluginStatus == "Registered" && e.Event.PluginName == "LuaPlugin") @@ -133,5 +156,22 @@ private bool IsApplicable(string FilePath) { return Path.GetExtension(FilePath) == ".lua"; } + + public override void Loop() + { + if (BootupEventsDeadline != null && BootupEventsDeadline <= DateTime.Now) + { + // We have collected the events published when LuaScripts were booting. To avoid + // publishing the same events multiple times, we remove duplicates and then publish it + foreach (var e in CapturedBootupEvents.Distinct()) + { + EventBus.PublishEvent(e); + } + + CapturedBootupEvents.Clear(); + + BootupEventsDeadline = null; + } + } } } \ No newline at end of file diff --git a/Slipstream.csproj b/Slipstream.csproj index f0b761e7..70a9be16 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -200,7 +200,7 @@ - + From 0ff2b969730129977bac4c52fac773b3d97b010d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20M=C3=B8llegaard?= Date: Sun, 10 Jan 2021 10:01:29 +0100 Subject: [PATCH 6/6] Rename InternalCommandDeduplicateEvents to LuaManagerCommandDeduplicateEvents --- Backend/Plugins/LuaManagerPlugin.cs | 6 +++--- Backend/Plugins/LuaPlugin.cs | 2 +- CHANGELOG.md | 2 ++ Shared/EventFactory.cs | 5 +++-- Shared/EventHandler.cs | 15 ++++++++++----- .../LuaManagerCommandDeduplicateEvents.cs} | 8 ++++---- Shared/IEventFactory.cs | 3 ++- Slipstream.csproj | 2 +- 8 files changed, 26 insertions(+), 17 deletions(-) rename Shared/Events/{Internal/InternalCommandDeduplicateEvents.cs => LuaManager/LuaManagerCommandDeduplicateEvents.cs} (76%) diff --git a/Backend/Plugins/LuaManagerPlugin.cs b/Backend/Plugins/LuaManagerPlugin.cs index d2bf70e8..afda895e 100644 --- a/Backend/Plugins/LuaManagerPlugin.cs +++ b/Backend/Plugins/LuaManagerPlugin.cs @@ -1,6 +1,6 @@ using Slipstream.Backend.Services; using Slipstream.Shared; -using Slipstream.Shared.Events.Internal; +using Slipstream.Shared.Events.LuaManager; using System; using System.Collections.Generic; using System.IO; @@ -44,14 +44,14 @@ public LuaManagerPlugin(string id, IEventFactory eventFactory, IEventBus eventBu EventHandler.OnFileMonitorFileRenamed += EventHandler_OnFileMonitorFileRenamed; EventHandler.OnFileMonitorScanCompleted += EventHandler_OnFileMonitorScanCompleted; EventHandler.OnInternalPluginState += EventHandler_OnInternalPluginState; - EventHandler.OnInternalCommandDeduplicateEvents += (s, e) => EventHandler_OnInternalCommandDeduplicateEvents(e.Event); + EventHandler.OnLuaManagerCommandDeduplicateEvents += (s, e) => EventHandler_OnInternalCommandDeduplicateEvents(e.Event); BootupEventsDeadline = DateTime.Now.AddMilliseconds(500); EventBus.PublishEvent(EventFactory.CreateFileMonitorCommandScan()); } - private void EventHandler_OnInternalCommandDeduplicateEvents(InternalCommandDeduplicateEvents @event) + private void EventHandler_OnInternalCommandDeduplicateEvents(LuaManagerCommandDeduplicateEvents @event) { foreach (var e in EventSerdeService.DeserializeMultiple(@event.Events)) { diff --git a/Backend/Plugins/LuaPlugin.cs b/Backend/Plugins/LuaPlugin.cs index 2352aa4c..a31df2f4 100644 --- a/Backend/Plugins/LuaPlugin.cs +++ b/Backend/Plugins/LuaPlugin.cs @@ -51,7 +51,7 @@ private void StartLua() var eventsCaptured = EventBus.CapturedEvents; EventBus.StopCapturing(); - EventBus.PublishEvent(EventFactory.CreateInternalCommandDeduplicateEvents(eventsCaptured)); + EventBus.PublishEvent(EventFactory.CreateLuaManagerCommandDeduplicateEvents(eventsCaptured)); } private void HandleLuaException(LuaException e) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f4ef1db..6ff556a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,8 @@ - UICommandWriteToConsole is now UIMethodCollection - InternalReconfigured is now InternalCommandReconfigure - InternalBootupEvents is now InternalCommandDeduplicateEvents + - InternalCommandDeduplicateEvents is now LuaManagerCommandDeduplicateEvents + - Plugin renamed: FileTriggerPlugin to LuaManagerPlugin - UI: Stores/restores Window position and size - Event: Adds TwitchReceivedMessage event (captures all messages, not only commands) - Event: Removes TwitchReceivedCommand event as this is already sent as a TwitchReceivedMessage diff --git a/Shared/EventFactory.cs b/Shared/EventFactory.cs index 28c806c7..dffe293b 100644 --- a/Shared/EventFactory.cs +++ b/Shared/EventFactory.cs @@ -3,6 +3,7 @@ using Slipstream.Shared.Events.FileMonitor; using Slipstream.Shared.Events.Internal; using Slipstream.Shared.Events.IRacing; +using Slipstream.Shared.Events.LuaManager; using Slipstream.Shared.Events.Twitch; using Slipstream.Shared.Events.UI; using System; @@ -75,7 +76,7 @@ public InternalCommandReconfigure CreateInternalCommandReconfigure() return new InternalCommandReconfigure(); } - public InternalCommandDeduplicateEvents CreateInternalCommandDeduplicateEvents(IEvent[] events) + public LuaManagerCommandDeduplicateEvents CreateLuaManagerCommandDeduplicateEvents(IEvent[] events) { string json = ""; @@ -84,7 +85,7 @@ public InternalCommandDeduplicateEvents CreateInternalCommandDeduplicateEvents(I json += EventSerdeService.Serialize(e) + "\n"; } - return new InternalCommandDeduplicateEvents + return new LuaManagerCommandDeduplicateEvents { Events = json }; diff --git a/Shared/EventHandler.cs b/Shared/EventHandler.cs index bab00774..494c08e5 100644 --- a/Shared/EventHandler.cs +++ b/Shared/EventHandler.cs @@ -37,9 +37,11 @@ public EventHandlerArgs(T e) public delegate void OnInternalCommandReconfigureHandler(EventHandler source, EventHandlerArgs e); public event OnInternalCommandReconfigureHandler? OnInternalCommandReconfigure; + #endregion - public delegate void OnInternalCommandDeduplicateEventsHandler(EventHandler source, EventHandlerArgs e); - public event OnInternalCommandDeduplicateEventsHandler? OnInternalCommandDeduplicateEvents; + #region Events: LuaManager + public delegate void OnInternalCommandDeduplicateEventsHandler(EventHandler source, EventHandlerArgs e); + public event OnInternalCommandDeduplicateEventsHandler? OnLuaManagerCommandDeduplicateEvents; #endregion @@ -207,11 +209,14 @@ public void HandleEvent(IEvent? ev) else OnInternalCommandReconfigure.Invoke(this, new EventHandlerArgs(tev)); break; - case Shared.Events.Internal.InternalCommandDeduplicateEvents tev: - if (OnInternalCommandDeduplicateEvents == null) + + // LuaManager + + case Shared.Events.LuaManager.LuaManagerCommandDeduplicateEvents tev: + if (OnLuaManagerCommandDeduplicateEvents == null) OnDefault?.Invoke(this, new EventHandlerArgs(tev)); else - OnInternalCommandDeduplicateEvents.Invoke(this, new EventHandlerArgs(tev)); + OnLuaManagerCommandDeduplicateEvents.Invoke(this, new EventHandlerArgs(tev)); break; // File Monitor diff --git a/Shared/Events/Internal/InternalCommandDeduplicateEvents.cs b/Shared/Events/LuaManager/LuaManagerCommandDeduplicateEvents.cs similarity index 76% rename from Shared/Events/Internal/InternalCommandDeduplicateEvents.cs rename to Shared/Events/LuaManager/LuaManagerCommandDeduplicateEvents.cs index 3e59586a..1c3a1729 100644 --- a/Shared/Events/Internal/InternalCommandDeduplicateEvents.cs +++ b/Shared/Events/LuaManager/LuaManagerCommandDeduplicateEvents.cs @@ -1,10 +1,10 @@ using System.Collections.Generic; -namespace Slipstream.Shared.Events.Internal +namespace Slipstream.Shared.Events.LuaManager { - public class InternalCommandDeduplicateEvents : IEvent + public class LuaManagerCommandDeduplicateEvents : IEvent { - public string EventType => "InternalCommandDeduplicateEvents"; + public string EventType => "LuaManagerCommandDeduplicateEvents"; public bool ExcludeFromTxrx => true; @@ -12,7 +12,7 @@ public class InternalCommandDeduplicateEvents : IEvent public override bool Equals(object obj) { - return obj is InternalCommandDeduplicateEvents events && + return obj is LuaManagerCommandDeduplicateEvents events && EventType == events.EventType && ExcludeFromTxrx == events.ExcludeFromTxrx && Events == events.Events; diff --git a/Shared/IEventFactory.cs b/Shared/IEventFactory.cs index f8ee9aa0..ecc09a34 100644 --- a/Shared/IEventFactory.cs +++ b/Shared/IEventFactory.cs @@ -2,6 +2,7 @@ using Slipstream.Shared.Events.FileMonitor; using Slipstream.Shared.Events.Internal; using Slipstream.Shared.Events.IRacing; +using Slipstream.Shared.Events.LuaManager; using Slipstream.Shared.Events.Twitch; using Slipstream.Shared.Events.UI; @@ -39,7 +40,7 @@ public enum IRacingSessionStateEnum InternalCommandPluginUnregister CreateInternalCommandPluginUnregister(string pluginId); InternalPluginState CreateInternalPluginState(string pluginId, string pluginName, string displayName, PluginStatusEnum pluginStatus); InternalCommandReconfigure CreateInternalCommandReconfigure(); - InternalCommandDeduplicateEvents CreateInternalCommandDeduplicateEvents(IEvent[] events); + LuaManagerCommandDeduplicateEvents CreateLuaManagerCommandDeduplicateEvents(IEvent[] events); IRacingCarCompletedLap CreateIRacingCarCompletedLap(double sessionTime, long carIdx, double time, int lapsCompleted, float? fuelDiff, bool localUser); IRacingCarInfo CreateIRacingCarInfo( diff --git a/Slipstream.csproj b/Slipstream.csproj index 70a9be16..68615558 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -171,7 +171,7 @@ - +