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;
|
2022-05-16 11:37:38 +00:00
|
|
|
|
using System.Threading;
|
2021-05-08 09:00:22 +00:00
|
|
|
|
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.Game.Beatmaps;
|
|
|
|
|
using osu.Game.IO;
|
2021-05-09 17:50:43 +00:00
|
|
|
|
using osu.Game.Overlays;
|
2021-05-08 09:00:22 +00:00
|
|
|
|
using osu.Game.Overlays.Settings.Sections.Maintenance;
|
|
|
|
|
using osu.Game.Scoring;
|
|
|
|
|
using osu.Game.Skinning;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
2021-05-09 17:50:43 +00:00
|
|
|
|
{
|
2021-11-25 06:39:05 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Handles migration of legacy user data from osu-stable.
|
|
|
|
|
/// </summary>
|
2021-11-25 08:12:15 +00:00
|
|
|
|
public partial class LegacyImportManager : Component
|
2021-05-08 09:00:22 +00:00
|
|
|
|
{
|
|
|
|
|
[Resolved]
|
2022-08-08 05:56:16 +00:00
|
|
|
|
private SkinManager skins { get; set; } = null!;
|
2021-05-08 09:00:22 +00:00
|
|
|
|
|
|
|
|
|
[Resolved]
|
2022-08-08 05:56:16 +00:00
|
|
|
|
private BeatmapManager beatmaps { get; set; } = null!;
|
2021-05-08 09:00:22 +00:00
|
|
|
|
|
|
|
|
|
[Resolved]
|
2022-08-08 05:56:16 +00:00
|
|
|
|
private ScoreManager scores { get; set; } = null!;
|
2021-05-08 09:00:22 +00:00
|
|
|
|
|
2022-08-08 05:56:16 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private OsuGame? game { get; set; }
|
2021-05-09 17:52:26 +00:00
|
|
|
|
|
2021-05-08 09:00:22 +00:00
|
|
|
|
[Resolved]
|
2022-08-08 05:56:16 +00:00
|
|
|
|
private IDialogOverlay dialogOverlay { get; set; } = null!;
|
2021-05-08 09:00:22 +00:00
|
|
|
|
|
2022-07-27 06:59:36 +00:00
|
|
|
|
[Resolved]
|
2022-08-08 05:56:16 +00:00
|
|
|
|
private RealmAccess realmAccess { get; set; } = null!;
|
2022-07-27 06:59:36 +00:00
|
|
|
|
|
2022-12-07 08:52:57 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private GameHost gameHost { get; set; } = null!;
|
2022-08-08 05:56:16 +00:00
|
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
|
private INotificationOverlay? notifications { get; set; }
|
2021-05-08 09:00:22 +00:00
|
|
|
|
|
2022-08-08 05:56:16 +00:00
|
|
|
|
private StableStorage? cachedStorage;
|
2021-05-08 09:00:22 +00:00
|
|
|
|
|
2021-05-09 15:12:58 +00:00
|
|
|
|
public bool SupportsImportFromStable => RuntimeInfo.IsDesktop;
|
|
|
|
|
|
2022-12-07 08:52:57 +00:00
|
|
|
|
public void UpdateStorage(string stablePath) => cachedStorage = new StableStorage(stablePath, gameHost as DesktopGameHost);
|
2022-05-16 12:07:42 +00:00
|
|
|
|
|
2022-10-13 09:39:52 +00:00
|
|
|
|
public bool CheckHardLinkAvailability()
|
|
|
|
|
{
|
|
|
|
|
var stableStorage = GetCurrentStableStorage();
|
|
|
|
|
|
|
|
|
|
if (stableStorage == null || gameHost is not DesktopGameHost desktopGameHost)
|
|
|
|
|
return false;
|
|
|
|
|
|
2022-12-13 11:02:57 +00:00
|
|
|
|
string testExistingPath = stableStorage.GetFullPath(string.Empty);
|
|
|
|
|
string testDestinationPath = desktopGameHost.Storage.GetFullPath(string.Empty);
|
2022-10-13 09:39:52 +00:00
|
|
|
|
|
2022-12-13 11:02:57 +00:00
|
|
|
|
return HardLinkHelper.CheckAvailability(testDestinationPath, testExistingPath);
|
2022-10-13 09:39:52 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-17 09:15:14 +00:00
|
|
|
|
public virtual async Task<int> GetImportCount(StableContent content, CancellationToken cancellationToken)
|
2022-05-16 10:57:00 +00:00
|
|
|
|
{
|
2022-05-16 11:37:38 +00:00
|
|
|
|
var stableStorage = GetCurrentStableStorage();
|
|
|
|
|
|
|
|
|
|
if (stableStorage == null)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2022-05-16 10:57:00 +00:00
|
|
|
|
|
|
|
|
|
switch (content)
|
|
|
|
|
{
|
|
|
|
|
case StableContent.Beatmaps:
|
2022-12-16 09:16:26 +00:00
|
|
|
|
return await new LegacyBeatmapImporter(beatmaps).GetAvailableCount(stableStorage).ConfigureAwait(false);
|
2022-05-16 10:57:00 +00:00
|
|
|
|
|
|
|
|
|
case StableContent.Skins:
|
2022-12-16 09:16:26 +00:00
|
|
|
|
return await new LegacySkinImporter(skins).GetAvailableCount(stableStorage).ConfigureAwait(false);
|
2022-05-16 10:57:00 +00:00
|
|
|
|
|
|
|
|
|
case StableContent.Collections:
|
2022-12-16 09:16:26 +00:00
|
|
|
|
return await new LegacyCollectionImporter(realmAccess).GetAvailableCount(stableStorage).ConfigureAwait(false);
|
2022-05-16 10:57:00 +00:00
|
|
|
|
|
|
|
|
|
case StableContent.Scores:
|
2022-12-16 09:16:26 +00:00
|
|
|
|
return await new LegacyScoreImporter(scores).GetAvailableCount(stableStorage).ConfigureAwait(false);
|
2022-05-16 10:57:00 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentException($"Only one {nameof(StableContent)} flag should be specified.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-16 11:37:38 +00:00
|
|
|
|
public async Task ImportFromStableAsync(StableContent content, bool interactiveLocateIfNotFound = true)
|
2021-05-08 09:00:22 +00:00
|
|
|
|
{
|
2022-05-16 11:37:38 +00:00
|
|
|
|
var stableStorage = GetCurrentStableStorage();
|
|
|
|
|
|
|
|
|
|
if (stableStorage == null)
|
|
|
|
|
{
|
|
|
|
|
if (!interactiveLocateIfNotFound)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
var taskCompletionSource = new TaskCompletionSource<string>(TaskCreationOptions.RunContinuationsAsynchronously);
|
|
|
|
|
Schedule(() => dialogOverlay.Push(new StableDirectoryLocationDialog(taskCompletionSource)));
|
|
|
|
|
string stablePath = await taskCompletionSource.Task.ConfigureAwait(false);
|
|
|
|
|
|
2022-05-16 12:07:42 +00:00
|
|
|
|
UpdateStorage(stablePath);
|
|
|
|
|
stableStorage = GetCurrentStableStorage();
|
2022-05-16 11:37:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 05:56:16 +00:00
|
|
|
|
if (stableStorage == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-05-08 09:00:22 +00:00
|
|
|
|
var importTasks = new List<Task>();
|
|
|
|
|
|
2021-05-17 14:30:13 +00:00
|
|
|
|
Task beatmapImportTask = Task.CompletedTask;
|
2021-05-08 09:00:22 +00:00
|
|
|
|
if (content.HasFlagFast(StableContent.Beatmaps))
|
2021-11-25 06:44:04 +00:00
|
|
|
|
importTasks.Add(beatmapImportTask = new LegacyBeatmapImporter(beatmaps).ImportFromStableAsync(stableStorage));
|
2021-05-09 16:15:21 +00:00
|
|
|
|
|
|
|
|
|
if (content.HasFlagFast(StableContent.Skins))
|
2021-11-25 06:44:04 +00:00
|
|
|
|
importTasks.Add(new LegacySkinImporter(skins).ImportFromStableAsync(stableStorage));
|
2021-05-08 09:00:22 +00:00
|
|
|
|
|
|
|
|
|
if (content.HasFlagFast(StableContent.Collections))
|
2022-08-08 05:56:16 +00:00
|
|
|
|
{
|
|
|
|
|
importTasks.Add(beatmapImportTask.ContinueWith(_ => new LegacyCollectionImporter(realmAccess)
|
|
|
|
|
{
|
|
|
|
|
// Other legacy importers import via model managers which handle the posting of notifications.
|
|
|
|
|
// Collections are an exception.
|
|
|
|
|
PostNotification = n => notifications?.Post(n)
|
|
|
|
|
}.ImportFromStorage(stableStorage), TaskContinuationOptions.OnlyOnRanToCompletion));
|
|
|
|
|
}
|
2021-05-08 09:00:22 +00:00
|
|
|
|
|
|
|
|
|
if (content.HasFlagFast(StableContent.Scores))
|
2021-11-25 06:44:04 +00:00
|
|
|
|
importTasks.Add(beatmapImportTask.ContinueWith(_ => new LegacyScoreImporter(scores).ImportFromStableAsync(stableStorage), TaskContinuationOptions.OnlyOnRanToCompletion));
|
2021-05-08 09:00:22 +00:00
|
|
|
|
|
|
|
|
|
await Task.WhenAll(importTasks.ToArray()).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-08 05:56:16 +00:00
|
|
|
|
public StableStorage? GetCurrentStableStorage()
|
2021-05-08 09:00:22 +00:00
|
|
|
|
{
|
|
|
|
|
if (cachedStorage != null)
|
|
|
|
|
return cachedStorage;
|
|
|
|
|
|
2022-05-17 09:15:14 +00:00
|
|
|
|
var stableStorage = game?.GetStorageForStableInstall();
|
2021-05-17 16:39:04 +00:00
|
|
|
|
if (stableStorage != null)
|
|
|
|
|
return cachedStorage = stableStorage;
|
|
|
|
|
|
2022-05-16 11:37:38 +00:00
|
|
|
|
return null;
|
2021-05-08 09:00:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Flags]
|
|
|
|
|
public enum StableContent
|
|
|
|
|
{
|
2021-05-17 19:02:45 +00:00
|
|
|
|
Beatmaps = 1 << 0,
|
|
|
|
|
Scores = 1 << 1,
|
|
|
|
|
Skins = 1 << 2,
|
|
|
|
|
Collections = 1 << 3,
|
2021-05-08 09:00:22 +00:00
|
|
|
|
All = Beatmaps | Scores | Skins | Collections
|
|
|
|
|
}
|
|
|
|
|
}
|