1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 18:32:55 +08:00

Fixed migration logic

This commit is contained in:
Shivam 2020-06-11 13:56:16 +02:00
parent c2e01e198f
commit b69ff307d8

View File

@ -15,15 +15,16 @@ namespace osu.Game.Tournament.IO
internal class TournamentStorage : WrappedStorage internal class TournamentStorage : WrappedStorage
{ {
private readonly GameHost host; private readonly GameHost host;
private readonly TournamentStorageManager storageConfig;
public readonly TournamentVideoStorage VideoStorage; public readonly TournamentVideoStorage VideoStorage;
private const string default_tournament = "default";
public TournamentStorage(GameHost host) public TournamentStorage(GameHost host)
: base(host.Storage.GetStorageForDirectory("tournaments"), string.Empty) : base(host.Storage.GetStorageForDirectory("tournaments"), string.Empty)
{ {
this.host = host; this.host = host;
storageConfig = new TournamentStorageManager(host.Storage); TournamentStorageManager storageConfig = new TournamentStorageManager(host.Storage);
var currentTournament = storageConfig.Get<string>(StorageConfig.CurrentTournament); var currentTournament = storageConfig.Get<string>(StorageConfig.CurrentTournament);
if (!string.IsNullOrEmpty(currentTournament)) if (!string.IsNullOrEmpty(currentTournament))
@ -32,35 +33,39 @@ namespace osu.Game.Tournament.IO
} }
else else
{ {
migrate();
Logger.Log("Migrating files from old storage to new."); Logger.Log("Migrating files from old storage to new.");
Migrate();
storageConfig.Set(StorageConfig.CurrentTournament, default_tournament);
storageConfig.Save();
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory(default_tournament));
} }
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() internal void Migrate()
{ {
const string default_path = "default";
var source = new DirectoryInfo(host.Storage.GetFullPath("tournament")); var source = new DirectoryInfo(host.Storage.GetFullPath("tournament"));
var destination = new DirectoryInfo(GetFullPath(default_path)); var destination = new DirectoryInfo(GetFullPath(default_tournament));
Directory.CreateDirectory(destination.FullName); if (!destination.Exists)
destination.Create();
if (host.Storage.Exists("bracket.json")) if (host.Storage.Exists("bracket.json"))
{ {
Logger.Log("Migrating bracket to default tournament storage."); Logger.Log("Migrating bracket to default tournament storage.");
var bracketFile = new System.IO.FileInfo(host.Storage.GetFullPath("bracket.json")); var bracketFile = new System.IO.FileInfo(host.Storage.GetFullPath("bracket.json"));
attemptOperation(() => bracketFile.CopyTo(Path.Combine(destination.FullName, bracketFile.Name), true)); attemptOperation(() => bracketFile.CopyTo(Path.Combine(destination.FullName, bracketFile.Name), true));
bracketFile.Delete();
} }
Logger.Log("Migrating other assets to default tournament storage."); if (source.Exists)
copyRecursive(source, destination); {
ChangeTargetStorage(UnderlyingStorage.GetStorageForDirectory(default_path)); Logger.Log("Migrating tournament assets to default tournament storage.");
storageConfig.Set(StorageConfig.CurrentTournament, default_path); copyRecursive(source, destination);
storageConfig.Save(); deleteRecursive(source);
deleteRecursive(source); }
} }
private void copyRecursive(DirectoryInfo source, DirectoryInfo destination) private void copyRecursive(DirectoryInfo source, DirectoryInfo destination)
@ -113,7 +118,7 @@ namespace osu.Game.Tournament.IO
} }
} }
} }
internal class TournamentVideoStorage : NamespacedResourceStore<byte[]> internal class TournamentVideoStorage : NamespacedResourceStore<byte[]>
{ {
public TournamentVideoStorage(Storage storage) public TournamentVideoStorage(Storage storage)