Skip to content

Commit

Permalink
Merge branch 'main' into no-version-specific-init.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
dennis committed Mar 24, 2021
2 parents 5698e9a + 424ceae commit 1741ba2
Show file tree
Hide file tree
Showing 24 changed files with 1,094 additions and 84 deletions.
3 changes: 0 additions & 3 deletions App.config
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
</dependentAssembly>
</assemblyBinding>
</runtime>
<appSettings>
<add key="UpdateLocation" value="https://github.com/dennis/slipstream/releases"/>
</appSettings>
<userSettings>
<Slipstream.Properties.Settings>
<setting name="TwitchUsername" serializeAs="String">
Expand Down
47 changes: 47 additions & 0 deletions Backend/Bootstrap/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- 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"

-- Monitors for new versions of the application and raise events for it
register_plugin({plugin_name = "ApplicationUpdatePlugin", updateLocation="https://github.com/dennis/slipstream", prerelease=true})

-- Listens for samples to play or text to speek. Disabling this will mute all
-- sounds. You can add optional argument "output" to device which device to use.
-- Make a small lua script with `audio: send_devices("AudioPlugin")` and look at
-- the incoming `AudioOutputDevice` events.The device index is the value you
-- want to use for the value for "output"
register_plugin({plugin_name = "AudioPlugin"})

-- Delivers IRacing events as they happen
register_plugin({ plugin_name = "IRacingPlugin"})

--Connects to Twitch(via the values provided in Settings) and provide
--a way to sende and receive twitch messages.Generate a token here: https://twitchapps.com/tmi/
--register_plugin({ plugin_name = "TwitchPlugin", twitch_username = "<username>", twitch_token = "<token>", twitch_channel = "<channel>"})

--Connect to Discord to receive and send messages.
--You need a bot account that can be created here https://discord.com/developers/applications
--register_plugin({ plugin_name = "DiscordPlugin", token = "<token>"})

--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({ plugin_name = "TransmitterPlugin", ip = " < yourip > ", port = < yourport >})
--register_plugin({ plugin_name = "ReceiverPlugin", ip = " < yourip > ", port = < yourport >})

--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({ plugin_name = "LuaManagerPlugin"})

--FileMonitorPlugin monitors the script directory and sends out events
-- every time a file is created, renamed, modified or deleted
register_plugin({ plugin_name = "FileMonitorPlugin"})

