Move responsibility for selecting paths to model managers

This commit is contained in:
HoLLy 2019-06-21 17:32:47 +02:00
parent f1f03dd541
commit 802da225d4
4 changed files with 16 additions and 20 deletions

View File

@ -62,8 +62,6 @@ namespace osu.Game.Beatmaps
protected override string ImportFromStablePath => "Songs"; protected override string ImportFromStablePath => "Songs";
protected override bool StableDirectoryBased => true;
private readonly RulesetStore rulesets; private readonly RulesetStore rulesets;
private readonly BeatmapStore beatmaps; private readonly BeatmapStore beatmaps;
@ -96,6 +94,8 @@ namespace osu.Game.Beatmaps
updateQueue = new BeatmapUpdateQueue(api); updateQueue = new BeatmapUpdateQueue(api);
} }
protected override IEnumerable<string> GetStableImportPaths() => GetStableStorage().GetDirectories(ImportFromStablePath);
protected override Task Populate(BeatmapSetInfo beatmapSet, ArchiveReader archive, CancellationToken cancellationToken = default) protected override Task Populate(BeatmapSetInfo beatmapSet, ArchiveReader archive, CancellationToken cancellationToken = default)
{ {
if (archive != null) if (archive != null)

View File

@ -146,12 +146,11 @@ namespace osu.Game.Database
notification.Progress = 0; notification.Progress = 0;
notification.Text = "Import is initialising..."; notification.Text = "Import is initialising...";
string[] filteredPaths = paths.Where(canImportPath).ToArray();
int current = 0; int current = 0;
var imported = new List<TModel>(); var imported = new List<TModel>();
await Task.WhenAll(filteredPaths.Select(async path => await Task.WhenAll(paths.Select(async path =>
{ {
notification.CancellationToken.ThrowIfCancellationRequested(); notification.CancellationToken.ThrowIfCancellationRequested();
@ -164,8 +163,8 @@ namespace osu.Game.Database
imported.Add(model); imported.Add(model);
current++; current++;
notification.Text = $"Imported {current} of {filteredPaths.Length} {HumanisedModelName}s"; notification.Text = $"Imported {current} of {paths.Length} {HumanisedModelName}s";
notification.Progress = (float)current / filteredPaths.Length; notification.Progress = (float)current / paths.Length;
} }
} }
catch (TaskCanceledException) catch (TaskCanceledException)
@ -201,8 +200,6 @@ namespace osu.Game.Database
notification.State = ProgressNotificationState.Completed; notification.State = ProgressNotificationState.Completed;
} }
bool canImportPath(string path) => StableDirectoryBased || HandledExtensions.Any(ext => Path.GetExtension(path)?.ToLowerInvariant() == ext);
} }
/// <summary> /// <summary>
@ -537,7 +534,7 @@ namespace osu.Game.Database
/// <summary> /// <summary>
/// Set a storage with access to an osu-stable install for import purposes. /// Set a storage with access to an osu-stable install for import purposes.
/// </summary> /// </summary>
public Func<Storage> GetStableStorage { private get; set; } public Func<Storage> GetStableStorage { protected get; set; }
/// <summary> /// <summary>
/// Denotes whether an osu-stable installation is present to perform automated imports from. /// Denotes whether an osu-stable installation is present to perform automated imports from.
@ -550,9 +547,9 @@ namespace osu.Game.Database
protected virtual string ImportFromStablePath => null; protected virtual string ImportFromStablePath => null;
/// <summary> /// <summary>
/// Does stable import look for directories rather than files /// Selects paths to import from.
/// </summary> /// </summary>
protected abstract bool StableDirectoryBased { get; } protected abstract IEnumerable<string> GetStableImportPaths();
/// <summary> /// <summary>
/// This is a temporary method and will likely be replaced by a full-fledged (and more correctly placed) migration process in the future. /// This is a temporary method and will likely be replaced by a full-fledged (and more correctly placed) migration process in the future.
@ -574,11 +571,7 @@ namespace osu.Game.Database
return Task.CompletedTask; return Task.CompletedTask;
} }
return Task.Run(async () => return Task.Run(async () => await Import(GetStableImportPaths().Select(f => stable.GetFullPath(f)).ToArray()));
{
var paths = StableDirectoryBased ? stable.GetDirectories(ImportFromStablePath) : stable.GetFiles(ImportFromStablePath);
await Import(paths.Select(f => stable.GetFullPath(f)).ToArray());
});
} }
#endregion #endregion

View File

@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@ -24,8 +25,6 @@ namespace osu.Game.Scoring
protected override string ImportFromStablePath => @"Data\r"; protected override string ImportFromStablePath => @"Data\r";
protected override bool StableDirectoryBased => false;
private readonly RulesetStore rulesets; private readonly RulesetStore rulesets;
private readonly Func<BeatmapManager> beatmaps; private readonly Func<BeatmapManager> beatmaps;
@ -55,6 +54,10 @@ namespace osu.Game.Scoring
} }
} }
protected override IEnumerable<string> GetStableImportPaths()
=> GetStableStorage().GetFiles(ImportFromStablePath)
.Where(p => HandledExtensions.Any(ext => Path.GetExtension(p)?.Equals(ext, StringComparison.InvariantCultureIgnoreCase) ?? false));
public Score GetScore(ScoreInfo score) => new LegacyDatabasedScore(score, rulesets, beatmaps(), Files.Store); public Score GetScore(ScoreInfo score) => new LegacyDatabasedScore(score, rulesets, beatmaps(), Files.Store);
public List<ScoreInfo> GetAllUsableScores() => ModelStore.ConsumableItems.Where(s => !s.DeletePending).ToList(); public List<ScoreInfo> GetAllUsableScores() => ModelStore.ConsumableItems.Where(s => !s.DeletePending).ToList();

View File

@ -32,8 +32,6 @@ namespace osu.Game.Skinning
protected override string ImportFromStablePath => "Skins"; protected override string ImportFromStablePath => "Skins";
protected override bool StableDirectoryBased => true;
public SkinManager(Storage storage, DatabaseContextFactory contextFactory, IIpcHost importHost, AudioManager audio) public SkinManager(Storage storage, DatabaseContextFactory contextFactory, IIpcHost importHost, AudioManager audio)
: base(storage, contextFactory, new SkinStore(contextFactory, storage), importHost) : base(storage, contextFactory, new SkinStore(contextFactory, storage), importHost)
{ {
@ -56,6 +54,8 @@ namespace osu.Game.Skinning
}; };
} }
protected override IEnumerable<string> GetStableImportPaths() => GetStableStorage().GetDirectories(ImportFromStablePath);
/// <summary> /// <summary>
/// Returns a list of all usable <see cref="SkinInfo"/>s. Includes the special default skin plus all skins from <see cref="GetAllUserSkins"/>. /// Returns a list of all usable <see cref="SkinInfo"/>s. Includes the special default skin plus all skins from <see cref="GetAllUserSkins"/>.
/// </summary> /// </summary>