// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.Diagnostics; using System.Linq; using JetBrains.Annotations; using osu.Framework.Logging; using osu.Framework.Platform; using osu.Game.Configuration; namespace osu.Game.IO { public class OsuStorage : MigratableStorage { /// /// Indicates the error (if any) that occurred when initialising the custom storage during initial startup. /// public readonly OsuStorageError Error; /// /// The custom storage path as selected by the user. /// [CanBeNull] public string CustomStoragePath => storageConfig.Get(StorageConfig.FullPath); /// /// The default storage path to be used if a custom storage path hasn't been selected or is not accessible. /// [NotNull] public string DefaultStoragePath => defaultStorage.GetFullPath("."); private readonly GameHost host; private readonly StorageConfigManager storageConfig; private readonly Storage defaultStorage; public override string[] IgnoreDirectories => new[] { "cache", "client.realm.management" }; public override string[] IgnoreFiles => new[] { "framework.ini", "storage.ini", "client.realm.note", "client.realm.lock", }; public OsuStorage(GameHost host, Storage defaultStorage) : base(defaultStorage, string.Empty) { this.host = host; this.defaultStorage = defaultStorage; storageConfig = new StorageConfigManager(defaultStorage); if (!string.IsNullOrEmpty(CustomStoragePath)) TryChangeToCustomStorage(out Error); } /// /// Resets the custom storage path, changing the target storage to the default location. /// public void ResetCustomStoragePath() { storageConfig.SetValue(StorageConfig.FullPath, string.Empty); storageConfig.Save(); ChangeTargetStorage(defaultStorage); } /// /// Attempts to change to the user's custom storage path. /// /// The error that occurred. /// Whether the custom storage path was used successfully. If not, will be populated with the reason. public bool TryChangeToCustomStorage(out OsuStorageError error) { Debug.Assert(!string.IsNullOrEmpty(CustomStoragePath)); error = OsuStorageError.None; Storage lastStorage = UnderlyingStorage; try { Storage userStorage = host.GetStorage(CustomStoragePath); if (!userStorage.ExistsDirectory(".") || !userStorage.GetFiles(".").Any()) error = OsuStorageError.AccessibleButEmpty; ChangeTargetStorage(userStorage); } catch { error = OsuStorageError.NotAccessible; ChangeTargetStorage(lastStorage); } return error == OsuStorageError.None; } protected override void ChangeTargetStorage(Storage newStorage) { var lastStorage = UnderlyingStorage; base.ChangeTargetStorage(newStorage); if (lastStorage != null) { // for now we assume that if there was a previous storage, this is a migration operation. // the logger shouldn't be set during initialisation as it can cause cross-talk in tests (due to being static). Logger.Storage = UnderlyingStorage.GetStorageForDirectory("logs"); } } public override void Migrate(Storage newStorage) { base.Migrate(newStorage); storageConfig.SetValue(StorageConfig.FullPath, newStorage.GetFullPath(".")); storageConfig.Save(); } } public enum OsuStorageError { /// /// No error. /// None, /// /// Occurs when the target storage directory is accessible but does not already contain game files. /// Only happens when the user changes the storage directory and then moves the files manually or mounts a different device to the same path. /// AccessibleButEmpty, /// /// Occurs when the target storage directory cannot be accessed at all. /// NotAccessible, } }