Skip to content

Commit

Permalink
Merge pull request #21 from dennis/consistency
Browse files Browse the repository at this point in the history
Rename plugins/events for consistency
  • Loading branch information
dennis committed Jan 10, 2021
2 parents 4b6db6b + 0ff2b96 commit 50ce9fd
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 101 deletions.
49 changes: 7 additions & 42 deletions Backend/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,35 @@
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<IEvent> CapturedBootupEvents = new List<IEvent>();

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();

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.OnInternalBootupEvents += (s, e) => OnInternalBootupEvents(e.Event);

BootupEventsDeadline = DateTime.Now.AddMilliseconds(500);
EventHandler.OnInternalCommandReconfigure += (s, e) => OnInternalReconfigured();

// Plugins..
{
Expand Down Expand Up @@ -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
Expand All @@ -91,17 +81,6 @@ public Engine(IEventFactory eventFactory, IEventBus eventBus, IPluginFactory plu
EventBus.Enabled = true;
}

private void OnInternalBootupEvents(InternalBootupEvents @event)
{
foreach(var e in EventSerdeService.DeserializeMultiple(@event.Events))
{
CapturedBootupEvents.Add(e);
}

// Postponing deadline
BootupEventsDeadline = DateTime.Now.AddMilliseconds(500);
}

private void OnInternalReconfigured()
{
PluginManager.RestartReconfigurablePlugins();
Expand Down Expand Up @@ -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));
}
}
Expand Down
7 changes: 4 additions & 3 deletions Backend/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
using Slipstream.Shared;
using Slipstream.Backend.Services;
using Slipstream.Shared;
using Slipstream.Shared.Events.LuaManager;
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<string, IPlugin> Scripts = new Dictionary<string, IPlugin>();

// At bootup we will receive zero or more FileCreated events ending with a ScanCompleted.
Expand All @@ -22,23 +27,41 @@ internal class FileTriggerPlugin : BasePlugin

private readonly List<string> WaitingForLuaScripts = new List<string>();

public FileTriggerPlugin(string id, IEventFactory eventFactory, IEventBus eventBus, IPluginManager pluginManager, IPluginFactory pluginFactory) : base(id, "FileTriggerPlugin", "FileTriggerPlugin", "Core")
private DateTime? BootupEventsDeadline;
private readonly List<IEvent> CapturedBootupEvents = new List<IEvent>();

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;
EventHandler.OnFileMonitorFileChanged += EventHandler_OnFileMonitorFileChanged;
EventHandler.OnFileMonitorFileRenamed += EventHandler_OnFileMonitorFileRenamed;
EventHandler.OnFileMonitorScanCompleted += EventHandler_OnFileMonitorScanCompleted;
EventHandler.OnInternalPluginState += EventHandler_OnInternalPluginState;
EventHandler.OnLuaManagerCommandDeduplicateEvents += (s, e) => EventHandler_OnInternalCommandDeduplicateEvents(e.Event);

BootupEventsDeadline = DateTime.Now.AddMilliseconds(500);

EventBus.PublishEvent(EventFactory.CreateFileMonitorCommandScan());
}

private void EventHandler_OnInternalCommandDeduplicateEvents(LuaManagerCommandDeduplicateEvents @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<Shared.Events.Internal.InternalPluginState> e)
{
if (e.Event.PluginStatus == "Registered" && e.Event.PluginName == "LuaPlugin")
Expand Down Expand Up @@ -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;
}
}
}
}
2 changes: 1 addition & 1 deletion Backend/Plugins/LuaPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private void StartLua()
var eventsCaptured = EventBus.CapturedEvents;
EventBus.StopCapturing();

EventBus.PublishEvent(EventFactory.CreateInternalBootupEvents(eventsCaptured));
EventBus.PublishEvent(EventFactory.CreateLuaManagerCommandDeduplicateEvents(eventsCaptured));
}

private void HandleLuaException(LuaException e)
Expand Down
23 changes: 3 additions & 20 deletions Backend/Services/LuaServiceLib/CoreMethodCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,24 @@ public DelayedExecution(LuaFunction luaFunction, DateTime triggersAt)
}
}

private readonly string Prefix;
private readonly IEventFactory EventFactory;
private readonly IEventBus EventBus;
private readonly IEventSerdeService EventSerdeService;

private readonly IDictionary<string, DelayedExecution> DebounceDelayedFunctions = new Dictionary<string, DelayedExecution>();
private readonly IDictionary<string, DelayedExecution> WaitDelayedFunctions = new Dictionary<string, DelayedExecution>();

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;
Expand All @@ -54,30 +52,19 @@ 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)
{
if (func != null)
{
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")]
Expand All @@ -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")]
Expand Down
4 changes: 2 additions & 2 deletions Backend/Services/LuaServiceLib/LuaContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 12 additions & 3 deletions Backend/Services/LuaServiceLib/UIMethodCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
- 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
- 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
Expand Down
2 changes: 1 addition & 1 deletion Frontend/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down
Loading

0 comments on commit 50ce9fd

Please sign in to comment.