2020-03-12 12:26:58 +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-06-08 09:03:57 +08:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
2020-03-12 12:26:58 +08:00
|
|
|
using osu.Framework.IO.Stores;
|
2020-06-08 06:46:40 +08:00
|
|
|
using osu.Framework.Logging;
|
2020-03-12 12:26:58 +08:00
|
|
|
using osu.Framework.Platform;
|
2020-06-08 06:46:40 +08:00
|
|
|
using osu.Game.IO;
|
2020-06-08 09:03:57 +08:00
|
|
|
using System.IO;
|
2020-06-08 06:46:40 +08:00
|
|
|
using osu.Game.Tournament.Configuration;
|
2020-03-12 12:26:58 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Tournament
|
|
|
|
{
|
2020-06-08 06:46:40 +08:00
|
|
|
internal class TournamentVideoStorage : NamespacedResourceStore<byte[]>
|
2020-03-12 12:26:58 +08:00
|
|
|
{
|
2020-06-08 06:46:40 +08:00
|
|
|
public TournamentVideoStorage(Storage storage)
|
|
|
|
: base(new StorageBackedResourceStore(storage), "videos")
|
2020-03-12 12:26:58 +08:00
|
|
|
{
|
|
|
|
AddExtension("m4v");
|
|
|
|
AddExtension("avi");
|
|
|
|
AddExtension("mp4");
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 06:46:40 +08:00
|
|
|
|
2020-06-08 06:47:47 +08:00
|
|
|
internal class TournamentStorage : WrappedStorage
|
2020-06-08 06:46:40 +08:00
|
|
|
{
|
|
|
|
private readonly GameHost host;
|
|
|
|
private readonly TournamentStorageManager storageConfig;
|
|
|
|
public readonly TournamentVideoStorage VideoStorage;
|
|
|
|
|
2020-06-08 06:47:47 +08:00
|
|
|
public TournamentStorage(GameHost host)
|
2020-06-08 06:46:40 +08:00
|
|
|
: base(host.Storage, string.Empty)
|
|
|
|
{
|
|
|
|
this.host = host;
|
|
|
|
|
|
|
|
storageConfig = new TournamentStorageManager(host.Storage);
|
2020-06-08 09:03:57 +08:00
|
|
|
var currentTournament = storageConfig.Get<string>(StorageConfig.CurrentTournament);
|
2020-06-08 06:46:40 +08:00
|
|
|
|
2020-06-08 09:03:57 +08:00
|
|
|
if (!string.IsNullOrEmpty(currentTournament))
|
2020-06-08 06:46:40 +08:00
|
|
|
{
|
2020-06-08 09:03:57 +08:00
|
|
|
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory("tournaments" + Path.DirectorySeparatorChar + currentTournament));
|
2020-06-08 06:46:40 +08:00
|
|
|
}
|
2020-06-08 09:03:57 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Migrating old storage format to the new one.
|
2020-06-08 09:12:37 +08:00
|
|
|
migrate();
|
2020-06-08 09:03:57 +08:00
|
|
|
Logger.Log("Migrating files from old storage to new.");
|
|
|
|
}
|
|
|
|
|
2020-06-08 06:46:40 +08:00
|
|
|
VideoStorage = new TournamentVideoStorage(this);
|
|
|
|
Logger.Log("Using tournament storage: " + GetFullPath(string.Empty));
|
|
|
|
}
|
2020-06-08 09:03:57 +08:00
|
|
|
|
2020-06-08 09:12:37 +08:00
|
|
|
private void migrate()
|
2020-06-08 09:03:57 +08:00
|
|
|
{
|
2020-06-08 09:12:37 +08:00
|
|
|
const string default_path = "tournaments/default";
|
2020-06-08 09:03:57 +08:00
|
|
|
var source = new DirectoryInfo(GetFullPath("tournament"));
|
2020-06-08 09:12:37 +08:00
|
|
|
var destination = new DirectoryInfo(GetFullPath(default_path));
|
2020-06-08 09:03:57 +08:00
|
|
|
|
|
|
|
Directory.CreateDirectory(destination.FullName);
|
2020-06-08 09:12:37 +08:00
|
|
|
|
2020-06-08 09:03:57 +08:00
|
|
|
if (host.Storage.Exists("bracket.json"))
|
2020-06-08 09:12:37 +08:00
|
|
|
{
|
2020-06-08 09:03:57 +08:00
|
|
|
Logger.Log("Migrating bracket to default tournament storage.");
|
|
|
|
var bracketFile = new System.IO.FileInfo(GetFullPath(string.Empty) + Path.DirectorySeparatorChar + GetFiles(string.Empty, "bracket.json").First());
|
|
|
|
attemptOperation(() => bracketFile.CopyTo(Path.Combine(destination.FullName, bracketFile.Name), true));
|
|
|
|
}
|
|
|
|
|
|
|
|
Logger.Log("Migrating other assets to default tournament storage.");
|
|
|
|
copyRecursive(source, destination);
|
2020-06-08 09:12:37 +08:00
|
|
|
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory(default_path));
|
|
|
|
storageConfig.Set(StorageConfig.CurrentTournament, default_path);
|
2020-06-08 09:03:57 +08:00
|
|
|
storageConfig.Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void copyRecursive(DirectoryInfo source, DirectoryInfo destination, bool topLevelExcludes = true)
|
|
|
|
{
|
|
|
|
// 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), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void attemptOperation(Action action, int attempts = 10)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
action();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
if (attempts-- == 0)
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
Thread.Sleep(250);
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 06:46:40 +08:00
|
|
|
}
|
2020-03-12 12:26:58 +08:00
|
|
|
}
|