Ejemplo n.º 1
0
        /// <summary>
        /// Duplicates content from <paramref name="path"/> to storage and returns a representing <see cref="BeatmapSetInfo"/>.
        /// </summary>
        /// <param name="path">Content location</param>
        /// <returns><see cref="BeatmapSetInfo"/></returns>
        private BeatmapSetInfo getBeatmapSet(string path)
        {
            string hash = null;

            BeatmapMetadata metadata;

            using (var reader = ArchiveReader.GetReader(storage, path))
                metadata = reader.ReadMetadata();

            if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader
            {
                using (var input = storage.GetStream(path))
                {
                    hash = input.GetMd5Hash();
                    input.Seek(0, SeekOrigin.Begin);
                    path = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash);
                    if (!storage.Exists(path))
                    {
                        using (var output = storage.GetStream(path, FileAccess.Write))
                            input.CopyTo(output);
                    }
                }
            }

            var existing = connection.Table <BeatmapSetInfo>().FirstOrDefault(b => b.Hash == hash);

            if (existing != null)
            {
                if (existing.DeletePending)
                {
                    existing.DeletePending = false;
                    Update(existing, false);
                    BeatmapSetAdded?.Invoke(existing);
                }

                return(existing);
            }

            var beatmapSet = new BeatmapSetInfo
            {
                OnlineBeatmapSetID = metadata.OnlineBeatmapSetID,
                Beatmaps           = new List <BeatmapInfo>(),
                Path     = path,
                Hash     = hash,
                Metadata = metadata
            };

            using (var archive = ArchiveReader.GetReader(storage, path))
            {
                string[] mapNames = archive.BeatmapFilenames;
                foreach (var name in mapNames)
                {
                    using (var raw = archive.GetStream(name))
                        using (var ms = new MemoryStream()) //we need a memory stream so we can seek and shit
                            using (var sr = new StreamReader(ms))
                            {
                                raw.CopyTo(ms);
                                ms.Position = 0;

                                var     decoder = BeatmapDecoder.GetDecoder(sr);
                                Beatmap beatmap = decoder.Decode(sr);

                                beatmap.BeatmapInfo.Path = name;
                                beatmap.BeatmapInfo.Hash = ms.GetMd5Hash();

                                // TODO: Diff beatmap metadata with set metadata and leave it here if necessary
                                beatmap.BeatmapInfo.Metadata = null;

                                beatmap.BeatmapInfo.StarDifficulty = beatmap.CalculateStarDifficulty();

                                beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo);
                            }
                }
                beatmapSet.StoryboardFile = archive.StoryboardFilename;
            }

            return(beatmapSet);
        }