mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 17:43:05 +08:00
Introduce new class MigratableStorage
This commit is contained in:
parent
02d66c4856
commit
dd9697032c
@ -11,7 +11,7 @@ using osu.Game.Tournament.Configuration;
|
|||||||
|
|
||||||
namespace osu.Game.Tournament.IO
|
namespace osu.Game.Tournament.IO
|
||||||
{
|
{
|
||||||
public class TournamentStorage : WrappedStorage
|
public class TournamentStorage : MigratableStorage
|
||||||
{
|
{
|
||||||
private readonly Storage storage;
|
private readonly Storage storage;
|
||||||
internal readonly TournamentVideoResourceStore VideoStore;
|
internal readonly TournamentVideoResourceStore VideoStore;
|
||||||
@ -52,8 +52,15 @@ namespace osu.Game.Tournament.IO
|
|||||||
var destination = new DirectoryInfo(GetFullPath(default_tournament));
|
var destination = new DirectoryInfo(GetFullPath(default_tournament));
|
||||||
var cfgDestination = new DirectoryInfo(GetFullPath(default_tournament + Path.DirectorySeparatorChar + config_directory));
|
var cfgDestination = new DirectoryInfo(GetFullPath(default_tournament + Path.DirectorySeparatorChar + config_directory));
|
||||||
|
|
||||||
if (!destination.Exists)
|
// if (!destination.Exists)
|
||||||
destination.Create();
|
// destination.Create();
|
||||||
|
|
||||||
|
if (source.Exists)
|
||||||
|
{
|
||||||
|
Logger.Log("Migrating tournament assets to default tournament storage.");
|
||||||
|
copyRecursive(source, destination);
|
||||||
|
deleteRecursive(source);
|
||||||
|
}
|
||||||
|
|
||||||
if (!cfgDestination.Exists)
|
if (!cfgDestination.Exists)
|
||||||
destination.CreateSubdirectory(config_directory);
|
destination.CreateSubdirectory(config_directory);
|
||||||
@ -62,13 +69,6 @@ namespace osu.Game.Tournament.IO
|
|||||||
moveFileIfExists("drawings.txt", destination);
|
moveFileIfExists("drawings.txt", destination);
|
||||||
moveFileIfExists("drawings_results.txt", destination);
|
moveFileIfExists("drawings_results.txt", destination);
|
||||||
moveFileIfExists("drawings.ini", cfgDestination);
|
moveFileIfExists("drawings.ini", cfgDestination);
|
||||||
|
|
||||||
if (source.Exists)
|
|
||||||
{
|
|
||||||
Logger.Log("Migrating tournament assets to default tournament storage.");
|
|
||||||
copyRecursive(source, destination);
|
|
||||||
deleteRecursive(source);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void moveFileIfExists(string file, DirectoryInfo destination)
|
private void moveFileIfExists(string file, DirectoryInfo destination)
|
||||||
@ -81,55 +81,5 @@ namespace osu.Game.Tournament.IO
|
|||||||
fileInfo.Delete();
|
fileInfo.Delete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void copyRecursive(DirectoryInfo source, DirectoryInfo destination)
|
|
||||||
{
|
|
||||||
// based off example code https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo
|
|
||||||
|
|
||||||
foreach (System.IO.FileInfo fi in source.GetFiles())
|
|
||||||
{
|
|
||||||
attemptOperation(() => fi.CopyTo(Path.Combine(destination.FullName, fi.Name), true));
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (DirectoryInfo dir in source.GetDirectories())
|
|
||||||
{
|
|
||||||
copyRecursive(dir, destination.CreateSubdirectory(dir.Name));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void deleteRecursive(DirectoryInfo target)
|
|
||||||
{
|
|
||||||
foreach (System.IO.FileInfo fi in target.GetFiles())
|
|
||||||
{
|
|
||||||
attemptOperation(() => fi.Delete());
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (DirectoryInfo dir in target.GetDirectories())
|
|
||||||
{
|
|
||||||
attemptOperation(() => dir.Delete(true));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (target.GetFiles().Length == 0 && target.GetDirectories().Length == 0)
|
|
||||||
attemptOperation(target.Delete);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void attemptOperation(Action action, int attempts = 10)
|
|
||||||
{
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
action();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
if (attempts-- == 0)
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
|
|
||||||
Thread.Sleep(250);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
95
osu.Game/IO/MigratableStorage.cs
Normal file
95
osu.Game/IO/MigratableStorage.cs
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
// 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.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using osu.Framework.Logging;
|
||||||
|
using osu.Framework.Platform;
|
||||||
|
using osu.Game.Configuration;
|
||||||
|
|
||||||
|
namespace osu.Game.IO
|
||||||
|
{
|
||||||
|
public abstract class MigratableStorage : WrappedStorage
|
||||||
|
{
|
||||||
|
|
||||||
|
virtual protected string[] IGNORE_DIRECTORIES { get; set; } = Array.Empty<string>();
|
||||||
|
|
||||||
|
virtual protected string[] IGNORE_FILES { get; set; } = Array.Empty<string>();
|
||||||
|
|
||||||
|
public MigratableStorage(Storage storage, string subPath = null)
|
||||||
|
: base(storage, subPath)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deleteRecursive(DirectoryInfo target, bool topLevelExcludes = true)
|
||||||
|
{
|
||||||
|
foreach (System.IO.FileInfo fi in target.GetFiles())
|
||||||
|
{
|
||||||
|
if (topLevelExcludes && IGNORE_FILES.Contains(fi.Name))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
attemptOperation(() => fi.Delete());
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (DirectoryInfo dir in target.GetDirectories())
|
||||||
|
{
|
||||||
|
if (topLevelExcludes && IGNORE_DIRECTORIES.Contains(dir.Name))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
attemptOperation(() => dir.Delete(true));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target.GetFiles().Length == 0 && target.GetDirectories().Length == 0)
|
||||||
|
attemptOperation(target.Delete);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void copyRecursive(DirectoryInfo source, DirectoryInfo destination, bool topLevelExcludes = true)
|
||||||
|
{
|
||||||
|
// based off example code https://docs.microsoft.com/en-us/dotnet/api/system.io.directoryinfo
|
||||||
|
if (!destination.Exists)
|
||||||
|
Directory.CreateDirectory(destination.FullName);
|
||||||
|
|
||||||
|
foreach (System.IO.FileInfo fi in source.GetFiles())
|
||||||
|
{
|
||||||
|
if (topLevelExcludes && IGNORE_FILES.Contains(fi.Name))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
attemptOperation(() => fi.CopyTo(Path.Combine(destination.FullName, fi.Name), true));
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (DirectoryInfo dir in source.GetDirectories())
|
||||||
|
{
|
||||||
|
if (topLevelExcludes && IGNORE_DIRECTORIES.Contains(dir.Name))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
copyRecursive(dir, destination.CreateSubdirectory(dir.Name), false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
protected static void attemptOperation(Action action, int attempts = 10)
|
||||||
|
{
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
action();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
if (attempts-- == 0)
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
|
Thread.Sleep(250);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user