2021-11-25 14:36:12 +08: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.Collections.Generic ;
using System.Linq ;
using System.Threading.Tasks ;
using osu.Framework.Logging ;
using osu.Framework.Platform ;
using osu.Game.IO ;
namespace osu.Game.Database
{
/// <summary>
2021-11-25 14:39:05 +08:00
/// A class which handles importing legacy user data of a single type from osu-stable.
2021-11-25 14:36:12 +08:00
/// </summary>
2021-11-25 15:33:04 +08:00
public abstract class LegacyModelImporter < TModel >
2022-01-26 12:37:33 +08:00
where TModel : class , IHasGuidPrimaryKey
2021-11-25 14:36:12 +08:00
{
/// <summary>
/// The relative path from osu-stable's data directory to import items from.
/// </summary>
protected virtual string ImportFromStablePath = > null ;
/// <summary>
/// Select paths to import from stable where all paths should be absolute. Default implementation iterates all directories in <see cref="ImportFromStablePath"/>.
/// </summary>
2022-05-17 15:55:41 +08:00
protected virtual IEnumerable < string > GetStableImportPaths ( Storage storage )
{
if ( ! storage . ExistsDirectory ( ImportFromStablePath ) )
return Enumerable . Empty < string > ( ) ;
return storage . GetDirectories ( ImportFromStablePath )
. Select ( path = > storage . GetFullPath ( path ) ) ;
}
2021-11-25 14:36:12 +08:00
protected readonly IModelImporter < TModel > Importer ;
2021-11-25 15:33:04 +08:00
protected LegacyModelImporter ( IModelImporter < TModel > importer )
2021-11-25 14:36:12 +08:00
{
Importer = importer ;
}
2022-05-19 13:01:24 +08:00
public Task < int > GetAvailableCount ( StableStorage stableStorage ) = > Task . Run ( ( ) = > GetStableImportPaths ( PrepareStableStorage ( stableStorage ) ) . Count ( ) ) ;
2022-05-16 18:57:00 +08:00
2021-11-25 14:36:12 +08:00
public Task ImportFromStableAsync ( StableStorage stableStorage )
{
var storage = PrepareStableStorage ( stableStorage ) ;
// Handle situations like when the user does not have a Skins folder.
if ( ! storage . ExistsDirectory ( ImportFromStablePath ) )
{
string fullPath = storage . GetFullPath ( ImportFromStablePath ) ;
Logger . Log ( @ $"Folder ""{fullPath}"" not available in the target osu!stable installation to import {Importer.HumanisedModelName}s." , LoggingTarget . Information , LogLevel . Error ) ;
return Task . CompletedTask ;
}
return Task . Run ( async ( ) = > await Importer . Import ( GetStableImportPaths ( storage ) . ToArray ( ) ) . ConfigureAwait ( false ) ) ;
}
/// <summary>
/// Run any required traversal operations on the stable storage location before performing operations.
/// </summary>
/// <param name="stableStorage">The stable storage.</param>
/// <returns>The usable storage. Return the unchanged <paramref name="stableStorage"/> if no traversal is required.</returns>
protected virtual Storage PrepareStableStorage ( StableStorage stableStorage ) = > stableStorage ;
}
}