예제 #1
0
        private void UpdateDiscordPresence(PlayState playerGetPlayState)
        {
            var metaDataDict = GenerateMetaDataDictionary();

            // Discord allows only strings with a min length of 2 or the update fails
            // so add some exotic space (Mongolian vovel seperator) to the string if it is smaller
            // Discord also disallows strings bigger than 128bytes so handle that as well
            string padString(string input)
            {
                if (!string.IsNullOrEmpty(input))
                {
                    if (input.Length < 2)
                    {
                        return(input + "\u180E");
                    }
                    if (Encoding.UTF8.GetBytes(input).Length > 128)
                    {
                        byte[] buffer     = new byte[128];
                        char[] inputChars = input.ToCharArray();
                        Encoding.UTF8.GetEncoder().Convert(
                            chars: inputChars,
                            charIndex: 0,
                            charCount: inputChars.Length,
                            bytes: buffer,
                            byteIndex: 0,
                            byteCount: buffer.Length,
                            flush: false,
                            charsUsed: out int charsUsed,
                            bytesUsed: out int bytesUsed,
                            completed: out bool completed);
                        return(Encoding.UTF8.GetString(buffer, 0, bytesUsed));
                    }
                }
                return(input);
            }

            void SetImage(string name)
            {
                _discordPresence.Assets.LargeImageKey  = "logo";
                _discordPresence.Assets.LargeImageText = padString(_layoutHandler.Render(_settings.ImageText, metaDataDict, _settings.Seperator));
                _discordPresence.Assets.SmallImageKey  = padString(name);
                _discordPresence.Assets.SmallImageText = padString(name);
            }

            _discordPresence.State = padString(_layoutHandler.Render(_settings.PresenceState, metaDataDict, _settings.Seperator));

            var t = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1));

            _discordPresence.Timestamps.StartUnixMilliseconds = (ulong)(Math.Round(t.TotalSeconds) - Math.Round(_mbApiInterface.Player_GetPosition() / 1000.0));

            switch (playerGetPlayState)
            {
            case PlayState.Playing:
                SetImage("play");
                break;

            case PlayState.Stopped:
                SetImage("stop");
                _discordPresence.Timestamps.Start = null;
                break;

            case PlayState.Paused:
                SetImage("pause");
                _discordPresence.Timestamps.Start = null;
                break;

            case PlayState.Undefined:
            case PlayState.Loading:
                break;
            }

            _discordPresence.Details = padString(_layoutHandler.Render(_settings.PresenceDetails, metaDataDict, _settings.Seperator));

            var trackcnt = -1;
            var trackno  = -1;

            try
            {
                trackcnt = int.Parse(_layoutHandler.Render(_settings.PresenceTrackCnt, metaDataDict, _settings.Seperator));
                trackno  = int.Parse(_layoutHandler.Render(_settings.PresenceTrackNo, metaDataDict, _settings.Seperator));
            }
            catch (Exception)
            {
                // ignored
            }

            if (trackcnt < trackno || trackcnt <= 0 || trackno <= 0)
            {
                _discordPresence.Party = null;
            }
            else
            {
                _discordPresence.Party = new Party
                {
                    ID   = "aaaa",
                    Max  = trackcnt,
                    Size = trackno
                };
            }

            if (!_settings.UpdatePresenceWhenStopped && (playerGetPlayState == PlayState.Paused || playerGetPlayState == PlayState.Stopped))
            {
                _discordClient.ClearPresence();
            }
            else
            {
                _discordClient.SetPresence(_discordPresence);
            }
        }
