1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-26 18:03:11 +08:00

Add dialog for storage options

This commit is contained in:
smoogipoo 2020-07-06 22:03:09 +09:00
parent dbbee481f6
commit 1effe71ec2
2 changed files with 161 additions and 13 deletions

View File

@ -2,9 +2,11 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using JetBrains.Annotations;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.Configuration;
@ -13,12 +15,30 @@ namespace osu.Game.IO
{
public class OsuStorage : WrappedStorage
{
/// <summary>
/// Indicates the error (if any) that occurred when initialising the custom storage during initial startup.
/// </summary>
public readonly OsuStorageError Error;
/// <summary>
/// The custom storage path as selected by the user.
/// </summary>
[CanBeNull]
public string CustomStoragePath => storageConfig.Get<string>(StorageConfig.FullPath);
/// <summary>
/// The default storage path to be used if a custom storage path hasn't been selected or is not accessible.
/// </summary>
[NotNull]
public string DefaultStoragePath => defaultStorage.GetFullPath(".");
private readonly GameHost host;
private readonly StorageConfigManager storageConfig;
private readonly Storage defaultStorage;
internal static readonly string[] IGNORE_DIRECTORIES = { "cache" };
public static readonly string[] IGNORE_DIRECTORIES = { "cache" };
internal static readonly string[] IGNORE_FILES =
public static readonly string[] IGNORE_FILES =
{
"framework.ini",
"storage.ini"
@ -28,23 +48,53 @@ namespace osu.Game.IO
: base(defaultStorage, string.Empty)
{
this.host = host;
this.defaultStorage = defaultStorage;
storageConfig = new StorageConfigManager(defaultStorage);
var customStoragePath = storageConfig.Get<string>(StorageConfig.FullPath);
if (!string.IsNullOrEmpty(CustomStoragePath))
TryChangeToCustomStorage(out Error);
}
if (!string.IsNullOrEmpty(customStoragePath))
/// <summary>
/// Resets the custom storage path, changing the target storage to the default location.
/// </summary>
public void ResetCustomStoragePath()
{
storageConfig.Set(StorageConfig.FullPath, string.Empty);
storageConfig.Save();
ChangeTargetStorage(defaultStorage);
}
/// <summary>
/// Attempts to change to the user's custom storage path.
/// </summary>
/// <param name="error">The error that occurred.</param>
/// <returns>Whether the custom storage path was used successfully. If not, <paramref name="error"/> will be populated with the reason.</returns>
public bool TryChangeToCustomStorage(out OsuStorageError error)
{
Debug.Assert(!string.IsNullOrEmpty(CustomStoragePath));
error = OsuStorageError.None;
Storage lastStorage = UnderlyingStorage;
try
{
try
{
ChangeTargetStorage(host.GetStorage(customStoragePath));
}
catch (Exception ex)
{
Logger.Log($"Couldn't use custom storage path ({customStoragePath}): {ex}. Using default path.", LoggingTarget.Runtime, LogLevel.Error);
ChangeTargetStorage(defaultStorage);
}
Storage userStorage = host.GetStorage(CustomStoragePath);
if (!userStorage.GetFiles(".").Any())
error = OsuStorageError.AccessibleButEmpty;
ChangeTargetStorage(userStorage);
}
catch
{
error = OsuStorageError.NotAccessible;
ChangeTargetStorage(lastStorage);
}
return error == OsuStorageError.None;
}
protected override void ChangeTargetStorage(Storage newStorage)
@ -155,4 +205,23 @@ namespace osu.Game.IO
}
}
}
public enum OsuStorageError
{
/// <summary>
/// No error.
/// </summary>
None,
/// <summary>
/// Occurs when the target storage directory is accessible but does not already contain game files.
/// Only happens when the user changes the storage directory and then moves the files manually or mounts a different device to the same path.
/// </summary>
AccessibleButEmpty,
/// <summary>
/// Occurs when the target storage directory cannot be accessed at all.
/// </summary>
NotAccessible,
}
}

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using osuTK;
using osuTK.Graphics;
@ -15,6 +16,7 @@ using osu.Framework.Screens;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.IO;
using osu.Game.Online.API;
using osu.Game.Overlays;
using osu.Game.Overlays.Dialog;
@ -171,6 +173,9 @@ namespace osu.Game.Screens.Menu
return s;
}
[Resolved]
private Storage storage { get; set; }
public override void OnEntering(IScreen last)
{
base.OnEntering(last);
@ -187,6 +192,9 @@ namespace osu.Game.Screens.Menu
Track.Start();
}
}
if (storage is OsuStorage osuStorage && osuStorage.Error != OsuStorageError.None)
dialogOverlay?.Push(new StorageErrorDialog(osuStorage, osuStorage.Error));
}
private bool exitConfirmed;
@ -308,5 +316,76 @@ namespace osu.Game.Screens.Menu
};
}
}
private class StorageErrorDialog : PopupDialog
{
[Resolved]
private DialogOverlay dialogOverlay { get; set; }
[Resolved]
private OsuGameBase osuGame { get; set; }
public StorageErrorDialog(OsuStorage storage, OsuStorageError error)
{
HeaderText = "osu! storage error";
Icon = FontAwesome.Solid.ExclamationTriangle;
var buttons = new List<PopupDialogButton>();
BodyText = $"osu! encountered an error when trying to use the custom storage path ('{storage.CustomStoragePath}').\n\n";
switch (error)
{
case OsuStorageError.NotAccessible:
BodyText += $"The default storage path ('{storage.DefaultStoragePath}') is currently being used because the custom storage path is not accessible.\n\n"
+ "Is it on a removable device that is not currently connected?";
buttons.AddRange(new PopupDialogButton[]
{
new PopupDialogOkButton
{
Text = "Try again",
Action = () =>
{
if (!storage.TryChangeToCustomStorage(out var nextError))
dialogOverlay.Push(new StorageErrorDialog(storage, nextError));
}
},
new PopupDialogOkButton
{
Text = "Use the default path from now on",
Action = storage.ResetCustomStoragePath
},
new PopupDialogCancelButton
{
Text = "Only use the default path for this session",
},
});
break;
case OsuStorageError.AccessibleButEmpty:
BodyText += "The custom storage path is currently being used but is empty.\n\n"
+ "Have you moved the files elsewhere?";
// Todo: Provide the option to search for the files similar to migration.
buttons.AddRange(new PopupDialogButton[]
{
new PopupDialogOkButton
{
Text = "Reset to default",
Action = storage.ResetCustomStoragePath
},
new PopupDialogCancelButton
{
Text = "Keep using the custom path"
}
});
break;
}
Buttons = buttons;
}
}
}
}