--Provides save / replay of events. Please be careful if you use this.There is
--not much filtering, so RegisterPlugin / Unregister plugins will actually make
--slipstream perform these actions
register_plugin({ plugin_name = "PlaybackPlugin"})
59 changes: 12 additions & 47 deletions Backend/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,52 +46,7 @@ public Engine(ILogger logger, IEventFactory eventFactory, IEventBus eventBus, IP
if (!File.Exists(initFilename))
{
Logger.Information("No {initcfg} file found, creating", 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. You can add optional argument ""output"" to device which device to use.
-- Make a small lua script with `audio: send_devices(""AudioPlugin"")` and look at
-- the incoming `AudioOutputDevice` events.The device index is the value you
-- want to use for the value for ""output""
register_plugin({plugin_name = ""AudioPlugin""})
-- Delivers IRacing events as they happen
register_plugin({ plugin_name = ""IRacingPlugin""})
--Connects to Twitch(via the values provided in Settings) and provide
--a way to sende and receive twitch messages.Generate a token here: https://twitchapps.com/tmi/
--register_plugin({ plugin_name = ""TwitchPlugin"", twitch_username = ""<username>"", twitch_token = ""<token>"", twitch_channel = ""<channel>""})
--Connect to Discord to receive and send messages.
--You need a bot account that can be created here https://discord.com/developers/applications
--register_plugin({ plugin_name = ""DiscordPlugin"", token = ""<token>""})
--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({ plugin_name = ""TransmitterPlugin"", ip = "" < yourip > "", port = < yourport >})
--register_plugin({ plugin_name = ""ReceiverPlugin"", ip = "" < yourip > "", port = < yourport >})
--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({ plugin_name = ""LuaManagerPlugin""})
--FileMonitorPlugin monitors the script directory and sends out events
-- every time a file is created, renamed, modified or deleted
register_plugin({ plugin_name = ""FileMonitorPlugin""})
--Provides save / replay of events. Please be careful if you use this.There is
--not much filtering, so RegisterPlugin / Unregister plugins will actually make
--slipstream perform these actions
register_plugin({ plugin_name = ""PlaybackPlugin""})
");
CreateInitLua(initFilename);
}

Logger.Information("Loading {initcfg}", initFilename);
Expand All @@ -105,6 +60,16 @@ public Engine(ILogger logger, IEventFactory eventFactory, IEventBus eventBus, IP
EventBus.Enabled = true;
}

private void CreateInitLua(string initFilename)
{
var assembly = this.GetType().Assembly;
using var initLuaStream = assembly.GetManifestResourceStream("Slipstream.Backend.Bootstrap.init.lua");
using var sr = new StreamReader(initLuaStream);
var initLuaContent = sr.ReadToEnd();

File.WriteAllText(initFilename, initLuaContent);
}

private void OnCommandPluginStates(InternalCommandPluginStates _)
{
PluginManager.ForAllPluginsExecute(
Expand Down Expand Up @@ -134,7 +99,7 @@ private void OnCommandPluginRegister(InternalCommandPluginRegister ev)
}
catch (Exception e)
{
Logger.Error(e, $"Failed creating plugin '{ev.Id}' ('{ev.PluginName}'): {e.Message}");
Logger.Error(e, $"Failed creating plugin '{ev.Id}' ('{ev.PluginName}'): {e.ToString()}");
}
}

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Next version
[Full Changelog](https://github.com/dennis/slipstream/compare/v0.4.1...main)
- No more version specific init.lua. From this point and forward, Slipstream only reads init.lua
- Event: Adds IRacingTrackPosition

## [0.4.1](https://github.com/dennis/slipstream/releases/tag/v0.4.1) (2021-03-20)
[Full Changelog](https://github.com/dennis/slipstream/compare/v0.4.0...v0.4.1)
Expand Down
29 changes: 29 additions & 0 deletions Components/AppilcationUpdate/ApplicationUpdate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Slipstream.Components.AppilcationUpdate.Plugins;

namespace Slipstream.Components.AppilcationUpdate
{
public class ApplicationUpdate : IComponent
{
private const string NAME = nameof(ApplicationUpdatePlugin);
void IComponent.Register(IComponentRegistrationContext ctx)
{
var eventFactory = new EventFactory.ApplicationUpdateEventFactory();

ctx.RegisterEventFactory(typeof(IApplicationUpdateEventFactory), eventFactory);
ctx.RegisterEventHandler(typeof(EventHandler.ApplicationUpdateEventHandler));
ctx.RegisterPlugin(NAME, CreatePlugin);
}

private IPlugin CreatePlugin(IComponentPluginCreationContext ctx)
{
return new ApplicationUpdatePlugin(
ctx.EventHandlerController,
ctx.PluginId,
ctx.EventFactory.Get<IApplicationUpdateEventFactory>(),
ctx.Logger.ForContext(typeof(ApplicationUpdate)),
ctx.EventBus,
ctx.PluginParameters
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Slipstream.Components.AppilcationUpdate.Events;

namespace Slipstream.Components.AppilcationUpdate.EventFactory
{
public class ApplicationUpdateEventFactory : IApplicationUpdateEventFactory
{
public ApplicationUpdateCommandCheckLatestVersion CreateApplicationUpdateCommandCheckLatestVersion()
{
return new ApplicationUpdateCommandCheckLatestVersion();
}

public ApplicationUpdateLatestVersionChanged CreateApplicationUpdateLatestVersionChanged(string version)
{
return new ApplicationUpdateLatestVersionChanged
{
LatestVersion = version
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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 ApplicationUpdateEventHandler(IEventHandlerController eventHandler)
{
Parent = eventHandler;
}

public IEventHandler.HandledStatus HandleEvent(IEvent @event)
{
return @event switch
{
ApplicationUpdateLatestVersionChanged tev => OnEvent(OnApplicationUpdateLatestVersionChanged, tev),
ApplicationUpdateCommandCheckLatestVersion tev => OnEvent(OnApplicationUpdateCommandCheckLatestVersion, tev),
_ => IEventHandler.HandledStatus.NotMine,
};
}

private IEventHandler.HandledStatus OnEvent<TEvent>(EventHandler<TEvent> onEvent, TEvent args)
{
if(onEvent != null)
{
onEvent.Invoke(Parent, args);
return IEventHandler.HandledStatus.Handled;
}
return IEventHandler.HandledStatus.UseDefault;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Slipstream.Shared;
using System.Collections.Generic;

namespace Slipstream.Components.AppilcationUpdate.Events
{
public class ApplicationUpdateCommandCheckLatestVersion : IEvent
{
public string EventType => nameof(ApplicationUpdateCommandCheckLatestVersion);

public bool ExcludeFromTxrx => true;

public ulong Uptime { get; set; }

public override bool Equals(object obj)
{
return obj is ApplicationUpdateCommandCheckLatestVersion version &&
EventType == version.EventType &&
ExcludeFromTxrx == version.ExcludeFromTxrx;
}

public override int GetHashCode()
{
int hashCode = -441302714;
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(EventType);
hashCode = hashCode * -1521134295 + ExcludeFromTxrx.GetHashCode();
return hashCode;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Slipstream.Shared;
using System.Collections.Generic;

namespace Slipstream.Components.AppilcationUpdate.Events
{
public class ApplicationUpdateLatestVersionChanged : IEvent
{
public string EventType => nameof(ApplicationUpdateLatestVersionChanged);

public bool ExcludeFromTxrx => true;

public ulong Uptime { get; set; }

public string LatestVersion { get; set; }

public override bool Equals(object obj)
{
return obj is ApplicationUpdateLatestVersionChanged changed &&
EventType == changed.EventType &&
ExcludeFromTxrx == changed.ExcludeFromTxrx &&
LatestVersion == changed.LatestVersion;
}

public override int GetHashCode()
{
int hashCode = 1556716928;
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(EventType);
hashCode = hashCode * -1521134295 + ExcludeFromTxrx.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(LatestVersion);
return hashCode;
}
}
}
10 changes: 10 additions & 0 deletions Components/AppilcationUpdate/IApplicationUpdateEventFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Slipstream.Components.AppilcationUpdate.Events;

namespace Slipstream.Components.AppilcationUpdate
{
public interface IApplicationUpdateEventFactory
{
ApplicationUpdateLatestVersionChanged CreateApplicationUpdateLatestVersionChanged(string version);
ApplicationUpdateCommandCheckLatestVersion CreateApplicationUpdateCommandCheckLatestVersion();
}
}
Loading

0 comments on commit 1741ba2

Please sign in to comment.