From fdc6666c711ba9bc745d0d08a5c6667cbad2c986 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 27 Jul 2017 15:50:34 +0900 Subject: [PATCH] Simplify hashing method Also exit the import process before importing files to the file store to avoid incorrect reference count increments. --- osu.Game/Beatmaps/BeatmapStore.cs | 51 +++++++++++++++---------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/osu.Game/Beatmaps/BeatmapStore.cs b/osu.Game/Beatmaps/BeatmapStore.cs index 092846170e..facb68e808 100644 --- a/osu.Game/Beatmaps/BeatmapStore.cs +++ b/osu.Game/Beatmaps/BeatmapStore.cs @@ -246,40 +246,39 @@ namespace osu.Game.Beatmaps /// The imported beatmap, or an existing instance if it is already present. private BeatmapSetInfo importToStorage(ArchiveReader reader) { + // for now, concatenate all .osu files in the set to create a unique hash. + MemoryStream hashable = new MemoryStream(); + foreach (string file in reader.Filenames.Where(f => f.EndsWith(".osu"))) + using (Stream s = reader.GetStream(file)) + s.CopyTo(hashable); + + var hash = hashable.GetMd5Hash(); + + // check if this beatmap has already been imported and exit early if so. + var beatmapSet = beatmaps.QueryAndPopulate().FirstOrDefault(b => b.Hash == hash); + if (beatmapSet != null) + { + Undelete(beatmapSet); + return beatmapSet; + } + + List fileInfos = new List(); + + // import files to store + foreach (string file in reader.Filenames) + using (Stream s = reader.GetStream(file)) + fileInfos.Add(files.Add(s, file)); + BeatmapMetadata metadata; using (var stream = new StreamReader(reader.GetStream(reader.Filenames.First(f => f.EndsWith(".osu"))))) metadata = BeatmapDecoder.GetDecoder(stream).Decode(stream).Metadata; - MemoryStream hashable = new MemoryStream(); - - List fileInfos = new List(); - - foreach (string file in reader.Filenames) - { - using (Stream s = reader.GetStream(file)) - { - fileInfos.Add(files.Add(s, file)); - s.CopyTo(hashable); - } - } - - var overallHash = hashable.GetMd5Hash(); - - var existing = beatmaps.Query().FirstOrDefault(b => b.Hash == overallHash); - - if (existing != null) - { - beatmaps.Populate(existing); - Undelete(existing); - return existing; - } - - var beatmapSet = new BeatmapSetInfo + beatmapSet = new BeatmapSetInfo { OnlineBeatmapSetID = metadata.OnlineBeatmapSetID, Beatmaps = new List(), - Hash = overallHash, + Hash = hash, Files = fileInfos, Metadata = metadata };