Correct and simplify BeatmapStore and BeatmapManager

This commit is contained in:
Dean Herbert
2017-10-16 22:04:22 +09:00
parent eff1c20e38
commit 81476ebe75
2 changed files with 57 additions and 66 deletions

View File

@ -5,7 +5,6 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Ionic.Zip;
using osu.Framework.Audio.Track;
@ -179,7 +178,8 @@ namespace osu.Game.Beatmaps
// If we have an ID then we already exist in the database.
if (beatmapSetInfo.ID != 0) return;
beatmaps.Add(beatmapSetInfo);
lock (beatmaps)
beatmaps.Add(beatmapSetInfo);
}
/// <summary>
@ -257,7 +257,8 @@ namespace osu.Game.Beatmaps
/// <param name="beatmapSet">The beatmap set to delete.</param>
public void Delete(BeatmapSetInfo beatmapSet)
{
if (!beatmaps.Delete(beatmapSet)) return;
lock (beatmaps)
if (!beatmaps.Delete(beatmapSet)) return;
if (!beatmapSet.Protected)
files.Dereference(beatmapSet.Files.Select(f => f.FileInfo).ToArray());
@ -267,13 +268,21 @@ namespace osu.Game.Beatmaps
/// Delete a beatmap difficulty.
/// </summary>
/// <param name="beatmap">The beatmap difficulty to hide.</param>
public void Hide(BeatmapInfo beatmap) => beatmaps.Hide(beatmap);
public void Hide(BeatmapInfo beatmap)
{
lock (beatmaps)
beatmaps.Hide(beatmap);
}
/// <summary>
/// Restore a beatmap difficulty.
/// </summary>
/// <param name="beatmap">The beatmap difficulty to restore.</param>
public void Restore(BeatmapInfo beatmap) => beatmaps.Restore(beatmap);
public void Restore(BeatmapInfo beatmap)
{
lock (beatmaps)
beatmaps.Restore(beatmap);
}
/// <summary>
/// Returns a <see cref="BeatmapSetInfo"/> to a usable state if it has previously been deleted but not yet purged.
@ -282,7 +291,8 @@ namespace osu.Game.Beatmaps
/// <param name="beatmapSet">The beatmap to restore.</param>
public void Undelete(BeatmapSetInfo beatmapSet)
{
if (!beatmaps.Undelete(beatmapSet)) return;
lock (beatmaps)
if (!beatmaps.Undelete(beatmapSet)) return;
if (!beatmapSet.Protected)
files.Reference(beatmapSet.Files.Select(f => f.FileInfo).ToArray());
@ -329,11 +339,7 @@ namespace osu.Game.Beatmaps
public BeatmapSetInfo QueryBeatmapSet(Func<BeatmapSetInfo, bool> query)
{
lock (beatmaps)
{
BeatmapSetInfo set = beatmaps.QueryBeatmapSet(query);
return set;
}
return beatmaps.BeatmapSets.FirstOrDefault(query);
}
/// <summary>
@ -348,9 +354,10 @@ namespace osu.Game.Beatmaps
/// </summary>
/// <param name="query">The query.</param>
/// <returns>Results from the provided query.</returns>
public List<BeatmapSetInfo> QueryBeatmapSets(Expression<Func<BeatmapSetInfo, bool>> query)
public List<BeatmapSetInfo> QueryBeatmapSets(Func<BeatmapSetInfo, bool> query)
{
return beatmaps.QueryBeatmapSets(query);
lock (beatmaps)
return beatmaps.BeatmapSets.Where(query).ToList();
}
/// <summary>
@ -360,9 +367,8 @@ namespace osu.Game.Beatmaps
/// <returns>The first result for the provided query, or null if no results were found.</returns>
public BeatmapInfo QueryBeatmap(Func<BeatmapInfo, bool> query)
{
BeatmapInfo set = beatmaps.QueryBeatmap(query);
return set;
lock (beatmaps)
return beatmaps.Beatmaps.FirstOrDefault(query);
}
/// <summary>
@ -370,9 +376,10 @@ namespace osu.Game.Beatmaps
/// </summary>
/// <param name="query">The query.</param>
/// <returns>Results from the provided query.</returns>
public List<BeatmapInfo> QueryBeatmaps(Expression<Func<BeatmapInfo, bool>> query)
public List<BeatmapInfo> QueryBeatmaps(Func<BeatmapInfo, bool> query)
{
lock (beatmaps) return beatmaps.QueryBeatmaps(query);
lock (beatmaps)
return beatmaps.Beatmaps.Where(query).ToList();
}
/// <summary>
@ -411,7 +418,7 @@ namespace osu.Game.Beatmaps
// check if this beatmap has already been imported and exit early if so.
BeatmapSetInfo beatmapSet;
lock (beatmaps)
beatmapSet = beatmaps.QueryBeatmapSet(b => b.Hash == hash);
beatmapSet = beatmaps.BeatmapSets.FirstOrDefault(b => b.Hash == hash);
if (beatmapSet != null)
{
@ -494,9 +501,7 @@ namespace osu.Game.Beatmaps
public List<BeatmapSetInfo> GetAllUsableBeatmapSets()
{
lock (beatmaps)
{
return beatmaps.QueryBeatmapSets(b => !b.DeletePending);
}
return beatmaps.BeatmapSets.ToList();
}
protected class BeatmapManagerWorkingBeatmap : WorkingBeatmap
@ -531,7 +536,10 @@ namespace osu.Game.Beatmaps
return beatmap;
}
catch { return null; }
catch
{
return null;
}
}
private string getPathForFile(string filename) => BeatmapSetInfo.Files.First(f => string.Equals(f.Filename, filename, StringComparison.InvariantCultureIgnoreCase)).FileInfo.StoragePath;
@ -545,7 +553,10 @@ namespace osu.Game.Beatmaps
{
return new TextureStore(new RawTextureLoaderStore(store), false).Get(getPathForFile(Metadata.BackgroundFile));
}
catch { return null; }
catch
{
return null;
}
}
protected override Track GetTrack()
@ -555,7 +566,10 @@ namespace osu.Game.Beatmaps
var trackData = store.GetStream(getPathForFile(Metadata.AudioFile));
return trackData == null ? null : new TrackBass(trackData);
}
catch { return new TrackVirtual(); }
catch
{
return new TrackVirtual();
}
}
protected override Waveform GetWaveform() => new Waveform(store.GetStream(getPathForFile(Metadata.AudioFile)));

