2020-05-06 17:27:10 +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.
|
|
|
|
|
2020-05-06 17:31:36 +08:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2020-05-07 18:00:59 +08:00
|
|
|
using System.Linq;
|
2020-05-11 20:38:41 +08:00
|
|
|
using System.Threading;
|
2020-05-06 17:27:10 +08:00
|
|
|
using osu.Framework.Logging;
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Game.Configuration;
|
|
|
|
|
|
|
|
namespace osu.Game.IO
|
|
|
|
{
|
2020-06-22 17:38:50 +08:00
|
|
|
public class OsuStorage : MigratableStorage
|
2020-05-06 17:27:10 +08:00
|
|
|
{
|
2020-05-06 17:31:36 +08:00
|
|
|
private readonly GameHost host;
|
|
|
|
private readonly StorageConfigManager storageConfig;
|
|
|
|
|
2020-05-06 17:27:10 +08:00
|
|
|
public OsuStorage(GameHost host)
|
|
|
|
: base(host.Storage, string.Empty)
|
|
|
|
{
|
2020-05-06 17:31:36 +08:00
|
|
|
this.host = host;
|
|
|
|
|
|
|
|
storageConfig = new StorageConfigManager(host.Storage);
|
2020-05-06 17:27:10 +08:00
|
|
|
|
|
|
|
var customStoragePath = storageConfig.Get<string>(StorageConfig.FullPath);
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(customStoragePath))
|
|
|
|
ChangeTargetStorage(host.GetStorage(customStoragePath));
|
2020-05-11 20:38:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ChangeTargetStorage(Storage newStorage)
|
|
|
|
{
|
|
|
|
base.ChangeTargetStorage(newStorage);
|
|
|
|
Logger.Storage = UnderlyingStorage.GetStorageForDirectory("logs");
|
2020-05-06 17:27:10 +08:00
|
|
|
}
|
2020-05-06 17:31:36 +08:00
|
|
|
|
|
|
|
public void Migrate(string newLocation)
|
|
|
|
{
|
2020-05-09 19:13:31 +08:00
|
|
|
var source = new DirectoryInfo(GetFullPath("."));
|
|
|
|
var destination = new DirectoryInfo(newLocation);
|
2020-05-06 17:31:36 +08:00
|
|
|
|
2020-05-14 21:42:42 +08: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 21:57:41 +08:00
|
|
|
throw new ArgumentException("Destination provided is already the current location", nameof(newLocation));
|
|
|
|
|
2020-05-14 21:42:42 +08:00
|
|
|
if (sourceUri.IsBaseOf(destinationUri))
|
2020-05-14 21:42:42 +08:00
|
|
|
throw new ArgumentException("Destination provided is inside the source", nameof(newLocation));
|
|
|
|
|
2020-05-06 17:31:36 +08:00
|
|
|
// ensure the new location has no files present, else hard abort
|
2020-05-09 19:13:31 +08:00
|
|
|
if (destination.Exists)
|
2020-05-06 17:31:36 +08:00
|
|
|
{
|
2020-05-13 19:19:14 +08:00
|
|
|
if (destination.GetFiles().Length > 0 || destination.GetDirectories().Length > 0)
|
2020-05-13 21:57:41 +08:00
|
|
|
throw new ArgumentException("Destination provided already has files or directories present", nameof(newLocation));
|
2020-05-06 17:31:36 +08:00
|
|
|
}
|
|
|
|
|
2020-05-07 18:00:59 +08:00
|
|
|
copyRecursive(source, destination);
|
2020-05-06 17:31:36 +08:00
|
|
|
|
|
|
|
ChangeTargetStorage(host.GetStorage(newLocation));
|
|
|
|
|
|
|
|
storageConfig.Set(StorageConfig.FullPath, newLocation);
|
|
|
|
storageConfig.Save();
|
2020-05-07 18:00:59 +08:00
|
|
|
|
|
|
|
deleteRecursive(source);
|
|
|
|
}
|
2020-05-06 17:27:10 +08:00
|
|
|
}
|
|
|
|
}
|