mirror of
https://github.com/ppy/osu.git
synced 2025-01-28 05:22:54 +08:00
Merge pull request #9418 from smoogipoo/custom-storage-startup-exception
This commit is contained in:
commit
069a2c8307
@ -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,13 +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))
|
||||
ChangeTargetStorage(host.GetStorage(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
|
||||
{
|
||||
Storage userStorage = host.GetStorage(CustomStoragePath);
|
||||
|
||||
if (!userStorage.ExistsDirectory(".") || !userStorage.GetFiles(".").Any())
|
||||
error = OsuStorageError.AccessibleButEmpty;
|
||||
|
||||
ChangeTargetStorage(userStorage);
|
||||
}
|
||||
catch
|
||||
{
|
||||
error = OsuStorageError.NotAccessible;
|
||||
ChangeTargetStorage(lastStorage);
|
||||
}
|
||||
|
||||
return error == OsuStorageError.None;
|
||||
}
|
||||
|
||||
protected override void ChangeTargetStorage(Storage newStorage)
|
||||
@ -145,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,
|
||||
}
|
||||
}
|
||||
|
@ -42,25 +42,34 @@ namespace osu.Game.Overlays.Dialog
|
||||
set => icon.Icon = value;
|
||||
}
|
||||
|
||||
private string text;
|
||||
private string headerText;
|
||||
|
||||
public string HeaderText
|
||||
{
|
||||
get => text;
|
||||
get => headerText;
|
||||
set
|
||||
{
|
||||
if (text == value)
|
||||
if (headerText == value)
|
||||
return;
|
||||
|
||||
text = value;
|
||||
|
||||
headerText = value;
|
||||
header.Text = value;
|
||||
}
|
||||
}
|
||||
|
||||
private string bodyText;
|
||||
|
||||
public string BodyText
|
||||
{
|
||||
set => body.Text = value;
|
||||
get => bodyText;
|
||||
set
|
||||
{
|
||||
if (bodyText == value)
|
||||
return;
|
||||
|
||||
bodyText = value;
|
||||
body.Text = value;
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<PopupDialogButton> Buttons
|
||||
|
34
osu.Game/Screens/Menu/ConfirmExitDialog.cs
Normal file
34
osu.Game/Screens/Menu/ConfirmExitDialog.cs
Normal file
@ -0,0 +1,34 @@
|
||||
// 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.
|
||||
|
||||
using System;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Game.Overlays.Dialog;
|
||||
|
||||
namespace osu.Game.Screens.Menu
|
||||
{
|
||||
public class ConfirmExitDialog : PopupDialog
|
||||
{
|
||||
public ConfirmExitDialog(Action confirm, Action cancel)
|
||||
{
|
||||
HeaderText = "Are you sure you want to exit?";
|
||||
BodyText = "Last chance to back out.";
|
||||
|
||||
Icon = FontAwesome.Solid.ExclamationTriangle;
|
||||
|
||||
Buttons = new PopupDialogButton[]
|
||||
{
|
||||
new PopupDialogOkButton
|
||||
{
|
||||
Text = @"Goodbye",
|
||||
Action = confirm
|
||||
},
|
||||
new PopupDialogCancelButton
|
||||
{
|
||||
Text = @"Just a little more",
|
||||
Action = cancel
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
// 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.
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using osuTK;
|
||||
using osuTK.Graphics;
|
||||
@ -9,15 +8,14 @@ using osu.Framework.Allocation;
|
||||
using osu.Framework.Audio.Track;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Framework.Platform;
|
||||
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;
|
||||
using osu.Game.Screens.Backgrounds;
|
||||
using osu.Game.Screens.Edit;
|
||||
using osu.Game.Screens.Multi;
|
||||
@ -171,6 +169,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 +188,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;
|
||||
@ -283,30 +287,5 @@ namespace osu.Game.Screens.Menu
|
||||
this.FadeOut(3000);
|
||||
return base.OnExiting(next);
|
||||
}
|
||||
|
||||
private class ConfirmExitDialog : PopupDialog
|
||||
{
|
||||
public ConfirmExitDialog(Action confirm, Action cancel)
|
||||
{
|
||||
HeaderText = "Are you sure you want to exit?";
|
||||
BodyText = "Last chance to back out.";
|
||||
|
||||
Icon = FontAwesome.Solid.ExclamationTriangle;
|
||||
|
||||
Buttons = new PopupDialogButton[]
|
||||
{
|
||||
new PopupDialogOkButton
|
||||
{
|
||||
Text = @"Goodbye",
|
||||
Action = confirm
|
||||
},
|
||||
new PopupDialogCancelButton
|
||||
{
|
||||
Text = @"Just a little more",
|
||||
Action = cancel
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
79
osu.Game/Screens/Menu/StorageErrorDialog.cs
Normal file
79
osu.Game/Screens/Menu/StorageErrorDialog.cs
Normal file
@ -0,0 +1,79 @@
|
||||
// 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.
|
||||
|
||||
using System.Collections.Generic;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Graphics.Sprites;
|
||||
using osu.Game.IO;
|
||||
using osu.Game.Overlays;
|
||||
using osu.Game.Overlays.Dialog;
|
||||
|
||||
namespace osu.Game.Screens.Menu
|
||||
{
|
||||
public 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>();
|
||||
|
||||
switch (error)
|
||||
{
|
||||
case OsuStorageError.NotAccessible:
|
||||
BodyText = $"The specified osu! data location (\"{storage.CustomStoragePath}\") is not accessible. If it is on external storage, please reconnect the device and try again.";
|
||||
|
||||
buttons.AddRange(new PopupDialogButton[]
|
||||
{
|
||||
new PopupDialogCancelButton
|
||||
{
|
||||
Text = "Try again",
|
||||
Action = () =>
|
||||
{
|
||||
if (!storage.TryChangeToCustomStorage(out var nextError))
|
||||
dialogOverlay.Push(new StorageErrorDialog(storage, nextError));
|
||||
}
|
||||
},
|
||||
new PopupDialogCancelButton
|
||||
{
|
||||
Text = "Use default location until restart",
|
||||
},
|
||||
new PopupDialogOkButton
|
||||
{
|
||||
Text = "Reset to default location",
|
||||
Action = storage.ResetCustomStoragePath
|
||||
},
|
||||
});
|
||||
break;
|
||||
|
||||
case OsuStorageError.AccessibleButEmpty:
|
||||
BodyText = $"The specified osu! data location (\"{storage.CustomStoragePath}\") is empty. If you have moved the files, please close osu! and move them back.";
|
||||
|
||||
// Todo: Provide the option to search for the files similar to migration.
|
||||
buttons.AddRange(new PopupDialogButton[]
|
||||
{
|
||||
new PopupDialogCancelButton
|
||||
{
|
||||
Text = "Start fresh at specified location"
|
||||
},
|
||||
new PopupDialogOkButton
|
||||
{
|
||||
Text = "Reset to default location",
|
||||
Action = storage.ResetCustomStoragePath
|
||||
},
|
||||
});
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Buttons = buttons;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user