public void HasDuplicates()
        {
            IPlaylist playlist           = new LegacyPlaylist("DuplicateTest", "Has Duplicates", "Test");
            bool      changedEventRaised = false;

            playlist.PlaylistChanged += (s, e) =>
            {
                changedEventRaised = true;
            };
            foreach (var song in DefaultSongs)
            {
                playlist.Add(song);
            }
            int uniqueSongs = DefaultSongs.Length;

            foreach (var song in DefaultSongs)
            {
                playlist.Add(new LegacyPlaylistSong(song));
            }
            changedEventRaised = false;
            Assert.AreEqual(uniqueSongs * 2, playlist.Count);
            playlist.RemoveDuplicates();
            Assert.IsTrue(changedEventRaised);
            Assert.AreEqual(uniqueSongs, playlist.Count);
        }
Esempio n. 2
0
        /// <summary>
        /// Convert a legacy playlist to a v2 playlist struct
        /// </summary>
        /// <param name="legacy">Legacy playlist</param>
        /// <returns></returns>
        public static Playlist ConvertLegacyPlaylist(LegacyPlaylist legacy)
        {
            Playlist playlist = new Playlist
            {
                Title       = legacy.PlaylistTitle,
                Author      = legacy.PlaylistAuthor,
                Description = legacy.PlaylistDescription,

                Cover = Utils.ParseBase64Image(legacy.Image),
                Maps  = new List <Beatmap>()
            };

            foreach (var song in legacy.Songs)
            {
                Beatmap map = new Beatmap();

                if (song.Hash != null)
                {
                    map.Type = "hash";

                    string hash    = song.Hash.ToLower();
                    bool   isValid = Utils.ValidHash(hash);
                    if (!isValid)
                    {
                        throw new InvalidMapHashException(hash);
                    }

                    map.Hash = song.Hash.ToLower();
                }
                else if (song.Key != null)
                {
                    map.Type = "key";

                    string key = Utils.ParseKey(song.Key);
                    map.Key = key ?? throw new InvalidMapKeyException(song.Key);
                }
                else if (song.LevelID != null)
                {
                    map.Type    = "levelID";
                    map.LevelID = song.LevelID;
                }

                playlist.Maps.Add(map);
            }

            return(playlist);
        }
Esempio n. 3
0
        /// <summary>
        /// Convert a legacy playlist to a v2 playlist struct
        /// </summary>
        /// <param name="legacy">Legacy playlist</param>
        /// <param name="flags">Flags used to control conversion logic</param>
        /// <returns></returns>
        public static Playlist ConvertLegacyPlaylist(LegacyPlaylist legacy, ConversionFlags flags = ConversionFlags.Default)
        {
            bool ignoreInvalidHashes = FlagUtils.HasFlag(flags, ConversionFlags.IgnoreInvalidHashes);
            bool ignoreInvalidKeys   = FlagUtils.HasFlag(flags, ConversionFlags.IgnoreInvalidKeys);
            bool ignoreInvalidCover  = FlagUtils.HasFlag(flags, ConversionFlags.IgnoreInvalidCover);

            Playlist playlist = new Playlist
            {
                Title       = legacy.PlaylistTitle,
                Author      = legacy.PlaylistAuthor,
                Description = legacy.PlaylistDescription,

                Maps = new List <Beatmap>()
            };

            try
            {
                byte[] cover    = Utils.ParseBase64Image(legacy.Image);
                string mimeType = MimeType.GetMimeType(cover);

                if (mimeType != MimeType.PNG && mimeType != MimeType.JPG)
                {
                    throw new InvalidCoverException(mimeType);
                }

                playlist.Cover = cover;
            }
            catch (InvalidBase64Exception ex)
            {
                if (ignoreInvalidCover)
                {
                    playlist.Cover = null;
                }
                else
                {
                    throw ex;
                }
            }

            foreach (var song in legacy.Songs)
            {
                Beatmap map = new Beatmap()
                {
                    DateAdded = DateTime.Now,
                };

                if (song.Hash != null)
                {
                    map.Type = BeatmapType.Hash;

                    string hash    = song.Hash.ToLower();
                    bool   isValid = Utils.ValidHash(hash);

                    if (isValid == false)
                    {
                        if (ignoreInvalidHashes)
                        {
                            continue;
                        }
                        else
                        {
                            throw new InvalidMapHashException(hash);
                        }
                    }

                    map.Hash = Utils.StringToByteArray(hash);
                }
                else if (song.Key != null)
                {
                    map.Type = BeatmapType.Key;

                    string key = Utils.ParseKey(song.Key);
                    if (key == null)
                    {
                        if (ignoreInvalidKeys)
                        {
                            continue;
                        }
                        else
                        {
                            throw new InvalidMapKeyException(song.Key);
                        }
                    }

                    map.Key = Convert.ToUInt32(key, 16);
                }
                else if (song.LevelID != null)
                {
                    map.Type    = BeatmapType.LevelID;
                    map.LevelID = song.LevelID;
                }

                playlist.Maps.Add(map);
            }

            return(playlist);
        }