Skip to content

Commit

Permalink
Merge pull request #123 from dennis/rework-ui
Browse files Browse the repository at this point in the history
Rework UI
  • Loading branch information
dennis committed Aug 18, 2021
2 parents ce5b868 + d016640 commit 3b295be
Show file tree
Hide file tree
Showing 72 changed files with 1,874 additions and 342 deletions.
51 changes: 19 additions & 32 deletions Backend/EventBus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Slipstream.Shared;

using System.Collections.Generic;
using System.Diagnostics;

Expand Down Expand Up @@ -34,38 +35,27 @@ public void PublishEvent(IEvent e)
{
e.Envelope.Uptime = Uptime() - StartedAt;

lock (Listeners)
if (enabled)
{
if (enabled)
lock (Events)
{
lock (Events)
if (Events.Count >= EVENT_MAX_SIZE)
{
if (Events.Count >= EVENT_MAX_SIZE)
{
// Is there a better way for deleting x elements from the beginning?
for (int i = 0; i < EVENT_DELETE_SIZE; i++)
Events.RemoveAt(0);
}
Events.Add(e);
// Is there a better way for deleting x elements from the beginning?
for (int i = 0; i < EVENT_DELETE_SIZE; i++)
Events.RemoveAt(0);
}
Events.Add(e);
}
}

lock (Listeners)
{
if (enabled)
{
foreach (var l in Listeners)
{
bool applicable = false;

foreach (var instanceId in l.InstanceIds)
{
if (e.Envelope.ContainsRecipient(instanceId))
{
applicable = true;
break;
}
}

if (applicable)
{
l.Add(e);
}
l.Add(e);
}
}
else
Expand All @@ -75,9 +65,9 @@ public void PublishEvent(IEvent e)
}
}

public IEventBusSubscription RegisterListener(string instanceId, bool fromStart = false)
public IEventBusSubscription RegisterListener(string instanceId, bool fromStart = false, bool promiscuousMode = false)
{
var subscription = new EventBusSubscription(this, instanceId);
var subscription = new EventBusSubscription(this, instanceId, promiscuousMode);

lock (Listeners)
{
Expand All @@ -87,10 +77,7 @@ public IEventBusSubscription RegisterListener(string instanceId, bool fromStart
{
foreach (var e in Events)
{
if (e.Envelope.ContainsRecipient(instanceId))
{
subscription.Add(e);
}
subscription.Add(e);
}
}
}
Expand Down Expand Up @@ -124,7 +111,7 @@ private void EnableAndFlushPendingEvents()
}
}

