2022-05-16 18:21:26 +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.
#nullable enable
2022-05-16 19:53:04 +08:00
using System.Collections.Generic ;
2022-05-16 19:37:38 +08:00
using System.Linq ;
using System.Threading ;
2022-05-16 18:21:26 +08:00
using osu.Framework.Allocation ;
2022-05-16 19:13:34 +08:00
using osu.Framework.Extensions ;
2022-05-16 18:21:26 +08:00
using osu.Framework.Graphics ;
using osu.Framework.Localisation ;
using osu.Game.Database ;
using osu.Game.Graphics ;
using osu.Game.Graphics.Containers ;
2022-05-16 19:37:38 +08:00
using osu.Game.Graphics.UserInterfaceV2 ;
2022-05-16 18:21:26 +08:00
using osu.Game.Localisation ;
using osu.Game.Overlays.Settings ;
using osuTK ;
namespace osu.Game.Overlays.FirstRunSetup
{
[LocalisableDescription(typeof(FirstRunSetupOverlayStrings), nameof(FirstRunSetupOverlayStrings.ImportTitle))]
public class ScreenImportFromStable : FirstRunSetupScreen
{
private ProgressRoundedButton importButton = null ! ;
2022-05-16 19:37:38 +08:00
private OsuTextFlowContainer currentStablePath = null ! ;
2022-05-16 18:21:26 +08:00
[Resolved]
private OsuColour colours { get ; set ; } = null ! ;
2022-05-16 19:13:34 +08:00
[Resolved]
private LegacyImportManager legacyImportManager { get ; set ; } = null ! ;
2022-05-16 18:21:26 +08:00
2022-05-16 19:37:38 +08:00
private CancellationTokenSource ? stablePathUpdateCancellation ;
2022-05-16 19:53:04 +08:00
private IEnumerable < ImportCheckbox > contentCheckboxes = > Content . Children . OfType < ImportCheckbox > ( ) ;
2022-05-16 18:21:26 +08:00
[BackgroundDependencyLoader(permitNulls: true)]
private void load ( )
{
Vector2 buttonSize = new Vector2 ( 400 , 50 ) ;
Content . Children = new Drawable [ ]
{
new OsuTextFlowContainer ( cp = > cp . Font = OsuFont . Default . With ( size : CONTENT_FONT_SIZE ) )
{
Colour = OverlayColourProvider . Content1 ,
Text =
"If you have an installation of a previous osu! version, you can choose to migrate your existing content. Note that this will create a copy, and not affect your existing installation." ,
RelativeSizeAxes = Axes . X ,
AutoSizeAxes = Axes . Y
} ,
2022-05-16 19:37:38 +08:00
currentStablePath = new OsuTextFlowContainer ( cp = > cp . Font = OsuFont . Default . With ( size : HEADER_FONT_SIZE , weight : FontWeight . SemiBold ) )
{
Colour = OverlayColourProvider . Content2 ,
RelativeSizeAxes = Axes . X ,
AutoSizeAxes = Axes . Y ,
} ,
new RoundedButton
{
Size = buttonSize ,
Anchor = Anchor . TopCentre ,
Origin = Anchor . TopCentre ,
BackgroundColour = colours . Blue3 ,
Text = "Locate osu!(stable) install" ,
Action = locateStable ,
} ,
2022-05-16 19:53:04 +08:00
new ImportCheckbox ( "Beatmaps" , StableContent . Beatmaps ) ,
new ImportCheckbox ( "Scores" , StableContent . Scores ) ,
new ImportCheckbox ( "Skins" , StableContent . Skins ) ,
new ImportCheckbox ( "Collections" , StableContent . Collections ) ,
2022-05-16 18:21:26 +08:00
importButton = new ProgressRoundedButton
{
Size = buttonSize ,
Anchor = Anchor . TopCentre ,
Origin = Anchor . TopCentre ,
BackgroundColour = colours . Blue3 ,
Text = FirstRunSetupOverlayStrings . ImportContentFromStable ,
Action = runImport
} ,
} ;
2022-05-16 19:13:34 +08:00
2022-05-16 19:37:38 +08:00
updateStablePath ( ) ;
}
private void locateStable ( )
{
legacyImportManager . ImportFromStableAsync ( 0 ) . ContinueWith ( task = >
{
Schedule ( updateStablePath ) ;
} ) ;
}
private void updateStablePath ( )
{
stablePathUpdateCancellation ? . Cancel ( ) ;
var storage = legacyImportManager . GetCurrentStableStorage ( ) ;
if ( storage = = null )
{
2022-05-16 19:53:04 +08:00
foreach ( var c in contentCheckboxes )
2022-05-16 19:37:38 +08:00
c . Current . Disabled = true ;
currentStablePath . Text = "No installation found" ;
return ;
}
2022-05-16 19:53:04 +08:00
foreach ( var c in contentCheckboxes )
{
2022-05-16 19:37:38 +08:00
c . Current . Disabled = false ;
2022-05-16 19:53:04 +08:00
c . UpdateCount ( ) ;
}
2022-05-16 19:37:38 +08:00
currentStablePath . Text = storage . GetFullPath ( string . Empty ) ;
stablePathUpdateCancellation = new CancellationTokenSource ( ) ;
2022-05-16 19:53:04 +08:00
}
2022-05-16 19:37:38 +08:00
2022-05-16 19:53:04 +08:00
private void runImport ( )
{
importButton . Enabled . Value = false ;
2022-05-16 19:37:38 +08:00
2022-05-16 19:53:04 +08:00
StableContent importableContent = 0 ;
2022-05-16 19:37:38 +08:00
2022-05-16 19:53:04 +08:00
foreach ( var c in contentCheckboxes . Where ( c = > c . Current . Value ) )
importableContent | = c . StableContent ;
2022-05-16 19:37:38 +08:00
2022-05-16 19:53:04 +08:00
legacyImportManager . ImportFromStableAsync ( importableContent , false ) . ContinueWith ( t = > Schedule ( ( ) = >
2022-05-16 19:37:38 +08:00
{
2022-05-16 19:53:04 +08:00
if ( t . IsCompletedSuccessfully )
importButton . Complete ( ) ;
else
{
importButton . Enabled . Value = true ;
importButton . Abort ( ) ;
}
2022-05-16 19:37:38 +08:00
} ) ) ;
2022-05-16 18:21:26 +08:00
}
2022-05-16 19:53:04 +08:00
private class ImportCheckbox : SettingsCheckbox
2022-05-16 18:21:26 +08:00
{
2022-05-16 19:53:04 +08:00
public readonly StableContent StableContent ;
2022-05-16 18:21:26 +08:00
2022-05-16 19:53:04 +08:00
private readonly LocalisableString title ;
[Resolved]
private LegacyImportManager legacyImportManager { get ; set ; } = null ! ;
private CancellationTokenSource ? countUpdateCancellation ;
public ImportCheckbox ( LocalisableString title , StableContent stableContent )
{
this . title = title ;
2022-05-16 18:21:26 +08:00
2022-05-16 19:53:04 +08:00
StableContent = stableContent ;
Current . Value = true ;
LabelText = title ;
}
public void UpdateCount ( )
{
LabelText = LocalisableString . Interpolate ( $"{title} (calculating...)" ) ;
countUpdateCancellation ? . Cancel ( ) ;
countUpdateCancellation = new CancellationTokenSource ( ) ;
legacyImportManager . GetImportCount ( StableContent , countUpdateCancellation . Token ) . ContinueWith ( task = > Schedule ( ( ) = >
{
if ( task . IsCanceled )
return ;
LabelText = LocalisableString . Interpolate ( $"{title} ({task.GetResultSafely()} items)" ) ;
} ) ) ;
}
2022-05-16 18:21:26 +08:00
}
}
}