예제 #2
0
        private void UpdateDiscordPresence(PlayState playerGetPlayState)
        {
            var metaDataDict = GenerateMetaDataDictionary();

            // Discord allows only strings with a min length of 2 or the update fails
            // so add some exotic space (Mongolian vovel seperator) to the string if it is smaller
            string padString(string input)
            {
                if (!string.IsNullOrEmpty(input) && input.Length < 2)
                {
                    return(input + "\u180E");
                }
                return(input);
            }

            void SetImage(string name)
            {
                _discordPresence.Assets.LargeImageKey  = "logo";
                _discordPresence.Assets.LargeImageText = padString(_layoutHandler.Render(_settings.ImageText, metaDataDict, _settings.Seperator));
                _discordPresence.Assets.SmallImageKey  = padString(name);
                _discordPresence.Assets.SmallImageText = padString(name);
            }

            _discordPresence.State = padString(_layoutHandler.Render(_settings.PresenceState, metaDataDict, _settings.Seperator));

            var t = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1));

            _discordPresence.Timestamps.StartUnixMilliseconds = (ulong)(Math.Round(t.TotalSeconds) - Math.Round(_mbApiInterface.Player_GetPosition() / 1000.0));

            switch (playerGetPlayState)
            {
            case PlayState.Playing:
                SetImage("play");
                break;

            case PlayState.Stopped:
                SetImage("stop");
                _discordPresence.Timestamps.Start = null;
                break;

            case PlayState.Paused:
                SetImage("pause");
                _discordPresence.Timestamps.Start = null;
                break;

            case PlayState.Undefined:
            case PlayState.Loading:
                break;
            }

            _discordPresence.Details  = padString(_layoutHandler.Render(_settings.PresenceDetails, metaDataDict, _settings.Seperator));
            _discordPresence.Party.ID = "aaaa";

            var trackcnt = 0;
            var trackno  = 0;

            try
            {
                trackcnt = int.Parse(_layoutHandler.Render(_settings.PresenceTrackCnt, metaDataDict, _settings.Seperator));
                trackno  = int.Parse(_layoutHandler.Render(_settings.PresenceTrackNo, metaDataDict, _settings.Seperator));
            }
            catch (Exception)
            {
                // ignored
            }
            _discordPresence.Party.Max  = trackcnt;
            _discordPresence.Party.Size = trackno;

            if (!_settings.UpdatePresenceWhenStopped && (playerGetPlayState == PlayState.Paused || playerGetPlayState == PlayState.Stopped))
            {
                _discordClient.ClearPresence();
            }
            else
            {
                _discordClient.SetPresence(_discordPresence);
            }
        }
