1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-13 20:47:26 +08:00
osu-lazer/osu.Game/Database/BeatmapDatabase.cs

50 lines
1.8 KiB
C#
Raw Normal View History

2016-10-04 23:31:10 +08:00
using System;
using System.Collections.Generic;
using System.IO;
2016-10-10 21:20:06 +08:00
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.IO;
2016-10-04 23:31:10 +08:00
using SQLite;
namespace osu.Game.Database
{
public class BeatmapDatabase
{
private static SQLiteConnection Connection { get; set; }
public BeatmapDatabase(BasicStorage storage)
2016-10-04 23:31:10 +08:00
{
if (Connection == null)
{
2016-10-05 02:23:34 +08:00
Connection = storage.GetDatabase(@"beatmaps");
2016-10-04 23:31:10 +08:00
Connection.CreateTable<BeatmapMetadata>();
2016-10-08 01:57:06 +08:00
Connection.CreateTable<BaseDifficulty>();
2016-10-04 23:31:10 +08:00
Connection.CreateTable<BeatmapSet>();
Connection.CreateTable<Beatmap>();
}
}
public void AddBeatmap(ArchiveReader input)
{
var metadata = input.ReadMetadata();
if (Connection.Table<BeatmapSet>().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0)
return;
string[] mapNames = input.ReadBeatmaps();
var beatmapSet = new BeatmapSet { BeatmapSetID = metadata.BeatmapSetID };
var maps = new List<Beatmap>();
foreach (var name in mapNames)
{
using (var stream = new StreamReader(input.ReadFile(name)))
{
var decoder = BeatmapDecoder.GetDecoder(stream);
var beatmap = decoder.Decode(stream);
2016-10-08 01:57:06 +08:00
maps.Add(beatmap);
beatmap.BaseDifficultyID = Connection.Insert(beatmap.BaseDifficulty);
}
}
2016-10-08 01:57:06 +08:00
beatmapSet.BeatmapMetadataID = Connection.Insert(metadata);
Connection.Insert(beatmapSet);
Connection.InsertAll(maps);
2016-10-04 23:31:10 +08:00
}
}
}