From 1fb3d11591858b9f0e05e35f41e4fbf4840b3b0e Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 29 Mar 2022 18:00:56 +0900 Subject: [PATCH] Add ability to "migrate" data to another folder which has an existing install Until now, migrating would always attempt to move files. There's a chance that a user is reinstalling osu! but has their data at a custom location. We want to allow the chance for them to continue using the external data. This seems like the easiest way to make it work. Would be nice if we had a `Game.Restart()` method, but maybe this is enough for now? Note that further down the road we will probably prompt the user to potentially select a custom install path (including one with existing data) before osu! gets to writing anything. --- osu.Game/IO/OsuStorage.cs | 17 +++++++--- .../Maintenance/MigrationSelectScreen.cs | 32 ++++++++++++++++++- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/osu.Game/IO/OsuStorage.cs b/osu.Game/IO/OsuStorage.cs index 6e7cb545e3..a3f7b4bfec 100644 --- a/osu.Game/IO/OsuStorage.cs +++ b/osu.Game/IO/OsuStorage.cs @@ -64,12 +64,22 @@ namespace osu.Game.IO /// public void ResetCustomStoragePath() { - storageConfig.SetValue(StorageConfig.FullPath, string.Empty); - storageConfig.Save(); + ChangeDataPath(string.Empty); ChangeTargetStorage(defaultStorage); } + /// + /// Updates the target data path without immediately switching. + /// This does NOT migrate any data. + /// The game should immediately be restarted after calling this. + /// + public void ChangeDataPath(string newPath) + { + storageConfig.SetValue(StorageConfig.FullPath, newPath); + storageConfig.Save(); + } + /// /// Attempts to change to the user's custom storage path. /// @@ -117,8 +127,7 @@ namespace osu.Game.IO { bool cleanupSucceeded = base.Migrate(newStorage); - storageConfig.SetValue(StorageConfig.FullPath, newStorage.GetFullPath(".")); - storageConfig.Save(); + ChangeDataPath(newStorage.GetFullPath(".")); return cleanupSucceeded; } diff --git a/osu.Game/Overlays/Settings/Sections/Maintenance/MigrationSelectScreen.cs b/osu.Game/Overlays/Settings/Sections/Maintenance/MigrationSelectScreen.cs index 1a60ab0638..9b20f2c0c3 100644 --- a/osu.Game/Overlays/Settings/Sections/Maintenance/MigrationSelectScreen.cs +++ b/osu.Game/Overlays/Settings/Sections/Maintenance/MigrationSelectScreen.cs @@ -3,11 +3,14 @@ using System; using System.IO; +using System.Linq; using osu.Framework.Allocation; using osu.Framework.Localisation; using osu.Framework.Logging; using osu.Framework.Platform; using osu.Framework.Screens; +using osu.Game.IO; +using osu.Game.Overlays.Dialog; namespace osu.Game.Overlays.Settings.Sections.Maintenance { @@ -16,6 +19,12 @@ namespace osu.Game.Overlays.Settings.Sections.Maintenance [Resolved] private Storage storage { get; set; } + [Resolved] + private OsuGameBase game { get; set; } + + [Resolved(canBeNull: true)] + private DialogOverlay dialogOverlay { get; set; } + protected override DirectoryInfo InitialPath => new DirectoryInfo(storage.GetFullPath(string.Empty)).Parent; public override bool AllowExternalScreenChange => false; @@ -32,8 +41,29 @@ namespace osu.Game.Overlays.Settings.Sections.Maintenance try { - if (target.GetDirectories().Length > 0 || target.GetFiles().Length > 0) + var directoryInfos = target.GetDirectories(); + var fileInfos = target.GetFiles(); + + if (directoryInfos.Length > 0 || fileInfos.Length > 0) + { + // Quick test for whether there's already an osu! install at the target path. + if (fileInfos.Any(f => f.Name == @"client.realm")) + { + dialogOverlay.Push(new ConfirmDialog("The target directory already seems to have an osu! install. Use this data instead?", () => + { + dialogOverlay.Push(new ConfirmDialog("To complete this operation, osu! will close. Please open it again to use the new data location.", () => + { + (storage as OsuStorage)?.ChangeDataPath(target.FullName); + game.GracefullyExit(); + }, () => { })); + }, + () => { })); + + return; + } + target = target.CreateSubdirectory("osu-lazer"); + } } catch (Exception e) {