Skip to content

Commit 17a6a05

Browse files
committed
Cleanup and fix #12
1 parent f2e8668 commit 17a6a05

File tree

5 files changed

+61
-93
lines changed

5 files changed

+61
-93
lines changed

MainPlugin.cs

Lines changed: 55 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ public void Initialize(ISharpControl control)
4444
_control = control;
4545
if (Utils.GetBooleanSetting("EnableRPC", true))
4646
{
47-
if (Utils.GetStringSetting("ClientID").Replace(" ", "").Length != 18)
48-
{
49-
Utils.SaveSetting("ClientID", "765213507321856078");
50-
}
5147
client = new DiscordRpcClient(Utils.GetStringSetting("ClientID"), pipe: discordPipe)
5248
{
5349
Logger = new ConsoleLogger(logLevel, true)
@@ -79,94 +75,73 @@ public void Initialize(ISharpControl control)
7975

8076
private async Task MainLoop()
8177
{
82-
loop_start:
83-
try
78+
await Task.Delay(2000).ConfigureAwait(false);
79+
LogWriter.WriteToFile($"MainLoop called {client.IsInitialized}");
80+
while (true)
8481
{
85-
await Task.Delay(2000).ConfigureAwait(false);
86-
LogWriter.WriteToFile($"MainLoop called {client.IsInitialized}");
87-
while (true)
82+
if (client != null && isConnected)
8883
{
89-
if (client != null && isConnected)
84+
LogWriter.WriteToFile("Waiting 1000ms in loop...");
85+
await Task.Delay(1000).ConfigureAwait(false); // 1 second delay
86+
if (_control.IsPlaying)
9087
{
91-
LogWriter.WriteToFile("Waiting 1000ms in loop...");
92-
await Task.Delay(1000).ConfigureAwait(false); // 1 second delay
93-
if (_control.IsPlaying)
94-
{
95-
presence.Assets.SmallImageKey = "play";
96-
playedBefore = true;
97-
}
98-
else if (!_control.IsPlaying && playedBefore)
99-
{
100-
presence.Assets.SmallImageKey = "pause";
101-
}
88+
presence.Assets.SmallImageKey = "play";
89+
playedBefore = true;
90+
}
91+
else if (!_control.IsPlaying && playedBefore)
92+
{
93+
presence.Assets.SmallImageKey = "pause";
94+
}
10295

103-
if (!playedBefore)
104-
{
105-
presence.Details = "Frequency: Not playing";
106-
presence.State = "Not playing";
107-
}
108-
else
96+
if (!playedBefore)
97+
{
98+
presence.Details = "Frequency: Not playing";
99+
presence.State = "Not playing";
100+
}
101+
else
102+
{
103+
try
109104
{
110-
try
111-
{
112-
// TODO: Check BandPlan.xml file and set in the status
113-
LogWriter.WriteToFile("Setting presence...");
114-
string frequency_text = $"Frequency: {$"{_control.Frequency:#,0,,0 Hz}"} | Bandwidth: {$"{_control.FilterBandwidth:#,0,,0 Hz}"} | {Enum.GetName(typeof(DetectorType), _control.DetectorType)}";
115-
presence.Details = frequency_text;
116-
switch (_control.DetectorType)
117-
{
118-
case DetectorType.WFM:
119-
if (!string.IsNullOrWhiteSpace(_control.RdsRadioText + _control.RdsProgramService))
120-
{
121-
string radio_text = string.IsNullOrWhiteSpace(_control.RdsRadioText) ? "" : $" - {_control.RdsRadioText}";
122-
presence.State = _control.FmStereo
123-
? $"RDS: ((( {_control.RdsProgramService} ))){radio_text}"
124-
: $"RDS: {_control.RdsProgramService}{radio_text}";
125-
}
126-
else
127-
{
128-
presence.State = "RDS: unknown";
129-
}
130-
break;
131-
default:
132-
presence.State = ""; // TODO: implement for every type; right now I don't really know what to add
133-
break;
134-
}
135-
}
136-
catch (Exception ex)
105+
// TODO: Check BandPlan.xml file and set in the status
106+
LogWriter.WriteToFile("Setting presence...");
107+
string frequency_text = $"Frequency: {$"{_control.Frequency:#,0,,0 Hz}"} | Bandwidth: {$"{_control.FilterBandwidth:#,0,,0 Hz}"} | {Enum.GetName(typeof(DetectorType), _control.DetectorType)}";
108+
presence.Details = frequency_text;
109+
switch (_control.DetectorType)
137110
{
138-
LogWriter.WriteToFile("Set rpc exception\n" + ex);
111+
case DetectorType.WFM:
112+
if (!string.IsNullOrWhiteSpace(_control.RdsRadioText + _control.RdsProgramService))
113+
{
114+
string radio_text = string.IsNullOrWhiteSpace(_control.RdsRadioText) ? "" : $" - {_control.RdsRadioText}";
115+
presence.State = _control.FmStereo
116+
? $"RDS: ((( {_control.RdsProgramService} ))){radio_text}"
117+
: $"RDS: {_control.RdsProgramService}{radio_text}";
118+
}
119+
else
120+
{
121+
presence.State = "RDS: unknown";
122+
}
123+
break;
124+
default:
125+
presence.State = ""; // TODO: implement for every type; right now I don't really know what to add
126+
break;
139127
}
140128
}
141-
try
142-
{
143-
client.SetPresence(presence);
144-
}
145-
catch (ObjectDisposedException ex)
129+
catch (Exception ex)
146130
{
147-
LogWriter.WriteToFile($"ObjectDisposedException exception for SetPresence\n{ex}");
131+
LogWriter.WriteToFile("Set rpc exception\n" + ex);
148132
}
149-
LogWriter.WriteToFile("SetPresence");
150-
_controlPanel.ChangeStatus = $"Presence Updated {DateTime.UtcNow}";
151133
}
134+
try
135+
{
136+
client.SetPresence(presence);
137+
}
138+
catch (ObjectDisposedException ex)
139+
{
140+
LogWriter.WriteToFile($"ObjectDisposedException exception for SetPresence\n{ex}");
141+
}
142+
LogWriter.WriteToFile("SetPresence");
143+
_controlPanel.ChangeStatus = $"Presence Updated {DateTime.UtcNow}";
152144
}
153-
if (client == null)
154-
{
155-
_controlPanel.ChangeStatus = "Client is null";
156-
}
157-
else
158-
{
159-
_controlPanel.ChangeStatus = "Presence stopped";
160-
}
161-
}
162-
catch (Exception ex)
163-
{
164-
if (ex.Message.Contains("The process cannot access the file")) // not important exception
165-
{
166-
goto loop_start;
167-
}
168-
_controlPanel.ChangeStatus = $"RPC Update Error\n{ex.Message}";
169-
LogWriter.WriteToFile(ex.ToString());
170145
}
171146
}
172147

SettingsForm.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SettingsForm.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,17 @@ private void Button2_Click(object sender, EventArgs e)
2222

2323
private void Button1_Click(object sender, EventArgs e) => Close();
2424

25-
private void CheckBox1_CheckedChanged(object sender, EventArgs e) => Utils.SaveSetting("ShowWelcomePage", !checkBox1.Checked);
26-
2725
private void Button5_Click(object sender, EventArgs e)
2826
{
2927
MessageBox.Show("MIT License\n\nCopyright (c) 2018 Lachee\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.", "discord-rpc-csharp");
3028
MessageBox.Show("The MIT License (MIT)\n\nCopyright(c) 2007 James Newton-King\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.", "Newtonsoft.Json");
3129
}
3230

33-
private void CheckBox1_CheckedChanged_1(object sender, EventArgs e) => Utils.SaveSetting("LogRPC", checkBox1.Checked);
31+
private void CheckBox1_CheckedChanged(object sender, EventArgs e) => Utils.SaveSetting("LogRPC", checkBox1.Checked);
3432

3533
private void Button4_Click(object sender, EventArgs e)
3634
{
3735
textBox1.Text.Replace(" ", "").Replace("\n", "").Replace("\r", "");
38-
if (!int.TryParse(textBox1.Text, out _) || textBox1.Text.Length != 18)
39-
{
40-
MessageBox.Show("Invalid Client ID!");
41-
}
4236
Utils.SaveSetting("ClientID", textBox1.Text);
4337
label1.Text = $"Configuration Updated.\nNew ID: {Utils.GetStringSetting("ClientID")}";
4438
}

SettingsPanel.Designer.cs

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SettingsPanel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using SDRSharp.Radio;
22
using System;
3-
using System.Threading.Tasks;
43
using System.Windows.Forms;
54

65
namespace EnderIce2.SDRSharpPlugin
@@ -25,9 +24,9 @@ public SettingsPanel()
2524
LogWriter.WriteToFile("User Control Loaded");
2625
}
2726

28-
private void Button1_Click(object sender, EventArgs e) => new SettingsForm().Show();
27+
private void SettingsButton_Click(object sender, EventArgs e) => new SettingsForm().Show();
2928

30-
private void CheckBox1_CheckedChanged(object sender, EventArgs e)
29+
private void EnableRPC_CheckedChanged(object sender, EventArgs e)
3130
{
3231
Utils.SaveSetting("EnableRPC", checkBox1.Checked);
3332
label1.Text = "Restart required";

0 commit comments

Comments
 (0)