2017-07-27 15:56:41 +08:00
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
2017-07-26 15:28:32 +08:00
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
using System;
|
2017-10-16 11:56:58 +08:00
|
|
|
using System.Collections.Generic;
|
2017-10-05 03:52:12 +08:00
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2017-07-27 15:56:41 +08:00
|
|
|
using osu.Game.Database;
|
2017-07-26 15:28:32 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Beatmaps
|
|
|
|
{
|
|
|
|
/// <summary>
|
2017-07-27 15:56:41 +08:00
|
|
|
/// Handles the storage and retrieval of Beatmaps/BeatmapSets to the database backing
|
2017-07-26 15:28:32 +08:00
|
|
|
/// </summary>
|
2017-07-27 15:56:41 +08:00
|
|
|
public class BeatmapStore : DatabaseBackedStore
|
2017-07-26 15:28:32 +08:00
|
|
|
{
|
|
|
|
public event Action<BeatmapSetInfo> BeatmapSetAdded;
|
2017-07-26 22:13:02 +08:00
|
|
|
public event Action<BeatmapSetInfo> BeatmapSetRemoved;
|
2017-07-26 15:28:32 +08:00
|
|
|
|
2017-08-31 14:49:56 +08:00
|
|
|
public event Action<BeatmapInfo> BeatmapHidden;
|
|
|
|
public event Action<BeatmapInfo> BeatmapRestored;
|
|
|
|
|
2017-10-05 03:52:12 +08:00
|
|
|
public BeatmapStore(OsuDbContext connection)
|
2017-07-27 15:56:41 +08:00
|
|
|
: base(connection)
|
2017-07-26 15:28:32 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-07-27 15:56:41 +08:00
|
|
|
protected override void Prepare(bool reset = false)
|
2017-07-26 15:28:32 +08:00
|
|
|
{
|
2017-07-27 15:56:41 +08:00
|
|
|
if (reset)
|
|
|
|
{
|
2017-10-05 03:52:12 +08:00
|
|
|
// https://stackoverflow.com/a/10450893
|
|
|
|
Connection.Database.ExecuteSqlCommand("DELETE FROM BeatmapMetadata");
|
|
|
|
Connection.Database.ExecuteSqlCommand("DELETE FROM BeatmapDifficulty");
|
|
|
|
Connection.Database.ExecuteSqlCommand("DELETE FROM BeatmapSetInfo");
|
|
|
|
Connection.Database.ExecuteSqlCommand("DELETE FROM BeatmapSetFileInfo");
|
|
|
|
Connection.Database.ExecuteSqlCommand("DELETE FROM BeatmapInfo");
|
2017-07-27 15:56:41 +08:00
|
|
|
}
|
2017-08-01 09:08:05 +08:00
|
|
|
}
|
2017-07-26 15:28:32 +08:00
|
|
|
|
2017-08-01 09:08:05 +08:00
|
|
|
protected override void StartupTasks()
|
|
|
|
{
|
|
|
|
base.StartupTasks();
|
2017-07-27 15:56:41 +08:00
|
|
|
cleanupPendingDeletions();
|
2017-07-26 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2017-07-27 15:56:41 +08:00
|
|
|
/// Add a <see cref="BeatmapSetInfo"/> to the database.
|
2017-07-26 15:28:32 +08:00
|
|
|
/// </summary>
|
2017-07-27 15:56:41 +08:00
|
|
|
/// <param name="beatmapSet">The beatmap to add.</param>
|
|
|
|
public void Add(BeatmapSetInfo beatmapSet)
|
2017-07-26 15:28:32 +08:00
|
|
|
{
|
2017-10-16 21:04:22 +08:00
|
|
|
Connection.BeatmapSetInfo.Add(beatmapSet);
|
2017-10-05 03:52:12 +08:00
|
|
|
Connection.SaveChanges();
|
2017-07-28 16:31:13 +08:00
|
|
|
|
2017-07-27 15:56:41 +08:00
|
|
|
BeatmapSetAdded?.Invoke(beatmapSet);
|
2017-07-26 15:28:32 +08:00
|
|
|
}
|
|
|
|
|
2017-07-26 22:13:02 +08:00
|
|
|
/// <summary>
|
2017-08-31 14:49:56 +08:00
|
|
|
/// Delete a <see cref="BeatmapSetInfo"/> from the database.
|
2017-07-26 22:13:02 +08:00
|
|
|
/// </summary>
|
2017-07-27 15:56:41 +08:00
|
|
|
/// <param name="beatmapSet">The beatmap to delete.</param>
|
|
|
|
/// <returns>Whether the beatmap's <see cref="BeatmapSetInfo.DeletePending"/> was changed.</returns>
|
|
|
|
public bool Delete(BeatmapSetInfo beatmapSet)
|
2017-07-26 22:13:02 +08:00
|
|
|
{
|
2017-07-27 15:56:41 +08:00
|
|
|
if (beatmapSet.DeletePending) return false;
|
2017-07-27 14:34:13 +08:00
|
|
|
|
2017-07-27 15:56:41 +08:00
|
|
|
beatmapSet.DeletePending = true;
|
2017-10-16 21:04:22 +08:00
|
|
|
Connection.BeatmapSetInfo.Update(beatmapSet);
|
2017-10-05 03:52:12 +08:00
|
|
|
Connection.SaveChanges();
|
2017-07-27 14:34:13 +08:00
|
|
|
|
2017-07-27 15:56:41 +08:00
|
|
|
BeatmapSetRemoved?.Invoke(beatmapSet);
|
|
|
|
return true;
|
2017-07-27 14:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2017-07-27 15:56:41 +08:00
|
|
|
/// Restore a previously deleted <see cref="BeatmapSetInfo"/>.
|
2017-07-27 14:34:13 +08:00
|
|
|
/// </summary>
|
2017-07-27 15:56:41 +08:00
|
|
|
/// <param name="beatmapSet">The beatmap to restore.</param>
|
|
|
|
/// <returns>Whether the beatmap's <see cref="BeatmapSetInfo.DeletePending"/> was changed.</returns>
|
|
|
|
public bool Undelete(BeatmapSetInfo beatmapSet)
|
2017-07-27 14:34:13 +08:00
|
|
|
{
|
2017-07-27 15:56:41 +08:00
|
|
|
if (!beatmapSet.DeletePending) return false;
|
2017-07-26 22:13:02 +08:00
|
|
|
|
2017-07-27 15:56:41 +08:00
|
|
|
beatmapSet.DeletePending = false;
|
2017-10-05 03:52:12 +08:00
|
|
|
Connection.BeatmapSetInfo.Update(beatmapSet);
|
2017-10-16 21:04:22 +08:00
|
|
|
Connection.SaveChanges();
|
2017-07-27 14:34:13 +08:00
|
|
|
|
2017-07-27 15:56:41 +08:00
|
|
|
BeatmapSetAdded?.Invoke(beatmapSet);
|
|
|
|
return true;
|
2017-07-26 19:22:02 +08:00
|
|
|
}
|
|
|
|
|
2017-08-31 14:49:56 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Hide a <see cref="BeatmapInfo"/> in the database.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="beatmap">The beatmap to hide.</param>
|
|
|
|
/// <returns>Whether the beatmap's <see cref="BeatmapInfo.Hidden"/> was changed.</returns>
|
|
|
|
public bool Hide(BeatmapInfo beatmap)
|
|
|
|
{
|
|
|
|
if (beatmap.Hidden) return false;
|
|
|
|
|
|
|
|
beatmap.Hidden = true;
|
2017-10-05 03:52:12 +08:00
|
|
|
Connection.BeatmapInfo.Update(beatmap);
|
2017-10-16 21:04:22 +08:00
|
|
|
Connection.SaveChanges();
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
|
|
BeatmapHidden?.Invoke(beatmap);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Restore a previously hidden <see cref="BeatmapInfo"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="beatmap">The beatmap to restore.</param>
|
|
|
|
/// <returns>Whether the beatmap's <see cref="BeatmapInfo.Hidden"/> was changed.</returns>
|
|
|
|
public bool Restore(BeatmapInfo beatmap)
|
|
|
|
{
|
|
|
|
if (!beatmap.Hidden) return false;
|
|
|
|
|
|
|
|
beatmap.Hidden = false;
|
2017-10-05 03:52:12 +08:00
|
|
|
Connection.BeatmapInfo.Update(beatmap);
|
2017-10-16 21:04:22 +08:00
|
|
|
Connection.SaveChanges();
|
2017-08-31 14:49:56 +08:00
|
|
|
|
|
|
|
BeatmapRestored?.Invoke(beatmap);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-27 15:56:41 +08:00
|
|
|
private void cleanupPendingDeletions()
|
2017-07-26 15:28:32 +08:00
|
|
|
{
|
2017-10-05 03:52:12 +08:00
|
|
|
Connection.BeatmapSetInfo.RemoveRange(Connection.BeatmapSetInfo.Where(b => b.DeletePending && !b.Protected));
|
2017-10-16 21:04:22 +08:00
|
|
|
Connection.SaveChanges();
|
2017-10-05 03:52:12 +08:00
|
|
|
}
|
2017-10-16 11:56:58 +08:00
|
|
|
|
2017-10-16 21:04:22 +08:00
|
|
|
public IEnumerable<BeatmapSetInfo> BeatmapSets => Connection.BeatmapSetInfo
|
|
|
|
.Include(s => s.Metadata)
|
|
|
|
.Include(s => s.Beatmaps).ThenInclude(s => s.Ruleset)
|
|
|
|
.Include(s => s.Beatmaps).ThenInclude(b => b.Difficulty)
|
|
|
|
.Include(s => s.Beatmaps).ThenInclude(b => b.Metadata)
|
|
|
|
.Include(s => s.Files).ThenInclude(f => f.FileInfo)
|
|
|
|
.Where(s => !s.DeletePending);
|
|
|
|
|
|
|
|
public IEnumerable<BeatmapInfo> Beatmaps => Connection.BeatmapInfo
|
|
|
|
.Include(b => b.BeatmapSet).ThenInclude(s => s.Metadata)
|
|
|
|
.Include(b => b.Metadata)
|
|
|
|
.Include(b => b.Ruleset)
|
|
|
|
.Include(b => b.Difficulty);
|
2017-07-26 15:28:32 +08:00
|
|
|
}
|
|
|
|
}
|