Skip to content

Commit

Permalink
Merge pull request #12 from dennis/config-via-lua
Browse files Browse the repository at this point in the history
Config via lua
  • Loading branch information
dennis committed Jan 4, 2021
2 parents 9f34518 + 32bb899 commit 4b54aaa
Show file tree
Hide file tree
Showing 48 changed files with 1,009 additions and 738 deletions.
178 changes: 62 additions & 116 deletions Backend/Engine.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
using Slipstream.Backend.Plugins;
using Slipstream.Backend.Services;
using Slipstream.Backend.Services;
using Slipstream.Shared;
using Slipstream.Shared.Events;
using Slipstream.Shared.Events.Internal;
using Slipstream.Shared.Events.Setting;
using System;
using System.IO;
using static Slipstream.Shared.IEventFactory;

#nullable enable

namespace Slipstream.Backend
{
class Engine : Worker, IEngine, IDisposable
partial class Engine : Worker, IEngine, IDisposable
{
private readonly IEventFactory EventFactory;
private readonly IEventBus EventBus;
private readonly PluginManager PluginManager;
private readonly IPluginManager PluginManager;
private readonly IEventBusSubscription Subscription;
private readonly IStateService StateService;
private readonly IApplicationConfiguration ApplicationConfiguration;
private readonly IPluginFactory PluginFactory;
private readonly Shared.EventHandler EventHandler = new Shared.EventHandler();

public Engine(IEventFactory eventFactory, IEventBus eventBus, IStateService stateService, IApplicationConfiguration applicationConfiguration) : base("engine")
public Engine(IEventFactory eventFactory, IEventBus eventBus, IPluginFactory pluginFactory, IPluginManager pluginManager, ILuaSevice luaService, IApplicationVersionService applicationVersionService) : base("engine")
{
EventFactory = eventFactory;
EventBus = eventBus;
StateService = stateService;
ApplicationConfiguration = applicationConfiguration;
PluginManager = new PluginManager(this, eventFactory, eventBus);
PluginFactory = pluginFactory;
PluginManager = pluginManager;

Subscription = EventBus.RegisterListener();

Expand All @@ -36,20 +32,66 @@ public Engine(IEventFactory eventFactory, IEventBus eventBus, IStateService stat
EventHandler.OnInternalCommandPluginEnable += (s, e) => PluginManager.FindPluginAndExecute(e.Event.Id, (plugin) => PluginManager.EnablePlugin(plugin));
EventHandler.OnInternalCommandPluginDisable += (s, e) => PluginManager.FindPluginAndExecute(e.Event.Id, (plugin) => PluginManager.DisablePlugin(plugin));
EventHandler.OnInternalCommandPluginStates += (s, e) => OnCommandPluginStates(e.Event);
EventHandler.OnInternalReconfigured += (s, e) => OnInternalReconfigured();

// Plugins..
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("FileMonitorPlugin", "FileMonitorPlugin", ApplicationConfiguration.GetFileMonitorSettingsEvent()));
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("FileTriggerPlugin", "FileTriggerPlugin"));
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("AudioPlugin", "AudioPlugin", ApplicationConfiguration.GetAudioSettingsEvent()));
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("IRacingPlugin", "IRacingPlugin"));
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("TwitchPlugin", "TwitchPlugin", ApplicationConfiguration.GetTwitchSettingsEvent()));
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("TransmitterPlugin", "TransmitterPlugin", ApplicationConfiguration.GetTxrxSettingsEvent()), false);
RegisterPlugin(EventFactory.CreateInternalCommandPluginRegister("ReceiverPlugin", "ReceiverPlugin", ApplicationConfiguration.GetTxrxSettingsEvent()), false);
{
var initFilename = $"init-{applicationVersionService.Version}.lua";

if (!File.Exists(initFilename))
{
File.WriteAllText(initFilename, @"
-- This file is auto generated upon startup, if it doesnt exist. So if you
-- ever break it, just rename/delete it, and a new working one is created.
-- There is no auto-reloading of this file - it is only evaluated at startup
print ""Initializing""
-- Listens for samples to play or text to speek. Disabling this will mute all
-- sounds
register_plugin(""AudioPlugin"", ""AudioPlugin"")
enable_plugin(""AudioPlugin"")
-- Delivers IRacing events as they happen
register_plugin(""IRacingPlugin"", ""IRacingPlugin"")
enable_plugin(""IRacingPlugin"")
-- Connects to Twitch (via the values provided in Settings) and provide
-- a way to sende and receive twitch messages
register_plugin(""TwitchPlugin"", ""TwitchPlugin"")
enable_plugin(""TwitchPlugin"")
-- Only one of these may be active at a time. ReceiverPlugin listens
-- for TCP connections, while Transmitter will send the events it sees
-- to the destination. Both are configured as Txrx in Settings.
register_plugin(""TransmitterPlugin"", ""TransmitterPlugin"")
register_plugin(""ReceiverPlugin"", ""ReceiverPlugin"")
-- FileMonitorPlugin monitors the script directory and sends out events
-- every time a file is created, renamed, modified or deleted
register_plugin(""FileMonitorPlugin"", ""FileMonitorPlugin"")
enable_plugin(""FileMonitorPlugin"")
-- 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
-- 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"")
enable_plugin(""FileTriggerPlugin"")");
}

luaService.Parse(filename: initFilename, logPrefix: "INIT");
}

