@@ -19,7 +19,11 @@ public class MainPlugin : ISharpPlugin
1919 private ISharpControl _control ;
2020 private bool playedBefore ;
2121
22- private TopWindowMessages windowMessages ;
22+ private DiscordRpcClient client ;
23+ private bool isRunning = true ;
24+ public bool HasGui => true ;
25+ public string DisplayName => "Discord RPC" ;
26+ public UserControl Gui => _controlPanel ;
2327
2428 private readonly RichPresence presence = new RichPresence ( )
2529 {
@@ -30,27 +34,14 @@ public class MainPlugin : ISharpPlugin
3034 LargeImageKey = "image_large" ,
3135 LargeImageText = "SDRSharp" ,
3236 SmallImageKey = "image_small" ,
33- SmallImageText = $ "SDR-RPC plugin v{ Assembly . LoadFrom ( "SDR-RPC.dll" ) . GetName ( ) . Version } by EnderIce2" // should show the correct version
37+ SmallImageText = $ "SDR-RPC Plugin v{ Assembly . LoadFrom ( "SDR-RPC.dll" ) . GetName ( ) . Version } "
3438 }
3539 } ;
3640
37- private DiscordRpcClient client ;
38- private bool isRunning = true ;
39- public bool HasGui => true ;
40- public string DisplayName => "Discord RPC" ;
41- public UserControl Gui => _controlPanel ;
42-
4341 public void Initialize ( ISharpControl control )
4442 {
45- if ( Utils . GetBooleanSetting ( "ShowWelcomePage" , true ) )
46- {
47- new WelcomeForm ( ) . ShowDialog ( ) ;
48- }
49-
5043 _controlPanel = new SettingsPanel ( ) ;
51- windowMessages = new TopWindowMessages ( ) ; // TODO: do something when "EnableRPCInvite" is set to false
5244 _control = control ;
53-
5445 if ( Utils . GetBooleanSetting ( "EnableRPC" , true ) )
5546 {
5647 if ( Utils . GetStringSetting ( "ClientID" ) . Replace ( " " , "" ) . Length != 18 )
@@ -70,40 +61,12 @@ public void Initialize(ISharpControl control)
7061 client . OnError += OnError ;
7162 client . OnConnectionEstablished += OnConnectionEstablished ;
7263 client . OnConnectionFailed += OnConnectionFailed ;
73- client . OnSubscribe += OnSubscribe ;
74- client . OnUnsubscribe += OnUnsubscribe ;
75- client . OnJoin += OnJoin ;
76- client . OnJoinRequested += OnJoinRequested ;
7764 presence . Timestamps = new Timestamps ( )
7865 {
7966 Start = DateTime . UtcNow
8067 } ;
81-
8268 client . SetPresence ( presence ) ;
8369 client . Initialize ( ) ;
84- try
85- {
86- //if (Utils.GetBooleanSetting("EnableRPCInvite", false))
87- //{
88- // _control.RegisterFrontControl(windowMessages, PluginPosition.Top);
89- // presence.Secrets = new Secrets()
90- // {
91- // JoinSecret = "invalid_secret"
92- // };
93- // presence.Party = new Party()
94- // {
95- // ID = Secrets.CreateFriendlySecret(new Random()),
96- // Size = 1,
97- // Max = 100
98- // };
99- // windowMessages.Show();
100- // client.SetSubscription(EventType.Join | EventType.JoinRequest);
101- //}
102- }
103- catch ( Exception ex )
104- {
105- MessageBox . Show ( ex . ToString ( ) ) ;
106- }
10770 _ = MainLoop ( ) ;
10871 }
10972 else
@@ -115,30 +78,16 @@ public void Initialize(ISharpControl control)
11578
11679 private async Task MainLoop ( )
11780 {
81+ loop_start :
11882 try
11983 {
12084 await Task . Delay ( 2000 ) . ConfigureAwait ( false ) ;
12185 isRunning = true ;
12286 LogWriter . WriteToFile ( $ "MainLoop called { isRunning } { client . IsInitialized } ") ;
12387 while ( client != null && isRunning )
12488 {
125- //if (Utils.GetBooleanSetting("EnableRPCInvite", false))
126- //{
127- // LogWriter.WriteToFile("Setting secret...");
128- // try
129- // {
130- // // TODO: Get spy server host + port address
131- // //string sdr_url = "sdr://" + host + ":" + port + "/";
132- // //LogWriter.WriteToFile(sdr_url);
133- // //presence.Secrets.JoinSecret = sdr_url;
134- // }
135- // catch (Exception ex)
136- // {
137- // LogWriter.WriteToFile(ex.ToString());
138- // }
139- //}
140- LogWriter . WriteToFile ( "Waiting 500ms in loop..." ) ;
141- await Task . Delay ( 500 ) . ConfigureAwait ( false ) ;
89+ LogWriter . WriteToFile ( "Waiting 5000ms in loop..." ) ;
90+ await Task . Delay ( 5000 ) . ConfigureAwait ( false ) ; // 5 second delay
14291 if ( _control . RdsRadioText != null )
14392 {
14493 if ( _control . IsPlaying )
@@ -160,36 +109,21 @@ private async Task MainLoop()
160109 {
161110 try
162111 {
163- string space_for_listen_list = " | " ;
164- if ( ! Utils . GetBooleanSetting ( "EnableRPCInvite" , false ) )
165- {
166- space_for_listen_list = "" ;
167- }
168-
169112 LogWriter . WriteToFile ( $ "Frequency: { _control . Frequency } ") ;
170113 LogWriter . WriteToFile ( $ "RdsRadioText: { _control . RdsRadioText } ") ;
171114 LogWriter . WriteToFile ( $ "RdsProgramService: { _control . RdsProgramService } ") ;
172115 LogWriter . WriteToFile ( "Setting presence..." ) ;
173- presence . Details = $ "Frequency: { string . Format ( "{0:#,0,,0 Hz}" , _control . Frequency ) } ";
174- if ( string . IsNullOrWhiteSpace ( _control . RdsRadioText + _control . RdsProgramService ) )
175- {
176- presence . State = $ "RDS: unknown{ space_for_listen_list } ";
177- }
178- else if ( _control . FmStereo )
179- {
180- presence . State = $ "RDS: ((( { _control . RdsProgramService } ))) - { _control . RdsRadioText } { space_for_listen_list } ";
181- }
182- else
183- {
184- presence . State = $ "RDS: { _control . RdsProgramService } - { _control . RdsRadioText } { space_for_listen_list } ";
185- }
116+ presence . Details = $ "Frequency: { $ "{ _control . Frequency : #,0,,0 Hz} "} ";
117+ presence . State = string . IsNullOrWhiteSpace ( _control . RdsRadioText + _control . RdsProgramService )
118+ ? "RDS: unknown"
119+ : _control . FmStereo
120+ ? $ "RDS: ((( { _control . RdsProgramService } ))) - { _control . RdsRadioText } "
121+ : $ "RDS: { _control . RdsProgramService } - { _control . RdsRadioText } ";
186122 }
187123 catch ( Exception ex )
188124 {
189125 LogWriter . WriteToFile ( ex . ToString ( ) ) ;
190126 }
191- /* presence.Secrets.JoinSecret = */
192- /* _control.RegisterFrontControl(Gui, PluginPosition.Top); */
193127 }
194128 try
195129 {
@@ -221,8 +155,7 @@ private async Task MainLoop()
221155 {
222156 if ( ex . Message . Contains ( "The process cannot access the file" ) )
223157 {
224- _ = MainLoop ( ) ;
225- return ;
158+ goto loop_start ;
226159 }
227160 _controlPanel . ChangeStatus = $ "RPC Update Error\n { ex . Message } ";
228161 LogWriter . WriteToFile ( ex . ToString ( ) ) ;
@@ -244,57 +177,14 @@ public void Close()
244177
245178 private void OnConnectionEstablished ( object sender , ConnectionEstablishedMessage args ) => _controlPanel . ChangeStatus = "RPC Connection Established!" ;
246179
247- private void OnSubscribe ( object sender , SubscribeMessage args ) => _controlPanel . ChangeStatus = $ "Subscribed: { args . Event } ";
248-
249- private void OnUnsubscribe ( object sender , UnsubscribeMessage args ) => _controlPanel . ChangeStatus = $ "Unsubscribed: { args . Event } ";
250-
251- private void OnError ( object sender , ErrorMessage args )
252- {
253- _controlPanel . ChangeStatus = $ "RPC Error:\n { args . Message } ";
254- windowMessages . ChangeLabel = "SDR# RPC | Internal error" ;
255- }
180+ private void OnError ( object sender , ErrorMessage args ) => _controlPanel . ChangeStatus = $ "RPC Error:\n { args . Message } ";
256181
257182 private void OnClose ( object sender , CloseMessage args )
258183 {
259184 _controlPanel . ChangeStatus = "RPC Closed" ;
260- windowMessages . ChangeLabel = "SDR# RPC | Closed" ;
261185 Close ( ) ;
262186 }
263187
264- private void OnReady ( object sender , ReadyMessage args )
265- {
266- _controlPanel . ChangeStatus = "RPC Ready" ;
267- windowMessages . ChangeLabel = "SDR# RPC | Ready" ;
268- }
269-
270- private void OnJoin ( object sender , JoinMessage args )
271- {
272- presence . Party . Size ++ ;
273- presence . Secrets . JoinSecret = args . Secret ;
274- MessageBox . Show ( "OnJoin: " + args . Secret ) ;
275- _control . StopRadio ( ) ;
276- _control . RefreshSource ( true ) ;
277- Utils . SaveSetting ( "spyserver.uri" , args . Secret ) ;
278- _control . StartRadio ( ) ;
279- }
280-
281- private async void OnJoinRequested ( object sender , JoinRequestMessage args )
282- {
283- try
284- {
285- if ( await windowMessages . RequestAnswer ( client , args ) )
286- {
287- MessageBox . Show ( "Accepted RequestAnswer" ) ;
288- }
289- else
290- {
291- MessageBox . Show ( "Declined RequestAnswer" ) ;
292- }
293- }
294- catch ( Exception ex )
295- {
296- MessageBox . Show ( ex . ToString ( ) ) ;
297- }
298- }
188+ private void OnReady ( object sender , ReadyMessage args ) => _controlPanel . ChangeStatus = "RPC Ready" ;
299189 }
300190}
0 commit comments