2021-11-25 06:36:12 +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.
2022-06-17 07:37:17 +00:00
#nullable disable
2021-11-25 06:36:12 +00:00
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 06:39:05 +00:00
/// A class which handles importing legacy user data of a single type from osu-stable.
2021-11-25 06:36:12 +00:00
/// </summary>
2021-11-25 07:33:04 +00:00
public abstract class LegacyModelImporter < TModel >
2022-01-26 04:37:33 +00:00
where TModel : class , IHasGuidPrimaryKey
2021-11-25 06:36:12 +00: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 07:55:41 +00: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 06:36:12 +00:00
protected readonly IModelImporter < TModel > Importer ;
2021-11-25 07:33:04 +00:00
protected LegacyModelImporter ( IModelImporter < TModel > importer )
2021-11-25 06:36:12 +00:00
{
Importer = importer ;
}
2022-05-19 05:01:24 +00:00
public Task < int > GetAvailableCount ( StableStorage stableStorage ) = > Task . Run ( ( ) = > GetStableImportPaths ( PrepareStableStorage ( stableStorage ) ) . Count ( ) ) ;
2022-05-16 10:57:00 +00:00
2021-11-25 06:36:12 +00: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 ;
}
}