Small code improvement

This commit is contained in:
EnderIce2 2021-05-21 20:17:35 +03:00
parent da9b48cf7c
commit f4845c49e0
6 changed files with 166 additions and 171 deletions

View File

@ -12,22 +12,17 @@ namespace EnderIce2.SDRSharpPlugin
{
public class MainPlugin : ISharpPlugin
{
private const string _displayName = "Discord RPC";
private SettingsPanel _controlPanel;
private const LogLevel logLevel = LogLevel.Trace;
private const int discordPipe = -1;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "<Pending>")]
private bool RPCalreadyLoaded;
private ISharpControl _control;
private bool playedBefore;
//private SDRSharp.FrontEnds.SpyServer.ControllerPanel controllerPanel;
private TopWindowMessages windowMessages;
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE0044:Add readonly modifier", Justification = "<Pending>")]
private RichPresence presence = new RichPresence()
private readonly RichPresence presence = new RichPresence()
{
Details = "Loading...",
State = "Loading...",
@ -42,13 +37,12 @@ namespace EnderIce2.SDRSharpPlugin
private DiscordRpcClient client;
private bool isRunning = true;
public string DisplayName => _displayName;
public bool HasGui => true;
public string DisplayName => "Discord RPC";
public UserControl Gui => _controlPanel;
public void Initialize(ISharpControl control)
{
IConfigurationPanelProvider configurationPanelProvider;
if (Utils.GetBooleanSetting("ShowWelcomePage", true))
{
new WelcomeForm().ShowDialog();
@ -62,6 +56,18 @@ namespace EnderIce2.SDRSharpPlugin
if (Utils.GetBooleanSetting("EnableRPCInvite", false))
{
_control.RegisterFrontControl(windowMessages, PluginPosition.Top);
presence.Secrets = new Secrets()
{
JoinSecret = "invalid_secret"
};
presence.Party = new Party()
{
ID = Secrets.CreateFriendlySecret(new Random()),
Size = 1,
Max = 100
};
windowMessages.Show();
client.SetSubscription(EventType.Join | EventType.JoinRequest);
}
}
catch (Exception ex)
@ -69,46 +75,16 @@ namespace EnderIce2.SDRSharpPlugin
MessageBox.Show(ex.ToString());
}
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)
{
_controlPanel.ChangeStatus = "Restart required";
return;
}
if (Utils.GetStringSetting("ClientID").Replace(" ", "").Length != 18)
{
client = new DiscordRpcClient("765213507321856078", pipe: discordPipe)
{
Logger = new ConsoleLogger(logLevel, true)
};
Utils.SaveSetting("ClientID", "765213507321856078");
}
else
client = new DiscordRpcClient(Utils.GetStringSetting("ClientID"), pipe: discordPipe)
{
client = new DiscordRpcClient(Utils.GetStringSetting("ClientID"), pipe: discordPipe)
{
Logger = new ConsoleLogger(logLevel, true)
};
}
Logger = new ConsoleLogger(logLevel, true)
};
client.RegisterUriScheme();
client.OnRpcMessage += Client_OnRpcMessage;
@ -127,16 +103,11 @@ namespace EnderIce2.SDRSharpPlugin
Start = DateTime.UtcNow
};
if (Utils.GetBooleanSetting("EnableRPCInvite", false))
{
client.SetSubscription(EventType.Join | EventType.JoinRequest);
}
client.SetPresence(presence);
client.Initialize();
try
{
configurationPanelProvider = (IConfigurationPanelProvider)_control.Source;
IConfigurationPanelProvider configurationPanelProvider = (IConfigurationPanelProvider)_control.Source;
//controllerPanel = (SDRSharp.FrontEnds.SpyServer.ControllerPanel)configurationPanelProvider.Gui;
}
catch (Exception ex)
@ -153,85 +124,6 @@ namespace EnderIce2.SDRSharpPlugin
LogWriter.WriteToFile("EOM Initialize");
}
private void Client_OnPresenceUpdate(object sender, PresenceMessage args)
{
LogWriter.WriteToFile($"[RpcMessage] | Presence state: {args.Presence.State}");
}
private void Client_OnRpcMessage(object sender, IMessage msg)
{
LogWriter.WriteToFile($"[RpcMessage] | {msg.Type} | {msg}");
}
private void OnConnectionFailed(object sender, ConnectionFailedMessage args)
{
_controlPanel.ChangeStatus = $"RPC Connection Failed!\n{args.Type} | {args.FailedPipe}";
}
private void OnConnectionEstablished(object sender, ConnectionEstablishedMessage args)
{
_controlPanel.ChangeStatus = "RPC Connection Established!";
}
private void OnError(object sender, ErrorMessage args)
{
_controlPanel.ChangeStatus = $"RPC Error:\n{args.Message}";
windowMessages.ChangeLabel = "SDR# RPC | Internal error";
}
private void OnClose(object sender, CloseMessage args)
{
_controlPanel.ChangeStatus = "RPC Closed";
windowMessages.ChangeLabel = "SDR# RPC | Closed";
Close();
}
private void OnReady(object sender, ReadyMessage args)
{
_controlPanel.ChangeStatus = "RPC Ready";
windowMessages.ChangeLabel = "SDR# RPC | Ready";
}
private void OnSubscribe(object sender, SubscribeMessage args)
{
_controlPanel.ChangeStatus = $"Subscribed: {args.Event}";
}
private void OnUnsubscribe(object sender, UnsubscribeMessage args)
{
_controlPanel.ChangeStatus = $"Unsubscribed: {args.Event}";
}
private void OnJoin(object sender, JoinMessage args)
{
presence.Party.Size++;
presence.Secrets.JoinSecret = args.Secret;
MessageBox.Show("OnJoin: " + args.Secret);
_control.StopRadio();
_control.RefreshSource(true);
Utils.SaveSetting("spyserver.uri", args.Secret);
_control.StartRadio();
}
private async void OnJoinRequested(object sender, JoinRequestMessage args)
{
try
{
if (await windowMessages.RequestAnswer(client, args))
{
MessageBox.Show("Accepted RequestAnswer");
}
else
{
MessageBox.Show("Declined RequestAnswer");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private async Task MainLoop()
{
try
@ -293,6 +185,10 @@ namespace EnderIce2.SDRSharpPlugin
{
presence.State = $"RDS: unknown{space_for_listen_list}";
}
else if (_control.FmStereo)
{
presence.State = $"RDS: ((( {_control.RdsProgramService} ))) - {_control.RdsRadioText}{space_for_listen_list}";
}
else
{
presence.State = $"RDS: {_control.RdsProgramService} - {_control.RdsRadioText}{space_for_listen_list}";
@ -349,5 +245,66 @@ namespace EnderIce2.SDRSharpPlugin
isRunning = false;
client.Dispose();
}
private void Client_OnPresenceUpdate(object sender, PresenceMessage args) => LogWriter.WriteToFile($"[RpcMessage] | Presence state: {args.Presence.State}");
private void Client_OnRpcMessage(object sender, IMessage msg) => LogWriter.WriteToFile($"[RpcMessage] | {msg.Type} | {msg}");
private void OnConnectionFailed(object sender, ConnectionFailedMessage args) => _controlPanel.ChangeStatus = $"RPC Connection Failed!\n{args.Type} | {args.FailedPipe}";
private void OnConnectionEstablished(object sender, ConnectionEstablishedMessage args) => _controlPanel.ChangeStatus = "RPC Connection Established!";
private void OnSubscribe(object sender, SubscribeMessage args) => _controlPanel.ChangeStatus = $"Subscribed: {args.Event}";
private void OnUnsubscribe(object sender, UnsubscribeMessage args) => _controlPanel.ChangeStatus = $"Unsubscribed: {args.Event}";
private void OnError(object sender, ErrorMessage args)
{
_controlPanel.ChangeStatus = $"RPC Error:\n{args.Message}";
windowMessages.ChangeLabel = "SDR# RPC | Internal error";
}
private void OnClose(object sender, CloseMessage args)
{
_controlPanel.ChangeStatus = "RPC Closed";
windowMessages.ChangeLabel = "SDR# RPC | Closed";
Close();
}
private void OnReady(object sender, ReadyMessage args)
{
_controlPanel.ChangeStatus = "RPC Ready";
windowMessages.ChangeLabel = "SDR# RPC | Ready";
}
private void OnJoin(object sender, JoinMessage args)
{
presence.Party.Size++;
presence.Secrets.JoinSecret = args.Secret;
MessageBox.Show("OnJoin: " + args.Secret);
_control.StopRadio();
_control.RefreshSource(true);
Utils.SaveSetting("spyserver.uri", args.Secret);
_control.StartRadio();
}
private async void OnJoinRequested(object sender, JoinRequestMessage args)
{
try
{
if (await windowMessages.RequestAnswer(client, args))
{
MessageBox.Show("Accepted RequestAnswer");
}
else
{
MessageBox.Show("Declined RequestAnswer");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}

View File

@ -167,6 +167,7 @@
</ItemGroup>
<ItemGroup>
<None Include=".editorconfig" />
<None Include="app.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

View File

@ -44,7 +44,7 @@
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Font = new System.Drawing.Font("Verdana", 7F);
this.checkBox1.ForeColor = System.Drawing.Color.Black;
this.checkBox1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(244)))), ((int)(((byte)(244)))));
this.checkBox1.Location = new System.Drawing.Point(3, 3);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(150, 21);
@ -60,7 +60,7 @@
| System.Windows.Forms.AnchorStyles.Right)));
this.label1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.label1.Font = new System.Drawing.Font("Verdana", 7F);
this.label1.ForeColor = System.Drawing.Color.Black;
this.label1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(244)))), ((int)(((byte)(244)))));
this.label1.Location = new System.Drawing.Point(0, 67);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(200, 57);
@ -72,23 +72,22 @@
//
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.textBox1.BackColor = System.Drawing.Color.White;
this.textBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(15)))), ((int)(((byte)(15)))));
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.textBox1.Font = new System.Drawing.Font("Verdana", 7F);
this.textBox1.ForeColor = System.Drawing.Color.Black;
this.textBox1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(244)))), ((int)(((byte)(244)))));
this.textBox1.Location = new System.Drawing.Point(59, 131);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(138, 19);
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);
//
// label2
//
this.label2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Verdana", 7F);
this.label2.ForeColor = System.Drawing.Color.Black;
this.label2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(244)))), ((int)(((byte)(244)))));
this.label2.Location = new System.Drawing.Point(0, 133);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(59, 12);
@ -100,7 +99,7 @@
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.button1.Font = new System.Drawing.Font("Verdana", 7F);
this.button1.ForeColor = System.Drawing.Color.Black;
this.button1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(244)))), ((int)(((byte)(244)))));
this.button1.Location = new System.Drawing.Point(151, 3);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(46, 23);
@ -114,7 +113,7 @@
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("Verdana", 6.5F);
this.checkBox2.ForeColor = System.Drawing.Color.Black;
this.checkBox2.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(244)))), ((int)(((byte)(244)))));
this.checkBox2.Location = new System.Drawing.Point(3, 22);
this.checkBox2.Name = "checkBox2";
this.checkBox2.Size = new System.Drawing.Size(162, 21);
@ -128,20 +127,25 @@
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("Verdana", 6.5F);
this.checkBox3.ForeColor = System.Drawing.Color.Black;
this.checkBox3.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(244)))), ((int)(((byte)(244)))));
this.checkBox3.Location = new System.Drawing.Point(3, 43);
this.checkBox3.Name = "checkBox3";
this.checkBox3.Size = new System.Drawing.Size(197, 21);
this.checkBox3.TabIndex = 6;
this.checkBox3.Text = "Enable Invite Feature (not tested)";
this.checkBox3.UseVisualStyleBackColor = true;
#if DEBUG
this.checkBox3.Visible = true;
#else
this.checkBox3.Visible = false;
#endif
this.checkBox3.CheckedChanged += new System.EventHandler(this.CheckBox3_CheckedChanged);
//
// SettingsPanel
//
this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
this.BackColor = System.Drawing.Color.White;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(15)))), ((int)(((byte)(15)))), ((int)(((byte)(15)))));
this.Controls.Add(this.checkBox3);
this.Controls.Add(this.button1);
this.Controls.Add(this.checkBox2);
@ -150,15 +154,17 @@
this.Controls.Add(this.label1);
this.Controls.Add(this.checkBox1);
this.Font = new System.Drawing.Font("Verdana", 7F);
this.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(244)))), ((int)(((byte)(244)))), ((int)(((byte)(244)))));
this.MinimumSize = new System.Drawing.Size(200, 160);
this.Name = "SettingsPanel";
this.Size = new System.Drawing.Size(200, 160);
this.Load += new System.EventHandler(this.SettingsPanel_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
#endregion
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Label label1;

View File

@ -28,6 +28,25 @@ namespace EnderIce2.SDRSharpPlugin
LogWriter.WriteToFile("SettingsPanel loaded");
}
private void Button1_Click(object sender, EventArgs e)
{
try
{
System.Diagnostics.Process.Start("https://github.com/EnderIce2/SDR-RPC"); // open the url (on some systems can show "The system cannot find the file specified.")
}
catch (System.ComponentModel.Win32Exception) // The system cannot find the file specified.
{
try
{
System.Diagnostics.Process.Start("iexplore", "https://github.com/EnderIce2/SDR-RPC"); // open the url with internet explorer
}
catch (System.ComponentModel.Win32Exception) // The system cannot find the file specified.
{
System.Diagnostics.Process.Start("notepad", "https://github.com/EnderIce2/SDR-RPC"); // if internet explorer is not installed, open the link in notepad
}
}
}
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Utils.SaveSetting("EnableRPC", checkBox1.Checked);
@ -36,41 +55,45 @@ namespace EnderIce2.SDRSharpPlugin
/* Utils.GetBooleanSetting("EnableRPC"); */
}
private void Button1_Click(object sender, EventArgs e) => System.Diagnostics.Process.Start("https://github.com/EnderIce2/SDR-RPC");
private void CheckBox2_CheckedChanged(object sender, EventArgs e) => Utils.SaveSetting("LogRPC", checkBox2.Checked);
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && (e.KeyChar != '.'))
{
e.Handled = true;
}
if ((e.KeyChar == '.') && ((sender as TextBox).Text.IndexOf('.') > -1))
{
e.Handled = true;
}
}
private async void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && textBox1.Text.Replace(" ", "").Length == 18)
{
Utils.SaveSetting("ClientID", textBox1.Text);
e.Handled = true;
e.SuppressKeyPress = true;
await Task.Delay(200).ConfigureAwait(false);
textBox1.Text = Utils.GetStringSetting("ClientID");
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}");
}
private async void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
textBox1.Text.Replace(" ", "");
if (!int.TryParse(textBox1.Text, out _))
{
MessageBox.Show("Invalid ID!");
}
if (e.KeyCode == Keys.Enter && textBox1.Text.Length == 18)
{
Utils.SaveSetting("ClientID", textBox1.Text);
}
else if (e.KeyCode == Keys.Enter && textBox1.Text.Length != 18)
{
Utils.SaveSetting("ClientID", "765213507321856078"); // TODO: do it better
}
e.Handled = true;
e.SuppressKeyPress = true;
await Task.Delay(100).ConfigureAwait(false);
textBox1.Text = Utils.GetStringSetting("ClientID");
label1.Text = $"Configuration Updated.\nNew ID: {Utils.GetStringSetting("ClientID")}";
}
private void SettingsPanel_Load(object sender, EventArgs e)
{
// can't use bcz System.Drawing.Color is from dotnet core 5
//MainPlugin._control.ThemeForeColor;
//BackColor = MainPlugin._control.ThemePanelColor;
//MainPlugin._control.ThemeBackColor;
BackColor = System.Drawing.Color.FromArgb(15, 15, 15);
ForeColor = System.Drawing.Color.FromArgb(244, 244, 244);
}
}
}

View File

@ -7,10 +7,7 @@ namespace EnderIce2.SDRSharpPlugin
{
public partial class TopWindowMessages : UserControl
{
public TopWindowMessages()
{
InitializeComponent();
}
public TopWindowMessages() => InitializeComponent();
private string _ChangeLabel;
public string ChangeLabel
{
@ -46,7 +43,7 @@ namespace EnderIce2.SDRSharpPlugin
SetDefaultTextInLabel(tmpansw);
return tmpansw;
}
private async Task SetDefaultTextInLabel(bool accepted)
private async void SetDefaultTextInLabel(bool accepted)
{
if (accepted)
{

11
app.config Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Drawing.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>