2020-03-12 04:26:58 +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-06-08 01:03:57 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading;
|
2020-06-07 22:46:40 +00:00
|
|
|
using osu.Framework.Logging;
|
2020-03-12 04:26:58 +00:00
|
|
|
using osu.Framework.Platform;
|
2020-06-07 22:46:40 +00:00
|
|
|
using osu.Game.IO;
|
2020-06-08 01:03:57 +00:00
|
|
|
using System.IO;
|
2020-06-07 22:46:40 +00:00
|
|
|
using osu.Game.Tournament.Configuration;
|
2020-03-12 04:26:58 +00:00
|
|
|
|
2020-06-09 15:28:42 +00:00
|
|
|
namespace osu.Game.Tournament.IO
|
2020-03-12 04:26:58 +00:00
|
|
|
{
|
2020-06-11 16:00:47 +00:00
|
|
|
public class TournamentStorage : WrappedStorage
|
2020-06-07 22:46:40 +00:00
|
|
|
{
|
|
|
|
private readonly GameHost host;
|
2020-06-11 13:05:28 +00:00
|
|
|
internal readonly TournamentVideoResourceStore VideoStore;
|
2020-06-11 16:07:24 +00:00
|
|
|
internal readonly Storage ConfigurationStorage;
|
2020-06-11 11:56:16 +00:00
|
|
|
private const string default_tournament = "default";
|
2020-06-07 22:46:40 +00:00
|
|
|
|
2020-06-07 22:47:47 +00:00
|
|
|
public TournamentStorage(GameHost host)
|
2020-06-08 16:25:20 +00:00
|
|
|
: base(host.Storage.GetStorageForDirectory("tournaments"), string.Empty)
|
2020-06-07 22:46:40 +00:00
|
|
|
{
|
|
|
|
this.host = host;
|
|
|
|
|
2020-06-11 11:56:16 +00:00
|
|
|
TournamentStorageManager storageConfig = new TournamentStorageManager(host.Storage);
|
|
|
|
|
2020-06-08 01:03:57 +00:00
|
|
|
var currentTournament = storageConfig.Get<string>(StorageConfig.CurrentTournament);
|
2020-06-07 22:46:40 +00:00
|
|
|
|
2020-06-08 01:03:57 +00:00
|
|
|
if (!string.IsNullOrEmpty(currentTournament))
|
2020-06-07 22:46:40 +00:00
|
|
|
{
|
2020-06-08 16:25:20 +00:00
|
|
|
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory(currentTournament));
|
2020-06-07 22:46:40 +00:00
|
|
|
}
|
2020-06-08 01:03:57 +00:00
|
|
|
else
|
|
|
|
{
|
2020-06-11 11:56:16 +00:00
|
|
|
Migrate();
|
|
|
|
storageConfig.Set(StorageConfig.CurrentTournament, default_tournament);
|
|
|
|
storageConfig.Save();
|
|
|
|
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory(default_tournament));
|
2020-06-08 01:03:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 16:07:24 +00:00
|
|
|
ConfigurationStorage = UnderlyingStorage.GetStorageForDirectory("config");
|
|
|
|
|
2020-06-11 13:05:28 +00:00
|
|
|
VideoStore = new TournamentVideoResourceStore(this);
|
2020-06-07 22:46:40 +00:00
|
|
|
Logger.Log("Using tournament storage: " + GetFullPath(string.Empty));
|
|
|
|
}
|
2020-06-08 01:03:57 +00:00
|
|
|
|
2020-06-11 11:56:16 +00:00
|
|
|
internal void Migrate()
|
2020-06-08 01:03:57 +00:00
|
|
|
{
|
2020-06-08 16:25:20 +00:00
|
|
|
var source = new DirectoryInfo(host.Storage.GetFullPath("tournament"));
|
2020-06-11 11:56:16 +00:00
|
|
|
var destination = new DirectoryInfo(GetFullPath(default_tournament));
|
2020-06-08 01:03:57 +00:00
|
|
|
|
2020-06-11 11:56:16 +00:00
|
|
|
if (!destination.Exists)
|
|
|
|
destination.Create();
|
2020-06-08 01:12:37 +00:00
|
|
|
|
2020-06-08 01:03:57 +00:00
|
|
|
if (host.Storage.Exists("bracket.json"))
|
2020-06-08 01:12:37 +00:00
|
|
|
{
|
2020-06-08 01:03:57 +00:00
|
|
|
Logger.Log("Migrating bracket to default tournament storage.");
|
2020-06-08 16:25:20 +00:00
|
|
|
var bracketFile = new System.IO.FileInfo(host.Storage.GetFullPath("bracket.json"));
|
2020-06-11 15:51:07 +00:00
|
|
|
moveFile(bracketFile, destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host.Storage.Exists("drawings.txt"))
|
|
|
|
{
|
|
|
|
Logger.Log("Migrating drawings to default tournament storage.");
|
|
|
|
var drawingsFile = new System.IO.FileInfo(host.Storage.GetFullPath("drawings.txt"));
|
|
|
|
moveFile(drawingsFile, destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host.Storage.Exists("drawings_results.txt"))
|
|
|
|
{
|
|
|
|
Logger.Log("Migrating drawings results to default tournament storage.");
|
|
|
|
var drawingsResultsFile = new System.IO.FileInfo(host.Storage.GetFullPath("drawings_results.txt"));
|
|
|
|
moveFile(drawingsResultsFile, destination);
|
2020-06-08 01:03:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 11:56:16 +00:00
|
|
|
if (source.Exists)
|
|
|
|
{
|
|
|
|
Logger.Log("Migrating tournament assets to default tournament storage.");
|
|
|
|
copyRecursive(source, destination);
|
|
|
|
deleteRecursive(source);
|
|
|
|
}
|
2020-06-08 01:03:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:51:44 +00:00
|
|
|
private void copyRecursive(DirectoryInfo source, DirectoryInfo destination)
|
2020-06-08 01:03:57 +00:00
|
|
|
{
|
|
|
|
// 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())
|
|
|
|
{
|
2020-06-08 17:51:44 +00:00
|
|
|
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));
|
2020-06-08 01:03:57 +00:00
|
|
|
}
|
2020-06-08 17:51:44 +00:00
|
|
|
|
|
|
|
if (target.GetFiles().Length == 0 && target.GetDirectories().Length == 0)
|
|
|
|
attemptOperation(target.Delete);
|
2020-06-08 01:03:57 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 15:51:07 +00:00
|
|
|
private void moveFile(System.IO.FileInfo file, DirectoryInfo destination)
|
|
|
|
{
|
|
|
|
attemptOperation(() => file.CopyTo(Path.Combine(destination.FullName, file.Name), true));
|
|
|
|
file.Delete();
|
|
|
|
}
|
|
|
|
|
2020-06-08 01:03:57 +00:00
|
|
|
private void attemptOperation(Action action, int attempts = 10)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
action();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
if (attempts-- == 0)
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.Sleep(250);
|
|
|
|
}
|
|
|
|
}
|
2020-06-07 22:46:40 +00:00
|
|
|
}
|
2020-03-12 04:26:58 +00:00
|
|
|
}
|