Skip to content

Commit

Permalink
Tidying up the PR
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis committed Jan 4, 2021
1 parent 4983071 commit c33d284
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 16 deletions.
1 change: 1 addition & 0 deletions Backend/IPluginFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace Slipstream.Backend
public interface IPluginFactory
{
IPlugin CreatePlugin(string id, string name);
IPlugin CreatePlugin<T>(string pluginId, string name, T configuration);
}
}
2 changes: 1 addition & 1 deletion Backend/IPluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public interface IPluginManager : IDisposable
public void DisablePlugin(IPlugin p);
public void FindPluginAndExecute(string pluginId, Action<IPlugin> a);
public void ForAllPluginsExecute(Action<IPlugin> a);
void RestartReconfigurablePlugins();
public void RestartReconfigurablePlugins();
}
}
13 changes: 12 additions & 1 deletion Backend/PluginManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public IPlugin CreatePlugin(string id, string name)
return name switch
{
"FileMonitorPlugin" => new FileMonitorPlugin(id, EventFactory, EventBus, ApplicationConfiguration),
"FileTriggerPlugin" => new FileTriggerPlugin(id, EventFactory, EventBus, StateService, this),
"FileTriggerPlugin" => new FileTriggerPlugin(id, EventFactory, EventBus, this, this),
"AudioPlugin" => new AudioPlugin(id, EventFactory, EventBus, ApplicationConfiguration),
"IRacingPlugin" => new IRacingPlugin(id, EventFactory, EventBus),
"TwitchPlugin" => new TwitchPlugin(id, EventFactory, EventBus, ApplicationConfiguration),
Expand All @@ -200,6 +200,17 @@ public IPlugin CreatePlugin(string id, string name)
};
}

public IPlugin CreatePlugin<T>(string pluginId, string name, T configuration)
{
return name switch
{
#pragma warning disable CS8604 // Possible null reference argument.
"LuaPlugin" when configuration is ILuaConfiguration => new LuaPlugin(pluginId, EventFactory, EventBus, StateService, configuration as ILuaConfiguration),
#pragma warning restore CS8604 // Possible null reference argument.
_ => throw new Exception($"Unknown configurable plugin '{name}'"),
};
}

public void Dispose()
{
lock(Plugins)
Expand Down
2 changes: 1 addition & 1 deletion Backend/Plugins/BasePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public bool Enabled
public bool PendingOnDisable { get { return pendingOnDisable; } set { pendingOnDisable = value; } }

public event IPlugin.OnStateChangedHandler? OnStateChanged;
public bool Reconfigurable { get; protected set; }
public bool Reconfigurable { get; private set; }

public string WorkerName
{
Expand Down
17 changes: 9 additions & 8 deletions Backend/Plugins/FileTriggerPlugin.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Slipstream.Backend.Services;
using Slipstream.Shared;
using Slipstream.Shared;
using System.Collections.Generic;
//using System.Diagnostics;
using System.IO;
using EventHandler = Slipstream.Shared.EventHandler;

Expand All @@ -13,8 +11,8 @@ class FileTriggerPlugin : BasePlugin
{
private readonly IEventFactory EventFactory;
private readonly IEventBus EventBus;
private readonly IStateService StateService;
private readonly IPluginManager PluginManager;
private readonly IPluginFactory PluginFactory;
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 @@ -23,12 +21,12 @@ class FileTriggerPlugin : BasePlugin
private bool BootUp = true;
private readonly List<string> WaitingForLuaScripts = new List<string>();

public FileTriggerPlugin(string id, IEventFactory eventFactory, IEventBus eventBus, IStateService stateService, IPluginManager pluginManager) : base(id, "FileTriggerPlugin", "FileTriggerPlugin", "Core")
public FileTriggerPlugin(string id, IEventFactory eventFactory, IEventBus eventBus, IPluginManager pluginManager, IPluginFactory pluginFactory) : base(id, "FileTriggerPlugin", "FileTriggerPlugin", "Core")
{
EventFactory = eventFactory;
EventBus = eventBus;
StateService = stateService;
PluginManager = pluginManager;
PluginFactory = pluginFactory;

EventHandler.OnFileMonitorFileCreated += EventHandler_OnFileMonitorFileCreated;
EventHandler.OnFileMonitorFileDeleted += EventHandler_OnFileMonitorFileDeleted;
Expand Down Expand Up @@ -78,8 +76,9 @@ private void NewFile(string filePath)
WaitingForLuaScripts.Add(pluginId);
}

// Use PluginManager director
var plugin = new LuaPlugin(pluginId, EventFactory, EventBus, StateService, new LuaConfiguration { FilePath = filePath });

var plugin = PluginFactory.CreatePlugin(pluginId, "LuaPlugin", (ILuaConfiguration)new LuaConfiguration { FilePath = filePath });

PluginManager.RegisterPlugin(plugin);
PluginManager.EnablePlugin(plugin);

Expand All @@ -95,6 +94,8 @@ private void DeletedFile(string filePath)

private void EventHandler_OnFileMonitorFileRenamed(EventHandler source, EventHandler.EventHandlerArgs<Shared.Events.FileMonitor.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)
return;

Expand Down
9 changes: 9 additions & 0 deletions Backend/Services/ILuaSevice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#nullable enable

namespace Slipstream.Backend.Services
{
public interface ILuaSevice
{
ILuaContext Parse(string filename, string logPrefix);
}
}
5 changes: 0 additions & 5 deletions Backend/Services/LuaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

namespace Slipstream.Backend.Services
{
public interface ILuaSevice
{
ILuaContext Parse(string filename, string logPrefix);
}

public class LuaService : ILuaSevice
{
private readonly IEventFactory EventFactory;
Expand Down
1 change: 1 addition & 0 deletions Slipstream.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
<Compile Include="Backend\Plugins\BasePlugin.cs" />
<Compile Include="Backend\Plugins\TwitchPlugin.cs" />
<Compile Include="Backend\Services\IEventSerdeService.cs" />
<Compile Include="Backend\Services\ILuaSevice.cs" />
<Compile Include="Backend\Services\IStateService.cs" />
<Compile Include="Backend\Services\EventSerdeService.cs" />
<Compile Include="Backend\Services\ITxrxService.cs" />
Expand Down

0 comments on commit c33d284

Please sign in to comment.