예제 #3
0
        private void UpdateDiscordPresence(PlayState playerGetPlayState)
        {
            Debug.WriteLine("DiscordBee: Updating Presence with PlayState {0}...", playerGetPlayState);
            var          metaDataDict     = GenerateMetaDataDictionary();
            RichPresence _discordPresence = new RichPresence
            {
                Assets     = new Assets(),
                Party      = new Party(),
                Timestamps = new Timestamps()
            };

            // 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)
            {
                if (!string.IsNullOrEmpty(input))
                {
                    if (input.Length < 2)
                    {
                        return(input + "\u180E");
                    }
                    if (Encoding.UTF8.GetBytes(input).Length > 128)
                    {
                        byte[] buffer     = new byte[128];
                        char[] inputChars = input.ToCharArray();
                        Encoding.UTF8.GetEncoder().Convert(
                            chars: inputChars,
                            charIndex: 0,
                            charCount: inputChars.Length,
                            bytes: buffer,
                            byteIndex: 0,
                            byteCount: buffer.Length,
                            flush: false,
                            charsUsed: out _,
                            bytesUsed: out int bytesUsed,
                            completed: out _);
                        return(Encoding.UTF8.GetString(buffer, 0, bytesUsed));
                    }
                }
                return(input);
            }

            // Button Functionality
            if (_settings.ShowButton)
            {
                var uri = _layoutHandler.RenderUrl(_settings.ButtonUrl, metaDataDict);
                Debug.WriteLine($"Url: {uri}");

                // Validate the URL again.
                if (ValidationHelpers.ValidateUri(uri))
                {
                    _discordPresence.Buttons = new Button[]
                    {
                        new Button
                        {
                            Label = padString(_settings.ButtonLabel),
                            Url   = uri
                        }
                    };
                }
            }

            void SetImage(string name, bool forceHideSmallImage = false)
            {
                if (_settings.TextOnly)
                {
                    _discordPresence.Assets.LargeImageKey  = null;
                    _discordPresence.Assets.LargeImageText = null;
                    _discordPresence.Assets.SmallImageKey  = null;
                    _discordPresence.Assets.SmallImageText = null;
                }
                else
                {
                    _discordPresence.Assets.LargeImageKey  = AssetManager.ASSET_LOGO;
                    _discordPresence.Assets.LargeImageText = padString(_layoutHandler.Render(_settings.LargeImageText, metaDataDict, _settings.Separator));

                    if (_settings.ShowPlayState && !forceHideSmallImage)
                    {
                        _discordPresence.Assets.SmallImageKey  = padString(name);
                        _discordPresence.Assets.SmallImageText = padString(_layoutHandler.Render(_settings.SmallImageText, metaDataDict, _settings.Separator));
                    }
                    else
                    {
                        _discordPresence.Assets.SmallImageKey  = null;
                        _discordPresence.Assets.SmallImageText = null;
                    }
                }
            }

            _discordPresence.State = padString(_layoutHandler.Render(_settings.PresenceState, metaDataDict, _settings.Separator));

            var t = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1));

            if (_settings.ShowTime)
            {
                if (_settings.ShowRemainingTime)
                {
                    // show remaining time
                    // subtract current track position from total duration for position seeking
                    var totalRemainingDuration = _mbApiInterface.NowPlaying_GetDuration() - _mbApiInterface.Player_GetPosition();
                    _discordPresence.Timestamps.EndUnixMilliseconds = (ulong)(Math.Round(t.TotalSeconds) + Math.Round(totalRemainingDuration / 1000.0));
                }
                else
                {
                    // show elapsed time
                    _discordPresence.Timestamps.StartUnixMilliseconds = (ulong)(Math.Round(t.TotalSeconds) - Math.Round(_mbApiInterface.Player_GetPosition() / 1000.0));
                }
            }

            switch (playerGetPlayState)
            {
            case PlayState.Playing:
                SetImage(AssetManager.ASSET_PLAY, _settings.ShowOnlyNonPlayingState);
                break;

            case PlayState.Stopped:
                SetImage(AssetManager.ASSET_STOP);
                _discordPresence.Timestamps.Start = null;
                _discordPresence.Timestamps.End   = null;
                break;

            case PlayState.Paused:
                SetImage(AssetManager.ASSET_PAUSE);
                _discordPresence.Timestamps.Start = null;
                _discordPresence.Timestamps.End   = null;
                break;

            case PlayState.Undefined:
            case PlayState.Loading:
                break;
            }

            _discordPresence.Details = padString(_layoutHandler.Render(_settings.PresenceDetails, metaDataDict, _settings.Separator));

            var trackcnt = -1;
            var trackno  = -1;

            try
            {
                trackcnt = int.Parse(_layoutHandler.Render(_settings.PresenceTrackCnt, metaDataDict, _settings.Separator));
                trackno  = int.Parse(_layoutHandler.Render(_settings.PresenceTrackNo, metaDataDict, _settings.Separator));
            }
#pragma warning disable RCS1075 // Avoid empty catch clause that catches System.Exception.
            catch (Exception)
#pragma warning restore RCS1075 // Avoid empty catch clause that catches System.Exception.
            {
                // ignored
            }

            if (trackcnt < trackno || trackcnt <= 0 || trackno <= 0)
            {
                _discordPresence.Party = null;
            }
            else
            {
                _discordPresence.Party = new Party
                {
                    ID   = "aaaa",
                    Max  = trackcnt,
                    Size = trackno
                };
            }

            if (!_settings.UpdatePresenceWhenStopped && (playerGetPlayState == PlayState.Paused || playerGetPlayState == PlayState.Stopped))
            {
                Debug.WriteLine("Clearing Presence...", "DiscordBee");
                _discordClient.ClearPresence();
            }
            else
            {
                Debug.WriteLine("Setting new Presence...", "DiscordBee");
                Task.Run(() => _discordClient.SetPresence(_discordPresence, GetAlbumCoverData(_mbApiInterface.NowPlaying_GetArtwork(), metaDataDict)));
            }
        }