2016-10-14 11:33:58 +08:00
|
|
|
|
using System;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2016-10-13 22:15:08 +08:00
|
|
|
|
using System.Security.Cryptography;
|
2016-10-10 21:20:06 +08:00
|
|
|
|
using osu.Framework.Platform;
|
2016-10-08 01:50:34 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
using osu.Game.Beatmaps.Formats;
|
|
|
|
|
using osu.Game.Beatmaps.IO;
|
2016-10-04 23:31:10 +08:00
|
|
|
|
using SQLite;
|
|
|
|
|
|
2016-10-14 11:33:58 +08:00
|
|
|
|
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");
|
|
|
|
|
connection.CreateTable<BeatmapMetadata>();
|
|
|
|
|
connection.CreateTable<BaseDifficulty>();
|
|
|
|
|
connection.CreateTable<BeatmapSet>();
|
|
|
|
|
connection.CreateTable<Beatmap>();
|
|
|
|
|
}
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
public void AddBeatmap(string path)
|
|
|
|
|
{
|
|
|
|
|
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();
|
2016-10-11 02:00:33 +08:00
|
|
|
|
if (connection.Table<BeatmapSet>().Count(b => b.BeatmapSetID == metadata.BeatmapSetID) != 0)
|
2016-10-14 11:33:58 +08:00
|
|
|
|
return; // TODO: Update this beatmap instead
|
|
|
|
|
string[] mapNames = reader.ReadBeatmaps();
|
|
|
|
|
var beatmapSet = new BeatmapSet
|
|
|
|
|
{
|
|
|
|
|
BeatmapSetID = metadata.BeatmapSetID,
|
|
|
|
|
Path = path,
|
|
|
|
|
Hash = hash,
|
|
|
|
|
};
|
|
|
|
|
var maps = new List<Beatmap>();
|
|
|
|
|
foreach (var name in mapNames)
|
|
|
|
|
{
|
|
|
|
|
using (var stream = new StreamReader(reader.ReadFile(name)))
|
2016-10-05 04:29:08 +08:00
|
|
|
|
{
|
2016-10-14 11:33:58 +08:00
|
|
|
|
var decoder = BeatmapDecoder.GetDecoder(stream);
|
|
|
|
|
Beatmap beatmap = new Beatmap();
|
|
|
|
|
decoder.Decode(stream, beatmap);
|
|
|
|
|
maps.Add(beatmap);
|
|
|
|
|
beatmap.BaseDifficultyID = connection.Insert(beatmap.BaseDifficulty);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
beatmapSet.BeatmapMetadataID = connection.Insert(metadata);
|
|
|
|
|
connection.Insert(beatmapSet);
|
|
|
|
|
connection.InsertAll(maps);
|
2016-10-13 22:15:08 +08:00
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
public ArchiveReader GetReader(BeatmapSet beatmapSet)
|
|
|
|
|
{
|
|
|
|
|
return ArchiveReader.GetReader(storage, beatmapSet.Path);
|
2016-10-13 22:29:30 +08:00
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
|
|
|
|
|
/// <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))
|
2016-10-13 22:29:30 +08:00
|
|
|
|
{
|
2016-10-14 11:33:58 +08:00
|
|
|
|
string[] mapNames = reader.ReadBeatmaps();
|
2016-10-13 22:29:30 +08:00
|
|
|
|
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);
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-04 23:31:10 +08:00
|
|
|
|
}
|