Skip to content

Commit

Permalink
Merge pull request #89 from dennis/autofac2
Browse files Browse the repository at this point in the history
Utilize autofac over own factories, servicelocators etc
  • Loading branch information
dennis committed Apr 5, 2021
2 parents 37620ac + 6f28a54 commit 83f56bd
Show file tree
Hide file tree
Showing 86 changed files with 684 additions and 2,048 deletions.
8 changes: 8 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<assemblyIdentity name="Microsoft.Win32.Registry" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Bcl.AsyncInterfaces" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<userSettings>
Expand Down
1 change: 1 addition & 0 deletions Backend/Bootstrap/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
28 changes: 18 additions & 10 deletions Backend/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<IInternalEventFactory>();
EventFactory = eventFactory;
EventBus = eventBus;
PluginFactory = pluginFactory;
PluginManager = pluginManager;
Logger = logger;
EventHandlerController = eventHandlerControllerBuilder.CreateEventHandlerController();
EventHandlerController = eventHandlerController;

Subscription = EventBus.RegisterListener();

var internalEventHandler = EventHandlerController.Get<Slipstream.Components.Internal.EventHandler.Internal>();

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..
{
Expand All @@ -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<Components.ILuaGlue> { 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);
}

Expand Down
5 changes: 2 additions & 3 deletions Backend/IEngine.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
21 changes: 21 additions & 0 deletions Backend/LifetimeScopeExtensions.cs
Original file line number Diff line number Diff line change
@@ -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<Type> GetImplementingTypes<T>(this ILifetimeScope scope)
{
return scope.ComponentRegistry
.RegistrationsFor(new TypedService(typeof(T)))
.Select(x => x.Activator)
.OfType<ReflectionActivator>()
.Select(x => x.LimitType);
}
}
}
35 changes: 19 additions & 16 deletions Backend/PluginManager.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#nullable enable

using Autofac;
using Serilog;
using Slipstream.Components;
using Slipstream.Components.Internal;
using Slipstream.Shared;
using Slipstream.Shared.Helpers.StrongParameters;
using System;
using System.Collections.Generic;
using System.Linq;
using static Slipstream.Components.Internal.IInternalEventFactory;

namespace Slipstream.Backend
Expand All @@ -17,29 +17,27 @@ public class PluginManager : IPluginManager, IPluginFactory
private readonly IInternalEventFactory InternalEventFactory;
private readonly IEventBus EventBus;
private readonly IDictionary<string, PluginWorker> PluginWorkers = new Dictionary<string, PluginWorker>();
private readonly IDictionary<string, Type> PluginFactories = new Dictionary<string, Type>();
private readonly ILogger Logger;
private readonly ComponentRegistrator Registrator;
private readonly List<ILuaGlueFactory> LuaGluesFactories = new List<ILuaGlueFactory>();
private readonly Dictionary<string, Func<IComponentPluginCreationContext, IPlugin>> ComponentPlugins = new Dictionary<string, Func<IComponentPluginCreationContext, IPlugin>>();
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<IPlugin>())
{
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<IInternalEventFactory>();
InternalEventFactory = internalEventFactory;
EventBus = eventBus;
Logger = logger;
LifetimeScope = lifetimeScope;
}

public void UnregisterPlugin(IPlugin p)
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion Backend/PluginWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public PluginWorker(IPlugin plugin, IEventBusSubscription subscription, IInterna
EventFactory = eventFactory;
}

private void Plugin_OnStateChanged(IPlugin plugin, IPlugin.EventHandlerArgs<IPlugin> e)
private void Plugin_OnStateChanged(object source, BasePlugin plugin)
{
EventBus.PublishEvent(EventFactory.CreateInternalPluginState(plugin.Id, plugin.Name, plugin.DisplayName, PluginStatusEnum.Registered));
}
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 0 additions & 29 deletions Components/AppilcationUpdate/ApplicationUpdate.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using Slipstream.Components.AppilcationUpdate.Events;
#nullable enable

using Slipstream.Components.AppilcationUpdate.Events;
using Slipstream.Shared;
using System;

namespace Slipstream.Components.AppilcationUpdate.EventHandler
{
internal class ApplicationUpdateEventHandler : IEventHandler
{
public event EventHandler<ApplicationUpdateLatestVersionChanged> OnApplicationUpdateLatestVersionChanged;
public event EventHandler<ApplicationUpdateCommandCheckLatestVersion> OnApplicationUpdateCommandCheckLatestVersion;
private readonly IEventHandlerController Parent;
public event EventHandler<ApplicationUpdateLatestVersionChanged>? OnApplicationUpdateLatestVersionChanged;

public ApplicationUpdateEventHandler(IEventHandlerController eventHandler)
{
Parent = eventHandler;
}
public event EventHandler<ApplicationUpdateCommandCheckLatestVersion>? OnApplicationUpdateCommandCheckLatestVersion;

public IEventHandler.HandledStatus HandleEvent(IEvent @event)
{
Expand All @@ -25,14 +22,14 @@ public IEventHandler.HandledStatus HandleEvent(IEvent @event)
};
}

private IEventHandler.HandledStatus OnEvent<TEvent>(EventHandler<TEvent> onEvent, TEvent args)
private IEventHandler.HandledStatus OnEvent<TEvent>(EventHandler<TEvent>? 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;
}
}
}
}
34 changes: 20 additions & 14 deletions Components/AppilcationUpdate/Plugins/ApplicationUpdatePlugin.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -67,18 +68,18 @@ private void SubscribeToInternalEvents()
{
// Registering for internal plugin state event
var internalEvents = EventHandlerController.Get<Internal.EventHandler.Internal>();
internalEvents.OnInternalPluginState += (s, e) => OnInternalPluginState(s, e);
internalEvents.OnInternalPluginState += (s, e) => OnInternalPluginState(e);
}

private void OnInternalPluginState(IEventHandlerController s, EventHandlerArgs<InternalPluginState> 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());
Expand Down Expand Up @@ -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();
Expand All @@ -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()));
Expand All @@ -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<ILuaGlue> CreateLuaGlues()
{
return new ILuaGlue[] { };
}
}
}
}
Loading

0 comments on commit 83f56bd

Please sign in to comment.