2020-05-13 21:58:05 +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.
2022-06-17 15:37:17 +08:00
#nullable disable
2020-05-14 16:40:43 +08:00
using System ;
using System.IO ;
2022-03-29 17:00:56 +08:00
using System.Linq ;
2020-05-13 21:58:05 +08:00
using osu.Framework.Allocation ;
2021-05-17 15:40:42 +08:00
using osu.Framework.Localisation ;
2020-05-14 16:40:43 +08:00
using osu.Framework.Logging ;
using osu.Framework.Platform ;
2020-05-13 21:58:05 +08:00
using osu.Framework.Screens ;
2022-03-29 17:00:56 +08:00
using osu.Game.IO ;
2022-09-16 17:31:02 +08:00
using osu.Game.Localisation ;
2022-03-29 17:00:56 +08:00
using osu.Game.Overlays.Dialog ;
2020-05-13 21:58:05 +08:00
namespace osu.Game.Overlays.Settings.Sections.Maintenance
{
2022-11-24 13:32:20 +08:00
public partial class MigrationSelectScreen : DirectorySelectScreen
2020-05-13 21:58:05 +08:00
{
2021-05-06 02:27:28 +08:00
[Resolved]
private Storage storage { get ; set ; }
2020-05-13 21:58:05 +08:00
2022-03-29 17:00:56 +08:00
[Resolved]
private OsuGameBase game { get ; set ; }
[Resolved(canBeNull: true)]
2022-04-18 17:09:14 +08:00
private IDialogOverlay dialogOverlay { get ; set ; }
2022-03-29 17:00:56 +08:00
2021-05-06 04:13:25 +08:00
protected override DirectoryInfo InitialPath = > new DirectoryInfo ( storage . GetFullPath ( string . Empty ) ) . Parent ;
2021-05-14 01:28:23 +08:00
public override bool AllowExternalScreenChange = > false ;
public override bool DisallowExternalBeatmapRulesetChanges = > true ;
public override bool HideOverlaysOnEnter = > true ;
2022-09-16 17:31:02 +08:00
public override LocalisableString HeaderText = > MaintenanceSettingsStrings . SelectNewLocation ;
2020-05-14 16:40:43 +08:00
2021-05-06 02:27:28 +08:00
protected override void OnSelection ( DirectoryInfo directory )
2020-05-13 21:58:05 +08:00
{
2021-05-06 02:27:28 +08:00
var target = directory ;
2020-05-14 16:40:43 +08:00
try
{
2022-03-29 17:00:56 +08:00
var directoryInfos = target . GetDirectories ( ) ;
var fileInfos = target . GetFiles ( ) ;
2023-01-31 15:14:21 +08:00
if ( directoryInfos . Length > 0 | | fileInfos . Length > 0 | | target . Parent = = null )
2022-03-29 17:00:56 +08:00
{
// Quick test for whether there's already an osu! install at the target path.
2022-03-30 12:34:48 +08:00
if ( fileInfos . Any ( f = > f . Name = = OsuGameBase . CLIENT_DATABASE_FILENAME ) )
2022-03-29 17:00:56 +08:00
{
2022-09-16 20:08:25 +08:00
dialogOverlay . Push ( new ConfirmDialog ( MaintenanceSettingsStrings . TargetDirectoryAlreadyInstalledOsu , ( ) = >
2022-03-29 17:00:56 +08:00
{
2022-09-16 20:08:25 +08:00
dialogOverlay . Push ( new ConfirmDialog ( MaintenanceSettingsStrings . RestartAndReOpenRequiredForCompletion , ( ) = >
2022-03-29 17:00:56 +08:00
{
( storage as OsuStorage ) ? . ChangeDataPath ( target . FullName ) ;
2022-06-19 11:39:58 +08:00
game . Exit ( ) ;
2022-03-29 17:00:56 +08:00
} , ( ) = > { } ) ) ;
} ,
( ) = > { } ) ) ;
return ;
}
2023-01-31 16:17:21 +08:00
// Not using CreateSubDirectory as it throws unexpectedly when attempting to create a directory when already at the root of a disk.
// See https://cs.github.com/dotnet/runtime/blob/f1bdd5a6182f43f3928b389b03f7bc26f826c8bc/src/libraries/System.Private.CoreLib/src/System/IO/DirectoryInfo.cs#L88-L94
target = Directory . CreateDirectory ( Path . Combine ( target . FullName , @"osu-lazer" ) ) ;
2022-03-29 17:00:56 +08:00
}
2020-05-14 16:40:43 +08:00
}
catch ( Exception e )
{
2020-05-14 18:05:35 +08:00
Logger . Log ( $"Error during migration: {e.Message}" , level : LogLevel . Error ) ;
2020-05-14 16:40:43 +08:00
return ;
}
2020-05-13 21:58:05 +08:00
ValidForResume = false ;
2020-05-14 18:05:35 +08:00
BeginMigration ( target ) ;
2020-05-13 21:58:05 +08:00
}
2020-05-14 18:05:35 +08:00
protected virtual void BeginMigration ( DirectoryInfo target ) = > this . Push ( new MigrationRunScreen ( target ) ) ;
2020-05-13 21:58:05 +08:00
}
}