private ulong Uptime()
private static ulong Uptime()
{
var tick = (double)Stopwatch.GetTimestamp() * 1000 / Stopwatch.Frequency;
return (ulong)tick;
Expand Down
22 changes: 19 additions & 3 deletions Backend/EventBusSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,33 @@ internal class EventBusSubscription : IEventBusSubscription
{
private readonly BlockingCollection<IEvent> Events = new BlockingCollection<IEvent>();
private readonly IEventBus EventBus;
public List<string> InstanceIds { get; set; } = new List<string>();
private readonly bool PromiscuousMode = false;
private List<string> InstanceIds { get; set; } = new List<string>();

public EventBusSubscription(IEventBus eventBus, string instanceId)
public EventBusSubscription(IEventBus eventBus, string instanceId, bool promiscuousMode = false)
{
EventBus = eventBus;
InstanceIds.Add(instanceId);
PromiscuousMode = promiscuousMode;
}

public void Add(IEvent ev)
{
Events.Add(ev);
if (PromiscuousMode)
{
Events.Add(ev);
}
else
{
foreach (var instanceId in InstanceIds)
{
if (ev.Envelope.ContainsRecipient(instanceId))
{
Events.Add(ev);
return;
}
}
}
}

public void Dispose()
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

## Next version
[Full Changelog](https://github.com/dennis/slipstream/compare/v0.8.0...main)
- WinFormUI: Reworked UI. Now allows you to see scripts and which instances they
use. And many other small tweaks
- Removed empty menu "Plugins"
- Lua: Added some lua code to make it possible to pick up events with the following code:
```lua
addEventHandler("InternalDependencyAdded", function(event)
-- Example event: {"EventType":"InternalDependencyAdded","Envelope":{"Sender":"Scripts\\simple.lua","Recipients":null,"Uptime":25020},"LuaLibrary":"api/lua","InstanceId":"Scripts\\simple.lua","DependsOn":"winformui"}
ui:print(util:event_to_json(event))
end)
```
It will setup a `handle()` function for you that wires it up.

## [0.8.0](https://github.com/dennis/slipstream/releases/tag/v0.8.0) (2021-08-04)
[Full Changelog](https://github.com/dennis/slipstream/compare/v0.7.0...v0.8.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.IO;
using System.Net;
using System.Runtime.Versioning;
using Slipstream.Components.Internal;

namespace Slipstream.Components.AppilcationUpdate.Lua
{
Expand All @@ -21,14 +22,14 @@ public class ApplicationUpdateInstanceThread : BaseInstanceThread, IApplicationU
{
private readonly IEventHandlerController EventHandlerController;
private readonly IApplicationUpdateEventFactory ApplicationUpdateEventFactory;
private readonly IEventBus EventBus;
private readonly IEventBusSubscription Subscription;
private readonly IApplicationVersionService ApplicationVersionService;
private readonly string UpdateLocation;
private readonly bool Prerelease;
private UpdateInfoEventArgs? LastUpdateInfoEventArgs;

public ApplicationUpdateInstanceThread(
string luaLibraryName,
string instanceId,
string location,
bool prerelease,
Expand All @@ -37,8 +38,9 @@ public ApplicationUpdateInstanceThread(
IApplicationUpdateEventFactory applicationUpdateEventFactory,
IEventBus eventBus,
IEventBusSubscription subscription,
IApplicationVersionService applicationVersionService
) : base(instanceId, logger, eventHandlerController)
IApplicationVersionService applicationVersionService,
IInternalEventFactory internalEventFactory
) : base(luaLibraryName, instanceId, logger, eventHandlerController, eventBus, internalEventFactory)
{
UpdateLocation = location;
Prerelease = prerelease;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#nullable enable

using Autofac;

using Slipstream.Components.Internal;
using Slipstream.Shared;
using Slipstream.Shared.Helpers.StrongParameters;
Expand All @@ -25,14 +26,18 @@ public ApplicationUpdateLuaLibrary(ILifetimeScope scope, IEventBus eventBus) : b
{
}

protected override IApplicationUpdateInstanceThread CreateInstance(ILifetimeScope scope, Parameters cfg)
protected override IApplicationUpdateInstanceThread CreateInstance(ILifetimeScope scope, string luaScriptInstanceId, Parameters cfg)
{
var instanceId = cfg.Extract<string>("id");
var updateLocation = cfg.Extract<string>("location");
var prerelease = cfg.ExtractOrDefault("prerelease", false);

var subscription = EventBus.RegisterListener(instanceId);

return scope.Resolve<IApplicationUpdateInstanceThread>(
new NamedParameter("luaLibraryName", Name),
new NamedParameter("instanceId", instanceId),
new TypedParameter(typeof(IEventBusSubscription), subscription),
new NamedParameter("location", updateLocation),
new NamedParameter("prerelease", prerelease)
);
Expand Down
16 changes: 13 additions & 3 deletions Components/Audio/Lua/AudioInstanceThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

using Slipstream.Components.Audio.EventHandler;
using Slipstream.Components.Audio.Events;
using Slipstream.Components.Internal;
using Slipstream.Shared;
using Slipstream.Shared.Lua;

Expand All @@ -27,10 +28,19 @@ public class AudioInstanceThread : BaseInstanceThread, IAudioInstanceThread, IDi
private readonly IEventHandlerController EventHandlerController;
private readonly IEventBusSubscription Subscription;
private readonly IAudioEventFactory EventFactory;
private readonly IEventBus EventBus;
private int OutputDeviceNumber = -1;

public AudioInstanceThread(string instanceId, int output, string path, IEventBusSubscription eventBusSubscription, IEventHandlerController eventHandlerController, IEventBus eventBus, IAudioEventFactory audioEventFactory, ILogger logger) : base(instanceId, logger, eventHandlerController)
public AudioInstanceThread(
string luaLibraryName,
string instanceId,
int output,
string path,
IEventBusSubscription eventBusSubscription,
IEventHandlerController eventHandlerController,
IEventBus eventBus,
IAudioEventFactory audioEventFactory,
IInternalEventFactory internalEventFactory,
ILogger logger) : base(luaLibraryName, instanceId, logger, eventHandlerController, eventBus, internalEventFactory)
{
Path = path;
Subscription = eventBusSubscription;
Expand Down Expand Up @@ -65,7 +75,7 @@ private void OnAudioCommandSetOutputDevice(AudioCommandSetOutputDevice @event)
OutputDeviceNumber = @event.DeviceIdx;
}

private void OnAudioCommandSendDevices(AudioCommandSendDevices @event)
private void OnAudioCommandSendDevices(AudioCommandSendDevices _)
{
for (int n = -1; n < WaveOut.DeviceCount; n++)
{
Expand Down
5 changes: 3 additions & 2 deletions Components/Audio/Lua/AudioLuaLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#nullable enable

using Autofac;
using Slipstream.Components.Internal;

using Slipstream.Shared;
using Slipstream.Shared.Helpers.StrongParameters;
using Slipstream.Shared.Helpers.StrongParameters.Validators;
Expand All @@ -25,7 +25,7 @@ public AudioLuaLibrary(ILifetimeScope scope, IEventBus eventBus) : base(Configur
{
}

protected override IAudioInstanceThread CreateInstance(ILifetimeScope scope, Parameters cfg)
protected override IAudioInstanceThread CreateInstance(ILifetimeScope scope, string luaScriptInstanceId, Parameters cfg)
{
var instanceId = cfg.Extract<string>("id");
var outputDeviceIdx = cfg.ExtractOrDefault("output", -1);
Expand All @@ -34,6 +34,7 @@ protected override IAudioInstanceThread CreateInstance(ILifetimeScope scope, Par
var subscription = EventBus.RegisterListener(instanceId);

return scope.Resolve<IAudioInstanceThread>(
new NamedParameter("luaLibraryName", Name),
new NamedParameter("instanceId", instanceId),
new NamedParameter("output", outputDeviceIdx),
new NamedParameter("path", path),
Expand Down
3 changes: 2 additions & 1 deletion Components/Discord/Lua/DiscordLuaLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ public DiscordLuaLibrary(ILifetimeScope scope, IEventBus eventBus) : base(Config
{
}

protected override IDiscordInstanceThread CreateInstance(ILifetimeScope scope, Parameters cfg)
protected override IDiscordInstanceThread CreateInstance(ILifetimeScope scope, string luaScriptInstanceId, Parameters cfg)
{
var instanceId = cfg.Extract<string>("id");
var token = cfg.Extract<string>("token");

var subscription = EventBus.RegisterListener(instanceId);

return scope.Resolve<IDiscordInstanceThread>(
new NamedParameter("luaLibraryName", Name),
new NamedParameter("instanceId", instanceId),
new NamedParameter("token", token),
new TypedParameter(typeof(IEventBusSubscription), subscription)
Expand Down
8 changes: 4 additions & 4 deletions Components/Discord/Lua/DiscordServiceThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

using Slipstream.Components.Discord.EventHandler;
using Slipstream.Components.Discord.Events;
using Slipstream.Components.Internal;
using Slipstream.Shared;
using Slipstream.Shared.Lua;

Expand All @@ -20,7 +21,6 @@ internal class DiscordServiceThread : BaseInstanceThread, IDiscordInstanceThread
{
private readonly IEventBusSubscription Subscription;
private readonly IEventHandlerController EventHandlerController;
private readonly IEventBus EventBus;
private readonly IDiscordEventFactory DiscordEventFactory;
private readonly string Token;
private readonly Thread ServiceThead;
Expand All @@ -29,13 +29,15 @@ internal class DiscordServiceThread : BaseInstanceThread, IDiscordInstanceThread
private bool RequestConnect = true;

public DiscordServiceThread(
string luaLibraryName,
string instanceId,
string token,
IEventBusSubscription eventBusSubscription,
IEventHandlerController eventHandlerController,
IEventBus eventBus,
IDiscordEventFactory discordEventFactory,
ILogger logger) : base(instanceId, logger, eventHandlerController)
IInternalEventFactory internalEventFactory,
ILogger logger) : base(luaLibraryName, instanceId, logger, eventHandlerController, eventBus, internalEventFactory)
{
Subscription = eventBusSubscription;
EventHandlerController = eventHandlerController;
Expand Down Expand Up @@ -80,8 +82,6 @@ override protected void Main()
RequestConnect = false;
}
}

Logger.Debug($"Stopping {nameof(DiscordServiceThread)} {InstanceId}");
}

private Task Client_Ready1(DiscordClient sender, DSharpPlus.EventArgs.ReadyEventArgs e)
Expand Down
23 changes: 18 additions & 5 deletions Components/FileMonitor/Lua/FileMonitorInstanceThread.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
#nullable enable

using Serilog;

using Slipstream.Components.FileMonitor.Events;
using Slipstream.Components.Internal;
using Slipstream.Shared;
using Slipstream.Shared.Lua;

using System;
using System.Collections.Generic;
using System.IO;

namespace Slipstream.Components.FileMonitor.Lua
{
public class FileMonitorInstanceThread : BaseInstanceThread, IFileMonitorInstanceThread
{
private readonly FileSystemWatcher[] Watchers = new FileSystemWatcher[] { };
private readonly IEventBus EventBus;
private readonly FileSystemWatcher[] Watchers = Array.Empty<FileSystemWatcher>();
private readonly IFileMonitorEventFactory EventFactory;
private readonly IEventBusSubscription Subscription;
private readonly IEventHandlerController EventHandlerController;

public FileMonitorInstanceThread(string instanceId, string[] paths, IEventBus eventBus, IFileMonitorEventFactory eventFactory, IEventBusSubscription eventBusSubscription, IEventHandlerController eventHandlerController, ILogger logger) : base(instanceId, logger, eventHandlerController)
public FileMonitorInstanceThread(
string luaLibraryName,
string instanceId,
string[] paths,
IEventBus eventBus,
IFileMonitorEventFactory eventFactory,
IEventBusSubscription eventBusSubscription,
IEventHandlerController eventHandlerController,
IInternalEventFactory internalEventFactory,
ILogger logger) : base(luaLibraryName, instanceId, logger, eventHandlerController, eventBus, internalEventFactory)
{
EventBus = eventBus;
EventFactory = eventFactory;
Subscription = eventBusSubscription;
EventHandlerController = eventHandlerController;
Expand Down Expand Up @@ -77,14 +88,16 @@ private void WatcherOnCreated(FileSystemEventArgs e)
EventBus.PublishEvent(EventFactory.CreateFileMonitorFileCreated(InstanceEnvelope, e.FullPath));
}

new public void Dispose()
public new void Dispose()
{
base.Dispose();

foreach (var w in Watchers)
{
w.Dispose();
}

GC.SuppressFinalize(this);
}

private void ScanExistingFiles(FileMonitorCommandScan e)
Expand Down
Loading

0 comments on commit 3b295be

Please sign in to comment.