diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 5d9fabb..6804bd9 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -2,4 +2,5 @@ CHANGE LOG: Release 2.0: Improved performance. Removed 500 album restriction. Added automatic prompt to re-authenticate if computer resumes from sleep. Release 2.0.2: Fixed error log spam. -Beta 3.1: Upgraded all API methods to 6.x.x spec. Implemented PKCE auth method with token persistence and automatic renewal. General speed improvements. \ No newline at end of file +Release 3.0.5: Upgraded all API methods to 6.x.x spec. Implemented PKCE auth method with token persistence and automatic renewal. General speed improvements. +Beta 3.1: Bug fixes for network disconnections, refreshing, various other things. \ No newline at end of file diff --git a/Plugins/SpotifyAPI.Web.Auth.dll b/Plugins/SpotifyAPI.Web.Auth.dll index 63b7470..415d789 100644 Binary files a/Plugins/SpotifyAPI.Web.Auth.dll and b/Plugins/SpotifyAPI.Web.Auth.dll differ diff --git a/Plugins/SpotifyAPI.Web.dll b/Plugins/SpotifyAPI.Web.dll index f60cf83..f05445f 100644 Binary files a/Plugins/SpotifyAPI.Web.dll and b/Plugins/SpotifyAPI.Web.dll differ diff --git a/Plugins/mb_Spotify-Plugin.dll b/Plugins/mb_Spotify-Plugin.dll index 6d20595..aa473b9 100644 Binary files a/Plugins/mb_Spotify-Plugin.dll and b/Plugins/mb_Spotify-Plugin.dll differ diff --git a/Source Files/PanelInterface.cs b/Source Files/PanelInterface.cs index 10c1539..e6c9cf5 100644 --- a/Source Files/PanelInterface.cs +++ b/Source Files/PanelInterface.cs @@ -15,12 +15,10 @@ public partial class Plugin private PluginInfo about = new PluginInfo(); private Control panel; public int panelHeight; - private static string _searchTerm; + private static string _searchTerm, _path; private bool _runOnce = true; Font largeBold, smallRegular, smallBold; private RSACryptoServiceProvider _rsaKey; - - // Create a new CspParameters object to specify a key container. CspParameters _cspParams = new CspParameters(); public PluginInfo Initialise(IntPtr apiInterfacePtr) @@ -34,23 +32,17 @@ public PluginInfo Initialise(IntPtr apiInterfacePtr) about.TargetApplication = "Spotify Plugin"; about.Type = PluginType.PanelView; about.VersionMajor = 3; - about.VersionMinor = 0; - about.Revision = 5; + about.VersionMinor = 1; + about.Revision = 0; about.MinInterfaceVersion = MinInterfaceVersion; about.MinApiRevision = MinApiRevision; about.ReceiveNotifications = (ReceiveNotificationFlags.PlayerEvents | ReceiveNotificationFlags.TagEvents); about.ConfigurationPanelHeight = 0; _path = mbApiInterface.Setting_GetPersistentStoragePath() + "token.xml"; - _cspParams.KeyContainerName = "SPOTIFY_XML_ENC_RSA_KEY"; - - // Create a new RSA key and save it in the container. This key will encrypt - // a symmetric key, which will then be encrypted in the XML document. _rsaKey = new RSACryptoServiceProvider(_cspParams); - //SystemEvents.PowerModeChanged += OnPowerChange; - return about; } @@ -59,7 +51,6 @@ public int OnDockablePanelCreated(Control panel) float dpiScaling = 0; - largeBold = new Font(panel.Font.FontFamily, 9, FontStyle.Bold); smallRegular = new Font(panel.Font.FontFamily, 8); smallBold = new Font(panel.Font.FontFamily, 8, FontStyle.Bold); @@ -69,7 +60,9 @@ public int OnDockablePanelCreated(Control panel) this.panel = panel; panelHeight = Convert.ToInt32(145 * dpiScaling); + return panelHeight; + } public string Truncate(string text, Font font) @@ -77,13 +70,10 @@ public string Truncate(string text, Font font) if (TextRenderer.MeasureText(text + "...", font).Width < panel.Width) { - return text; - } else { - int i = text.Length; while (TextRenderer.MeasureText(text + "...", font).Width > panel.Width) { @@ -110,14 +100,13 @@ private void DrawPanel(object sender, PaintEventArgs e) { SpotifyWebAuth(); _trackMissing = 1; + mbApiInterface.MB_RefreshPanels(); panel.Invalidate(); _runOnce = false; } - // re-draws when file is found? if (_auth == 1 && _trackMissing != 1) { - //MessageBox.Show("Drawing."); TextRenderer.DrawText(e.Graphics, _title, largeBold, new Point(5, 10), text1); TextRenderer.DrawText(e.Graphics, _artist, smallRegular, new Point(5, 30), text1); @@ -162,16 +151,11 @@ private void DrawPanel(object sender, PaintEventArgs e) } else if (_auth == 1 && _trackMissing == 1) { - - //MessageBox.Show("Drawing."); TextRenderer.DrawText(e.Graphics, "No Track Found!", new Font(panel.Font.FontFamily, 12), new Point(5, 70), text1); - } else if (_auth == 0) { - //MessageBox.Show("Drawing."); TextRenderer.DrawText(e.Graphics, "Please Click Here to \nAuthenticate Spotify.", new Font(panel.Font.FontFamily, 14), new Point(4, 50), text1); - } } @@ -188,23 +172,12 @@ public List GetMenuItems() return list; } - //private void OnPowerChange(object s, PowerModeChangedEventArgs e) - //{ - // switch (e.Mode) - // { - // case PowerModes.Resume: - - // _auth = 0; - - // break; - // } - //} - public void reAuthSpotify(object sender, EventArgs e) { File.Delete(_path); SpotifyWebAuth(); _trackMissing = 1; + mbApiInterface.MB_RefreshPanels(); panel.Invalidate(); } @@ -220,7 +193,6 @@ private void PanelClick(object sender, EventArgs e) _trackMissing = 1; panel.Invalidate(); - //panel.Paint += DrawPanel; } else if (_auth == 1 && me.Button == System.Windows.Forms.MouseButtons.Left) @@ -238,13 +210,11 @@ private void PanelClick(object sender, EventArgs e) { UnfollowArtist(); panel.Invalidate(); - //panel.Paint += DrawPanel; } else { FollowArtist(); panel.Invalidate(); - //panel.Paint += DrawPanel; } } @@ -255,13 +225,11 @@ private void PanelClick(object sender, EventArgs e) { RemoveAlbum(); panel.Invalidate(); - //panel.Paint += DrawPanel; } else { SaveAlbum(); panel.Invalidate(); - //panel.Paint += DrawPanel; } } @@ -272,21 +240,17 @@ private void PanelClick(object sender, EventArgs e) { RemoveTrack(); panel.Invalidate(); - //panel.Paint += DrawPanel; } else { SaveTrack(); panel.Invalidate(); - //panel.Paint += DrawPanel; } } - } - } public async void ReceiveNotification(string sourceFileUrl, NotificationType type) @@ -308,7 +272,6 @@ public async void ReceiveNotification(string sourceFileUrl, NotificationType typ } panel.Invalidate(); - break; } @@ -321,12 +284,10 @@ public bool Configure(IntPtr panelHandle) public void SaveSettings() { - } public void Close(PluginCloseReason reason) { - //SystemEvents.PowerModeChanged -= OnPowerChange; } public void Uninstall() diff --git a/Source Files/Properties/AssemblyInfo.cs b/Source Files/Properties/AssemblyInfo.cs index a954099..0d43e08 100644 --- a/Source Files/Properties/AssemblyInfo.cs +++ b/Source Files/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("3.0.5.0")] -[assembly: AssemblyFileVersion("3.0.5.0")] +[assembly: AssemblyVersion("3.1.0.0")] +[assembly: AssemblyFileVersion("3.1.0.0")] diff --git a/Source Files/SpotifyIntegration.cs b/Source Files/SpotifyIntegration.cs index 0a59851..7a351b0 100644 --- a/Source Files/SpotifyIntegration.cs +++ b/Source Files/SpotifyIntegration.cs @@ -18,19 +18,27 @@ public partial class Plugin private static SpotifyClient _spotify; private static int _auth, _num, _trackMissing = 0; private static bool _trackLIB, _albumLIB, _artistLIB = false; - private static string _title, _album, _artist, _trackID, _albumID, _artistID, _imageURL, _path; + private static string _title, _album, _artist, _trackID, _albumID, _artistID, _imageURL; private static string _clientID = "9076681768d94feda885a7b5eced926d"; public void SerializeConfig(PKCETokenResponse data, string path, RSACryptoServiceProvider rsaKey) { - using (StreamWriter file = new StreamWriter(path, false)) + + try { - XmlSerializer controlsDefaultsSerializer = new XmlSerializer(typeof(PKCETokenResponse)); - controlsDefaultsSerializer.Serialize(file, data); - file.Close(); + // Serialize + using (StreamWriter file = new StreamWriter(path, false)) + { + XmlSerializer controlsDefaultsSerializer = new XmlSerializer(typeof(PKCETokenResponse)); + controlsDefaultsSerializer.Serialize(file, data); + file.Close(); + } } - - try + catch (Exception e) + { + Console.WriteLine(e.Message); + } + finally { // Encrypt XmlDocument xmlDoc = new XmlDocument(); @@ -39,11 +47,6 @@ public void SerializeConfig(PKCETokenResponse data, string path, RSACryptoServic Encrypt(xmlDoc, "AccessToken", "AccessToken", rsaKey, "rsaKey"); Encrypt(xmlDoc, "RefreshToken", "RefreshToken", _rsaKey, "rsaKey"); xmlDoc.Save(path); - - } - catch (Exception e) - { - Console.WriteLine(e.Message); } } @@ -85,38 +88,22 @@ public PKCETokenResponse DeserializeConfig(string path, RSACryptoServiceProvider } } - //static void WriteOutput(PKCETokenResponse InitialToken) - //{ - // MessageBox.Show( - // InitialToken.AccessToken + '\n' + - // InitialToken.CreatedAt.ToString() + '\n' + - // InitialToken.ExpiresIn + '\n' + - // InitialToken.Scope + '\n' + - // InitialToken.TokenType + '\n' + - // InitialToken.RefreshToken + '\n' - // ); - //} - - async void SpotifyWebAuth() { try { - if(File.Exists(_path)) + if (File.Exists(_path)) { var token_response = DeserializeConfig(_path, _rsaKey); - //WriteOutput(token_response); - var authenticator = new PKCEAuthenticator(_clientID, token_response, _path); var config = SpotifyClientConfig.CreateDefault() - .WithAuthenticator(authenticator); + .WithAuthenticator(authenticator); _spotify = new SpotifyClient(config); SerializeConfig(token_response, _path, _rsaKey); - //WriteOutput(token_response); // This appears to be the easiest way to check if the Spotify client works, but it's not great: try @@ -124,14 +111,13 @@ async void SpotifyWebAuth() await _spotify.Search.Item(new SearchRequest(SearchRequest.Types.Track, "fasdofimasdofiasdnfaosnf")); _auth = 1; } - catch(APIException) + catch (Exception e) { - Console.WriteLine("Spotify agent dead."); + Console.WriteLine("Spotify agent dead: " + e); throw new System.NullReferenceException(); } - } - else { throw new System.NullReferenceException(); } + else { throw new System.NullReferenceException("Token.xml not found!"); } } catch (System.NullReferenceException) { @@ -153,7 +139,7 @@ async void SpotifyWebAuth() await server.Stop(); var initialResponse = await new OAuthClient().RequestToken( - new PKCETokenRequest(_clientID, response.Code, server.BaseUri, verifier) + new PKCETokenRequest(_clientID, response.Code, server.BaseUri, verifier) ); //WriteOutput(initialResponse); @@ -161,7 +147,7 @@ async void SpotifyWebAuth() var authenticator = new PKCEAuthenticator(_clientID, initialResponse, _path); var config = SpotifyClientConfig.CreateDefault() - .WithAuthenticator(authenticator); + .WithAuthenticator(authenticator); _spotify = new SpotifyClient(config); //WriteOutput(initialResponse); @@ -180,6 +166,15 @@ async void SpotifyWebAuth() _auth = 1; } + catch (System.Net.WebException) + { + _auth = 0; + } + finally + { + mbApiInterface.MB_RefreshPanels(); + panel.Invalidate(); + } } @@ -211,12 +206,33 @@ public async Task TrackSearch() Console.WriteLine("Error Msg: " + e.Message); return null; } - catch (System.ArgumentOutOfRangeException e) + catch (System.ArgumentOutOfRangeException) { Console.WriteLine("Song not found!"); _trackMissing = 1; return null; } + catch (System.NullReferenceException) + { + Console.WriteLine("Auth error!"); + _auth = 0; + _trackMissing = 1; + return null; + } + catch (System.Net.WebException) + { + Console.WriteLine("Auth error!"); + _auth = 0; + _trackMissing = 1; + return null; + } + catch (System.Net.Http.HttpRequestException) + { + Console.WriteLine("Auth error!"); + _auth = 0; + _trackMissing = 1; + return null; + } } @@ -295,8 +311,6 @@ public void FollowArtist() } - - public void RemoveTrack() { try @@ -372,8 +386,6 @@ public void UnfollowArtist() } - - public Boolean CheckTrack(string id) { var tracks = new LibraryCheckTracksRequest(new List { id }); @@ -408,7 +420,6 @@ public Boolean CheckAlbum(string id) return false; } - } public Boolean CheckArtist(string id) @@ -429,6 +440,5 @@ public Boolean CheckArtist(string id) } } - } }