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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2021-03-29 21:03:10 +08:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
2020-12-07 20:11:13 +08:00
|
|
|
using osu.Framework.Bindables;
|
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;
|
|
|
|
using osu.Game.Tournament.Configuration;
|
2020-03-12 12:26:58 +08:00
|
|
|
|
2020-06-09 23:28:42 +08:00
|
|
|
namespace osu.Game.Tournament.IO
|
2020-03-12 12:26:58 +08:00
|
|
|
{
|
2020-06-16 23:39:20 +08:00
|
|
|
public class TournamentStorage : MigratableStorage
|
2020-06-08 06:46:40 +08:00
|
|
|
{
|
2020-06-28 21:27:50 +08:00
|
|
|
private const string default_tournament = "default";
|
2020-06-16 23:00:20 +08:00
|
|
|
private readonly Storage storage;
|
2021-03-29 21:03:10 +08:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The storage where all tournaments are located.
|
|
|
|
/// </summary>
|
|
|
|
public readonly Storage AllTournaments;
|
|
|
|
|
2020-06-28 21:27:50 +08:00
|
|
|
private readonly TournamentStorageManager storageConfig;
|
2020-12-07 20:11:13 +08:00
|
|
|
public readonly Bindable<string> CurrentTournament;
|
2020-06-24 06:00:21 +08:00
|
|
|
|
2020-06-16 23:00:20 +08:00
|
|
|
public TournamentStorage(Storage storage)
|
|
|
|
: base(storage.GetStorageForDirectory("tournaments"), string.Empty)
|
2020-06-08 06:46:40 +08:00
|
|
|
{
|
2020-06-16 23:00:20 +08:00
|
|
|
this.storage = storage;
|
2021-03-29 21:03:10 +08:00
|
|
|
AllTournaments = UnderlyingStorage;
|
2020-06-08 06:46:40 +08:00
|
|
|
|
2020-06-28 21:27:50 +08:00
|
|
|
storageConfig = new TournamentStorageManager(storage);
|
2020-06-11 19:56:16 +08:00
|
|
|
|
2020-06-24 08:40:22 +08:00
|
|
|
if (storage.Exists("tournament.ini"))
|
2020-06-08 06:46:40 +08:00
|
|
|
{
|
2021-03-29 21:03:10 +08:00
|
|
|
ChangeTargetStorage(AllTournaments.GetStorageForDirectory(storageConfig.Get<string>(StorageConfig.CurrentTournament)));
|
2020-06-08 06:46:40 +08:00
|
|
|
}
|
2020-06-08 09:03:57 +08:00
|
|
|
else
|
2021-03-29 21:03:10 +08:00
|
|
|
Migrate(AllTournaments.GetStorageForDirectory(default_tournament));
|
2020-06-08 09:03:57 +08:00
|
|
|
|
2020-12-08 04:38:15 +08:00
|
|
|
CurrentTournament = storageConfig.GetBindable<string>(StorageConfig.CurrentTournament);
|
2020-06-08 06:46:40 +08:00
|
|
|
Logger.Log("Using tournament storage: " + GetFullPath(string.Empty));
|
2020-12-07 20:11:13 +08:00
|
|
|
|
2020-12-08 04:38:15 +08:00
|
|
|
CurrentTournament.BindValueChanged(updateTournament);
|
2020-06-08 06:46:40 +08:00
|
|
|
}
|
2020-06-08 09:03:57 +08:00
|
|
|
|
2020-12-07 20:11:13 +08:00
|
|
|
private void updateTournament(ValueChangedEvent<string> newTournament)
|
|
|
|
{
|
2021-03-29 21:03:10 +08:00
|
|
|
ChangeTargetStorage(AllTournaments.GetStorageForDirectory(newTournament.NewValue));
|
2020-12-07 20:11:13 +08:00
|
|
|
Logger.Log("Changing tournament storage: " + GetFullPath(string.Empty));
|
|
|
|
}
|
|
|
|
|
2021-11-28 23:14:12 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:03:10 +08:00
|
|
|
public IEnumerable<string> ListTournaments() => AllTournaments.GetDirectories(string.Empty);
|
2020-12-07 20:11:13 +08:00
|
|
|
|
2022-02-10 17:48:37 +08:00
|
|
|
public override bool Migrate(Storage newStorage)
|
2020-06-08 09:03:57 +08:00
|
|
|
{
|
2020-10-19 14:08:49 +08: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)
|
|
|
|
|
2020-06-16 23:00:20 +08:00
|
|
|
var source = new DirectoryInfo(storage.GetFullPath("tournament"));
|
2020-07-02 04:57:16 +08:00
|
|
|
var destination = new DirectoryInfo(newStorage.GetFullPath("."));
|
2020-06-08 09:03:57 +08:00
|
|
|
|
2020-06-16 23:39:20 +08:00
|
|
|
if (source.Exists)
|
|
|
|
{
|
|
|
|
Logger.Log("Migrating tournament assets to default tournament storage.");
|
2020-06-22 18:43:01 +08:00
|
|
|
CopyRecursive(source, destination);
|
|
|
|
DeleteRecursive(source);
|
2020-06-16 23:39:20 +08:00
|
|
|
}
|
2020-06-12 02:11:44 +08:00
|
|
|
|
2021-12-03 15:04:11 +08:00
|
|
|
moveFileIfExists(TournamentGameBase.BRACKET_FILENAME, destination);
|
2020-06-16 23:14:54 +08:00
|
|
|
moveFileIfExists("drawings.txt", destination);
|
|
|
|
moveFileIfExists("drawings_results.txt", destination);
|
2020-06-22 18:41:43 +08:00
|
|
|
moveFileIfExists("drawings.ini", destination);
|
2020-10-19 14:08:49 +08:00
|
|
|
|
2020-07-02 04:57:16 +08:00
|
|
|
ChangeTargetStorage(newStorage);
|
2021-03-17 15:10:16 +08:00
|
|
|
storageConfig.SetValue(StorageConfig.CurrentTournament, default_tournament);
|
2020-06-28 21:27:50 +08:00
|
|
|
storageConfig.Save();
|
2022-02-10 17:48:37 +08:00
|
|
|
|
|
|
|
return true;
|
2020-06-08 09:03:57 +08:00
|
|
|
}
|
|
|
|
|
2020-06-16 23:14:54 +08:00
|
|
|
private void moveFileIfExists(string file, DirectoryInfo destination)
|
|
|
|
{
|
2020-06-24 06:00:21 +08: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 23:14:54 +08:00
|
|
|
}
|
2020-06-08 06:46:40 +08:00
|
|
|
}
|
2020-03-12 12:26:58 +08:00
|
|
|
}
|