1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-06 09:42:55 +08:00

Move beatmap download logic out of BeatmapManager

This commit is contained in:
Dean Herbert 2021-11-25 17:23:46 +09:00
parent d8a23aad4d
commit 716543b5b3
10 changed files with 45 additions and 67 deletions

View File

@ -12,9 +12,9 @@ using osu.Game.Tests.Visual;
namespace osu.Game.Tests.Online namespace osu.Game.Tests.Online
{ {
[HeadlessTest] [HeadlessTest]
public class TestSceneBeatmapManager : OsuTestScene public class TestSceneBeatmapDownloading : OsuTestScene
{ {
private BeatmapManager beatmaps; private BeatmapModelDownloader beatmaps;
private ProgressNotification recentNotification; private ProgressNotification recentNotification;
private static readonly BeatmapSetInfo test_db_model = new BeatmapSetInfo private static readonly BeatmapSetInfo test_db_model = new BeatmapSetInfo
@ -43,7 +43,7 @@ namespace osu.Game.Tests.Online
}; };
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(BeatmapManager beatmaps) private void load(BeatmapModelDownloader beatmaps)
{ {
this.beatmaps = beatmaps; this.beatmaps = beatmaps;

View File

@ -33,6 +33,7 @@ namespace osu.Game.Tests.Online
{ {
private RulesetStore rulesets; private RulesetStore rulesets;
private TestBeatmapManager beatmaps; private TestBeatmapManager beatmaps;
private TestBeatmapModelDownloader beatmapDownloader;
private string testBeatmapFile; private string testBeatmapFile;
private BeatmapInfo testBeatmapInfo; private BeatmapInfo testBeatmapInfo;
@ -46,6 +47,7 @@ namespace osu.Game.Tests.Online
{ {
Dependencies.Cache(rulesets = new RulesetStore(ContextFactory)); Dependencies.Cache(rulesets = new RulesetStore(ContextFactory));
Dependencies.CacheAs<BeatmapManager>(beatmaps = new TestBeatmapManager(LocalStorage, ContextFactory, rulesets, API, audio, Resources, host, Beatmap.Default)); Dependencies.CacheAs<BeatmapManager>(beatmaps = new TestBeatmapManager(LocalStorage, ContextFactory, rulesets, API, audio, Resources, host, Beatmap.Default));
Dependencies.CacheAs<BeatmapModelDownloader>(beatmapDownloader = new TestBeatmapModelDownloader(beatmaps, API, host));
} }
[SetUp] [SetUp]
@ -80,13 +82,13 @@ namespace osu.Game.Tests.Online
AddAssert("ensure beatmap unavailable", () => !beatmaps.IsAvailableLocally(testBeatmapSet)); AddAssert("ensure beatmap unavailable", () => !beatmaps.IsAvailableLocally(testBeatmapSet));
addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded); addAvailabilityCheckStep("state not downloaded", BeatmapAvailability.NotDownloaded);
AddStep("start downloading", () => beatmaps.Download(testBeatmapSet)); AddStep("start downloading", () => beatmapDownloader.Download(testBeatmapSet));
addAvailabilityCheckStep("state downloading 0%", () => BeatmapAvailability.Downloading(0.0f)); addAvailabilityCheckStep("state downloading 0%", () => BeatmapAvailability.Downloading(0.0f));
AddStep("set progress 40%", () => ((TestDownloadRequest)beatmaps.GetExistingDownload(testBeatmapSet)).SetProgress(0.4f)); AddStep("set progress 40%", () => ((TestDownloadRequest)beatmapDownloader.GetExistingDownload(testBeatmapSet)).SetProgress(0.4f));
addAvailabilityCheckStep("state downloading 40%", () => BeatmapAvailability.Downloading(0.4f)); addAvailabilityCheckStep("state downloading 40%", () => BeatmapAvailability.Downloading(0.4f));
AddStep("finish download", () => ((TestDownloadRequest)beatmaps.GetExistingDownload(testBeatmapSet)).TriggerSuccess(testBeatmapFile)); AddStep("finish download", () => ((TestDownloadRequest)beatmapDownloader.GetExistingDownload(testBeatmapSet)).TriggerSuccess(testBeatmapFile));
addAvailabilityCheckStep("state importing", BeatmapAvailability.Importing); addAvailabilityCheckStep("state importing", BeatmapAvailability.Importing);
AddStep("allow importing", () => beatmaps.AllowImport.SetResult(true)); AddStep("allow importing", () => beatmaps.AllowImport.SetResult(true));
@ -171,22 +173,6 @@ namespace osu.Game.Tests.Online
return new TestBeatmapModelManager(this, storage, contextFactory, rulesets, api, host); return new TestBeatmapModelManager(this, storage, contextFactory, rulesets, api, host);
} }
protected override BeatmapModelDownloader CreateBeatmapModelDownloader(IModelImporter<BeatmapSetInfo> manager, IAPIProvider api, GameHost host)
{
return new TestBeatmapModelDownloader(manager, api, host);
}
internal class TestBeatmapModelDownloader : BeatmapModelDownloader
{
public TestBeatmapModelDownloader(IModelImporter<BeatmapSetInfo> importer, IAPIProvider apiProvider, GameHost gameHost)
: base(importer, apiProvider, gameHost)
{
}
protected override ArchiveDownloadRequest<IBeatmapSetInfo> CreateDownloadRequest(IBeatmapSetInfo set, bool minimiseDownloadSize)
=> new TestDownloadRequest(set);
}
internal class TestBeatmapModelManager : BeatmapModelManager internal class TestBeatmapModelManager : BeatmapModelManager
{ {
private readonly TestBeatmapManager testBeatmapManager; private readonly TestBeatmapManager testBeatmapManager;
@ -205,6 +191,17 @@ namespace osu.Game.Tests.Online
} }
} }
internal class TestBeatmapModelDownloader : BeatmapModelDownloader
{
public TestBeatmapModelDownloader(IModelImporter<BeatmapSetInfo> importer, IAPIProvider apiProvider, GameHost gameHost)
: base(importer, apiProvider, gameHost)
{
}
protected override ArchiveDownloadRequest<IBeatmapSetInfo> CreateDownloadRequest(IBeatmapSetInfo set, bool minimiseDownloadSize)
=> new TestDownloadRequest(set);
}
private class TestDownloadRequest : ArchiveDownloadRequest<IBeatmapSetInfo> private class TestDownloadRequest : ArchiveDownloadRequest<IBeatmapSetInfo>
{ {
public new void SetProgress(float progress) => base.SetProgress(progress); public new void SetProgress(float progress) => base.SetProgress(progress);

View File

@ -29,12 +29,11 @@ namespace osu.Game.Beatmaps
/// Handles general operations related to global beatmap management. /// Handles general operations related to global beatmap management.
/// </summary> /// </summary>
[ExcludeFromDynamicCompile] [ExcludeFromDynamicCompile]
public class BeatmapManager : IModelDownloader<IBeatmapSetInfo>, IModelManager<BeatmapSetInfo>, IModelFileManager<BeatmapSetInfo, BeatmapSetFileInfo>, IModelImporter<BeatmapSetInfo>, IWorkingBeatmapCache, IDisposable public class BeatmapManager : IModelManager<BeatmapSetInfo>, IModelFileManager<BeatmapSetInfo, BeatmapSetFileInfo>, IModelImporter<BeatmapSetInfo>, IWorkingBeatmapCache, IDisposable
{ {
public ITrackStore BeatmapTrackStore { get; } public ITrackStore BeatmapTrackStore { get; }
private readonly BeatmapModelManager beatmapModelManager; private readonly BeatmapModelManager beatmapModelManager;
private readonly BeatmapModelDownloader beatmapModelDownloader;
private readonly WorkingBeatmapCache workingBeatmapCache; private readonly WorkingBeatmapCache workingBeatmapCache;
private readonly BeatmapOnlineLookupQueue onlineBeatmapLookupQueue; private readonly BeatmapOnlineLookupQueue onlineBeatmapLookupQueue;
@ -46,7 +45,6 @@ namespace osu.Game.Beatmaps
BeatmapTrackStore = audioManager.GetTrackStore(userResources); BeatmapTrackStore = audioManager.GetTrackStore(userResources);
beatmapModelManager = CreateBeatmapModelManager(storage, contextFactory, rulesets, api, host); beatmapModelManager = CreateBeatmapModelManager(storage, contextFactory, rulesets, api, host);
beatmapModelDownloader = CreateBeatmapModelDownloader(beatmapModelManager, api, host);
workingBeatmapCache = CreateWorkingBeatmapCache(audioManager, gameResources, userResources, defaultBeatmap, host); workingBeatmapCache = CreateWorkingBeatmapCache(audioManager, gameResources, userResources, defaultBeatmap, host);
workingBeatmapCache.BeatmapManager = beatmapModelManager; workingBeatmapCache.BeatmapManager = beatmapModelManager;
@ -59,11 +57,6 @@ namespace osu.Game.Beatmaps
} }
} }
protected virtual BeatmapModelDownloader CreateBeatmapModelDownloader(IModelImporter<BeatmapSetInfo> modelManager, IAPIProvider api, GameHost host)
{
return new BeatmapModelDownloader(modelManager, api, host);
}
protected virtual WorkingBeatmapCache CreateWorkingBeatmapCache(AudioManager audioManager, IResourceStore<byte[]> resources, IResourceStore<byte[]> storage, WorkingBeatmap defaultBeatmap, GameHost host) protected virtual WorkingBeatmapCache CreateWorkingBeatmapCache(AudioManager audioManager, IResourceStore<byte[]> resources, IResourceStore<byte[]> storage, WorkingBeatmap defaultBeatmap, GameHost host)
{ {
return new WorkingBeatmapCache(BeatmapTrackStore, audioManager, resources, storage, defaultBeatmap, host); return new WorkingBeatmapCache(BeatmapTrackStore, audioManager, resources, storage, defaultBeatmap, host);
@ -185,11 +178,7 @@ namespace osu.Game.Beatmaps
/// </summary> /// </summary>
public Action<Notification> PostNotification public Action<Notification> PostNotification
{ {
set set => beatmapModelManager.PostNotification = value;
{
beatmapModelManager.PostNotification = value;
beatmapModelDownloader.PostNotification = value;
}
} }
/// <summary> /// <summary>
@ -267,28 +256,6 @@ namespace osu.Game.Beatmaps
#endregion #endregion
#region Implementation of IModelDownloader<BeatmapSetInfo>
public event Action<ArchiveDownloadRequest<IBeatmapSetInfo>> DownloadBegan
{
add => beatmapModelDownloader.DownloadBegan += value;
remove => beatmapModelDownloader.DownloadBegan -= value;
}
public event Action<ArchiveDownloadRequest<IBeatmapSetInfo>> DownloadFailed
{
add => beatmapModelDownloader.DownloadFailed += value;
remove => beatmapModelDownloader.DownloadFailed -= value;
}
public bool Download(IBeatmapSetInfo model, bool minimiseDownloadSize = false) =>
beatmapModelDownloader.Download(model, minimiseDownloadSize);
public ArchiveDownloadRequest<IBeatmapSetInfo> GetExistingDownload(IBeatmapSetInfo model) =>
beatmapModelDownloader.GetExistingDownload(model);
#endregion
#region Implementation of ICanAcceptFiles #region Implementation of ICanAcceptFiles
public Task Import(params string[] paths) public Task Import(params string[] paths)

View File

@ -24,7 +24,7 @@ namespace osu.Game.Beatmaps.Drawables.Cards.Buttons
private Bindable<bool> preferNoVideo = null!; private Bindable<bool> preferNoVideo = null!;
[Resolved] [Resolved]
private BeatmapManager beatmaps { get; set; } = null!; private BeatmapModelDownloader beatmaps { get; set; } = null!;
public DownloadButton(APIBeatmapSet beatmapSet) public DownloadButton(APIBeatmapSet beatmapSet)
{ {

View File

@ -15,6 +15,9 @@ namespace osu.Game.Online
[Resolved(CanBeNull = true)] [Resolved(CanBeNull = true)]
protected BeatmapManager? Manager { get; private set; } protected BeatmapManager? Manager { get; private set; }
[Resolved(CanBeNull = true)]
protected BeatmapModelDownloader? Downloader { get; private set; }
private ArchiveDownloadRequest<IBeatmapSetInfo>? attachedRequest; private ArchiveDownloadRequest<IBeatmapSetInfo>? attachedRequest;
public BeatmapDownloadTracker(IBeatmapSetInfo trackedItem) public BeatmapDownloadTracker(IBeatmapSetInfo trackedItem)
@ -25,7 +28,7 @@ namespace osu.Game.Online
[BackgroundDependencyLoader(true)] [BackgroundDependencyLoader(true)]
private void load() private void load()
{ {
if (Manager == null) if (Manager == null || Downloader == null)
return; return;
// Used to interact with manager classes that don't support interface types. Will eventually be replaced. // Used to interact with manager classes that don't support interface types. Will eventually be replaced.
@ -34,10 +37,10 @@ namespace osu.Game.Online
if (Manager.IsAvailableLocally(beatmapSetInfo)) if (Manager.IsAvailableLocally(beatmapSetInfo))
UpdateState(DownloadState.LocallyAvailable); UpdateState(DownloadState.LocallyAvailable);
else else
attachDownload(Manager.GetExistingDownload(beatmapSetInfo)); attachDownload(Downloader.GetExistingDownload(beatmapSetInfo));
Manager.DownloadBegan += downloadBegan; Downloader.DownloadBegan += downloadBegan;
Manager.DownloadFailed += downloadFailed; Downloader.DownloadFailed += downloadFailed;
Manager.ItemUpdated += itemUpdated; Manager.ItemUpdated += itemUpdated;
Manager.ItemRemoved += itemRemoved; Manager.ItemRemoved += itemRemoved;
} }
@ -115,10 +118,14 @@ namespace osu.Game.Online
base.Dispose(isDisposing); base.Dispose(isDisposing);
attachDownload(null); attachDownload(null);
if (Downloader != null)
{
Downloader.DownloadBegan -= downloadBegan;
Downloader.DownloadFailed -= downloadFailed;
}
if (Manager != null) if (Manager != null)
{ {
Manager.DownloadBegan -= downloadBegan;
Manager.DownloadFailed -= downloadFailed;
Manager.ItemUpdated -= itemUpdated; Manager.ItemUpdated -= itemUpdated;
Manager.ItemRemoved -= itemRemoved; Manager.ItemRemoved -= itemRemoved;
} }

View File

@ -656,6 +656,7 @@ namespace osu.Game
BeatmapManager.PostNotification = n => Notifications.Post(n); BeatmapManager.PostNotification = n => Notifications.Post(n);
BeatmapManager.PostImport = items => PresentBeatmap(items.First().Value); BeatmapManager.PostImport = items => PresentBeatmap(items.First().Value);
BeatmapDownloader.PostNotification = n => Notifications.Post(n);
ScoreManager.PostNotification = n => Notifications.Post(n); ScoreManager.PostNotification = n => Notifications.Post(n);
ScoreManager.PostImport = items => PresentScore(items.First().Value); ScoreManager.PostImport = items => PresentScore(items.First().Value);

View File

@ -96,6 +96,8 @@ namespace osu.Game
protected BeatmapManager BeatmapManager { get; private set; } protected BeatmapManager BeatmapManager { get; private set; }
protected BeatmapModelDownloader BeatmapDownloader { get; private set; }
protected ScoreManager ScoreManager { get; private set; } protected ScoreManager ScoreManager { get; private set; }
protected SkinManager SkinManager { get; private set; } protected SkinManager SkinManager { get; private set; }
@ -235,6 +237,7 @@ namespace osu.Game
dependencies.Cache(ScoreManager = new ScoreManager(RulesetStore, () => BeatmapManager, Storage, API, contextFactory, Scheduler, Host, () => difficultyCache, LocalConfig)); dependencies.Cache(ScoreManager = new ScoreManager(RulesetStore, () => BeatmapManager, Storage, API, contextFactory, Scheduler, Host, () => difficultyCache, LocalConfig));
dependencies.Cache(BeatmapManager = new BeatmapManager(Storage, contextFactory, RulesetStore, API, Audio, Resources, Host, defaultBeatmap, performOnlineLookups: true)); dependencies.Cache(BeatmapManager = new BeatmapManager(Storage, contextFactory, RulesetStore, API, Audio, Resources, Host, defaultBeatmap, performOnlineLookups: true));
dependencies.Cache(BeatmapDownloader = new BeatmapModelDownloader(BeatmapManager, API, Host));
// the following realm components are not actively used yet, but initialised and kept up to date for initial testing. // the following realm components are not actively used yet, but initialised and kept up to date for initial testing.
realmRulesetStore = new RealmRulesetStore(realmFactory, Storage); realmRulesetStore = new RealmRulesetStore(realmFactory, Storage);

View File

@ -65,7 +65,7 @@ namespace osu.Game.Overlays.BeatmapListing.Panels
} }
[BackgroundDependencyLoader(true)] [BackgroundDependencyLoader(true)]
private void load(OsuGame game, BeatmapManager beatmaps, OsuConfigManager osuConfig) private void load(OsuGame game, BeatmapModelDownloader beatmaps, OsuConfigManager osuConfig)
{ {
noVideoSetting = osuConfig.GetBindable<bool>(OsuSetting.PreferNoVideo); noVideoSetting = osuConfig.GetBindable<bool>(OsuSetting.PreferNoVideo);

View File

@ -51,7 +51,7 @@ namespace osu.Game.Overlays.BeatmapSet.Buttons
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(IAPIProvider api, BeatmapManager beatmaps) private void load(IAPIProvider api, BeatmapModelDownloader beatmaps)
{ {
FillFlowContainer textSprites; FillFlowContainer textSprites;

View File

@ -49,6 +49,9 @@ namespace osu.Game.Screens.Play
[Resolved] [Resolved]
private BeatmapManager beatmaps { get; set; } private BeatmapManager beatmaps { get; set; }
[Resolved]
private BeatmapModelDownloader beatmapDownloader { get; set; }
private Container beatmapPanelContainer; private Container beatmapPanelContainer;
private TriangleButton watchButton; private TriangleButton watchButton;
private SettingsCheckbox automaticDownload; private SettingsCheckbox automaticDownload;
@ -244,7 +247,7 @@ namespace osu.Game.Screens.Play
if (beatmaps.IsAvailableLocally(new BeatmapSetInfo { OnlineID = beatmapSet.OnlineID })) if (beatmaps.IsAvailableLocally(new BeatmapSetInfo { OnlineID = beatmapSet.OnlineID }))
return; return;
beatmaps.Download(beatmapSet); beatmapDownloader.Download(beatmapSet);
} }
public override bool OnExiting(IScreen next) public override bool OnExiting(IScreen next)