osu/osu.Game.Tournament/IO/TournamentStorage.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

115 lines
4.8 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.
2022-06-17 07:37:17 +00:00
#nullable disable
using System.Collections.Generic;
using System.IO;
2020-12-07 12:11:13 +00:00
using osu.Framework.Bindables;
using osu.Framework.Logging;
using osu.Framework.Platform;
using osu.Game.IO;
using osu.Game.Tournament.Configuration;
namespace osu.Game.Tournament.IO
{
2020-06-16 15:39:20 +00:00
public class TournamentStorage : MigratableStorage
{
private const string default_tournament = "default";
private readonly Storage storage;
/// <summary>
/// The storage where all tournaments are located.
/// </summary>
public readonly Storage AllTournaments;
private readonly TournamentStorageManager storageConfig;
2020-12-07 12:11:13 +00:00
public readonly Bindable<string> CurrentTournament;
2020-06-23 22:00:21 +00:00
public TournamentStorage(Storage storage)
: base(storage.GetStorageForDirectory("tournaments"), string.Empty)
{
this.storage = storage;
AllTournaments = UnderlyingStorage;
storageConfig = new TournamentStorageManager(storage);
2020-06-11 11:56:16 +00:00
if (storage.Exists("tournament.ini"))
{
ChangeTargetStorage(AllTournaments.GetStorageForDirectory(storageConfig.Get<string>(StorageConfig.CurrentTournament)));
}
else
Migrate(AllTournaments.GetStorageForDirectory(default_tournament));
2020-12-07 20:38:15 +00:00
CurrentTournament = storageConfig.GetBindable<string>(StorageConfig.CurrentTournament);
Logger.Log("Using tournament storage: " + GetFullPath(string.Empty));
2020-12-07 12:11:13 +00:00
2020-12-07 20:38:15 +00:00
CurrentTournament.BindValueChanged(updateTournament);
}
2020-12-07 12:11:13 +00:00
private void updateTournament(ValueChangedEvent<string> newTournament)
{
ChangeTargetStorage(AllTournaments.GetStorageForDirectory(newTournament.NewValue));
2020-12-07 12:11:13 +00:00
Logger.Log("Changing tournament storage: " + GetFullPath(string.Empty));
}
protected override void ChangeTargetStorage(Storage newStorage)
{
// due to an unfortunate oversight, on OSes that are sensitive to pathname casing
// the custom flags directory needed to be named `Flags` (uppercase),
// while custom mods and videos directories needed to be named `mods` and `videos` respectively (lowercase).
// to unify handling to uppercase, move any non-compliant directories automatically for the user to migrate.
// can be removed 20220528
if (newStorage.ExistsDirectory("flags"))
AttemptOperation(() => Directory.Move(newStorage.GetFullPath("flags"), newStorage.GetFullPath("Flags")));
if (newStorage.ExistsDirectory("mods"))
AttemptOperation(() => Directory.Move(newStorage.GetFullPath("mods"), newStorage.GetFullPath("Mods")));
if (newStorage.ExistsDirectory("videos"))
AttemptOperation(() => Directory.Move(newStorage.GetFullPath("videos"), newStorage.GetFullPath("Videos")));
base.ChangeTargetStorage(newStorage);
}
public IEnumerable<string> ListTournaments() => AllTournaments.GetDirectories(string.Empty);
2020-12-07 12:11:13 +00:00
public override bool Migrate(Storage newStorage)
{
2020-10-19 06:08:49 +00:00
// this migration only happens once on moving to the per-tournament storage system.
// listed files are those known at that point in time.
// this can be removed at some point in the future (6 months obsoletion would mean 2021-04-19)
var source = new DirectoryInfo(storage.GetFullPath("tournament"));
var destination = new DirectoryInfo(newStorage.GetFullPath("."));
2020-06-16 15:39:20 +00:00
if (source.Exists)
{
Logger.Log("Migrating tournament assets to default tournament storage.");
2020-06-22 10:43:01 +00:00
CopyRecursive(source, destination);
DeleteRecursive(source);
2020-06-16 15:39:20 +00:00
}
2020-06-11 18:11:44 +00:00
moveFileIfExists(TournamentGameBase.BRACKET_FILENAME, destination);
2020-06-16 15:14:54 +00:00
moveFileIfExists("drawings.txt", destination);
moveFileIfExists("drawings_results.txt", destination);
moveFileIfExists("drawings.ini", destination);
2020-10-19 06:08:49 +00:00
ChangeTargetStorage(newStorage);
storageConfig.SetValue(StorageConfig.CurrentTournament, default_tournament);
storageConfig.Save();
return true;
}
2020-06-16 15:14:54 +00:00
private void moveFileIfExists(string file, DirectoryInfo destination)
{
2020-06-23 22:00:21 +00:00
if (!storage.Exists(file))
return;
Logger.Log($"Migrating {file} to default tournament storage.");
var fileInfo = new System.IO.FileInfo(storage.GetFullPath(file));
AttemptOperation(() => fileInfo.CopyTo(Path.Combine(destination.FullName, fileInfo.Name), true));
fileInfo.Delete();
2020-06-16 15:14:54 +00:00
}
}
}