osu/osu.Game/Database/StableImportManager.cs

94 lines
2.9 KiB
C#
Raw Normal View History

2021-05-08 09:00:22 +00:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
2021-05-09 15:12:58 +00:00
using osu.Framework;
2021-05-08 09:00:22 +00:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.EnumExtensions;
using osu.Framework.Graphics;
using osu.Framework.Platform;
using osu.Framework.Screens;
using osu.Game.Beatmaps;
using osu.Game.Collections;
using osu.Game.IO;
using osu.Game.Overlays.Settings.Sections.Maintenance;
using osu.Game.Scoring;
using osu.Game.Skinning;
namespace osu.Game.Database
2021-05-09 15:12:58 +00:00
{
2021-05-08 09:00:22 +00:00
public class StableImportManager : Component
{
[Resolved]
private SkinManager skins { get; set; }
[Resolved]
private BeatmapManager beatmaps { get; set; }
[Resolved]
private ScoreManager scores { get; set; }
[Resolved]
private CollectionManager collections { get; set; }
[Resolved]
private OsuGame game { get; set; }
[Resolved(CanBeNull = true)]
private DesktopGameHost desktopGameHost { get; set; }
private StableStorage cachedStorage;
2021-05-09 15:12:58 +00:00
public bool SupportsImportFromStable => RuntimeInfo.IsDesktop;
2021-05-08 09:00:22 +00:00
public async Task ImportFromStableAsync(StableContent content)
{
2021-05-09 15:12:58 +00:00
var stableStorage = await getStableStorage().ConfigureAwait(false);
2021-05-08 09:00:22 +00:00
var importTasks = new List<Task>();
if (content.HasFlagFast(StableContent.Beatmaps))
2021-05-09 15:12:58 +00:00
importTasks.Add(beatmaps.ImportFromStableAsync(stableStorage));
2021-05-08 09:00:22 +00:00
if (content.HasFlagFast(StableContent.Collections))
2021-05-09 15:12:58 +00:00
importTasks.Add(collections.ImportFromStableAsync(stableStorage));
2021-05-08 09:00:22 +00:00
if (content.HasFlagFast(StableContent.Scores))
2021-05-09 15:12:58 +00:00
importTasks.Add(scores.ImportFromStableAsync(stableStorage));
2021-05-08 09:00:22 +00:00
if (content.HasFlagFast(StableContent.Skins))
2021-05-09 15:12:58 +00:00
importTasks.Add(skins.ImportFromStableAsync(stableStorage));
2021-05-08 09:00:22 +00:00
await Task.WhenAll(importTasks.ToArray()).ConfigureAwait(false);
}
private async Task<StableStorage> getStableStorage()
{
var stableStorage = game.GetStorageForStableInstall();
if (stableStorage != null)
return stableStorage;
if (cachedStorage != null)
return cachedStorage;
var taskCompletionSource = new TaskCompletionSource<string>();
Schedule(() => game.PerformFromScreen(t => t.Push(new StableDirectorySelectScreen(taskCompletionSource))));
var stablePath = await taskCompletionSource.Task.ConfigureAwait(false);
return cachedStorage = new StableStorage(stablePath, desktopGameHost);
}
}
[Flags]
public enum StableContent
{
2021-05-09 15:12:58 +00:00
Beatmaps = 1,
Scores = 2,
Skins = 4,
Collections = 8,
2021-05-08 09:00:22 +00:00
All = Beatmaps | Scores | Skins | Collections
}
}