Refactor the database code

This commit is contained in:
Drew DeVault
2016-10-18 13:35:01 -04:00
parent 406ffdafbc
commit 449f04c07b
17 changed files with 208 additions and 198 deletions

View File

@ -0,0 +1,18 @@
using System;
using SQLite.Net.Attributes;
namespace osu.Game.Database
{
public class BaseDifficulty
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public float DrainRate { get; set; }
public float CircleSize { get; set; }
public float OverallDifficulty { get; set; }
public float ApproachRate { get; set; }
public float SliderMultiplier { get; set; }
public float SliderTickRate { get; set; }
}
}

View File

@ -6,7 +6,8 @@ using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.IO;
using SQLite;
using SQLite.Net;
using SQLiteNetExtensions.Extensions;
namespace osu.Game.Database
{
@ -23,11 +24,12 @@ namespace osu.Game.Database
connection = storage.GetDatabase(@"beatmaps");
connection.CreateTable<BeatmapMetadata>();
connection.CreateTable<BaseDifficulty>();
connection.CreateTable<BeatmapSet>();
connection.CreateTable<Beatmap>();
connection.CreateTable<BeatmapSetInfo>();
connection.CreateTable<BeatmapInfo>();
}
}
public void AddBeatmap(string path)
{
string hash = null;
ArchiveReader reader;
@ -47,54 +49,38 @@ namespace osu.Game.Database
else
reader = ArchiveReader.GetReader(storage, path);
var metadata = reader.ReadMetadata();
var metadata = reader.ReadMetadata();
if (connection.Table<BeatmapSetInfo>().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0)
return; // TODO: Update this beatmap instead
string[] mapNames = reader.ReadBeatmaps();
string[] mapNames = reader.ReadBeatmaps();
var beatmapSet = new BeatmapSetInfo
{
BeatmapSetID = metadata.BeatmapSetID,
Path = path,
Hash = hash,
};
};
var maps = new List<BeatmapInfo>();
foreach (var name in mapNames)
{
using (var stream = new StreamReader(reader.ReadFile(name)))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
var decoder = BeatmapDecoder.GetDecoder(stream);
Beatmap beatmap = new Beatmap();
decoder.Decode(stream, beatmap);
maps.Add(beatmap);
Beatmap beatmap = decoder.Decode(stream);
// TODO: Diff beatmap metadata with set metadata and insert if necessary
beatmap.BeatmapInfo.Metadata = null;
maps.Add(beatmap.BeatmapInfo);
connection.Insert(beatmap.BeatmapInfo.BaseDifficulty);
connection.Insert(beatmap.BeatmapInfo);
connection.UpdateWithChildren(beatmap.BeatmapInfo);
}
}
}
connection.Insert(beatmapSet);
connection.Insert(beatmapSet);
beatmapSet.BeatmapMetadataID = connection.Insert(metadata);
connection.UpdateWithChildren(beatmapSet);
}
}
public ArchiveReader GetReader(BeatmapSetInfo beatmapSet)
{
return ArchiveReader.GetReader(storage, beatmapSet.Path);
}
return ArchiveReader.GetReader(storage, beatmapSet.Path);
}
/// <summary>
/// Given a BeatmapSet pulled from the database, loads the rest of its data from disk.
/// </summary>
public void PopulateBeatmap(BeatmapSet beatmapSet)
{
using (var reader = GetReader(beatmapSet))
{
string[] mapNames = reader.ReadBeatmaps();
foreach (var name in mapNames)
{
using (var stream = new StreamReader(reader.ReadFile(name)))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
Beatmap beatmap = new Beatmap();
decoder.Decode(stream, beatmap);
beatmapSet.Beatmaps.Add(beatmap);
}
}
}

View File

@ -0,0 +1,63 @@
using System;
using System.Linq;
using osu.Game.Beatmaps.Samples;
using osu.Game.GameModes.Play;
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
namespace osu.Game.Database
{
public class BeatmapInfo
{
public BeatmapInfo()
{
BaseDifficulty = new BaseDifficulty();
}
[PrimaryKey]
public int BeatmapID { get; set; }
[NotNull, Indexed]
public int BeatmapSetID { get; set; }
[ForeignKey(typeof(BeatmapMetadata))]
public int BeatmapMetadataID { get; set; }
[ForeignKey(typeof(BaseDifficulty)), NotNull]
public int BaseDifficultyID { get; set; }
[OneToOne]
public BeatmapMetadata Metadata { get; set; }
[OneToOne]
public BaseDifficulty BaseDifficulty { get; set; }
// General
public int AudioLeadIn { get; set; }
public bool Countdown { get; set; }
public SampleSet SampleSet { get; set; }
public float StackLeniency { get; set; }
public bool SpecialStyle { get; set; }
public PlayMode Mode { get; set; }
public bool LetterboxInBreaks { get; set; }
public bool WidescreenStoryboard { get; set; }
// Editor
// This bookmarks stuff is necessary because DB doesn't know how to store int[]
public string StoredBookmarks { get; internal set; }
[Ignore]
public int[] Bookmarks
{
get
{
return StoredBookmarks.Split(',').Select(b => int.Parse(b)).ToArray();
}
set
{
StoredBookmarks = string.Join(",", value);
}
}
public double DistanceSpacing { get; set; }
public int BeatDivisor { get; set; }
public int GridSize { get; set; }
public double TimelineZoom { get; set; }
// Metadata
public string Version { get; set; }
}
}

View File

@ -0,0 +1,26 @@
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Game.GameModes.Play;
using SQLite.Net.Attributes;
namespace osu.Game.Database
{
public class BeatmapMetadata
{
[PrimaryKey]
public int ID { get; set; }
public int BeatmapSetID { get; set; }
public string Title { get; set; }
public string TitleUnicode { get; set; }
public string Artist { get; set; }
public string ArtistUnicode { get; set; }
public string Author { get; set; }
public string Source { get; set; }
public string Tags { get; set; }
public int PreviewTime { get; set; }
public string AudioFile { get; set; }
public string BackgroundFile { get; set; }
}
}

View File

@ -0,0 +1,19 @@
using System;
using SQLite.Net.Attributes;
using SQLiteNetExtensions.Attributes;
namespace osu.Game.Database
{
public class BeatmapSetInfo
{
[PrimaryKey]
public int BeatmapSetID { get; set; }
[OneToOne]
public BeatmapMetadata Metadata { get; set; }
[NotNull, ForeignKey(typeof(BeatmapMetadata))]
public int BeatmapMetadataID { get; set; }
public string Hash { get; set; }
public string Path { get; set; }
}
}