diff --git a/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs b/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs index a6367ebfab..13c881d1f8 100644 --- a/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs +++ b/osu.Desktop/Beatmaps/IO/LegacyFilesystemReader.cs @@ -20,8 +20,8 @@ namespace osu.Desktop.Beatmaps.IO private string basePath { get; set; } private string[] beatmaps { get; set; } - private Beatmap firstMap { get; set; } - + private Beatmap firstMap { get; set; } + public LegacyFilesystemReader(string path) { basePath = path; diff --git a/osu.Game/Beatmaps/BeatmapSet.cs b/osu.Game/Beatmaps/BeatmapSet.cs index 5f1be7f9b0..8ba8ef0d53 100644 --- a/osu.Game/Beatmaps/BeatmapSet.cs +++ b/osu.Game/Beatmaps/BeatmapSet.cs @@ -22,5 +22,7 @@ namespace osu.Game.Beatmaps public BeatmapMetadata Metadata { get; set; } [Ignore] public User Creator { get; set; } + public string Hash { get; set; } + public string Path { get; set; } } } diff --git a/osu.Game/Beatmaps/IO/OszArchiveReader.cs b/osu.Game/Beatmaps/IO/OszArchiveReader.cs index b70bee076c..b83cf8ad04 100644 --- a/osu.Game/Beatmaps/IO/OszArchiveReader.cs +++ b/osu.Game/Beatmaps/IO/OszArchiveReader.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Linq; +using System.Security.Cryptography; using Ionic.Zip; using osu.Game.Beatmaps.Formats; @@ -20,7 +21,7 @@ namespace osu.Game.Beatmaps.IO private ZipFile archive { get; set; } private string[] beatmaps { get; set; } - private Beatmap firstMap { get; set; } + private Beatmap firstMap { get; set; } public OszArchiveReader(Stream archiveStream) { diff --git a/osu.Game/Database/BeatmapDatabase.cs b/osu.Game/Database/BeatmapDatabase.cs index 36f8776eb0..dcfc6c91df 100644 --- a/osu.Game/Database/BeatmapDatabase.cs +++ b/osu.Game/Database/BeatmapDatabase.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Security.Cryptography; using osu.Framework.Platform; using osu.Game.Beatmaps; using osu.Game.Beatmaps.Formats; @@ -12,9 +13,11 @@ namespace osu.Game.Database public class BeatmapDatabase { private static SQLiteConnection connection { get; set; } + private BasicStorage storage; public BeatmapDatabase(BasicStorage storage) { + this.storage = storage; if (connection == null) { connection = storage.GetDatabase(@"beatmaps"); @@ -24,17 +27,39 @@ namespace osu.Game.Database connection.CreateTable(); } } - public void AddBeatmap(ArchiveReader input) + public void AddBeatmap(string path) { - var metadata = input.ReadMetadata(); + string hash = null; + ArchiveReader reader; + if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader + { + using (var md5 = MD5.Create()) + using (var input = storage.GetStream(path)) + { + hash = BitConverter.ToString(md5.ComputeHash(input)).Replace("-", "").ToLowerInvariant(); + input.Seek(0, SeekOrigin.Begin); + var outputPath = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash); + using (var output = storage.GetStream(outputPath, FileAccess.Write)) + input.CopyTo(output); + reader = ArchiveReader.GetReader(storage, path = outputPath); + } + } + else + reader = ArchiveReader.GetReader(storage, path); + var metadata = reader.ReadMetadata(); if (connection.Table().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0) return; - string[] mapNames = input.ReadBeatmaps(); - var beatmapSet = new BeatmapSet { BeatmapSetID = metadata.BeatmapSetID }; + string[] mapNames = reader.ReadBeatmaps(); + var beatmapSet = new BeatmapSet + { + BeatmapSetID = metadata.BeatmapSetID, + Path = path, + Hash = hash, + }; var maps = new List(); foreach (var name in mapNames) { - using (var stream = new StreamReader(input.ReadFile(name))) + using (var stream = new StreamReader(reader.ReadFile(name))) { var decoder = BeatmapDecoder.GetDecoder(stream); var beatmap = decoder.Decode(stream); @@ -45,6 +70,13 @@ namespace osu.Game.Database beatmapSet.BeatmapMetadataID = connection.Insert(metadata); connection.Insert(beatmapSet); connection.InsertAll(maps); + } + + /// + /// Given a BeatmapSet pulled from the database, loads the rest of its data from disk. + /// public void PopulateBeatmap(BeatmapSet beatmap) + { + // TODO } } } \ No newline at end of file diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index 9eb32117c5..27e75629d4 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -73,8 +73,7 @@ namespace osu.Game { try { - var reader = ArchiveReader.GetReader(Host.Storage, message.Path); - Beatmaps.AddBeatmap(reader); + Beatmaps.AddBeatmap(message.Path); // TODO: Switch to beatmap list and select the new song } catch (Exception ex)