2021-01-25 02:18:16 +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-01-25 02:18:16 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.IO
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A storage pointing to an osu-stable installation.
|
|
|
|
|
/// Provides methods for handling installations with a custom Song folder location.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class StableStorage : DesktopStorage
|
|
|
|
|
{
|
2022-06-15 11:31:26 +08:00
|
|
|
|
public const string STABLE_DEFAULT_SONGS_PATH = "Songs";
|
2021-01-25 02:18:16 +08:00
|
|
|
|
|
|
|
|
|
private readonly DesktopGameHost host;
|
2021-01-27 02:07:05 +08:00
|
|
|
|
private readonly Lazy<string> songsPath;
|
2021-01-25 02:18:16 +08:00
|
|
|
|
|
|
|
|
|
public StableStorage(string path, DesktopGameHost host)
|
|
|
|
|
: base(path, host)
|
|
|
|
|
{
|
|
|
|
|
this.host = host;
|
2021-02-12 11:57:57 +08:00
|
|
|
|
|
2021-01-27 02:07:05 +08:00
|
|
|
|
songsPath = new Lazy<string>(locateSongsDirectory);
|
2021-01-25 02:18:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a <see cref="Storage"/> pointing to the osu-stable Songs directory.
|
|
|
|
|
/// </summary>
|
2021-01-27 02:07:05 +08:00
|
|
|
|
public Storage GetSongStorage() => new DesktopStorage(songsPath.Value, host);
|
2021-01-25 02:18:16 +08:00
|
|
|
|
|
|
|
|
|
private string locateSongsDirectory()
|
|
|
|
|
{
|
2022-01-29 12:27:19 +08:00
|
|
|
|
var configurationFiles = GetFiles(".", $"osu!.{Environment.UserName}.cfg");
|
2021-01-27 02:07:05 +08:00
|
|
|
|
|
2022-01-29 12:27:19 +08:00
|
|
|
|
// GetFiles returns case insensitive results, so multiple files could exist.
|
|
|
|
|
// Prefer a case-correct match, but fallback to any available.
|
|
|
|
|
string usableConfigFile =
|
|
|
|
|
configurationFiles.FirstOrDefault(f => f.Contains(Environment.UserName, StringComparison.Ordinal))
|
|
|
|
|
?? configurationFiles.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
if (usableConfigFile != null)
|
2021-01-25 02:18:16 +08:00
|
|
|
|
{
|
2022-01-29 12:27:19 +08:00
|
|
|
|
using (var stream = GetStream(usableConfigFile))
|
2021-02-12 11:57:57 +08:00
|
|
|
|
using (var textReader = new StreamReader(stream))
|
2021-01-25 02:18:16 +08:00
|
|
|
|
{
|
2021-02-12 11:57:57 +08:00
|
|
|
|
string line;
|
|
|
|
|
|
|
|
|
|
while ((line = textReader.ReadLine()) != null)
|
2021-01-27 02:07:05 +08:00
|
|
|
|
{
|
2021-02-12 11:57:57 +08:00
|
|
|
|
if (!line.StartsWith("BeatmapDirectory", StringComparison.OrdinalIgnoreCase)) continue;
|
|
|
|
|
|
2021-10-27 12:04:41 +08:00
|
|
|
|
string customDirectory = line.Split('=').LastOrDefault()?.Trim();
|
2021-02-12 11:57:57 +08:00
|
|
|
|
if (customDirectory != null && Path.IsPathFullyQualified(customDirectory))
|
|
|
|
|
return customDirectory;
|
2021-01-27 02:07:05 +08:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-01-25 02:18:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-15 11:31:26 +08:00
|
|
|
|
return GetFullPath(STABLE_DEFAULT_SONGS_PATH);
|
2021-01-25 02:18:16 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|