osu/osu.Game/Database/BeatmapDatabase.cs

50 lines
1.8 KiB
C#
Raw Normal View History

2016-10-04 15:31:10 +00:00
using System;
using System.Collections.Generic;
using System.IO;
2016-10-10 13:20:06 +00:00
using osu.Framework.Platform;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Formats;
using osu.Game.Beatmaps.IO;
2016-10-04 15:31:10 +00:00
using SQLite;
namespace osu.Game.Database
{
public class BeatmapDatabase
{
2016-10-10 18:00:33 +00:00
private static SQLiteConnection connection { get; set; }
2016-10-04 15:31:10 +00:00
public BeatmapDatabase(BasicStorage storage)
2016-10-04 15:31:10 +00:00
{
2016-10-10 18:00:33 +00:00
if (connection == null)
2016-10-04 15:31:10 +00:00
{
2016-10-10 18:00:33 +00:00
connection = storage.GetDatabase(@"beatmaps");
connection.CreateTable<BeatmapMetadata>();
connection.CreateTable<BaseDifficulty>();
connection.CreateTable<BeatmapSet>();
connection.CreateTable<Beatmap>();
2016-10-04 15:31:10 +00:00
}
}
public void AddBeatmap(ArchiveReader input)
{
var metadata = input.ReadMetadata();
2016-10-10 18:00:33 +00:00
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-07 17:57:06 +00:00
maps.Add(beatmap);
2016-10-10 18:00:33 +00:00
beatmap.BaseDifficultyID = connection.Insert(beatmap.BaseDifficulty);
}
}
2016-10-10 18:00:33 +00:00
beatmapSet.BeatmapMetadataID = connection.Insert(metadata);
connection.Insert(beatmapSet);
connection.InsertAll(maps);
2016-10-04 15:31:10 +00:00
}
}
}