Merge pull request #24807 from peppy/import-task-improve

Clean up `ImportTask` / `ArchiveReader` implementations
This commit is contained in:
Bartłomiej Dach 2023-09-14 20:24:32 +02:00 committed by GitHub
commit b2b18092cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 64 additions and 51 deletions

View File

@ -319,7 +319,7 @@ namespace osu.Game.Beatmaps
{
DateTimeOffset dateAdded = DateTimeOffset.UtcNow;
if (reader is LegacyDirectoryArchiveReader legacyReader)
if (reader is DirectoryArchiveReader legacyReader)
{
var beatmaps = reader.Filenames.Where(f => f.EndsWith(".osu", StringComparison.OrdinalIgnoreCase));

View File

@ -46,9 +46,29 @@ namespace osu.Game.Database
/// </summary>
public ArchiveReader GetReader()
{
return Stream != null
? getReaderFrom(Stream)
: getReaderFrom(Path);
if (Stream == null)
{
if (ZipUtils.IsZipArchive(Path))
return new ZipArchiveReader(File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.Read), System.IO.Path.GetFileName(Path));
if (Directory.Exists(Path))
return new DirectoryArchiveReader(Path);
if (File.Exists(Path))
return new SingleFileArchiveReader(Path);
throw new InvalidFormatException($"{Path} is not a valid archive");
}
if (Stream is not MemoryStream memoryStream)
{
// Path used primarily in tests (converting `ManifestResourceStream`s to `MemoryStream`s).
memoryStream = new MemoryStream(Stream.ReadAllBytesToArray());
Stream.Dispose();
}
if (ZipUtils.IsZipArchive(memoryStream))
return new ZipArchiveReader(memoryStream, Path);
return new MemoryStreamArchiveReader(memoryStream, Path);
}
/// <summary>
@ -60,43 +80,6 @@ namespace osu.Game.Database
File.Delete(Path);
}
/// <summary>
/// Creates an <see cref="ArchiveReader"/> from a stream.
/// </summary>
/// <param name="stream">A seekable stream containing the archive content.</param>
/// <returns>A reader giving access to the archive's content.</returns>
private ArchiveReader getReaderFrom(Stream stream)
{
if (!(stream is MemoryStream memoryStream))
{
// This isn't used in any current path. May need to reconsider for performance reasons (ie. if we don't expect the incoming stream to be copied out).
memoryStream = new MemoryStream(stream.ReadAllBytesToArray());
stream.Dispose();
}
if (ZipUtils.IsZipArchive(memoryStream))
return new ZipArchiveReader(memoryStream, Path);
return new LegacyByteArrayReader(memoryStream.ToArray(), Path);
}
/// <summary>
/// Creates an <see cref="ArchiveReader"/> from a valid storage path.
/// </summary>
/// <param name="path">A file or folder path resolving the archive content.</param>
/// <returns>A reader giving access to the archive's content.</returns>
private ArchiveReader getReaderFrom(string path)
{
if (ZipUtils.IsZipArchive(path))
return new ZipArchiveReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read), System.IO.Path.GetFileName(path));
if (Directory.Exists(path))
return new LegacyDirectoryArchiveReader(path);
if (File.Exists(path))
return new LegacyFileArchiveReader(path);
throw new InvalidFormatException($"{path} is not a valid archive");
}
public override string ToString() => System.IO.Path.GetFileName(Path);
}
}

View File

@ -9,11 +9,11 @@ namespace osu.Game.IO.Archives
/// <summary>
/// Allows reading a single file from the provided byte array.
/// </summary>
public class LegacyByteArrayReader : ArchiveReader
public class ByteArrayArchiveReader : ArchiveReader
{
private readonly byte[] content;
public LegacyByteArrayReader(byte[] content, string filename)
public ByteArrayArchiveReader(byte[] content, string filename)
: base(filename)
{
this.content = content;

View File

@ -8,13 +8,13 @@ using System.Linq;
namespace osu.Game.IO.Archives
{
/// <summary>
/// Reads an archive from a directory on disk.
/// Reads an archive directly from a directory on disk.
/// </summary>
public class LegacyDirectoryArchiveReader : ArchiveReader
public class DirectoryArchiveReader : ArchiveReader
{
private readonly string path;
public LegacyDirectoryArchiveReader(string path)
public DirectoryArchiveReader(string path)
: base(Path.GetFileName(path))
{
// re-get full path to standardise with Directory.GetFiles return values below.

View File

@ -0,0 +1,30 @@
// 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.
using System.Collections.Generic;
using System.IO;
namespace osu.Game.IO.Archives
{
/// <summary>
/// Allows reading a single file from the provided memory stream.
/// </summary>
public class MemoryStreamArchiveReader : ArchiveReader
{
private readonly MemoryStream stream;
public MemoryStreamArchiveReader(MemoryStream stream, string filename)
: base(filename)
{
this.stream = stream;
}
public override Stream GetStream(string name) => new MemoryStream(stream.GetBuffer(), 0, (int)stream.Length);
public override void Dispose()
{
}
public override IEnumerable<string> Filenames => new[] { Name };
}
}

View File

@ -7,14 +7,14 @@ using System.IO;
namespace osu.Game.IO.Archives
{
/// <summary>
/// Reads a file on disk as an archive.
/// Reads a single file on disk as an archive.
/// Note: In this case, the file is not an extractable archive, use <see cref="ZipArchiveReader"/> instead.
/// </summary>
public class LegacyFileArchiveReader : ArchiveReader
public class SingleFileArchiveReader : ArchiveReader
{
private readonly string path;
public LegacyFileArchiveReader(string path)
public SingleFileArchiveReader(string path)
: base(Path.GetFileName(path))
{
// re-get full path to standardise

View File

@ -1144,14 +1144,14 @@ namespace osu.Game.Screens.Play
if (DrawableRuleset.ReplayScore != null)
return Task.CompletedTask;
LegacyByteArrayReader replayReader = null;
ByteArrayArchiveReader replayReader = null;
if (score.ScoreInfo.Ruleset.IsLegacyRuleset())
{
using (var stream = new MemoryStream())
{
new LegacyScoreEncoder(score, GameplayState.Beatmap).Encode(stream);
replayReader = new LegacyByteArrayReader(stream.ToArray(), "replay.osr");
replayReader = new ByteArrayArchiveReader(stream.ToArray(), "replay.osr");
}
}