Beispiel #1
0
        private BeatmapSetInfo getBeatmapSet(ArchiveReader archiveReader)
        {
            BeatmapMetadata metadata;

            using (var stream = new StreamReader(archiveReader.GetStream(archiveReader.BeatmapFilenames[0])))
                metadata = BeatmapDecoder.GetDecoder(stream).Decode(stream).Metadata;

            string hash;
            string path;

            using (var input = archiveReader.GetUnderlyingStream())
            {
                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)
            {
                GetChildren(existing);

                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;

                                // TODO: this should be done in a better place once we actually need to dynamically update it.
                                beatmap.BeatmapInfo.Ruleset        = rulesets.Query <RulesetInfo>().FirstOrDefault(r => r.ID == beatmap.BeatmapInfo.RulesetID);
                                beatmap.BeatmapInfo.StarDifficulty = rulesets.Query <RulesetInfo>().FirstOrDefault(r => r.ID == beatmap.BeatmapInfo.RulesetID)?.CreateInstance()?.CreateDifficultyCalculator(beatmap).Calculate() ?? 0;

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

            return(beatmapSet);
        }