View File

@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using osu.Game.Database;
@ -51,7 +50,7 @@ namespace osu.Game.Beatmaps
/// <param name="beatmapSet">The beatmap to add.</param>
public void Add(BeatmapSetInfo beatmapSet)
{
Connection.BeatmapSetInfo.Update(beatmapSet);
Connection.BeatmapSetInfo.Add(beatmapSet);
Connection.SaveChanges();
BeatmapSetAdded?.Invoke(beatmapSet);
@ -67,7 +66,7 @@ namespace osu.Game.Beatmaps
if (beatmapSet.DeletePending) return false;
beatmapSet.DeletePending = true;
Connection.BeatmapSetInfo.Remove(beatmapSet);
Connection.BeatmapSetInfo.Update(beatmapSet);
Connection.SaveChanges();
BeatmapSetRemoved?.Invoke(beatmapSet);
@ -85,6 +84,7 @@ namespace osu.Game.Beatmaps
beatmapSet.DeletePending = false;
Connection.BeatmapSetInfo.Update(beatmapSet);
Connection.SaveChanges();
BeatmapSetAdded?.Invoke(beatmapSet);
return true;
@ -101,6 +101,7 @@ namespace osu.Game.Beatmaps
beatmap.Hidden = true;
Connection.BeatmapInfo.Update(beatmap);
Connection.SaveChanges();
BeatmapHidden?.Invoke(beatmap);
return true;
@ -117,6 +118,7 @@ namespace osu.Game.Beatmaps
beatmap.Hidden = false;
Connection.BeatmapInfo.Update(beatmap);
Connection.SaveChanges();
BeatmapRestored?.Invoke(beatmap);
return true;
@ -125,46 +127,21 @@ namespace osu.Game.Beatmaps
private void cleanupPendingDeletions()
{
Connection.BeatmapSetInfo.RemoveRange(Connection.BeatmapSetInfo.Where(b => b.DeletePending && !b.Protected));
Connection.SaveChanges();
}
public BeatmapSetInfo QueryBeatmapSet(Func<BeatmapSetInfo, bool> query)
{
return Connection.BeatmapSetInfo
.Include(b => b.Metadata)
.Include(b => b.Beatmaps).ThenInclude(b => b.Ruleset)
.Include(b => b.Beatmaps).ThenInclude(b => b.Difficulty)
.Include(b => b.Files).ThenInclude(f => f.FileInfo)
.FirstOrDefault(query);
}
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 List<BeatmapSetInfo> QueryBeatmapSets(Expression<Func<BeatmapSetInfo, bool>> query)
{
return Connection.BeatmapSetInfo
.Include(b => b.Metadata)
.Include(b => b.Beatmaps).ThenInclude(b => b.Ruleset)
.Include(b => b.Beatmaps).ThenInclude(b => b.Difficulty)
.Include(b => b.Files).ThenInclude(f => f.FileInfo)
.Where(query).ToList();
}
public BeatmapInfo QueryBeatmap(Func<BeatmapInfo, bool> query)
{
return Connection.BeatmapInfo
.Include(b => b.BeatmapSet)
.Include(b => b.Metadata)
.Include(b => b.Ruleset)
.Include(b => b.Difficulty)
.FirstOrDefault(query);
}
public List<BeatmapInfo> QueryBeatmaps(Expression<Func<BeatmapInfo, bool>> query)
{
return Connection.BeatmapInfo
.Include(b => b.BeatmapSet)
.Include(b => b.Metadata)
.Include(b => b.Ruleset)
.Include(b => b.Difficulty)
.Where(query).ToList();
}
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);
}
}