Use a lazy for delegating Songs directory locating until it is actually used.

This commit is contained in:
Lucas A 2021-01-26 19:07:05 +01:00
parent 043385f919
commit 2a2b6f347e

View File

@ -17,32 +17,36 @@ namespace osu.Game.IO
private const string stable_default_songs_path = "Songs";
private readonly DesktopGameHost host;
private readonly string songsPath;
private readonly Lazy<string> songsPath;
public StableStorage(string path, DesktopGameHost host)
: base(path, host)
{
this.host = host;
songsPath = locateSongsDirectory();
songsPath = new Lazy<string>(locateSongsDirectory);
}
/// <summary>
/// Returns a <see cref="Storage"/> pointing to the osu-stable Songs directory.
/// </summary>
public Storage GetSongStorage() => new DesktopStorage(songsPath, host);
public Storage GetSongStorage() => new DesktopStorage(songsPath.Value, host);
private string locateSongsDirectory()
{
var configFile = GetStream(GetFiles(".", "osu!.*.cfg").First());
var textReader = new StreamReader(configFile);
var songsDirectoryPath = Path.Combine(BasePath, stable_default_songs_path);
while (!textReader.EndOfStream)
{
var line = textReader.ReadLine();
var configFile = GetFiles(".", "osu!.*.cfg").FirstOrDefault();
if (line?.StartsWith("BeatmapDirectory", StringComparison.OrdinalIgnoreCase) == true)
if (configFile == null)
return songsDirectoryPath;
using (var textReader = new StreamReader(GetStream(configFile)))
{
string line;
while ((line = textReader.ReadLine()) != null)
{
if (line.StartsWith("BeatmapDirectory", StringComparison.OrdinalIgnoreCase))
{
var directory = line.Split('=')[1].TrimStart();
if (Path.IsPathFullyQualified(directory))
@ -51,6 +55,7 @@ namespace osu.Game.IO
break;
}
}
}
return songsDirectoryPath;
}