mirror of
https://github.com/ppy/osu.git
synced 2025-01-26 12:35:34 +08:00
Implement migration for TournamentStorage
This commit is contained in:
parent
9a20ffa8a3
commit
ba5a747ac9
@ -1,10 +1,14 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// 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.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
using osu.Framework.IO.Stores;
|
using osu.Framework.IO.Stores;
|
||||||
using osu.Framework.Logging;
|
using osu.Framework.Logging;
|
||||||
using osu.Framework.Platform;
|
using osu.Framework.Platform;
|
||||||
using osu.Game.IO;
|
using osu.Game.IO;
|
||||||
|
using System.IO;
|
||||||
using osu.Game.Tournament.Configuration;
|
using osu.Game.Tournament.Configuration;
|
||||||
|
|
||||||
namespace osu.Game.Tournament
|
namespace osu.Game.Tournament
|
||||||
@ -32,16 +36,77 @@ namespace osu.Game.Tournament
|
|||||||
this.host = host;
|
this.host = host;
|
||||||
|
|
||||||
storageConfig = new TournamentStorageManager(host.Storage);
|
storageConfig = new TournamentStorageManager(host.Storage);
|
||||||
var customTournamentPath = storageConfig.Get<string>(StorageConfig.CurrentTournament);
|
var currentTournament = storageConfig.Get<string>(StorageConfig.CurrentTournament);
|
||||||
|
|
||||||
if (!string.IsNullOrEmpty(customTournamentPath))
|
if (!string.IsNullOrEmpty(currentTournament))
|
||||||
{
|
{
|
||||||
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory("tournaments/" + customTournamentPath));
|
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory("tournaments" + Path.DirectorySeparatorChar + currentTournament));
|
||||||
} else {
|
|
||||||
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory("tournaments/default"));
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Migrating old storage format to the new one.
|
||||||
|
Migrate();
|
||||||
|
Logger.Log("Migrating files from old storage to new.");
|
||||||
|
}
|
||||||
|
|
||||||
VideoStorage = new TournamentVideoStorage(this);
|
VideoStorage = new TournamentVideoStorage(this);
|
||||||
Logger.Log("Using tournament storage: " + GetFullPath(string.Empty));
|
Logger.Log("Using tournament storage: " + GetFullPath(string.Empty));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void Migrate()
|
||||||
|
{
|
||||||
|
var defaultPath = "tournaments/default";
|
||||||
|
var source = new DirectoryInfo(GetFullPath("tournament"));
|
||||||
|
var destination = new DirectoryInfo(GetFullPath(defaultPath));
|
||||||
|
|
||||||
|
Directory.CreateDirectory(destination.FullName);
|
||||||
|
|
||||||
|
if (host.Storage.Exists("bracket.json"))
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory(defaultPath));
|
||||||
|
storageConfig.Set<string>(StorageConfig.CurrentTournament, defaultPath);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user