Exemplo n.º 1
0
        private void LoadPlaylist(string name)
        {
            if (m_guild == null || string.IsNullOrWhiteSpace(name))
            {
                Logging.LogError(LogType.Bot, $"Server or playlist name is empty ignoring playlist.");
                m_isValid = false;
                return;
            }

            string fileLocation = Path.Combine(Utilities.DataFolder, m_guild.Id.ToString(), c_playlistFolder, $"{name}.xml");

            if (!File.Exists(fileLocation))
            {
                CreateNew(name, fileLocation);
                return;
            }

            XDocument doc     = XDocument.Load(fileLocation);
            XElement  root    = doc.Root;
            XElement  info    = root.Element("Info");
            string    xmlName = string.Empty;

            if (info.TryGetAttribute("Name", out xmlName))
            {
                if (xmlName != name)
                {
                    Logging.LogError(LogType.Bot, $"Xml document {fileLocation} has info Name {xmlName} but Name {name} was used for loading playlist in server {m_guild.Name}.");
                    m_isValid = false;
                    return;
                }

                m_name = xmlName;

                bool repeat = false;
                if (info.TryGetAttribute("Repeat", out repeat))
                {
                    m_repeat = repeat;
                }
                bool shuffle = false;
                if (info.TryGetAttribute("Shuffle", out shuffle))
                {
                    m_suffleOnLoad = shuffle;
                }
                float volume = 1.0f;
                if (info.TryGetAttribute("Volume", out volume))
                {
                    m_volume          = Utilities.Clamp(0.0f, 1.0f, volume);
                    SongStream.Volume = m_volume;
                }

                XElement songs = root.Element("Songs");
                if (songs.HasElements)
                {
                    foreach (var song in songs.Elements("Song"))
                    {
                        SongInfo songInfo = GetSongInfoFromXml(song);
                        if (!string.IsNullOrEmpty(songInfo.Title))
                        {
                            Song addSong = new Song(songInfo);
                            addSong.SetPlaylist(this);
                            m_playlistCollection.Add(addSong);
                            m_activePlaylist.AddLast(addSong);
                        }
                    }
                }

                if (m_suffleOnLoad)
                {
                    Shuffle();
                }

                m_playlistFile = fileLocation;

                UpdatePlaylist();
                m_isValid = true;
                return;
            }
        }
Exemplo n.º 2
0
        public async Task Play(IAudioClient client)
        {
            if (Paused)
            {
                return;
            }

            if (client != null && client.ConnectionState == ConnectionState.Connected && m_activePlaylist.Count != 0)
            {
                lock (m_mutex)
                {
                    m_currentSong = m_activePlaylist.First.Value;
                    m_activePlaylist.RemoveFirst();
                    if (m_lastSong != null)
                    {
                        m_currentQueueTime = m_currentQueueTime.Subtract(m_lastSong.Duration);
                    }

                    Logging.LogInfo(LogType.Bot, $"Changing song to {m_currentSong.Name} on server {GuildName}");
                }

                if (!await Utilities.CheckUriAsync(m_currentSong.Info.Uri))
                {
                    Logging.LogInfo(LogType.Bot, $"Changing song to {m_currentSong.Name} has an dead link re-resolving song on server {GuildName}");
                    m_currentSong = (await MediaPlayer.ResolveSong(m_currentSong.Info.Query)).FirstOrDefault();
                    if (m_currentSong != null)
                    {
                        m_currentSong.SetPlaylist(this);
                        UpdateSong(m_currentSong);
                    }
                    else
                    {
                        Logging.LogInfo(LogType.Bot, $"Song {m_currentSong.Name} failed to re-resolving dead link on server {GuildName}");
                        RemoveSong(m_currentSong.Name);
                        m_currentSong = m_lastSong;
                        return;
                    }
                }

                try
                {
                    if (HasSong(m_currentSong.Info))
                    {
                        await m_currentSong.Play(client, m_songCancellationSource.Token);

                        RepeatSong();

                        m_lastSong    = m_currentSong;
                        m_currentSong = null;
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogException(LogType.Bot, ex, $"Exception occured in playlist {Name} for server {GuildName}.");
                }
                finally
                {
                    if (!m_songCancellationSource.Token.IsCancellationRequested)
                    {
                        m_songCancellationSource.Cancel();
                    }

                    m_songCancellationSource = new CancellationTokenSource();
                }
            }
            else if (m_activePlaylist.Count == 0 && m_currentSong == null && m_currentQueueTime != new TimeSpan(0))
            {
                m_currentQueueTime = new TimeSpan(0);
            }

            await Task.Delay(500).ConfigureAwait(false);
        }