From 3ada6d3689a00d9ad428147539b5efaeef1f4cc7 Mon Sep 17 00:00:00 2001 From: EnderIce2 Date: Sun, 25 Oct 2020 18:15:11 +0200 Subject: [PATCH] Small bug fix and added option to disable the invite feature --- .editorconfig | 4 ++ LogWriter.cs | 16 ++---- MainPlugin.cs | 84 ++++++++++++++++++-------------- SDRSharpPlugin.DiscordRPC.csproj | 1 + SDRSharpPlugin.DiscordRPC.sln | 5 ++ SettingsPanel.Designer.cs | 32 +++++++++--- SettingsPanel.cs | 33 +++++++------ TopWindowMessages.cs | 11 +---- WelcomeForm.Designer.cs | 6 +-- WelcomeForm.cs | 13 ++--- 10 files changed, 113 insertions(+), 92 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..bc332e4 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CA1031: Do not catch general exception types +dotnet_diagnostic.CA1031.severity = none diff --git a/LogWriter.cs b/LogWriter.cs index d47f007..6a5f8cf 100644 --- a/LogWriter.cs +++ b/LogWriter.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace EnderIce2.SDRSharpPlugin { @@ -21,17 +17,13 @@ namespace EnderIce2.SDRSharpPlugin string filepath = AppDomain.CurrentDomain.BaseDirectory + "\\RPCLogs\\DiscordRPCLog_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_') + ".log"; if (!File.Exists(filepath)) { - using (StreamWriter sw = File.CreateText(filepath)) - { - sw.WriteLine($"[{DateTime.Now}] {Message}"); - } + using StreamWriter sw = File.CreateText(filepath); + sw.WriteLine($"[{DateTime.Now}] {Message}"); } else { - using (StreamWriter sw = File.AppendText(filepath)) - { - sw.WriteLine($"[{DateTime.Now}] {Message}"); - } + using StreamWriter sw = File.AppendText(filepath); + sw.WriteLine($"[{DateTime.Now}] {Message}"); } } } diff --git a/MainPlugin.cs b/MainPlugin.cs index 4222d95..ecbfed2 100644 --- a/MainPlugin.cs +++ b/MainPlugin.cs @@ -1,18 +1,12 @@ -using System; -using System.Windows.Forms; -using System.ComponentModel; +using DiscordRPC; +using DiscordRPC.Logging; +using DiscordRPC.Message; using SDRSharp.Common; using SDRSharp.Radio; -using SDRSharp.PanView; -using DiscordRPC.Logging; -using DiscordRPC; -using System.Text; -using System.Threading.Tasks; -using DiscordRPC.Message; -using DiscordRPC.IO; -using System.IO; +using System; using System.Reflection; -using System.Runtime.InteropServices.ComTypes; +using System.Threading.Tasks; +using System.Windows.Forms; namespace EnderIce2.SDRSharpPlugin { @@ -20,14 +14,18 @@ namespace EnderIce2.SDRSharpPlugin { private const string _displayName = "Discord RPC"; private SettingsPanel _controlPanel; + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "")] private static LogLevel logLevel = LogLevel.Trace; + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "")] private static int discordPipe = -1; + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "")] bool RPCalreadyLoaded = false; private ISharpControl _control; bool playedBefore = false; private IConfigurationPanelProvider configurationPanelProvider; private SDRSharp.FrontEnds.SpyServer.ControllerPanel controllerPanel; public TopWindowMessages windowMessages; + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "")] private RichPresence presence = new RichPresence() { Details = "Loading...", @@ -38,16 +36,6 @@ namespace EnderIce2.SDRSharpPlugin LargeImageText = "SDRSharp", SmallImageKey = "image_small", SmallImageText = $"SDR# RPC plugin v{Assembly.GetEntryAssembly().GetName().Version} by EnderIce2" - }, - Secrets = new Secrets() - { - JoinSecret = "invalid_secret" - }, - Party = new Party() - { - ID = Secrets.CreateFriendlySecret(new Random()), - Size = 1, - Max = 100 } }; private static DiscordRpcClient client; @@ -69,17 +57,32 @@ namespace EnderIce2.SDRSharpPlugin if (Utils.GetBooleanSetting("ShowWelcomePage", true)) new WelcomeForm().ShowDialog(); _controlPanel = new SettingsPanel(); - windowMessages = new TopWindowMessages(); + windowMessages = new TopWindowMessages(); // TODO: do something when "EnableRPCInvite" is set to false _control = control; try { - _control.RegisterFrontControl(windowMessages, PluginPosition.Top); + if (Utils.GetBooleanSetting("EnableRPCInvite", false)) + _control.RegisterFrontControl(windowMessages, PluginPosition.Top); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } - windowMessages.Show(); + if (Utils.GetBooleanSetting("EnableRPCInvite", false)) + { + presence.Secrets = new Secrets() + { + JoinSecret = "invalid_secret" + }; + presence.Party = new Party() + { + ID = Secrets.CreateFriendlySecret(new Random()), + Size = 1, + Max = 100 + }; + } + if (Utils.GetBooleanSetting("EnableRPCInvite", false)) + windowMessages.Show(); if (Utils.GetBooleanSetting("EnableRPC", true)) { if (RPCalreadyLoaded) @@ -114,7 +117,8 @@ namespace EnderIce2.SDRSharpPlugin { Start = DateTime.UtcNow }; - client.SetSubscription(EventType.Join | EventType.JoinRequest); + if (Utils.GetBooleanSetting("EnableRPCInvite", false)) + client.SetSubscription(EventType.Join | EventType.JoinRequest); client.SetPresence(presence); client.Initialize(); try @@ -210,16 +214,19 @@ namespace EnderIce2.SDRSharpPlugin LogWriter.WriteToFile($"MainLoop called {isRunning} {client.IsInitialized}"); while (client != null && isRunning) { - LogWriter.WriteToFile("Setting secret..."); - try + if (Utils.GetBooleanSetting("EnableRPCInvite", false)) { - string sdr_url = "sdr://" + controllerPanel.Host + ":" + controllerPanel.Port + "/"; - LogWriter.WriteToFile(sdr_url); - presence.Secrets.JoinSecret = sdr_url; - } - catch (Exception ex) - { - LogWriter.WriteToFile(ex.ToString()); + LogWriter.WriteToFile("Setting secret..."); + try + { + string sdr_url = "sdr://" + controllerPanel.Host + ":" + controllerPanel.Port + "/"; + LogWriter.WriteToFile(sdr_url); + presence.Secrets.JoinSecret = sdr_url; + } + catch (Exception ex) + { + LogWriter.WriteToFile(ex.ToString()); + } } LogWriter.WriteToFile("Waiting 500ms in loop..."); await Task.Delay(500); @@ -262,7 +269,10 @@ namespace EnderIce2.SDRSharpPlugin { client.SetPresence(presence); } - catch (ObjectDisposedException) {; } + catch (ObjectDisposedException ex) + { + LogWriter.WriteToFile($"ObjectDisposedException exception for SetPresence\n{ex}"); + } LogWriter.WriteToFile("SetPresence"); _controlPanel.ChangeStatus = $"Presence Updated {DateTime.UtcNow}"; } @@ -295,4 +305,4 @@ namespace EnderIce2.SDRSharpPlugin client.Dispose(); } } -} +} \ No newline at end of file diff --git a/SDRSharpPlugin.DiscordRPC.csproj b/SDRSharpPlugin.DiscordRPC.csproj index cc651a0..64cbf17 100644 --- a/SDRSharpPlugin.DiscordRPC.csproj +++ b/SDRSharpPlugin.DiscordRPC.csproj @@ -167,6 +167,7 @@ + diff --git a/SDRSharpPlugin.DiscordRPC.sln b/SDRSharpPlugin.DiscordRPC.sln index c920b94..941df0c 100644 --- a/SDRSharpPlugin.DiscordRPC.sln +++ b/SDRSharpPlugin.DiscordRPC.sln @@ -5,6 +5,11 @@ VisualStudioVersion = 16.0.30517.126 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SDRSharpPlugin.DiscordRPC", "SDRSharpPlugin.DiscordRPC.csproj", "{72E8628F-BA39-4915-BF3C-DD48BF477D30}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{446ABA84-7848-4219-AE53-4EE2132D0710}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/SettingsPanel.Designer.cs b/SettingsPanel.Designer.cs index 97cfd94..bc741a9 100644 --- a/SettingsPanel.Designer.cs +++ b/SettingsPanel.Designer.cs @@ -34,10 +34,13 @@ this.label2 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.checkBox2 = new System.Windows.Forms.CheckBox(); + this.checkBox3 = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // checkBox1 // + this.checkBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); this.checkBox1.Checked = true; this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked; this.checkBox1.Location = new System.Drawing.Point(3, 3); @@ -55,9 +58,9 @@ | System.Windows.Forms.AnchorStyles.Right))); this.label1.AutoEllipsis = true; this.label1.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.label1.Location = new System.Drawing.Point(0, 49); + this.label1.Location = new System.Drawing.Point(0, 67); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(196, 88); + this.label1.Size = new System.Drawing.Size(196, 70); this.label1.TabIndex = 1; this.label1.Text = "Loading status..."; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; @@ -71,8 +74,8 @@ this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(134, 13); this.textBox1.TabIndex = 2; - this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown); - this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress); + this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBox1_KeyDown); + this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.TextBox1_KeyPress); // // label2 // @@ -93,10 +96,12 @@ this.button1.TabIndex = 4; this.button1.Text = "Help"; this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.button1_Click); + this.button1.Click += new System.EventHandler(this.Button1_Click); // // checkBox2 // + this.checkBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); this.checkBox2.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.25F); this.checkBox2.Location = new System.Drawing.Point(3, 25); this.checkBox2.Name = "checkBox2"; @@ -104,12 +109,26 @@ this.checkBox2.TabIndex = 5; this.checkBox2.Text = "Log RPC (for debugging)"; this.checkBox2.UseVisualStyleBackColor = true; - this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged); + this.checkBox2.CheckedChanged += new System.EventHandler(this.CheckBox2_CheckedChanged); + // + // checkBox3 + // + this.checkBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.checkBox3.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.25F); + this.checkBox3.Location = new System.Drawing.Point(3, 43); + this.checkBox3.Name = "checkBox3"; + this.checkBox3.Size = new System.Drawing.Size(193, 21); + this.checkBox3.TabIndex = 6; + this.checkBox3.Text = "Enable Invite Feature (not tested)"; + this.checkBox3.UseVisualStyleBackColor = true; + this.checkBox3.CheckedChanged += new System.EventHandler(this.CheckBox3_CheckedChanged); // // SettingsPanel // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.checkBox3); this.Controls.Add(this.button1); this.Controls.Add(this.checkBox2); this.Controls.Add(this.label2); @@ -131,5 +150,6 @@ private System.Windows.Forms.Label label2; private System.Windows.Forms.Button button1; private System.Windows.Forms.CheckBox checkBox2; + private System.Windows.Forms.CheckBox checkBox3; } } diff --git a/SettingsPanel.cs b/SettingsPanel.cs index 4b600ce..299d76e 100644 --- a/SettingsPanel.cs +++ b/SettingsPanel.cs @@ -1,15 +1,7 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Data; -using System.Linq; -using System.Text; +using SDRSharp.Radio; +using System; using System.Threading.Tasks; using System.Windows.Forms; -using SDRSharp.Radio; -using System.Security.Policy; -using System.Media; namespace EnderIce2.SDRSharpPlugin { @@ -41,6 +33,10 @@ namespace EnderIce2.SDRSharpPlugin checkBox2.Checked = true; else checkBox2.Checked = false; + if (Utils.GetBooleanSetting("EnableRPCInvite", false)) + checkBox3.Checked = true; + else + checkBox3.Checked = false; LogWriter.WriteToFile("SettingsPanel loaded"); } @@ -52,17 +48,17 @@ namespace EnderIce2.SDRSharpPlugin //Utils.GetBooleanSetting("EnableRPC"); } - private void button1_Click(object sender, EventArgs e) + private void Button1_Click(object sender, EventArgs e) { - System.Diagnostics.Process.Start("https://github.com/EnderIce2/SDRSharpRPC"); + System.Diagnostics.Process.Start("https://github.com/EnderIce2/SDR-RPC"); } - private void checkBox2_CheckedChanged(object sender, EventArgs e) + private void CheckBox2_CheckedChanged(object sender, EventArgs e) { Utils.SaveSetting("LogRPC", checkBox2.Checked); } - private void textBox1_KeyPress(object sender, KeyPressEventArgs e) + private void TextBox1_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.')) e.Handled = true; @@ -70,7 +66,7 @@ namespace EnderIce2.SDRSharpPlugin e.Handled = true; } - private async void textBox1_KeyDown(object sender, KeyEventArgs e) + private async void TextBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter && textBox1.Text.Replace(" ", "").Length == 18) { @@ -82,5 +78,12 @@ namespace EnderIce2.SDRSharpPlugin label1.Text = "Saved."; } } + + private void CheckBox3_CheckedChanged(object sender, EventArgs e) + { + Utils.SaveSetting("EnableRPCInvite", checkBox3.Checked); + label1.Text = "Restart required"; + LogWriter.WriteToFile($"checkbox on SettingsPanel clicked {checkBox3.Checked}"); + } } } diff --git a/TopWindowMessages.cs b/TopWindowMessages.cs index e89852c..3bb662b 100644 --- a/TopWindowMessages.cs +++ b/TopWindowMessages.cs @@ -1,14 +1,7 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Drawing; -using System.Data; -using System.Linq; -using System.Text; +using DiscordRPC; +using DiscordRPC.Message; using System.Threading.Tasks; using System.Windows.Forms; -using DiscordRPC.Message; -using DiscordRPC; namespace EnderIce2.SDRSharpPlugin { diff --git a/WelcomeForm.Designer.cs b/WelcomeForm.Designer.cs index 3d697da..3694459 100644 --- a/WelcomeForm.Designer.cs +++ b/WelcomeForm.Designer.cs @@ -55,7 +55,7 @@ this.button1.TabIndex = 1; this.button1.Text = "&OK"; this.button1.UseVisualStyleBackColor = true; - this.button1.Click += new System.EventHandler(this.button1_Click); + this.button1.Click += new System.EventHandler(this.Button1_Click); // // checkBox1 // @@ -66,7 +66,7 @@ this.checkBox1.TabIndex = 2; this.checkBox1.Text = "Don\'t show me this next time"; this.checkBox1.UseVisualStyleBackColor = true; - this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged); + this.checkBox1.CheckedChanged += new System.EventHandler(this.CheckBox1_CheckedChanged); // // label1 // @@ -95,7 +95,7 @@ this.button2.TabIndex = 5; this.button2.Text = "Support Me"; this.button2.UseVisualStyleBackColor = true; - this.button2.Click += new System.EventHandler(this.button2_Click); + this.button2.Click += new System.EventHandler(this.Button2_Click); // // WelcomeForm // diff --git a/WelcomeForm.cs b/WelcomeForm.cs index 9bbb7f8..6f88d14 100644 --- a/WelcomeForm.cs +++ b/WelcomeForm.cs @@ -1,12 +1,5 @@ using SDRSharp.Radio; using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; namespace EnderIce2.SDRSharpPlugin @@ -18,17 +11,17 @@ namespace EnderIce2.SDRSharpPlugin InitializeComponent(); } - private void button2_Click(object sender, EventArgs e) + private void Button2_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start("https://ko-fi.com/enderice2"); } - private void button1_Click(object sender, EventArgs e) + private void Button1_Click(object sender, EventArgs e) { Close(); } - private void checkBox1_CheckedChanged(object sender, EventArgs e) + private void CheckBox1_CheckedChanged(object sender, EventArgs e) { Utils.SaveSetting("ShowWelcomePage", !checkBox1.Checked); }