// Tell Plugins that we're live - this will make eventbus distribute events
EventBus.Enabled = true;
}

private void OnInternalReconfigured()
{
PluginManager.RestartReconfigurablePlugins();
}

private void OnCommandPluginStates(InternalCommandPluginStates _)
{
PluginManager.ForAllPluginsExecute(
Expand All @@ -58,20 +100,6 @@ private void OnCommandPluginStates(InternalCommandPluginStates _)
));
}

private void RegisterPlugin(InternalCommandPluginRegister e, bool enable = true)
{
OnCommandPluginRegister(e);
if (enable)
{
EventBus.PublishEvent(EventFactory.CreateInternalCommandPluginEnable(e.Id));
}
}

public IEventBusSubscription RegisterListener()
{
return EventBus.RegisterListener();
}

private void OnCommandPluginUnregister(InternalCommandPluginUnregister ev)
{
PluginManager.UnregisterPlugin(ev.Id);
Expand All @@ -84,89 +112,7 @@ public void UnregisterSubscription(IEventBusSubscription subscription)

private void OnCommandPluginRegister(Shared.Events.Internal.InternalCommandPluginRegister ev)
{
switch (ev.PluginName)
{
case "FileMonitorPlugin":
{
if (!(ev.Settings is FileMonitorSettings settings))
{
throw new Exception("Unexpected settings for FileMonitorPlugin");
}
else
{
PluginManager.RegisterPlugin(new FileMonitorPlugin(ev.Id, EventFactory, EventBus, settings));
}
}
break;
case "FileTriggerPlugin":
PluginManager.RegisterPlugin(new FileTriggerPlugin(ev.Id, EventFactory, EventBus));
break;
case "LuaPlugin":
{
if (!(ev.Settings is LuaSettings settings))
{
throw new Exception("Unexpected settings for LuaPlugin");
}
else
{
PluginManager.RegisterPlugin(new LuaPlugin(ev.Id, EventFactory, EventBus, StateService, settings));
}
}
break;
case "AudioPlugin":
{
if (!(ev.Settings is AudioSettings settings))
{
throw new Exception("Unexpected settings for AudioPlugin");
}
else
{
PluginManager.RegisterPlugin(new AudioPlugin(ev.Id, EventFactory, EventBus, settings));
}
}
break;
case "IRacingPlugin":
PluginManager.RegisterPlugin(new IRacingPlugin(ev.Id, EventFactory, EventBus));
break;
case "TwitchPlugin":
{
if (!(ev.Settings is TwitchSettings settings))
{
throw new Exception("Unexpected settings for TwitchPlugin");
}
else
{
PluginManager.RegisterPlugin(new TwitchPlugin(ev.Id, EventFactory, EventBus, settings));
}
}
break;
case "TransmitterPlugin":
{
if (!(ev.Settings is TxrxSettings settings))
{
throw new Exception("Unexpected settings for TransmitterPlugin");
}
else
{
PluginManager.RegisterPlugin(new TransmitterPlugin(ev.Id, EventFactory, EventBus, settings));
}
}
break;
case "ReceiverPlugin":
{
if (!(ev.Settings is TxrxSettings settings))
{
throw new Exception("Unexpected settings for ReceiverPlugin");
}
else
{
PluginManager.RegisterPlugin(new ReceiverPlugin(ev.Id, EventFactory, EventBus, settings));
}
}
break;
default:
throw new Exception($"Unknown plugin '{ev.PluginName}'");
}
PluginManager.RegisterPlugin(PluginFactory.CreatePlugin(ev.Id, ev.PluginName));
}

protected override void Main()
Expand Down
1 change: 0 additions & 1 deletion Backend/IEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Slipstream.Backend
{
public interface IEngine
{
IEventBusSubscription RegisterListener();
void UnregisterSubscription(IEventBusSubscription subscription);
void Start();
void Dispose();
Expand Down
2 changes: 1 addition & 1 deletion Backend/IPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public EventHandlerArgs(T e)
public bool PendingOnEnable { get; set; }
public bool PendingOnDisable { get; set; }
public Shared.EventHandler EventHandler { get; }
public bool Reconfigurable { get; }
void Enable();
void Disable();
void OnEnable();
void OnDisable();

void Loop();
}
}
10 changes: 10 additions & 0 deletions Backend/IPluginFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#nullable enable

namespace Slipstream.Backend
{
public interface IPluginFactory
{
IPlugin CreatePlugin(string id, string name);
IPlugin CreatePlugin<T>(string pluginId, string name, T configuration);
}
}
19 changes: 19 additions & 0 deletions Backend/IPluginManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#nullable enable

using System;

namespace Slipstream.Backend
{
public interface IPluginManager : IDisposable
{
public void UnregisterPlugin(IPlugin p);
public void UnregisterPlugin(string id);
public void RegisterPlugin(IPlugin plugin);
public void EnablePlugin(IPlugin p);
public void DisablePlugins();
public void DisablePlugin(IPlugin p);
public void FindPluginAndExecute(string pluginId, Action<IPlugin> a);
public void ForAllPluginsExecute(Action<IPlugin> a);
public void RestartReconfigurablePlugins();
}
}
Loading

0 comments on commit 4b54aaa

Please sign in to comment.