2020-05-06 09:27:10 +00: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.
|
|
|
|
|
2020-05-06 09:31:36 +00:00
|
|
|
using System;
|
2020-07-06 13:03:09 +00:00
|
|
|
using System.Diagnostics;
|
2020-05-06 09:31:36 +00:00
|
|
|
using System.IO;
|
2020-05-07 10:00:59 +00:00
|
|
|
using System.Linq;
|
2020-05-11 12:38:41 +00:00
|
|
|
using System.Threading;
|
2020-07-06 13:03:09 +00:00
|
|
|
using JetBrains.Annotations;
|
2020-05-06 09:27:10 +00:00
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
|
|
|
|
namespace osu.Game.IO
|
|
|
|
{
|
|
|
|
public class OsuStorage : WrappedStorage
|
|
|
|
{
|
2020-07-06 13:03:09 +00:00
|
|
|
/// <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(".");
|
|
|
|
|
2020-05-06 09:31:36 +00:00
|
|
|
private readonly GameHost host;
|
|
|
|
private readonly StorageConfigManager storageConfig;
|
2020-07-06 13:03:09 +00:00
|
|
|
private readonly Storage defaultStorage;
|
2020-05-06 09:31:36 +00:00
|
|
|
|
2020-07-06 13:03:09 +00:00
|
|
|
public static readonly string[] IGNORE_DIRECTORIES = { "cache" };
|
2020-05-07 10:00:59 +00:00
|
|
|
|
2020-07-06 13:03:09 +00:00
|
|
|
public static readonly string[] IGNORE_FILES =
|
2020-05-07 10:00:59 +00:00
|
|
|
{
|
|
|
|
"framework.ini",
|
|
|
|
"storage.ini"
|
|
|
|
};
|
|
|
|
|
2020-07-01 08:12:07 +00:00
|
|
|
public OsuStorage(GameHost host, Storage defaultStorage)
|
|
|
|
: base(defaultStorage, string.Empty)
|
2020-05-06 09:27:10 +00:00
|
|
|
{
|
2020-05-06 09:31:36 +00:00
|
|
|
this.host = host;
|
2020-07-06 13:03:09 +00:00
|
|
|
this.defaultStorage = defaultStorage;
|
2020-05-06 09:31:36 +00:00
|
|
|
|
2020-07-01 08:12:07 +00:00
|
|
|
storageConfig = new StorageConfigManager(defaultStorage);
|
2020-05-06 09:27:10 +00:00
|
|
|
|
2020-07-06 13:03:09 +00:00
|
|
|
if (!string.IsNullOrEmpty(CustomStoragePath))
|
|
|
|
TryChangeToCustomStorage(out Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <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;
|
2020-05-06 09:27:10 +00:00
|
|
|
|
2020-07-06 13:03:09 +00:00
|
|
|
try
|
2020-07-01 08:47:29 +00:00
|
|
|
{
|
2020-07-06 13:03:09 +00:00
|
|
|
Storage userStorage = host.GetStorage(CustomStoragePath);
|
|
|
|
|
|
|
|
if (!userStorage.GetFiles(".").Any())
|
|
|
|
error = OsuStorageError.AccessibleButEmpty;
|
|
|
|
|
|
|
|
ChangeTargetStorage(userStorage);
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
error = OsuStorageError.NotAccessible;
|
|
|
|
ChangeTargetStorage(lastStorage);
|
2020-07-01 08:47:29 +00:00
|
|
|
}
|
2020-07-06 13:03:09 +00:00
|
|
|
|
|
|
|
return error == OsuStorageError.None;
|
2020-05-11 12:38:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ChangeTargetStorage(Storage newStorage)
|
|
|
|
{
|
|
|
|
base.ChangeTargetStorage(newStorage);
|
|
|
|
Logger.Storage = UnderlyingStorage.GetStorageForDirectory("logs");
|
2020-05-06 09:27:10 +00:00
|
|
|
}
|
2020-05-06 09:31:36 +00:00
|
|
|
|
|
|
|
public void Migrate(string newLocation)
|
|
|
|
{
|
2020-05-09 11:13:31 +00:00
|
|
|
var source = new DirectoryInfo(GetFullPath("."));
|
|
|
|
var destination = new DirectoryInfo(newLocation);
|
2020-05-06 09:31:36 +00:00
|
|
|
|
2020-05-14 13:42:42 +00:00
|
|
|
// using Uri is the easiest way to check equality and contains (https://stackoverflow.com/a/7710620)
|
|
|
|
var sourceUri = new Uri(source.FullName + Path.DirectorySeparatorChar);
|
|
|
|
var destinationUri = new Uri(destination.FullName + Path.DirectorySeparatorChar);
|
|
|
|
|
|
|
|
if (sourceUri == destinationUri)
|
2020-05-13 13:57:41 +00:00
|
|
|
throw new ArgumentException("Destination provided is already the current location", nameof(newLocation));
|
|
|
|
|
2020-05-14 13:42:42 +00:00
|
|
|
if (sourceUri.IsBaseOf(destinationUri))
|
2020-05-14 13:42:42 +00:00
|
|
|
throw new ArgumentException("Destination provided is inside the source", nameof(newLocation));
|
|
|
|
|
2020-05-06 09:31:36 +00:00
|
|
|
// ensure the new location has no files present, else hard abort
|
2020-05-09 11:13:31 +00:00
|
|
|
if (destination.Exists)
|
2020-05-06 09:31:36 +00:00
|
|
|
{
|
2020-05-13 11:19:14 +00:00
|
|
|
if (destination.GetFiles().Length > 0 || destination.GetDirectories().Length > 0)
|
2020-05-13 13:57:41 +00:00
|
|
|
throw new ArgumentException("Destination provided already has files or directories present", nameof(newLocation));
|
2020-05-06 09:31:36 +00:00
|
|
|
|
2020-05-09 11:13:31 +00:00
|
|
|
deleteRecursive(destination);
|
2020-05-06 09:31:36 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 10:00:59 +00:00
|
|
|
copyRecursive(source, destination);
|
2020-05-06 09:31:36 +00:00
|
|
|
|
|
|
|
ChangeTargetStorage(host.GetStorage(newLocation));
|
|
|
|
|
|
|
|
storageConfig.Set(StorageConfig.FullPath, newLocation);
|
|
|
|
storageConfig.Save();
|
2020-05-07 10:00:59 +00:00
|
|
|
|
|
|
|
deleteRecursive(source);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void deleteRecursive(DirectoryInfo target, bool topLevelExcludes = true)
|
|
|
|
{
|
|
|
|
foreach (System.IO.FileInfo fi in target.GetFiles())
|
|
|
|
{
|
2020-05-12 03:39:04 +00:00
|
|
|
if (topLevelExcludes && IGNORE_FILES.Contains(fi.Name))
|
2020-05-07 10:00:59 +00:00
|
|
|
continue;
|
|
|
|
|
2020-05-15 04:19:03 +00:00
|
|
|
attemptOperation(() => fi.Delete());
|
2020-05-07 10:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DirectoryInfo dir in target.GetDirectories())
|
|
|
|
{
|
2020-05-12 03:39:04 +00:00
|
|
|
if (topLevelExcludes && IGNORE_DIRECTORIES.Contains(dir.Name))
|
2020-05-07 10:00:59 +00:00
|
|
|
continue;
|
|
|
|
|
2020-05-15 04:19:03 +00:00
|
|
|
attemptOperation(() => dir.Delete(true));
|
2020-05-07 10:00:59 +00:00
|
|
|
}
|
2020-05-13 14:02:28 +00:00
|
|
|
|
|
|
|
if (target.GetFiles().Length == 0 && target.GetDirectories().Length == 0)
|
2020-05-15 04:19:03 +00:00
|
|
|
attemptOperation(target.Delete);
|
2020-05-07 10:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static void copyRecursive(DirectoryInfo source, DirectoryInfo destination, bool topLevelExcludes = true)
|
|
|
|
{
|
|
|
|
// based off example code https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo
|
|
|
|
Directory.CreateDirectory(destination.FullName);
|
|
|
|
|
|
|
|
foreach (System.IO.FileInfo fi in source.GetFiles())
|
|
|
|
{
|
2020-05-12 03:39:04 +00:00
|
|
|
if (topLevelExcludes && IGNORE_FILES.Contains(fi.Name))
|
2020-05-07 10:00:59 +00:00
|
|
|
continue;
|
|
|
|
|
2020-05-15 04:19:03 +00:00
|
|
|
attemptOperation(() => fi.CopyTo(Path.Combine(destination.FullName, fi.Name), true));
|
2020-05-07 10:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach (DirectoryInfo dir in source.GetDirectories())
|
|
|
|
{
|
2020-05-12 03:39:04 +00:00
|
|
|
if (topLevelExcludes && IGNORE_DIRECTORIES.Contains(dir.Name))
|
2020-05-07 10:00:59 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
copyRecursive(dir, destination.CreateSubdirectory(dir.Name), false);
|
|
|
|
}
|
2020-05-06 09:31:36 +00:00
|
|
|
}
|
2020-05-13 11:29:15 +00:00
|
|
|
|
2020-05-15 04:19:03 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Attempt an IO operation multiple times and only throw if none of the attempts succeed.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="action">The action to perform.</param>
|
|
|
|
/// <param name="attempts">The number of attempts (250ms wait between each).</param>
|
|
|
|
private static void attemptOperation(Action action, int attempts = 10)
|
2020-05-13 11:29:15 +00:00
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-05-15 04:19:03 +00:00
|
|
|
action();
|
2020-05-13 11:29:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
2020-05-15 04:19:03 +00:00
|
|
|
if (attempts-- == 0)
|
2020-05-13 11:29:15 +00:00
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
2020-05-15 04:19:03 +00:00
|
|
|
Thread.Sleep(250);
|
2020-05-13 11:29:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 09:27:10 +00:00
|
|
|
}
|
2020-07-06 13:03:09 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2020-05-06 09:27:10 +00:00
|
|
|
}
|