コード例 #1
0
ファイル: PlaylistConverter.cs プロジェクト: lolPants/Blister
        /// <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);
        }