diff --git a/osu.Game/Database/BeatmapDatabase.cs b/osu.Game/Database/BeatmapDatabase.cs index 324e8a6a24..45f835c514 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.Linq; using System.Security.Cryptography; using osu.Framework.Platform; using osu.Game.Beatmaps; @@ -65,6 +66,7 @@ namespace osu.Game.Database { var decoder = BeatmapDecoder.GetDecoder(stream); Beatmap beatmap = decoder.Decode(stream); + beatmap.BeatmapInfo.Path = name; // TODO: Diff beatmap metadata with set metadata and insert if necessary beatmap.BeatmapInfo.Metadata = null; maps.Add(beatmap.BeatmapInfo); @@ -82,5 +84,43 @@ namespace osu.Game.Database { return ArchiveReader.GetReader(storage, beatmapSet.Path); } + + public Beatmap GetBeatmap(BeatmapInfo beatmapInfo) + { + var beatmapSet = Query() + .Where(s => s.BeatmapSetID == beatmapInfo.BeatmapSetID).FirstOrDefault(); + if (beatmapSet == null) + throw new InvalidOperationException( + $@"Beatmap set {beatmapInfo.BeatmapSetID} is not in the local database."); + using (var reader = GetReader(beatmapSet)) + using (var stream = new StreamReader(reader.ReadFile(beatmapInfo.Path))) + { + var decoder = BeatmapDecoder.GetDecoder(stream); + return decoder.Decode(stream); + } + } + + public TableQuery Query() where T : class + { + return connection.Table(); + } + + readonly Type[] validTypes = new[] + { + typeof(BeatmapSetInfo), + typeof(BeatmapInfo), + typeof(BeatmapMetadata), + typeof(BaseDifficulty), + }; + + public void Update(T record, bool cascade = true) where T : class + { + if (!validTypes.Any(t => t == typeof(T))) + throw new ArgumentException(nameof(T), "Must be a type managed by BeatmapDatabase"); + if (cascade) + connection.UpdateWithChildren(record); + else + connection.Update(record); + } } } \ No newline at end of file diff --git a/osu.Game/Database/BeatmapInfo.cs b/osu.Game/Database/BeatmapInfo.cs index c03130baa0..4487485def 100644 --- a/osu.Game/Database/BeatmapInfo.cs +++ b/osu.Game/Database/BeatmapInfo.cs @@ -2,7 +2,7 @@ using System.Linq; using osu.Game.Beatmaps.Samples; using osu.Game.GameModes.Play; -using SQLite.Net.Attributes; +using SQLite.Net.Attributes; using SQLiteNetExtensions.Attributes; namespace osu.Game.Database @@ -27,6 +27,8 @@ namespace osu.Game.Database [OneToOne] public BaseDifficulty BaseDifficulty { get; set; } + public string Path { get; set; } + // General public int AudioLeadIn { get; set; } public bool Countdown { get; set; }