2016-12-06 17:56:20 +08:00
|
|
|
|
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
2016-10-20 01:13:14 +08:00
|
|
|
|
using System.Collections;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
using System.Collections.Generic;
|
2017-01-23 12:02:03 +08:00
|
|
|
|
using System.Diagnostics;
|
2016-10-05 04:29:08 +08:00
|
|
|
|
using System.IO;
|
2016-10-19 03:38:59 +08:00
|
|
|
|
using System.Linq;
|
2016-10-20 00:51:18 +08:00
|
|
|
|
using System.Linq.Expressions;
|
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-21 17:25:22 +08:00
|
|
|
|
using osu.Game.IPC;
|
2016-10-19 01:35:01 +08:00
|
|
|
|
using SQLite.Net;
|
|
|
|
|
using SQLiteNetExtensions.Extensions;
|
2016-10-04 23:31:10 +08:00
|
|
|
|
|
2016-10-14 11:33:58 +08:00
|
|
|
|
namespace osu.Game.Database
|
|
|
|
|
{
|
|
|
|
|
public class BeatmapDatabase
|
|
|
|
|
{
|
2016-10-27 16:34:43 +08:00
|
|
|
|
private SQLiteConnection connection { get; set; }
|
2016-10-14 11:33:58 +08:00
|
|
|
|
private BasicStorage storage;
|
2016-10-12 01:52:16 +08:00
|
|
|
|
public event Action<BeatmapSetInfo> BeatmapSetAdded;
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
|
|
|
|
private BeatmapImporter ipc;
|
|
|
|
|
|
2016-10-27 16:34:43 +08:00
|
|
|
|
public BeatmapDatabase(BasicStorage storage, BasicGameHost importHost = null)
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2016-10-27 16:34:43 +08:00
|
|
|
|
this.storage = storage;
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
2016-10-27 16:34:43 +08:00
|
|
|
|
if (importHost != null)
|
|
|
|
|
ipc = new BeatmapImporter(importHost, this);
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
2016-10-27 16:08:53 +08:00
|
|
|
|
if (connection == null)
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2017-01-23 12:02:03 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-01-23 17:13:06 +08:00
|
|
|
|
connection = prepareConnection();
|
2017-01-23 12:02:03 +08:00
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2017-01-23 17:13:06 +08:00
|
|
|
|
Console.WriteLine(@"Failed to initialise the beatmap database! Trying again with a clean database...");
|
2017-01-23 12:02:03 +08:00
|
|
|
|
storage.DeleteDatabase(@"beatmaps");
|
2017-01-23 17:13:06 +08:00
|
|
|
|
connection = prepareConnection();
|
2017-01-23 12:02:03 +08:00
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-10-05 04:29:08 +08:00
|
|
|
|
}
|
2016-10-19 01:35:01 +08:00
|
|
|
|
|
2017-01-23 17:13:06 +08:00
|
|
|
|
private SQLiteConnection prepareConnection()
|
|
|
|
|
{
|
|
|
|
|
var conn = storage.GetDatabase(@"beatmaps");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
conn.CreateTable<BeatmapMetadata>();
|
|
|
|
|
conn.CreateTable<BaseDifficulty>();
|
|
|
|
|
conn.CreateTable<BeatmapSetInfo>();
|
|
|
|
|
conn.CreateTable<BeatmapInfo>();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
conn.Close();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return conn;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-21 16:01:46 +08:00
|
|
|
|
public void Reset()
|
|
|
|
|
{
|
|
|
|
|
foreach (var setInfo in Query<BeatmapSetInfo>())
|
2016-10-27 16:08:53 +08:00
|
|
|
|
{
|
|
|
|
|
if (storage.Exists(setInfo.Path))
|
|
|
|
|
storage.Delete(setInfo.Path);
|
|
|
|
|
}
|
2016-10-21 16:01:46 +08:00
|
|
|
|
|
2016-10-27 16:08:53 +08:00
|
|
|
|
connection.DeleteAll<BeatmapMetadata>();
|
|
|
|
|
connection.DeleteAll<BaseDifficulty>();
|
|
|
|
|
connection.DeleteAll<BeatmapSetInfo>();
|
|
|
|
|
connection.DeleteAll<BeatmapInfo>();
|
2016-10-21 16:01:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-21 17:25:22 +08:00
|
|
|
|
public void Import(params string[] paths)
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2016-10-21 17:25:22 +08:00
|
|
|
|
foreach (string p in paths)
|
|
|
|
|
{
|
|
|
|
|
var path = p;
|
|
|
|
|
string hash = null;
|
2016-10-21 16:01:34 +08:00
|
|
|
|
|
2016-10-21 17:25:22 +08:00
|
|
|
|
BeatmapMetadata metadata;
|
2016-10-21 16:01:34 +08:00
|
|
|
|
|
2016-10-21 17:25:22 +08:00
|
|
|
|
using (var reader = ArchiveReader.GetReader(storage, path))
|
|
|
|
|
metadata = reader.ReadMetadata();
|
2016-10-21 16:01:34 +08:00
|
|
|
|
|
2017-01-09 21:05:01 +08:00
|
|
|
|
if (metadata.OnlineBeatmapSetID.HasValue &&
|
|
|
|
|
connection.Table<BeatmapSetInfo>().Count(b => b.OnlineBeatmapSetID == metadata.OnlineBeatmapSetID) != 0)
|
2016-12-21 14:47:56 +08:00
|
|
|
|
return; // TODO: Update this beatmap instead
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
|
|
|
|
if (File.Exists(path)) // Not always the case, i.e. for LegacyFilesystemReader
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2016-10-21 17:25:22 +08:00
|
|
|
|
using (var md5 = MD5.Create())
|
|
|
|
|
using (var input = storage.GetStream(path))
|
|
|
|
|
{
|
|
|
|
|
hash = BitConverter.ToString(md5.ComputeHash(input)).Replace("-", "").ToLowerInvariant();
|
|
|
|
|
input.Seek(0, SeekOrigin.Begin);
|
|
|
|
|
path = Path.Combine(@"beatmaps", hash.Remove(1), hash.Remove(2), hash);
|
|
|
|
|
using (var output = storage.GetStream(path, FileAccess.Write))
|
|
|
|
|
input.CopyTo(output);
|
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-10-21 17:25:22 +08:00
|
|
|
|
var beatmapSet = new BeatmapSetInfo
|
2016-10-05 04:29:08 +08:00
|
|
|
|
{
|
2017-01-09 21:05:01 +08:00
|
|
|
|
OnlineBeatmapSetID = metadata.OnlineBeatmapSetID,
|
2016-10-21 17:25:22 +08:00
|
|
|
|
Beatmaps = new List<BeatmapInfo>(),
|
|
|
|
|
Path = path,
|
|
|
|
|
Hash = hash,
|
|
|
|
|
Metadata = metadata
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using (var reader = ArchiveReader.GetReader(storage, path))
|
|
|
|
|
{
|
|
|
|
|
string[] mapNames = reader.ReadBeatmaps();
|
|
|
|
|
foreach (var name in mapNames)
|
2016-10-19 23:00:11 +08:00
|
|
|
|
{
|
2016-11-05 19:00:14 +08:00
|
|
|
|
using (var stream = new StreamReader(reader.GetStream(name)))
|
2016-10-21 17:25:22 +08:00
|
|
|
|
{
|
|
|
|
|
var decoder = BeatmapDecoder.GetDecoder(stream);
|
|
|
|
|
Beatmap beatmap = decoder.Decode(stream);
|
|
|
|
|
beatmap.BeatmapInfo.Path = name;
|
2016-10-21 16:01:34 +08:00
|
|
|
|
|
2016-10-21 17:25:22 +08:00
|
|
|
|
// TODO: Diff beatmap metadata with set metadata and leave it here if necessary
|
|
|
|
|
beatmap.BeatmapInfo.Metadata = null;
|
2016-10-21 16:01:34 +08:00
|
|
|
|
|
2016-10-21 17:25:22 +08:00
|
|
|
|
beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo);
|
|
|
|
|
}
|
2016-10-19 23:00:11 +08:00
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-10-27 16:08:53 +08:00
|
|
|
|
|
|
|
|
|
Import(new[] { beatmapSet });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Import(IEnumerable<BeatmapSetInfo> beatmapSets)
|
|
|
|
|
{
|
|
|
|
|
connection.BeginTransaction();
|
|
|
|
|
|
|
|
|
|
foreach (var s in beatmapSets)
|
|
|
|
|
{
|
|
|
|
|
connection.InsertWithChildren(s, true);
|
|
|
|
|
BeatmapSetAdded?.Invoke(s);
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-10-27 16:08:53 +08:00
|
|
|
|
|
|
|
|
|
connection.Commit();
|
2016-10-13 22:29:30 +08:00
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
|
2016-10-19 01:35:01 +08:00
|
|
|
|
public ArchiveReader GetReader(BeatmapSetInfo beatmapSet)
|
2016-10-14 11:33:58 +08:00
|
|
|
|
{
|
2016-11-01 22:24:14 +08:00
|
|
|
|
if (string.IsNullOrEmpty(beatmapSet.Path))
|
|
|
|
|
return null;
|
|
|
|
|
|
2016-10-19 01:35:01 +08:00
|
|
|
|
return ArchiveReader.GetReader(storage, beatmapSet.Path);
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-10-28 13:14:45 +08:00
|
|
|
|
|
2016-10-19 03:48:24 +08:00
|
|
|
|
public BeatmapSetInfo GetBeatmapSet(int id)
|
|
|
|
|
{
|
2017-01-09 21:05:01 +08:00
|
|
|
|
return Query<BeatmapSetInfo>().FirstOrDefault(s => s.OnlineBeatmapSetID == id);
|
2016-10-19 03:48:24 +08:00
|
|
|
|
}
|
2016-10-28 13:14:45 +08:00
|
|
|
|
|
2016-10-28 19:24:14 +08:00
|
|
|
|
public WorkingBeatmap GetWorkingBeatmap(BeatmapInfo beatmapInfo, WorkingBeatmap previous = null)
|
2016-10-19 03:38:59 +08:00
|
|
|
|
{
|
2016-12-20 23:56:49 +08:00
|
|
|
|
var beatmapSetInfo = Query<BeatmapSetInfo>().FirstOrDefault(s => s.ID == beatmapInfo.BeatmapSetInfoID);
|
2016-10-28 19:24:14 +08:00
|
|
|
|
|
|
|
|
|
//we need metadata
|
2016-11-23 14:34:32 +08:00
|
|
|
|
GetChildren(beatmapSetInfo);
|
2016-10-28 19:24:14 +08:00
|
|
|
|
|
|
|
|
|
if (beatmapSetInfo == null)
|
2016-12-20 23:56:49 +08:00
|
|
|
|
throw new InvalidOperationException($@"Beatmap set {beatmapInfo.BeatmapSetInfoID} is not in the local database.");
|
2016-10-28 13:14:45 +08:00
|
|
|
|
|
2016-10-28 19:24:14 +08:00
|
|
|
|
if (beatmapInfo.Metadata == null)
|
|
|
|
|
beatmapInfo.Metadata = beatmapSetInfo.Metadata;
|
|
|
|
|
|
2016-11-05 17:16:15 +08:00
|
|
|
|
var working = new WorkingBeatmap(beatmapInfo, beatmapSetInfo, this);
|
2016-10-28 19:24:14 +08:00
|
|
|
|
|
|
|
|
|
previous?.TransferTo(working);
|
2016-10-28 13:14:45 +08:00
|
|
|
|
|
2016-10-28 19:24:14 +08:00
|
|
|
|
return working;
|
2016-10-19 03:38:59 +08:00
|
|
|
|
}
|
2016-10-28 13:14:45 +08:00
|
|
|
|
|
|
|
|
|
public Beatmap GetBeatmap(BeatmapInfo beatmapInfo)
|
|
|
|
|
{
|
2016-10-28 18:55:48 +08:00
|
|
|
|
using (WorkingBeatmap data = GetWorkingBeatmap(beatmapInfo))
|
2016-10-28 13:14:45 +08:00
|
|
|
|
return data.Beatmap;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-19 03:38:59 +08:00
|
|
|
|
public TableQuery<T> Query<T>() where T : class
|
|
|
|
|
{
|
2016-10-27 16:08:53 +08:00
|
|
|
|
return connection.Table<T>();
|
2016-10-19 03:38:59 +08:00
|
|
|
|
}
|
2016-10-28 13:14:45 +08:00
|
|
|
|
|
2016-10-20 00:51:18 +08:00
|
|
|
|
public T GetWithChildren<T>(object id) where T : class
|
|
|
|
|
{
|
2016-10-27 16:08:53 +08:00
|
|
|
|
return connection.GetWithChildren<T>(id);
|
2016-10-20 00:51:18 +08:00
|
|
|
|
}
|
2016-10-28 13:14:45 +08:00
|
|
|
|
|
2016-11-22 16:21:42 +08:00
|
|
|
|
public List<T> GetAllWithChildren<T>(Expression<Func<T, bool>> filter = null, bool recursive = true)
|
|
|
|
|
where T : class
|
2016-10-20 00:51:18 +08:00
|
|
|
|
{
|
2016-11-22 16:21:42 +08:00
|
|
|
|
return connection.GetAllWithChildren(filter, recursive);
|
2016-10-20 00:51:18 +08:00
|
|
|
|
}
|
2016-10-28 13:14:45 +08:00
|
|
|
|
|
2016-11-23 14:34:32 +08:00
|
|
|
|
public T GetChildren<T>(T item, bool recursive = false)
|
2016-10-21 03:42:09 +08:00
|
|
|
|
{
|
2016-10-21 16:02:00 +08:00
|
|
|
|
if (item == null) return default(T);
|
|
|
|
|
|
2016-10-27 16:08:53 +08:00
|
|
|
|
connection.GetChildren(item, recursive);
|
2016-10-21 16:02:00 +08:00
|
|
|
|
return item;
|
2016-10-21 03:42:09 +08:00
|
|
|
|
}
|
2016-10-19 03:38:59 +08:00
|
|
|
|
|
|
|
|
|
readonly Type[] validTypes = new[]
|
|
|
|
|
{
|
|
|
|
|
typeof(BeatmapSetInfo),
|
|
|
|
|
typeof(BeatmapInfo),
|
|
|
|
|
typeof(BeatmapMetadata),
|
|
|
|
|
typeof(BaseDifficulty),
|
|
|
|
|
};
|
2016-10-21 17:25:22 +08:00
|
|
|
|
|
2016-10-19 03:38:59 +08:00
|
|
|
|
public void Update<T>(T record, bool cascade = true) where T : class
|
|
|
|
|
{
|
2016-11-22 16:21:42 +08:00
|
|
|
|
if (validTypes.All(t => t != typeof(T)))
|
2016-10-19 03:38:59 +08:00
|
|
|
|
throw new ArgumentException(nameof(T), "Must be a type managed by BeatmapDatabase");
|
|
|
|
|
if (cascade)
|
2016-10-27 16:08:53 +08:00
|
|
|
|
connection.UpdateWithChildren(record);
|
2016-10-19 03:38:59 +08:00
|
|
|
|
else
|
2016-10-27 16:08:53 +08:00
|
|
|
|
connection.Update(record);
|
2016-10-19 03:38:59 +08:00
|
|
|
|
}
|
2016-10-14 11:33:58 +08:00
|
|
|
|
}
|
2016-10-19 23:00:11 +08:00
|
|
|
|
}
|