1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 01:27:29 +08:00
osu-lazer/osu.Game.Tournament/IO/TournamentStorage.cs

136 lines
4.7 KiB
C#
Raw Normal View History

// 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.Threading;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.IO;
using System.IO;
using osu.Game.Tournament.Configuration;
namespace osu.Game.Tournament.IO
{
public class TournamentStorage : WrappedStorage
{
private readonly Storage storage;
2020-06-11 21:05:28 +08:00
internal readonly TournamentVideoResourceStore VideoStore;
internal readonly Storage ConfigurationStorage;
2020-06-11 19:56:16 +08:00
private const string default_tournament = "default";
private const string config_directory = "config";
public TournamentStorage(Storage storage)
: base(storage.GetStorageForDirectory("tournaments"), string.Empty)
{
this.storage = storage;
TournamentStorageManager storageConfig = new TournamentStorageManager(storage);
2020-06-11 19:56:16 +08:00
var currentTournament = storageConfig.Get<string>(StorageConfig.CurrentTournament);
if (!string.IsNullOrEmpty(currentTournament))
{
2020-06-09 00:25:20 +08:00
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory(currentTournament));
}
else
{
2020-06-11 19:56:16 +08:00
Migrate();
storageConfig.Set(StorageConfig.CurrentTournament, default_tournament);
storageConfig.Save();
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory(default_tournament));
}
ConfigurationStorage = UnderlyingStorage.GetStorageForDirectory(config_directory);
2020-06-11 21:05:28 +08:00
VideoStore = new TournamentVideoResourceStore(this);
Logger.Log("Using tournament storage: " + GetFullPath(string.Empty));
}
2020-06-11 19:56:16 +08:00
internal void Migrate()
{
var source = new DirectoryInfo(storage.GetFullPath("tournament"));
2020-06-11 19:56:16 +08:00
var destination = new DirectoryInfo(GetFullPath(default_tournament));
var cfgDestination = new DirectoryInfo(GetFullPath(default_tournament + Path.DirectorySeparatorChar + config_directory));
2020-06-11 19:56:16 +08:00
if (!destination.Exists)
destination.Create();
2020-06-12 02:11:44 +08:00
if (!cfgDestination.Exists)
destination.CreateSubdirectory(config_directory);
2020-06-16 23:15:43 +08:00
2020-06-16 23:14:54 +08:00
moveFileIfExists("bracket.json", destination);
moveFileIfExists("drawings.txt", destination);
moveFileIfExists("drawings_results.txt", destination);
moveFileIfExists("drawings.ini", cfgDestination);
2020-06-11 19:56:16 +08:00
if (source.Exists)
{
Logger.Log("Migrating tournament assets to default tournament storage.");
copyRecursive(source, destination);
deleteRecursive(source);
}
}
2020-06-16 23:14:54 +08:00
private void moveFileIfExists(string file, DirectoryInfo destination)
{
if (storage.Exists(file))
{
Logger.Log($"Migrating {file} to default tournament storage.");
var fileInfo = new System.IO.FileInfo(storage.GetFullPath(file));
2020-06-16 23:15:43 +08:00
attemptOperation(() => fileInfo.CopyTo(Path.Combine(destination.FullName, fileInfo.Name), true));
fileInfo.Delete();
2020-06-16 23:14:54 +08:00
}
}
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);
}
}
}
}