diff --git a/App.config b/App.config index d98a7f34..24c0a4e2 100644 --- a/App.config +++ b/App.config @@ -43,6 +43,9 @@ + + False + diff --git a/Backend/Plugins/TwitchPlugin.cs b/Backend/Plugins/TwitchPlugin.cs index 1b299840..8e584b2b 100644 --- a/Backend/Plugins/TwitchPlugin.cs +++ b/Backend/Plugins/TwitchPlugin.cs @@ -23,6 +23,9 @@ class TwitchPlugin : BasePlugin private string? TwitchUsername; private string? TwitchChannel; private string? TwitchToken; + private bool TwitchLog; + private bool RequestReconnect; + private bool AnnouncedConnected = false; public TwitchPlugin(string id, IEventBus eventBus, TwitchSettings settings) : base(id, "TwitchPlugin", "TwitchPlugin", "TwitchPlugin") { @@ -43,14 +46,18 @@ public TwitchPlugin(string id, IEventBus eventBus, TwitchSettings settings) : ba private void OnTwitchSettings(TwitchSettings settings) { { - if (TwitchUsername != settings.TwitchUsername || TwitchToken != settings.TwitchToken || TwitchChannel != settings.TwitchChannel) + if (TwitchUsername != settings.TwitchUsername || TwitchToken != settings.TwitchToken || TwitchChannel != settings.TwitchChannel || TwitchLog != settings.TwitchLog) { TwitchUsername = settings.TwitchUsername; TwitchChannel = settings.TwitchChannel; TwitchToken = settings.TwitchToken; + TwitchLog = settings.TwitchLog; + + if (String.IsNullOrEmpty(TwitchChannel)) + TwitchChannel = TwitchUsername; Disconnect(); - Connnect(); + Connect(); } }; } @@ -62,69 +69,98 @@ public override void OnDisable() private void Disconnect() { + AnnounceDisconnected(); Client?.Disconnect(); Client = null; } public override void OnEnable() { - Connnect(); + Connect(); + } + + public override void Loop() + { + if(RequestReconnect) + { + Disconnect(); + Connect(); + + RequestReconnect = false; + } + + System.Threading.Thread.Sleep(500); + } + + private void AnnounceConnected() + { + if (!AnnouncedConnected) + { + EventBus.PublishEvent(new TwitchConnected()); + AnnouncedConnected = true; + } } - private void Connnect() + private void AnnounceDisconnected() + { + if (AnnouncedConnected) + { + EventBus.PublishEvent(new TwitchDisconnected()); + AnnouncedConnected = false; + } + } + + private void Connect() { if (!Enabled) return; - - if (TwitchUsername == null || TwitchToken == null || TwitchUsername.Length == 0 || TwitchToken.Length == 0) + + if (string.IsNullOrEmpty(TwitchUsername) || string.IsNullOrEmpty(TwitchToken)) return; if (Client != null && Client.IsConnected) return; - if (Client == null) + ConnectionCredentials credentials = new ConnectionCredentials(TwitchUsername, TwitchToken, "ws://irc-ws.chat.twitch.tv:80"); + var clientOptions = new ClientOptions { - ConnectionCredentials credentials = new ConnectionCredentials(TwitchUsername, TwitchToken, "ws://irc-ws.chat.twitch.tv:80"); - var clientOptions = new ClientOptions - { - MessagesAllowedInPeriod = 750, - ThrottlingPeriod = TimeSpan.FromSeconds(30) - }; - - WebSocketClient customClient = new WebSocketClient(clientOptions); - Client = new TwitchClient(customClient); - Client.Initialize(credentials, TwitchChannel); - - Client.OnConnected += OnConnected; - Client.OnChatCommandReceived += OnChatCommandReceived; - Client.OnDisconnected += OnDisconnect; - Client.OnError += Client_OnError; - Client.OnIncorrectLogin += Client_OnIncorrectLogin; - Client.OnReconnected += Client_OnReconnected; - Client.OnLog += Client_OnLog; - - Client.Connect(); - } + MessagesAllowedInPeriod = 750, + ThrottlingPeriod = TimeSpan.FromSeconds(30) + }; + + WebSocketClient customClient = new WebSocketClient(clientOptions); + Client = new TwitchClient(customClient); + Client.Initialize(credentials, TwitchChannel); + + Client.OnConnected += OnConnected; + Client.OnChatCommandReceived += OnChatCommandReceived; + Client.OnDisconnected += OnDisconnect; + Client.OnError += OnError; + Client.OnIncorrectLogin += OnIncorrectLogin; + Client.OnJoinedChannel += OnJoinedChannel; + if(TwitchLog) + Client.OnLog += OnLog; + + Client.Connect(); } - private void Client_OnLog(object sender, OnLogArgs e) + private void OnJoinedChannel(object sender, OnJoinedChannelArgs e) { - EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch: {e.Data}" }); + AnnounceConnected(); } - private void Client_OnReconnected(object sender, OnReconnectedEventArgs e) + private void OnLog(object sender, OnLogArgs e) { - EventBus.PublishEvent(new TwitchConnected()); - EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch connected as {TwitchUsername} to channel {TwitchChannel}" }); + EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch log: {e.Data}" }); } - private void Client_OnIncorrectLogin(object sender, OnIncorrectLoginArgs e) + private void OnIncorrectLogin(object sender, OnIncorrectLoginArgs e) { EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch Error: {e.Exception.Message}" }); EventBus.PublishEvent(new Shared.Events.Internal.CommandPluginDisable() { Id = this.Id }); } - private void Client_OnError(object sender, OnErrorEventArgs e) + private void OnError(object sender, OnErrorEventArgs e) { EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch Error: {e.Exception.Message}" }); EventBus.PublishEvent(new Shared.Events.Internal.CommandPluginDisable() { Id = this.Id }); @@ -132,9 +168,8 @@ private void Client_OnError(object sender, OnErrorEventArgs e) private void OnDisconnect(object sender, OnDisconnectedEventArgs e) { - EventBus.PublishEvent(new TwitchDisconnected()); - if (Enabled) - Client?.Reconnect(); + AnnounceDisconnected(); + RequestReconnect = true; } private void OnChatCommandReceived(object sender, OnChatCommandReceivedArgs e) @@ -158,8 +193,7 @@ private void OnChatCommandReceived(object sender, OnChatCommandReceivedArgs e) private void OnConnected(object sender, OnConnectedArgs e) { - EventBus.PublishEvent(new TwitchConnected()); - EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch connected as {TwitchUsername}" }); + EventBus.PublishEvent(new CommandWriteToConsole { Message = $"Twitch connected as {TwitchUsername} to channel {TwitchChannel}" }); } } } diff --git a/CHANGELOG.md b/CHANGELOG.md index 67b9e84a..3aeed885 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ - Replace DebugOutputPlugin with a Lua script (LuaScript/debug.lua) - Twitch: Allow Twitchchannel to be configured (it was assumed that username and channel would be the same) - Added TransmitterPlugin and ReceiverPlugin. Allows one instance of Slipstream to forward its events to another instance using a TCP/IP connection. This features SHOULD ONLY be used locally. There is no authentication or encryption of the events. + - Twitch: Optionally show twitch log + - TwitchConnected event are now sent when we joined the channel (not just connected to twitch servers) **Bugfixes** - Auto-create both "Audio" and "Scripts" directories @@ -14,4 +16,4 @@ ## [0.1.0](https://github.com/dennis/slipstream/releases/tag/v0.1.0) (2020-12-23) [Full Changelog](https://github.com/dennis/slipstream/compare/be57351b1d0c5ff75a87ece10b3e7c272a980446...v0.1.0) -**Initial version** \ No newline at end of file +**Initial version** diff --git a/Frontend/ApplicationConfiguration.cs b/Frontend/ApplicationConfiguration.cs index fbd46abe..087f547e 100644 --- a/Frontend/ApplicationConfiguration.cs +++ b/Frontend/ApplicationConfiguration.cs @@ -41,7 +41,8 @@ public TwitchSettings GetTwitchSettingsEvent() { TwitchUsername = Settings.TwitchUsername, TwitchChannel = Settings.TwitchChannel, - TwitchToken = Settings.TwitchToken + TwitchToken = Settings.TwitchToken, + TwitchLog = Settings.TwitchLog, }; } diff --git a/Frontend/SettingsForm.Designer.cs b/Frontend/SettingsForm.Designer.cs index a736846f..c1ddd562 100644 --- a/Frontend/SettingsForm.Designer.cs +++ b/Frontend/SettingsForm.Designer.cs @@ -30,11 +30,11 @@ private void InitializeComponent() { this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel(); this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.label3 = new System.Windows.Forms.Label(); - this.DiscardButton = new System.Windows.Forms.Button(); - this.ApplyButton = new System.Windows.Forms.Button(); + this.TwitchLogCheckBox = new System.Windows.Forms.CheckBox(); + this.TwitchTokenTextBox = new System.Windows.Forms.TextBox(); this.TwitchTokenGeneratorLabel = new System.Windows.Forms.LinkLabel(); this.TwitchChannelTextBox = new System.Windows.Forms.TextBox(); + this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.TwitchUsernameTextBox = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); @@ -43,7 +43,8 @@ private void InitializeComponent() this.label4 = new System.Windows.Forms.Label(); this.TXRXHostPortTextBox = new System.Windows.Forms.TextBox(); this.groupBox3 = new System.Windows.Forms.GroupBox(); - this.TwitchTokenTextBox = new System.Windows.Forms.TextBox(); + this.DiscardButton = new System.Windows.Forms.Button(); + this.ApplyButton = new System.Windows.Forms.Button(); this.flowLayoutPanel1.SuspendLayout(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); @@ -58,11 +59,12 @@ private void InitializeComponent() this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0); this.flowLayoutPanel1.Name = "flowLayoutPanel1"; - this.flowLayoutPanel1.Size = new System.Drawing.Size(677, 251); + this.flowLayoutPanel1.Size = new System.Drawing.Size(677, 266); this.flowLayoutPanel1.TabIndex = 0; // // groupBox1 // + this.groupBox1.Controls.Add(this.TwitchLogCheckBox); this.groupBox1.Controls.Add(this.TwitchTokenTextBox); this.groupBox1.Controls.Add(this.TwitchTokenGeneratorLabel); this.groupBox1.Controls.Add(this.TwitchChannelTextBox); @@ -72,39 +74,27 @@ private void InitializeComponent() this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location = new System.Drawing.Point(3, 3); this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(667, 106); + this.groupBox1.Size = new System.Drawing.Size(667, 123); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "Twitch"; // - // label3 - // - this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(6, 80); - this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(38, 13); - this.label3.TabIndex = 7; - this.label3.Text = "Token"; - // - // DiscardButton + // TwitchLogCheckBox // - this.DiscardButton.Location = new System.Drawing.Point(0, 18); - this.DiscardButton.Name = "DiscardButton"; - this.DiscardButton.Size = new System.Drawing.Size(336, 23); - this.DiscardButton.TabIndex = 6; - this.DiscardButton.Text = "Cancel"; - this.DiscardButton.UseVisualStyleBackColor = true; - this.DiscardButton.Click += new System.EventHandler(this.DiscardButton_Click); + this.TwitchLogCheckBox.AutoSize = true; + this.TwitchLogCheckBox.Location = new System.Drawing.Point(105, 101); + this.TwitchLogCheckBox.Name = "TwitchLogCheckBox"; + this.TwitchLogCheckBox.Size = new System.Drawing.Size(105, 17); + this.TwitchLogCheckBox.TabIndex = 10; + this.TwitchLogCheckBox.Text = "Show Twitch log"; + this.TwitchLogCheckBox.UseVisualStyleBackColor = true; // - // ApplyButton + // TwitchTokenTextBox // - this.ApplyButton.Location = new System.Drawing.Point(348, 19); - this.ApplyButton.Name = "ApplyButton"; - this.ApplyButton.Size = new System.Drawing.Size(313, 23); - this.ApplyButton.TabIndex = 5; - this.ApplyButton.Text = "Apply"; - this.ApplyButton.UseVisualStyleBackColor = true; - this.ApplyButton.Click += new System.EventHandler(this.ApplyButton_Click); + this.TwitchTokenTextBox.Location = new System.Drawing.Point(105, 77); + this.TwitchTokenTextBox.Name = "TwitchTokenTextBox"; + this.TwitchTokenTextBox.Size = new System.Drawing.Size(211, 20); + this.TwitchTokenTextBox.TabIndex = 8; // // TwitchTokenGeneratorLabel // @@ -124,6 +114,15 @@ private void InitializeComponent() this.TwitchChannelTextBox.Size = new System.Drawing.Size(211, 20); this.TwitchChannelTextBox.TabIndex = 2; // + // label3 + // + this.label3.AutoSize = true; + this.label3.Location = new System.Drawing.Point(6, 77); + this.label3.Name = "label3"; + this.label3.Size = new System.Drawing.Size(73, 13); + this.label3.TabIndex = 7; + this.label3.Text = "Twitch Token"; + // // label2 // this.label2.AutoSize = true; @@ -154,7 +153,7 @@ private void InitializeComponent() this.groupBox2.Controls.Add(this.label5); this.groupBox2.Controls.Add(this.label4); this.groupBox2.Controls.Add(this.TXRXHostPortTextBox); - this.groupBox2.Location = new System.Drawing.Point(3, 115); + this.groupBox2.Location = new System.Drawing.Point(3, 132); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(667, 82); this.groupBox2.TabIndex = 1; @@ -190,24 +189,37 @@ private void InitializeComponent() // this.groupBox3.Controls.Add(this.DiscardButton); this.groupBox3.Controls.Add(this.ApplyButton); - this.groupBox3.Location = new System.Drawing.Point(3, 203); + this.groupBox3.Location = new System.Drawing.Point(3, 220); this.groupBox3.Name = "groupBox3"; this.groupBox3.Size = new System.Drawing.Size(667, 41); this.groupBox3.TabIndex = 7; this.groupBox3.TabStop = false; // - // TwitchTokenTextBox + // DiscardButton // - this.TwitchTokenTextBox.Location = new System.Drawing.Point(105, 77); - this.TwitchTokenTextBox.Name = "TwitchTokenTextBox"; - this.TwitchTokenTextBox.Size = new System.Drawing.Size(211, 20); - this.TwitchTokenTextBox.TabIndex = 8; + this.DiscardButton.Location = new System.Drawing.Point(4, 11); + this.DiscardButton.Name = "DiscardButton"; + this.DiscardButton.Size = new System.Drawing.Size(338, 23); + this.DiscardButton.TabIndex = 5; + this.DiscardButton.Text = "Cancel"; + this.DiscardButton.UseVisualStyleBackColor = true; + this.DiscardButton.Click += new System.EventHandler(this.DiscardButton_Click); + // + // ApplyButton + // + this.ApplyButton.Location = new System.Drawing.Point(348, 11); + this.ApplyButton.Name = "ApplyButton"; + this.ApplyButton.Size = new System.Drawing.Size(313, 23); + this.ApplyButton.TabIndex = 5; + this.ApplyButton.Text = "Apply"; + this.ApplyButton.UseVisualStyleBackColor = true; + this.ApplyButton.Click += new System.EventHandler(this.ApplyButton_Click); // // SettingsForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(677, 251); + this.ClientSize = new System.Drawing.Size(677, 266); this.Controls.Add(this.flowLayoutPanel1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.Name = "SettingsForm"; @@ -240,5 +252,6 @@ private void InitializeComponent() private System.Windows.Forms.GroupBox groupBox3; private System.Windows.Forms.Label label5; private System.Windows.Forms.TextBox TwitchTokenTextBox; + private System.Windows.Forms.CheckBox TwitchLogCheckBox; } } \ No newline at end of file diff --git a/Frontend/SettingsForm.cs b/Frontend/SettingsForm.cs index bbe12cb9..fc4fa035 100644 --- a/Frontend/SettingsForm.cs +++ b/Frontend/SettingsForm.cs @@ -9,10 +9,14 @@ public SettingsForm() { InitializeComponent(); - TwitchUsernameTextBox.Text = Properties.Settings.Default.TwitchUsername; - TwitchChannelTextBox.Text = Properties.Settings.Default.TwitchChannel; - TwitchTokenTextBox.Text = Properties.Settings.Default.TwitchToken; - TXRXHostPortTextBox.Text = Properties.Settings.Default.TxrxIpPort; + var settings = Properties.Settings.Default; + + TwitchUsernameTextBox.Text = settings.TwitchUsername; + TwitchChannelTextBox.Text = settings.TwitchChannel; + TwitchTokenTextBox.Text = settings.TwitchToken; + TwitchLogCheckBox.Checked = settings.TwitchLog; + + TXRXHostPortTextBox.Text = settings.TxrxIpPort; } private void DiscardButton_Click(object sender, EventArgs e) @@ -26,8 +30,10 @@ private void ApplyButton_Click(object sender, EventArgs e) settings.TwitchChannel = TwitchChannelTextBox.Text; settings.TwitchUsername = TwitchUsernameTextBox.Text; - settings.TxrxIpPort = TXRXHostPortTextBox.Text; settings.TwitchToken = TwitchTokenTextBox.Text; + settings.TwitchLog = TwitchLogCheckBox.Checked; + settings.TxrxIpPort = TXRXHostPortTextBox.Text; + settings.Save(); Close(); diff --git a/Properties/Settings.Designer.cs b/Properties/Settings.Designer.cs index 2f53d238..d3a9e993 100644 --- a/Properties/Settings.Designer.cs +++ b/Properties/Settings.Designer.cs @@ -70,5 +70,17 @@ public string TwitchChannel { this["TwitchChannel"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("False")] + public bool TwitchLog { + get { + return ((bool)(this["TwitchLog"])); + } + set { + this["TwitchLog"] = value; + } + } } } diff --git a/Properties/Settings.settings b/Properties/Settings.settings index 00097cd4..7debe11a 100644 --- a/Properties/Settings.settings +++ b/Properties/Settings.settings @@ -11,6 +11,9 @@ + + False + diff --git a/Shared/Events/Setting/TwitchSettings.cs b/Shared/Events/Setting/TwitchSettings.cs index 04c954a8..358732a3 100644 --- a/Shared/Events/Setting/TwitchSettings.cs +++ b/Shared/Events/Setting/TwitchSettings.cs @@ -7,5 +7,6 @@ public class TwitchSettings : IEvent public string TwitchUsername { get; set; } = string.Empty; public string TwitchChannel { get; set; } = string.Empty; public string TwitchToken { get; set; } = string.Empty; + public bool TwitchLog { get; set; } } } diff --git a/Slipstream.csproj b/Slipstream.csproj index 38e30be1..a33dbaa7 100644 --- a/Slipstream.csproj +++ b/Slipstream.csproj @@ -221,6 +221,7 @@ True Resources.resx +