diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..e69de29 diff --git a/DiscordBee.cs b/DiscordBee.cs index 9da9812..b671547 100644 --- a/DiscordBee.cs +++ b/DiscordBee.cs @@ -1,14 +1,15 @@ namespace MusicBeePlugin { using DiscordRPC; - using MusicBeePlugin.DiscordTools; - using MusicBeePlugin.DiscordTools.Assets; - using MusicBeePlugin.DiscordTools.Assets.Uploader; - using MusicBeePlugin.UI; + using DiscordTools; + using DiscordTools.Assets; + using DiscordTools.Assets.Uploader; + using UI; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; + using System.Linq; using System.Reflection; using System.Text; using System.Text.RegularExpressions; @@ -69,7 +70,7 @@ public PluginInfo Initialise(IntPtr apiInterfacePtr) _about.TargetApplication = ""; // current only applies to artwork, lyrics or instant messenger name that appears in the provider drop down selector or target Instant Messenger _about.Type = PluginType.General; _about.VersionMajor = 3; // your plugin version - _about.VersionMinor = 1; + _about.VersionMinor = 2; _about.Revision = 0; _about.MinInterfaceVersion = MinInterfaceVersion; _about.MinApiRevision = MinApiRevision; @@ -79,7 +80,7 @@ public PluginInfo Initialise(IntPtr apiInterfacePtr) var workingDir = _mbApiInterface.Setting_GetPersistentStoragePath() + _about.Name; var settingsFilePath = $"{workingDir}\\{_about.Name}.settings"; _imgurAssetCachePath = $"{workingDir}\\{_about.Name}-Imgur.cache"; - _imgurAlbum = $"{workingDir}\\{_about.Name}-Imgur.album"; + _imgurAlbum = $"{workingDir}\\{_about.Name}-FreeImageHost.album"; _settings = Settings.GetInstance(settingsFilePath); _settings.SettingChanged += SettingChangedCallback; @@ -190,6 +191,7 @@ public void ReceiveNotification(string _, NotificationType type) switch (type) { case NotificationType.PluginStartup: + case NotificationType.NowPlayingArtworkReady: var playState = _mbApiInterface.Player_GetPlayState(); // assuming MusicBee wasn't closed and started again in the same Discord session if (_settings.UpdatePresenceWhenStopped || (playState != PlayState.Paused && playState != PlayState.Stopped)) @@ -251,14 +253,13 @@ private void UpdateDiscordPresence(PlayState playerGetPlayState) RichPresence _discordPresence = new RichPresence { Assets = new Assets(), - Party = new Party(), - Timestamps = new Timestamps() + Type = ActivityType.Listening }; // Discord allows only strings with a min length of 2 or the update fails // so add some exotic space (Mongolian vowel separator) to the string if it is smaller // Discord also disallows strings bigger than 128bytes so handle that as well - string padString(string input) + string padString(string input, int maxLength = 128) { if (!string.IsNullOrEmpty(input)) { @@ -266,9 +267,9 @@ string padString(string input) { return input + "\u180E"; } - if (Encoding.UTF8.GetBytes(input).Length > 128) + if (Encoding.UTF8.GetBytes(input).Length > maxLength) { - byte[] buffer = new byte[128]; + byte[] buffer = new byte[maxLength]; char[] inputChars = input.ToCharArray(); Encoding.UTF8.GetEncoder().Convert( chars: inputChars, @@ -288,23 +289,38 @@ string padString(string input) } // Button Functionality - if (_settings.ShowButton) + bool tryCreateButton(string label, string uriValue, out Button button) { - var uri = _layoutHandler.RenderUrl(_settings.ButtonUrl, metaDataDict, '\\'); - Debug.WriteLine($"Url: {uri}"); - - // Validate the URL again. - if (ValidationHelpers.ValidateUri(uri)) + var uri = _layoutHandler.RenderUrl(uriValue, metaDataDict, '\\'); + if (ValidationHelpers.ValidateUri(uriValue) && !string.IsNullOrEmpty(label)) { - _discordPresence.Buttons = new Button[] + button = new Button { - new Button - { - Label = padString(_settings.ButtonLabel), - Url = uri - } + Label = padString(label, 32), + Url = uri }; + + return true; } + + button = null; + return false; + } + + var buttons = new List